From 2fed236f04b88fb6c64629b8c2efb98afc0dca7d Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Thu, 25 Mar 2021 00:32:24 +0900 Subject: [PATCH] Disable DM if tab removed and do not add favs to more list if tab added --- .../instagrabber/activities/MainActivity.java | 31 ++++++----- .../TabOrderPreferenceDialogFragment.java | 10 +++- .../fragments/main/ProfileFragment.java | 52 +++++++++++-------- .../settings/GeneralPreferencesFragment.java | 19 +++++-- .../settings/MorePreferencesFragment.java | 26 +++++++--- app/src/main/res/values/strings.xml | 2 + 6 files changed, 92 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index fe5fa220..3e8139fa 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -116,6 +116,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage private boolean isBackStackEmpty = false; private boolean isLoggedIn; private HideBottomViewOnScrollBehavior behavior; + private List currentTabs; private final ServiceConnection serviceConnection = new ServiceConnection() { @Override @@ -357,12 +358,14 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage } final List result = new ArrayList<>(); if (isLoggedIn) { - if (body.getList() != null) result.addAll(searchHash ? body.getList() - .stream() - .filter(i -> i.getUser() == null) - .collect(Collectors.toList()) : body.getList()); - } - else { + if (body.getList() != null) { + result.addAll(searchHash ? body.getList() + .stream() + .filter(i -> i.getUser() == null) + .collect(Collectors.toList()) + : body.getList()); + } + } else { if (body.getUsers() != null && !searchHash) result.addAll(body.getUsers()); if (body.getHashtags() != null) result.addAll(body.getHashtags()); if (body.getPlaces() != null) result.addAll(body.getPlaces()); @@ -431,7 +434,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage } prevSuggestionAsync = searchService.search(isLoggedIn, searchUser || searchHash ? currentSearchQuery.substring(1) - : currentSearchQuery, + : currentSearchQuery, searchUser ? "user" : (searchHash ? "hashtag" : "blended")); suggestionAdapter.changeCursor(null); prevSuggestionAsync.enqueue(cb); @@ -462,13 +465,13 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage } private void setupBottomNavigationBar(final boolean setDefaultTabFromSettings) { - final List tabs = !isLoggedIn ? setupAnonBottomNav() : setupMainBottomNav(); + currentTabs = !isLoggedIn ? setupAnonBottomNav() : setupMainBottomNav(); - final List mainNavList = tabs.stream() - .map(Tab::getNavigationResId) - .collect(Collectors.toList()); + final List mainNavList = currentTabs.stream() + .map(Tab::getNavigationResId) + .collect(Collectors.toList()); if (setDefaultTabFromSettings) { - setSelectedTab(tabs); + setSelectedTab(currentTabs); } final LiveData navControllerLiveData = setupWithNavController( binding.bottomNavView, @@ -875,4 +878,8 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage public Toolbar getToolbar() { return binding.toolbar; } + + public List getCurrentTabs() { + return currentTabs; + } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/dialogs/TabOrderPreferenceDialogFragment.java b/app/src/main/java/awais/instagrabber/dialogs/TabOrderPreferenceDialogFragment.java index 83b4193f..0ac2dd23 100644 --- a/app/src/main/java/awais/instagrabber/dialogs/TabOrderPreferenceDialogFragment.java +++ b/app/src/main/java/awais/instagrabber/dialogs/TabOrderPreferenceDialogFragment.java @@ -92,7 +92,15 @@ public class TabOrderPreferenceDialogFragment extends DialogFragment { setSaveButtonState(newOrderTabs); // submit these tab lists to adapter if (adapter == null) return; - adapter.submitList(newOrderTabs, newOtherTabs, () -> list.postDelayed(() -> adapter.notifyDataSetChanged(), 500)); + adapter.submitList(newOrderTabs, newOtherTabs, () -> list.postDelayed(() -> { + adapter.notifyDataSetChanged(); + if (tab.getNavigationRootId() == R.id.direct_messages_nav_graph) { + final ConfirmDialogFragment dialogFragment = ConfirmDialogFragment.newInstance( + 111, 0, R.string.dm_remove_warning, R.string.ok, 0, 0 + ); + dialogFragment.show(getChildFragmentManager(), "dm_warning_dialog"); + } + }, 500)); } private void setSaveButtonState(final List newOrderTabs) { 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 a9b9f722..4c305f75 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -307,6 +307,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe private AccountRepository accountRepository; private FavoriteRepository favoriteRepository; private AppStateViewModel appStateViewModel; + private boolean disableDm = false; @Override public void onCreate(@Nullable final Bundle savedInstanceState) { @@ -322,8 +323,10 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe mediaService = isLoggedIn ? MediaService.getInstance(null, null, 0) : null; userService = isLoggedIn ? UserService.getInstance() : null; graphQLService = isLoggedIn ? null : GraphQLService.getInstance(); - accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(getContext())); - favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(getContext())); + final Context context = getContext(); + if (context == null) return; + accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(context)); + favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context)); appStateViewModel = new ViewModelProvider(fragmentActivity).get(AppStateViewModel.class); setHasOptionsMenu(true); } @@ -578,6 +581,9 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } private void init() { + disableDm = fragmentActivity.getCurrentTabs() + .stream() + .noneMatch(tab -> tab.getNavigationRootId() == R.id.direct_messages_nav_graph); if (getArguments() != null) { final ProfileFragmentArgs fragmentArgs = ProfileFragmentArgs.fromBundle(getArguments()); username = fragmentArgs.getUsername(); @@ -612,8 +618,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe username = profileModel.getUsername(); setUsernameDelayed(); setProfileDetails(); - } - else if (isLoggedIn) { + } else if (isLoggedIn) { userService.getUsernameInfo(usernameTemp, new ServiceCallback() { @Override public void onSuccess(final User user) { @@ -647,8 +652,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } catch (final Throwable ignored) {} } }); - } - else { + } else { graphQLService.fetchUser(usernameTemp, new ServiceCallback() { @Override public void onSuccess(final User user) { @@ -939,7 +943,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } profileDetailsBinding.btnSaved.setVisibility(View.GONE); profileDetailsBinding.btnLiked.setVisibility(View.GONE); - profileDetailsBinding.btnDM.setVisibility(View.VISIBLE); + profileDetailsBinding.btnDM.setVisibility(disableDm ? View.GONE : View.VISIBLE); profileDetailsBinding.btnFollow.setVisibility(View.VISIBLE); final Context context = getContext(); if (context == null) return; @@ -1116,23 +1120,25 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe PostItemType.TAGGED); NavHostFragment.findNavController(this).navigate(action); }); - profileDetailsBinding.btnDM.setOnClickListener(v -> { - profileDetailsBinding.btnDM.setEnabled(false); - new CreateThreadAction(cookie, profileModel.getPk(), thread -> { - if (thread == null) { - Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + if (!disableDm) { + profileDetailsBinding.btnDM.setOnClickListener(v -> { + profileDetailsBinding.btnDM.setEnabled(false); + new CreateThreadAction(cookie, profileModel.getPk(), thread -> { + if (thread == null) { + Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + profileDetailsBinding.btnDM.setEnabled(true); + return; + } + 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); - return; - } - 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(); - }); + }).execute(); + }); + } profileDetailsBinding.mainProfileImage.setOnClickListener(v -> { if (!hasStories) { // show profile pic diff --git a/app/src/main/java/awais/instagrabber/fragments/settings/GeneralPreferencesFragment.java b/app/src/main/java/awais/instagrabber/fragments/settings/GeneralPreferencesFragment.java index 62758946..69ed1f8e 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/GeneralPreferencesFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/settings/GeneralPreferencesFragment.java @@ -2,7 +2,6 @@ package awais.instagrabber.fragments.settings; import android.content.Context; import android.content.res.TypedArray; -import android.util.Log; import androidx.annotation.NonNull; import androidx.preference.ListPreference; @@ -13,6 +12,7 @@ import androidx.preference.SwitchPreferenceCompat; import java.util.List; import awais.instagrabber.R; +import awais.instagrabber.dialogs.ConfirmDialogFragment; import awais.instagrabber.dialogs.TabOrderPreferenceDialogFragment; import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.CookieUtils; @@ -34,9 +34,10 @@ public class GeneralPreferencesFragment extends BasePreferencesFragment implemen } screen.addPreference(getUpdateCheckPreference(context)); screen.addPreference(getFlagSecurePreference(context)); - final List preferences = FlavorSettings.getInstance().getPreferences(context, - getChildFragmentManager(), - SettingCategory.GENERAL); + final List preferences = FlavorSettings.getInstance() + .getPreferences(context, + getChildFragmentManager(), + SettingCategory.GENERAL); if (preferences != null) { for (final Preference preference : preferences) { screen.addPreference(preference); @@ -101,7 +102,15 @@ public class GeneralPreferencesFragment extends BasePreferencesFragment implemen @Override public void onSave(final boolean orderHasChanged) { - Log.d("", "onSave: " + orderHasChanged); + if (!orderHasChanged) return; + final ConfirmDialogFragment dialogFragment = ConfirmDialogFragment.newInstance( + 111, + 0, + R.string.tab_order_start_next_launch, + R.string.ok, + 0, + 0); + dialogFragment.show(getChildFragmentManager(), "tab_order_set_dialog"); } @Override diff --git a/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java b/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java index 54bf518f..30137d7b 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java @@ -27,6 +27,7 @@ import java.util.List; import awais.instagrabber.BuildConfig; import awais.instagrabber.R; import awais.instagrabber.activities.Login; +import awais.instagrabber.activities.MainActivity; import awais.instagrabber.databinding.PrefAccountSwitcherBinding; import awais.instagrabber.db.datasources.AccountDataSource; import awais.instagrabber.db.entities.Account; @@ -157,13 +158,24 @@ public class MorePreferencesFragment extends BasePreferencesFragment { return true; })); } - screen.addPreference(getPreference(R.string.title_favorites, R.drawable.ic_star_24, preference -> { - if (isSafeToNavigate(navController)) { - final NavDirections navDirections = MorePreferencesFragmentDirections.actionMorePreferencesFragmentToFavoritesFragment(); - navController.navigate(navDirections); - } - return true; - })); + + // Check if favorites has been added as a tab. And if so, do not add in this list + boolean showFavorites = true; + final MainActivity activity = (MainActivity) getActivity(); + if (activity != null && activity.getCurrentTabs() != null) { + showFavorites = activity.getCurrentTabs() + .stream() + .noneMatch(tab -> tab.getNavigationRootId() == R.id.favorites_nav_graph); + } + if (showFavorites) { + screen.addPreference(getPreference(R.string.title_favorites, R.drawable.ic_star_24, preference -> { + if (isSafeToNavigate(navController)) { + final NavDirections navDirections = MorePreferencesFragmentDirections.actionMorePreferencesFragmentToFavoritesFragment(); + navController.navigate(navDirections); + } + return true; + })); + } screen.addPreference(getDivider(context)); screen.addPreference(getPreference(R.string.action_settings, R.drawable.ic_outline_settings_24, preference -> { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 25bddb2c..b997d98c 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -477,4 +477,6 @@ Select an email app to send crash logs Screen order Other tabs + The tab order will be reflected on next launch + If saved, all DM related features will be disabled on next launch