Handle new created threads using inbox manager

This commit is contained in:
Ammar Githam 2021-03-20 12:40:51 +09:00
parent 737931821e
commit 6f67dadb31
15 changed files with 74 additions and 46 deletions

View File

@ -72,7 +72,6 @@ import awais.instagrabber.interfaces.FetchListener;
import awais.instagrabber.models.IntentModel;
import awais.instagrabber.models.SuggestionModel;
import awais.instagrabber.models.enums.SuggestionType;
import awais.instagrabber.repositories.responses.directmessages.DirectThread;
import awais.instagrabber.services.ActivityCheckerService;
import awais.instagrabber.services.DMSyncAlarmReceiver;
import awais.instagrabber.utils.AppExecutors;
@ -251,7 +250,17 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
@Override
public void onBackPressed() {
if (isTaskRoot() && isBackStackEmpty) {
int currentNavControllerBackStack = 2;
if (currentNavControllerLiveData != null) {
final NavController navController = currentNavControllerLiveData.getValue();
if (navController != null) {
@SuppressLint("RestrictedApi") final Deque<NavBackStackEntry> backStack = navController.getBackStack();
if (backStack != null) {
currentNavControllerBackStack = backStack.size();
}
}
}
if (isTaskRoot() && isBackStackEmpty && currentNavControllerBackStack == 2) {
finishAfterTransition();
return;
}
@ -577,10 +586,10 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
private void showThread(@NonNull final Intent intent) {
final String threadId = intent.getStringExtra(Constants.DM_THREAD_ACTION_EXTRA_THREAD_ID);
final String threadTitle = intent.getStringExtra(Constants.DM_THREAD_ACTION_EXTRA_THREAD_TITLE);
navigateToThread(threadId, threadTitle, null);
navigateToThread(threadId, threadTitle);
}
public void navigateToThread(final String threadId, final String threadTitle, final DirectThread backup) {
public void navigateToThread(final String threadId, final String threadTitle) {
if (threadId == null || threadTitle == null) return;
currentNavControllerLiveData.observe(this, new Observer<NavController>() {
@Override
@ -594,7 +603,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
// need handler.post() to wait for the fragment manager to be ready to navigate
new Handler().post(() -> {
final DirectMessageInboxFragmentDirections.ActionInboxToThread action = DirectMessageInboxFragmentDirections
.actionInboxToThread(threadId, threadTitle, backup);
.actionInboxToThread(threadId, threadTitle);
navController.navigate(action);
});
return;
@ -607,7 +616,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
@Nullable final Bundle arguments) {
if (destination.getId() == R.id.directMessagesInboxFragment) {
final DirectMessageInboxFragmentDirections.ActionInboxToThread action = DirectMessageInboxFragmentDirections
.actionInboxToThread(threadId, threadTitle, backup);
.actionInboxToThread(threadId, threadTitle);
controller.navigate(action);
controller.removeOnDestinationChangedListener(this);
}

View File

@ -257,7 +257,7 @@ public class DirectMessageInboxFragment extends Fragment implements SwipeRefresh
navigating = true;
if (isAdded()) {
final DirectMessageInboxFragmentDirections.ActionInboxToThread directions = DirectMessageInboxFragmentDirections
.actionInboxToThread(thread.getThreadId(), thread.getThreadTitle(), thread);
.actionInboxToThread(thread.getThreadId(), thread.getThreadTitle());
NavHostFragment.findNavController(this).navigate(directions);
}
navigating = false;

View File

@ -81,7 +81,6 @@ public class DirectMessageSettingsFragment extends Fragment implements ConfirmDi
final AppStateViewModel appStateViewModel = new ViewModelProvider(fragmentActivity).get(AppStateViewModel.class);
viewModel = new ViewModelProvider(this, new DirectSettingsViewModelFactory(fragmentActivity.getApplication(),
args.getThreadId(),
args.getBackup(),
args.getPending(),
appStateViewModel.getCurrentUser()))
.get(DirectSettingsViewModel.class);
@ -350,8 +349,7 @@ public class DirectMessageSettingsFragment extends Fragment implements ConfirmDi
(position, user, selected) -> {
if (!TextUtils.isEmpty(user.getFbId())) {
Utils.openURL(context, "https://facebook.com/" + user.getFbId());
}
else {
} else {
final ProfileNavGraphDirections.ActionGlobalProfileFragment directions = ProfileNavGraphDirections
.actionGlobalProfileFragment()
.setUsername("@" + user.getUsername());

View File

@ -329,7 +329,6 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact
final DirectMessageThreadFragmentArgs fragmentArgs = DirectMessageThreadFragmentArgs.fromBundle(arguments);
viewModel = new ViewModelProvider(this, new DirectThreadViewModelFactory(fragmentActivity.getApplication(),
fragmentArgs.getThreadId(),
fragmentArgs.getBackup(),
fragmentArgs.getPending(),
appStateViewModel.getCurrentUser()))
.get(DirectThreadViewModel.class);
@ -379,7 +378,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact
final int itemId = item.getItemId();
if (itemId == R.id.info) {
final DirectMessageThreadFragmentDirections.ActionThreadToSettings directions = DirectMessageThreadFragmentDirections
.actionThreadToSettings(viewModel.getThreadId(), null, null);
.actionThreadToSettings(viewModel.getThreadId(), null);
final Boolean pending = viewModel.isPending().getValue();
directions.setPending(pending == null ? false : pending);
NavHostFragment.findNavController(this).navigate(directions);
@ -464,6 +463,12 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact
cleanup();
}
@Override
public void onDestroy() {
viewModel.deleteThreadIfRequired();
super.onDestroy();
}
@SuppressLint("UnsafeExperimentalUsageError")
private void cleanup() {
if (prevTitleRunnable != null) {

View File

@ -163,7 +163,7 @@ public class DirectPendingInboxFragment extends Fragment implements SwipeRefresh
navigating = true;
if (isAdded()) {
final DirectPendingInboxFragmentDirections.ActionPendingInboxToThread directions = DirectPendingInboxFragmentDirections
.actionPendingInboxToThread(thread.getThreadId(), thread.getThreadTitle(), thread);
.actionPendingInboxToThread(thread.getThreadId(), thread.getThreadTitle());
directions.setPending(true);
NavHostFragment.findNavController(this).navigate(directions);
}

View File

@ -74,6 +74,8 @@ import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment;
import awais.instagrabber.dialogs.ProfilePicDialogFragment;
import awais.instagrabber.fragments.PostViewV2Fragment;
import awais.instagrabber.interfaces.FetchListener;
import awais.instagrabber.managers.DirectMessagesManager;
import awais.instagrabber.managers.InboxManager;
import awais.instagrabber.models.HighlightModel;
import awais.instagrabber.models.PostsLayoutPreferences;
import awais.instagrabber.models.StoryModel;
@ -1073,7 +1075,12 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
profileDetailsBinding.btnDM.setEnabled(true);
return;
}
fragmentActivity.navigateToThread(thread.getThreadId(), profileModel.getUsername(), thread);
final InboxManager inboxManager = DirectMessagesManager.getInstance().getInboxManager();
if (!inboxManager.containsThread(thread.getThreadId())) {
thread.setTemp(true);
inboxManager.addThread(thread, 0);
}
fragmentActivity.navigateToThread(thread.getThreadId(), profileModel.getUsername());
profileDetailsBinding.btnDM.setEnabled(true);
}).execute();
});

View File

@ -74,9 +74,8 @@ public final class DirectMessagesManager {
public ThreadManager getThreadManager(@NonNull final String threadId,
final boolean pending,
final DirectThread backup,
@NonNull final User currentUser,
@NonNull final ContentResolver contentResolver) {
return ThreadManager.getInstance(threadId, pending, backup, currentUser, contentResolver);
return ThreadManager.getInstance(threadId, pending, currentUser, contentResolver);
}
}

View File

@ -18,6 +18,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@ -357,4 +358,15 @@ public final class InboxManager {
public void setPendingRequestsTotal(final int total) {
pendingRequestsTotal.postValue(total);
}
public boolean containsThread(final String threadId) {
if (threadId == null) return false;
synchronized (this.inbox) {
final DirectInbox currentDirectInbox = getCurrentDirectInbox();
if (currentDirectInbox == null) return false;
final List<DirectThread> threads = currentDirectInbox.getThreads();
if (threads == null) return false;
return threads.stream().anyMatch(thread -> Objects.equals(thread.getThreadId(), threadId));
}
}
}

View File

@ -123,7 +123,6 @@ public final class ThreadManager {
public static ThreadManager getInstance(@NonNull final String threadId,
final boolean pending,
final DirectThread backup,
@NonNull final User currentUser,
@NonNull final ContentResolver contentResolver) {
ThreadManager instance = INSTANCE_MAP.get(threadId);
@ -131,7 +130,7 @@ public final class ThreadManager {
synchronized (LOCK) {
instance = INSTANCE_MAP.get(threadId);
if (instance == null) {
instance = new ThreadManager(threadId, pending, backup, currentUser, contentResolver);
instance = new ThreadManager(threadId, pending, currentUser, contentResolver);
INSTANCE_MAP.put(threadId, instance);
}
}
@ -145,7 +144,6 @@ public final class ThreadManager {
private ThreadManager(@NonNull final String threadId,
final boolean pending,
final DirectThread backup,
@NonNull final User currentUser,
@NonNull final ContentResolver contentResolver) {
final DirectMessagesManager messagesManager = DirectMessagesManager.getInstance();
@ -164,17 +162,17 @@ public final class ThreadManager {
service = DirectMessagesService.getInstance(csrfToken, viewerId, deviceUuid);
mediaService = MediaService.getInstance(deviceUuid, csrfToken, viewerId);
friendshipService = FriendshipService.getInstance(deviceUuid, csrfToken, viewerId);
setupTransformations(backup);
setupTransformations();
// fetchChats();
}
public void moveFromPending() {
final DirectMessagesManager messagesManager = DirectMessagesManager.getInstance();
this.inboxManager = messagesManager.getInboxManager();
setupTransformations(null);
setupTransformations();
}
private void setupTransformations(final DirectThread backup) {
private void setupTransformations() {
// Transformations
thread = distinctUntilChanged(map(inboxManager.getInbox(), inboxResource -> {
if (inboxResource == null) {
@ -188,7 +186,7 @@ public final class ThreadManager {
final DirectThread thread = threads.stream()
.filter(t -> t.getThreadId().equals(threadId))
.findFirst()
.orElse(backup);
.orElse(null);
if (thread != null) {
cursor = thread.getOldestCursor();
hasOlder = thread.hasOlder();

View File

@ -45,6 +45,7 @@ public class DirectThread implements Serializable, Cloneable {
private boolean approvalRequiredForNewMembers;
private int inputMode;
private final List<ThreadContext> threadContextItems;
private boolean isTemp;
public DirectThread(final String threadId,
final String threadV2Id,
@ -292,6 +293,14 @@ public class DirectThread implements Serializable, Cloneable {
return threadContextItems;
}
public boolean isTemp() {
return isTemp;
}
public void setTemp(final boolean isTemp) {
this.isTemp = isTemp;
}
@Nullable
public DirectItem getFirstDirectItem() {
DirectItem firstItem = null;

View File

@ -45,7 +45,6 @@ public class DirectSettingsViewModel extends AndroidViewModel {
public DirectSettingsViewModel(final Application application,
@NonNull final String threadId,
final DirectThread backup,
final boolean pending,
@NonNull final User currentUser) {
super(application);
@ -59,7 +58,7 @@ public class DirectSettingsViewModel extends AndroidViewModel {
final ContentResolver contentResolver = application.getContentResolver();
resources = getApplication().getResources();
final DirectMessagesManager messagesManager = DirectMessagesManager.getInstance();
threadManager = messagesManager.getThreadManager(threadId, pending, backup, currentUser, contentResolver);
threadManager = messagesManager.getThreadManager(threadId, pending, currentUser, contentResolver);
}
@NonNull

View File

@ -23,6 +23,7 @@ import java.util.stream.Collectors;
import awais.instagrabber.customviews.emoji.Emoji;
import awais.instagrabber.managers.DirectMessagesManager;
import awais.instagrabber.managers.InboxManager;
import awais.instagrabber.managers.ThreadManager;
import awais.instagrabber.models.Resource;
import awais.instagrabber.repositories.responses.User;
@ -57,7 +58,6 @@ public class DirectThreadViewModel extends AndroidViewModel {
public DirectThreadViewModel(@NonNull final Application application,
@NonNull final String threadId,
final DirectThread backup,
final boolean pending,
@NonNull final User currentUser) {
super(application);
@ -74,7 +74,7 @@ public class DirectThreadViewModel extends AndroidViewModel {
contentResolver = application.getContentResolver();
recordingsDir = DirectoryUtils.getOutputMediaDirectory(application, "Recordings");
final DirectMessagesManager messagesManager = DirectMessagesManager.getInstance();
threadManager = messagesManager.getThreadManager(threadId, pending, backup, currentUser, contentResolver);
threadManager = messagesManager.getThreadManager(threadId, pending, currentUser, contentResolver);
threadManager.fetchPendingRequests();
}
@ -302,4 +302,13 @@ public class DirectThreadViewModel extends AndroidViewModel {
}
threadManager.markAsSeen(directItem);
}
public void deleteThreadIfRequired() {
final DirectThread thread = getThread().getValue();
if (thread == null) return;
if (thread.isTemp() && (thread.getItems() == null || thread.getItems().isEmpty())) {
final InboxManager inboxManager = DirectMessagesManager.getInstance().getInboxManager();
inboxManager.removeThread(threadId);
}
}
}

View File

@ -7,25 +7,21 @@ import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import awais.instagrabber.repositories.responses.User;
import awais.instagrabber.repositories.responses.directmessages.DirectThread;
import awais.instagrabber.viewmodels.DirectSettingsViewModel;
public class DirectSettingsViewModelFactory implements ViewModelProvider.Factory {
private final Application application;
private final String threadId;
private final DirectThread backup;
private final boolean pending;
private final User currentUser;
public DirectSettingsViewModelFactory(@NonNull final Application application,
@NonNull final String threadId,
@NonNull final DirectThread backup,
final boolean pending,
@NonNull final User currentUser) {
this.application = application;
this.threadId = threadId;
this.backup = backup;
this.pending = pending;
this.currentUser = currentUser;
}
@ -34,6 +30,6 @@ public class DirectSettingsViewModelFactory implements ViewModelProvider.Factory
@Override
public <T extends ViewModel> T create(@NonNull final Class<T> modelClass) {
//noinspection unchecked
return (T) new DirectSettingsViewModel(application, threadId, backup, pending, currentUser);
return (T) new DirectSettingsViewModel(application, threadId, pending, currentUser);
}
}

View File

@ -7,25 +7,21 @@ import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import awais.instagrabber.repositories.responses.User;
import awais.instagrabber.repositories.responses.directmessages.DirectThread;
import awais.instagrabber.viewmodels.DirectThreadViewModel;
public class DirectThreadViewModelFactory implements ViewModelProvider.Factory {
private final Application application;
private final String threadId;
private final DirectThread backup;
private final boolean pending;
private final User currentUser;
public DirectThreadViewModelFactory(@NonNull final Application application,
@NonNull final String threadId,
final DirectThread backup,
final boolean pending,
@NonNull final User currentUser) {
this.application = application;
this.threadId = threadId;
this.backup = backup;
this.pending = pending;
this.currentUser = currentUser;
}
@ -34,6 +30,6 @@ public class DirectThreadViewModelFactory implements ViewModelProvider.Factory {
@Override
public <T extends ViewModel> T create(@NonNull final Class<T> modelClass) {
//noinspection unchecked
return (T) new DirectThreadViewModel(application, threadId, backup, pending, currentUser);
return (T) new DirectThreadViewModel(application, threadId, pending, currentUser);
}
}

View File

@ -122,10 +122,6 @@
android:name="pending"
android:defaultValue="false"
app:argType="boolean" />
<argument
android:name="backup"
app:nullable="true"
app:argType="awais.instagrabber.repositories.responses.directmessages.DirectThread" />
<action
android:id="@+id/action_thread_to_settings"
app:destination="@id/directMessagesSettingsFragment" />
@ -156,11 +152,6 @@
android:defaultValue="false"
app:argType="boolean" />
<argument
android:name="backup"
app:nullable="true"
app:argType="awais.instagrabber.repositories.responses.directmessages.DirectThread" />
<action
android:id="@+id/action_settings_to_inbox"
app:destination="@id/directMessagesInboxFragment"