From ea31842dba63bf4092fc84ffb2a0a84075b7e92a Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 23 Oct 2022 11:59:50 +0200 Subject: [PATCH] Fix crashes --- .../app/fedilab/android/BaseMainActivity.java | 3 +-- .../app/fedilab/android/helper/Helper.java | 24 +++++++++++++++++++ .../android/helper/PinnedTimelineHelper.java | 20 ++++++++++------ .../FragmentMastodonConversation.java | 7 ++++-- .../FragmentMastodonNotification.java | 7 +++--- .../timeline/FragmentMastodonTimeline.java | 22 +++++++---------- 6 files changed, 55 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/BaseMainActivity.java index 4374051b..e1b53a11 100644 --- a/app/src/main/java/app/fedilab/android/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/BaseMainActivity.java @@ -164,7 +164,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt public static String regex_home, regex_local, regex_public; public static BaseAccount currentAccount; Fragment currentFragment; - public static String slugOfFirstFragment; + private AppBarConfiguration mAppBarConfiguration; private ActivityMainBinding binding; private final BroadcastReceiver broadcast_error_message = new BroadcastReceiver() { @@ -871,7 +871,6 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt .observe(this, pinned -> { this.pinned = pinned; //Initialize the slug of the first fragment - slugOfFirstFragment = PinnedTimelineHelper.firstTimelineSlug(BaseMainActivity.this, pinned, bottomMenu); //First it's taken from db (last stored values) PinnedTimelineHelper.redrawTopBarPinned(BaseMainActivity.this, binding, pinned, bottomMenu, null); //Fetch remote lists for the authenticated account and update them diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index 1d375e74..52aa9134 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -153,6 +153,7 @@ import app.fedilab.android.client.entities.api.Status; import app.fedilab.android.client.entities.app.Account; import app.fedilab.android.client.entities.app.BaseAccount; import app.fedilab.android.client.entities.app.ReleaseNote; +import app.fedilab.android.client.entities.app.Timeline; import app.fedilab.android.databinding.PopupReleaseNotesBinding; import app.fedilab.android.exception.DBException; import app.fedilab.android.interfaces.OnDownloadInterface; @@ -217,6 +218,7 @@ public class Helper { public static final String ARG_STATUS_DRAFT = "ARG_STATUS_DRAFT"; public static final String ARG_STATUS_SCHEDULED = "ARG_STATUS_SCHEDULED"; + public static final String ARG_SLUG_OF_FIRST_FRAGMENT = "ARG_SLUG_OF_FIRST_FRAGMENT"; public static final String ARG_STATUS_DRAFT_ID = "ARG_STATUS_DRAFT_ID"; public static final String ARG_STATUS_REPLY = "ARG_STATUS_REPLY"; @@ -1871,4 +1873,26 @@ public class Helper { public interface OnAttachmentCopied { void onAttachmentCopied(Attachment attachment); } + + + //Allow to store in shared preference first visible fragment when the app starts + private static String slugOfFirstFragment; + + public static String getSlugOfFirstFragment(Context context, String userId, String instance) { + if (slugOfFirstFragment != null) { + return slugOfFirstFragment; + } + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context); + return sharedpreferences.getString(Helper.ARG_SLUG_OF_FIRST_FRAGMENT + userId + instance, Timeline.TimeLineEnum.HOME.getValue()); + } + + public static void setSlugOfFirstFragment(Context context, String slug, String userId, String instance) { + if (slug != null) { + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences.Editor editor = sharedpreferences.edit(); + slugOfFirstFragment = slug; + editor.putString(Helper.ARG_SLUG_OF_FIRST_FRAGMENT + userId + instance, slug); + editor.apply(); + } + } } diff --git a/app/src/main/java/app/fedilab/android/helper/PinnedTimelineHelper.java b/app/src/main/java/app/fedilab/android/helper/PinnedTimelineHelper.java index ba4fb6ea..9a21b33a 100644 --- a/app/src/main/java/app/fedilab/android/helper/PinnedTimelineHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/PinnedTimelineHelper.java @@ -102,17 +102,19 @@ public class PinnedTimelineHelper { * @return String - slug */ public static String firstTimelineSlug(Context context, Pinned pinned, BottomMenu bottomMenu) { - String slug = null; + String slug = Timeline.TimeLineEnum.HOME.getValue(); SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context); boolean singleBar = sharedpreferences.getBoolean(context.getString(R.string.SET_USE_SINGLE_TOPBAR), false); PinnedTimeline pinnedTimelineMin = null; if (singleBar) { - for (PinnedTimeline pinnedTimeline : pinned.pinnedTimelines) { - if (pinnedTimeline.displayed) { - if (pinnedTimelineMin == null) { - pinnedTimelineMin = pinnedTimeline; - } else if (pinnedTimelineMin.position > pinnedTimeline.position) { - pinnedTimelineMin = pinnedTimeline; + if (pinned != null && pinned.pinnedTimelines != null) { + for (PinnedTimeline pinnedTimeline : pinned.pinnedTimelines) { + if (pinnedTimeline.displayed) { + if (pinnedTimelineMin == null) { + pinnedTimelineMin = pinnedTimeline; + } else if (pinnedTimelineMin.position > pinnedTimeline.position) { + pinnedTimelineMin = pinnedTimeline; + } } } } @@ -155,6 +157,10 @@ public class PinnedTimelineHelper { if (pinned.pinnedTimelines == null) { pinned.pinnedTimelines = new ArrayList<>(); } + //Set the slug of first visible fragment + String slugOfFirstFragment = PinnedTimelineHelper.firstTimelineSlug(activity, pinned, bottomMenu); + Helper.setSlugOfFirstFragment(activity, slugOfFirstFragment, currentUserID, currentInstance); + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(activity); boolean singleBar = sharedpreferences.getBoolean(activity.getString(R.string.SET_USE_SINGLE_TOPBAR), false); boolean timeInList = sharedpreferences.getBoolean(activity.getString(R.string.SET_TIMELINES_IN_A_LIST), false); diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java index 4320383f..57b04639 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java @@ -15,7 +15,8 @@ package app.fedilab.android.ui.fragment.timeline; * see . */ -import static app.fedilab.android.BaseMainActivity.slugOfFirstFragment; +import static app.fedilab.android.BaseMainActivity.currentInstance; +import static app.fedilab.android.BaseMainActivity.currentUserID; import android.content.SharedPreferences; import android.os.Bundle; @@ -42,6 +43,7 @@ import app.fedilab.android.client.entities.app.StatusCache; import app.fedilab.android.client.entities.app.Timeline; import app.fedilab.android.databinding.FragmentPaginationBinding; import app.fedilab.android.exception.DBException; +import app.fedilab.android.helper.Helper; import app.fedilab.android.helper.MastodonHelper; import app.fedilab.android.helper.ThemeHelper; import app.fedilab.android.ui.drawer.ConversationAdapter; @@ -83,7 +85,7 @@ public class FragmentMastodonConversation extends Fragment implements Conversati @Override public void onResume() { super.onResume(); - if (Timeline.TimeLineEnum.CONVERSATION.getValue().compareTo(slugOfFirstFragment) != 0 && !isViewInitialized) { + if (Timeline.TimeLineEnum.CONVERSATION.getValue().compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) != 0 && !isViewInitialized) { isViewInitialized = true; initializeConversationCommonView(initialConversations); } @@ -218,6 +220,7 @@ public class FragmentMastodonConversation extends Fragment implements Conversati public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); + isViewInitialized = Timeline.TimeLineEnum.CONVERSATION.getValue().compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) == 0; int c1 = getResources().getColor(R.color.cyanea_accent_reference); binding.swipeContainer.setProgressBackgroundColorSchemeColor(getResources().getColor(R.color.cyanea_primary_reference)); binding.swipeContainer.setColorSchemeColors( diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java index 8265ead6..fa2b61e8 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java @@ -14,7 +14,8 @@ package app.fedilab.android.ui.fragment.timeline; * You should have received a copy of the GNU General Public License along with Fedilab; if not, * see . */ -import static app.fedilab.android.BaseMainActivity.slugOfFirstFragment; +import static app.fedilab.android.BaseMainActivity.currentInstance; +import static app.fedilab.android.BaseMainActivity.currentUserID; import android.content.BroadcastReceiver; import android.content.Context; @@ -138,7 +139,7 @@ public class FragmentMastodonNotification extends Fragment implements Notificati ViewGroup container, Bundle savedInstanceState) { flagLoading = false; - isViewInitialized = Timeline.TimeLineEnum.NOTIFICATION.getValue().compareTo(slugOfFirstFragment) == 0; + isViewInitialized = Timeline.TimeLineEnum.NOTIFICATION.getValue().compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) == 0; binding = FragmentPaginationBinding.inflate(inflater, container, false); View root = binding.getRoot(); if (getArguments() != null) { @@ -294,7 +295,7 @@ public class FragmentMastodonNotification extends Fragment implements Notificati @Override public void onResume() { super.onResume(); - if (Timeline.TimeLineEnum.NOTIFICATION.getValue().compareTo(slugOfFirstFragment) != 0 && !isViewInitialized) { + if (Timeline.TimeLineEnum.NOTIFICATION.getValue().compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) != 0 && !isViewInitialized) { isViewInitialized = true; initializeNotificationView(initialNotifications); } diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java index 710e0c8e..f9d91c60 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java @@ -15,8 +15,9 @@ package app.fedilab.android.ui.fragment.timeline; * see . */ +import static app.fedilab.android.BaseMainActivity.currentInstance; +import static app.fedilab.android.BaseMainActivity.currentUserID; import static app.fedilab.android.BaseMainActivity.networkAvailable; -import static app.fedilab.android.BaseMainActivity.slugOfFirstFragment; import android.content.BroadcastReceiver; import android.content.Context; @@ -145,7 +146,7 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. @Override public void onResume() { super.onResume(); - if (slug != null && slugOfFirstFragment != null && slug.compareTo(slugOfFirstFragment) != 0 && !isViewInitialized) { + if (slug != null && slug.compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) != 0 && !isViewInitialized) { isViewInitialized = true; initializeStatusesCommonView(initialStatuses); } @@ -255,10 +256,8 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. //Only fragment in main view pager should not have the view initialized //AND Only the first fragment will initialize its view if (!isViewInitialized) { - if (slug != null && slugOfFirstFragment != null) { - isViewInitialized = slug.compareTo(slugOfFirstFragment) == 0; - } else if (timelineType != null) { - isViewInitialized = timelineType.compareTo(Timeline.TimeLineEnum.HOME) == 0; + if (slug != null) { + isViewInitialized = slug.compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) == 0; } } SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity()); @@ -546,8 +545,8 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. * @param direction - DIRECTION null if first call, then is set to TOP or BOTTOM depending of scroll */ private void routeCommon(DIRECTION direction, boolean fetchingMissing, Status status) { - if (direction == null && !isViewInitialized && slug != null && slugOfFirstFragment != null) { - isViewInitialized = slug.compareTo(slugOfFirstFragment) == 0; + if (direction == null && !isViewInitialized && slug != null) { + isViewInitialized = slug.compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) == 0; } if (binding == null || getActivity() == null || !isAdded()) { return; @@ -604,18 +603,13 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. boolean useCache = sharedpreferences.getBoolean(getString(R.string.SET_USE_CACHE), true); Handler handler = new Handler(); - //The action for fetching new messages is delayed for other timelines than HOME - if (slugOfFirstFragment == null) { - slugOfFirstFragment = slug; - } - handler.postDelayed(() -> { if (useCache && direction != DIRECTION.SCROLL_TOP && direction != DIRECTION.FETCH_NEW) { getCachedStatus(direction, fetchingMissing, timelineParams); } else { getLiveStatus(direction, fetchingMissing, timelineParams, status); } - }, slug.compareTo(slugOfFirstFragment) == 0 ? 0 : 1000); + }, slug.compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) == 0 ? 0 : 1000); }