From 6f67dadb3130233d403c348dfa11c386a175c976 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sat, 20 Mar 2021 12:40:51 +0900 Subject: [PATCH] Handle new created threads using inbox manager --- .../instagrabber/activities/MainActivity.java | 21 +++++++++++++------ .../DirectMessageInboxFragment.java | 2 +- .../DirectMessageSettingsFragment.java | 4 +--- .../DirectMessageThreadFragment.java | 9 ++++++-- .../DirectPendingInboxFragment.java | 2 +- .../fragments/main/ProfileFragment.java | 9 +++++++- .../managers/DirectMessagesManager.java | 3 +-- .../instagrabber/managers/InboxManager.java | 12 +++++++++++ .../instagrabber/managers/ThreadManager.java | 12 +++++------ .../directmessages/DirectThread.java | 9 ++++++++ .../viewmodels/DirectSettingsViewModel.java | 3 +-- .../viewmodels/DirectThreadViewModel.java | 13 ++++++++++-- .../DirectSettingsViewModelFactory.java | 6 +----- .../DirectThreadViewModelFactory.java | 6 +----- .../navigation/direct_messages_nav_graph.xml | 9 -------- 15 files changed, 74 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index 7aaf4d43..fb56d027 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -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 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() { @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); } diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java index 5c62d592..4b8bece2 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java @@ -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; diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.java index 1ca1552a..e99259d2 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.java @@ -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()); diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java index 7edbe49c..26b57fa5 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java @@ -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) { diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectPendingInboxFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectPendingInboxFragment.java index c242e564..8ee0d1c4 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectPendingInboxFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectPendingInboxFragment.java @@ -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); } diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java index 4cd53a0b..de355f2f 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -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(); }); diff --git a/app/src/main/java/awais/instagrabber/managers/DirectMessagesManager.java b/app/src/main/java/awais/instagrabber/managers/DirectMessagesManager.java index 99b07b50..6bc7f4da 100644 --- a/app/src/main/java/awais/instagrabber/managers/DirectMessagesManager.java +++ b/app/src/main/java/awais/instagrabber/managers/DirectMessagesManager.java @@ -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); } } diff --git a/app/src/main/java/awais/instagrabber/managers/InboxManager.java b/app/src/main/java/awais/instagrabber/managers/InboxManager.java index 70457e03..98e12b46 100644 --- a/app/src/main/java/awais/instagrabber/managers/InboxManager.java +++ b/app/src/main/java/awais/instagrabber/managers/InboxManager.java @@ -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 threads = currentDirectInbox.getThreads(); + if (threads == null) return false; + return threads.stream().anyMatch(thread -> Objects.equals(thread.getThreadId(), threadId)); + } + } } diff --git a/app/src/main/java/awais/instagrabber/managers/ThreadManager.java b/app/src/main/java/awais/instagrabber/managers/ThreadManager.java index 6e7de7ab..00457980 100644 --- a/app/src/main/java/awais/instagrabber/managers/ThreadManager.java +++ b/app/src/main/java/awais/instagrabber/managers/ThreadManager.java @@ -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(); diff --git a/app/src/main/java/awais/instagrabber/repositories/responses/directmessages/DirectThread.java b/app/src/main/java/awais/instagrabber/repositories/responses/directmessages/DirectThread.java index 67b58db4..1f6ffd66 100644 --- a/app/src/main/java/awais/instagrabber/repositories/responses/directmessages/DirectThread.java +++ b/app/src/main/java/awais/instagrabber/repositories/responses/directmessages/DirectThread.java @@ -45,6 +45,7 @@ public class DirectThread implements Serializable, Cloneable { private boolean approvalRequiredForNewMembers; private int inputMode; private final List 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; diff --git a/app/src/main/java/awais/instagrabber/viewmodels/DirectSettingsViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/DirectSettingsViewModel.java index 32883c92..e9d33085 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/DirectSettingsViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/DirectSettingsViewModel.java @@ -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 diff --git a/app/src/main/java/awais/instagrabber/viewmodels/DirectThreadViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/DirectThreadViewModel.java index 4c01c658..dab20a92 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/DirectThreadViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/DirectThreadViewModel.java @@ -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); + } + } } diff --git a/app/src/main/java/awais/instagrabber/viewmodels/factories/DirectSettingsViewModelFactory.java b/app/src/main/java/awais/instagrabber/viewmodels/factories/DirectSettingsViewModelFactory.java index 61c11fc1..2d3503bf 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/factories/DirectSettingsViewModelFactory.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/factories/DirectSettingsViewModelFactory.java @@ -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 create(@NonNull final Class modelClass) { //noinspection unchecked - return (T) new DirectSettingsViewModel(application, threadId, backup, pending, currentUser); + return (T) new DirectSettingsViewModel(application, threadId, pending, currentUser); } } diff --git a/app/src/main/java/awais/instagrabber/viewmodels/factories/DirectThreadViewModelFactory.java b/app/src/main/java/awais/instagrabber/viewmodels/factories/DirectThreadViewModelFactory.java index 2d309d07..586e1cc0 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/factories/DirectThreadViewModelFactory.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/factories/DirectThreadViewModelFactory.java @@ -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 create(@NonNull final Class modelClass) { //noinspection unchecked - return (T) new DirectThreadViewModel(application, threadId, backup, pending, currentUser); + return (T) new DirectThreadViewModel(application, threadId, pending, currentUser); } } diff --git a/app/src/main/res/navigation/direct_messages_nav_graph.xml b/app/src/main/res/navigation/direct_messages_nav_graph.xml index af473a45..1cf39e25 100644 --- a/app/src/main/res/navigation/direct_messages_nav_graph.xml +++ b/app/src/main/res/navigation/direct_messages_nav_graph.xml @@ -122,10 +122,6 @@ android:name="pending" android:defaultValue="false" app:argType="boolean" /> - @@ -156,11 +152,6 @@ android:defaultValue="false" app:argType="boolean" /> - -