From da887298dfdcd8079d8770b21af2d29a7f443f94 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 22 Oct 2022 11:53:09 +0200 Subject: [PATCH] Fix some crashes --- .../app/fedilab/android/BaseMainActivity.java | 34 +++++++++++-------- .../android/activities/ComposeActivity.java | 2 +- .../android/activities/ProfileActivity.java | 2 +- .../android/helper/CrossActionHelper.java | 2 +- .../app/fedilab/android/helper/Helper.java | 2 +- .../ui/drawer/NotificationAdapter.java | 12 ++++--- .../FragmentNotificationsSettings.java | 8 ++++- .../timeline/FragmentMastodonTimeline.java | 22 ++++++++---- .../ui/pageadapter/FedilabPageAdapter.java | 13 ++++--- 9 files changed, 63 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/BaseMainActivity.java index 4aa32056..4374051b 100644 --- a/app/src/main/java/app/fedilab/android/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/BaseMainActivity.java @@ -612,6 +612,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt .makeSceneTransitionAnimation(BaseMainActivity.this, headerMainBinding.instanceInfoContainer, getString(R.string.activity_porfile_pp)); startActivity(intent, options.toBundle()); }); + headerMainBinding.accountAcc.setOnClickListener(v -> headerMainBinding.changeAccount.callOnClick()); headerMainBinding.changeAccount.setOnClickListener(v -> { headerMenuOpen = !headerMenuOpen; @@ -858,12 +859,28 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt MastodonHelper.loadProfileMediaMastodon(headerMainBinding.backgroundImage, currentAccount.mastodon_account, MastodonHelper.MediaAccountType.HEADER); /* * Some general data are loaded when the app starts such; + * - Pinned timelines (in app feature) * - Instance info (for limits) * - Emoji for picker * - Filters for timelines - * - Pinned timelines (in app feature) + */ + //Update pinned timelines + new ViewModelProvider(BaseMainActivity.this).get(TopBarVM.class).getDBPinned() + .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 + new ViewModelProvider(BaseMainActivity.this).get(TimelinesVM.class).getLists(currentInstance, currentToken) + .observe(this, mastodonLists -> + PinnedTimelineHelper.redrawTopBarPinned(BaseMainActivity.this, binding, pinned, bottomMenu, mastodonLists) + ); + }); + //Update emoji in db for the current instance new ViewModelProvider(BaseMainActivity.this).get(InstancesVM.class).getEmoji(currentInstance); //Retrieve instance info @@ -891,20 +908,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt } }).start(); }); - //Update pinned timelines - new ViewModelProvider(BaseMainActivity.this).get(TopBarVM.class).getDBPinned() - .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 - new ViewModelProvider(BaseMainActivity.this).get(TimelinesVM.class).getLists(currentInstance, currentToken) - .observe(this, mastodonLists -> - PinnedTimelineHelper.redrawTopBarPinned(BaseMainActivity.this, binding, pinned, bottomMenu, mastodonLists) - ); - }); + }; mainHandler.post(myRunnable); }).start(); diff --git a/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java b/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java index 54df8ac8..d3aecaf2 100644 --- a/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java @@ -873,7 +873,7 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana private boolean canBeSent(StatusDraft statusDraft) { - if (statusDraft == null || statusDraft.statusDraftList == null || statusDraft.statusDraftList.size() == 0) { + if (statusDraft == null || statusDraft.statusDraftList == null || statusDraft.statusDraftList.isEmpty()) { return false; } Status statusCheck = statusDraft.statusDraftList.get(0); diff --git a/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java b/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java index 2de680bc..569016b3 100644 --- a/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java @@ -666,7 +666,7 @@ public class ProfileActivity extends BaseActivity { String[] splitAcct = account.acct.split("@"); SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(ProfileActivity.this); AlertDialog.Builder builderInner = null; - final boolean isOwner = account.id.compareToIgnoreCase(BaseMainActivity.currentUserID) == 0; + final boolean isOwner = account.id != null && BaseMainActivity.currentUserID != null && account.id.compareToIgnoreCase(BaseMainActivity.currentUserID) == 0; final String[] stringArrayConf; if (isOwner) { stringArrayConf = getResources().getStringArray(R.array.more_action_owner_confirm); diff --git a/app/src/main/java/app/fedilab/android/helper/CrossActionHelper.java b/app/src/main/java/app/fedilab/android/helper/CrossActionHelper.java index 97b7fdc1..5e279050 100644 --- a/app/src/main/java/app/fedilab/android/helper/CrossActionHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/CrossActionHelper.java @@ -151,7 +151,7 @@ public class CrossActionHelper { } else if (targetedStatus != null) { searchVM.search(ownerAccount.instance, ownerAccount.token, targetedStatus.uri, null, "statuses", false, true, false, 0, null, null, 1) .observe((LifecycleOwner) context, results -> { - if (results.statuses != null && results.statuses.size() > 0) { + if (results != null && results.statuses != null && results.statuses.size() > 0) { Status status = results.statuses.get(0); applyAction(context, actionType, ownerAccount, null, status); } else { 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 7dce373f..1d375e74 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -252,7 +252,7 @@ public class Helper { public static final String ARG_MEDIA_ATTACHMENT = "ARG_MEDIA_ATTACHMENT"; public static final String ARG_SHOW_REPLIES = "ARG_SHOW_REPLIES"; public static final String ARG_SHOW_REBLOGS = "ARG_SHOW_REBLOGS"; - + public static final String ARG_INITIALIZE_VIEW = "ARG_INITIALIZE_VIEW"; public static final String ARG_SHOW_PINNED = "ARG_SHOW_PINNED"; public static final String ARG_SHOW_MEDIA_ONY = "ARG_SHOW_MEDIA_ONY"; public static final String ARG_MENTION = "ARG_MENTION"; diff --git a/app/src/main/java/app/fedilab/android/ui/drawer/NotificationAdapter.java b/app/src/main/java/app/fedilab/android/ui/drawer/NotificationAdapter.java index 04c31fad..0739a66c 100644 --- a/app/src/main/java/app/fedilab/android/ui/drawer/NotificationAdapter.java +++ b/app/src/main/java/app/fedilab/android/ui/drawer/NotificationAdapter.java @@ -155,13 +155,17 @@ public class NotificationAdapter extends RecyclerView.Adapter accountsVM.rejectFollow(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, notification.account.id) .observe((LifecycleOwner) context, relationShip -> { - notificationList.remove(position); - notifyItemRemoved(position); + if (notificationList.size() > position) { + notificationList.remove(position); + notifyItemRemoved(position); + } })); holderFollow.binding.acceptButton.setOnClickListener(v -> accountsVM.acceptFollow(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, notification.account.id) .observe((LifecycleOwner) context, relationShip -> { - notificationList.remove(position); - notifyItemRemoved(position); + if (notificationList.size() > position) { + notificationList.remove(position); + notifyItemRemoved(position); + } })); holderFollow.binding.avatar.setOnClickListener(v -> { Intent intent = new Intent(context, ProfileActivity.class); diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/settings/FragmentNotificationsSettings.java b/app/src/main/java/app/fedilab/android/ui/fragment/settings/FragmentNotificationsSettings.java index e7baefef..e88aadd5 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/settings/FragmentNotificationsSettings.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/settings/FragmentNotificationsSettings.java @@ -221,7 +221,13 @@ public class FragmentNotificationsSettings extends PreferenceFragmentCompat impl try { int value = sharedPreferences.getInt(key, 0); sharedPreferences.edit().putInt(getString(R.string.SET_LED_COLOUR_VAL), value).apply(); - } catch (NumberFormatException e) { + } catch (Exception e) { + try { + String value = sharedPreferences.getString(key, "0"); + sharedPreferences.edit().putInt(getString(R.string.SET_LED_COLOUR_VAL), Integer.parseInt(value)).apply(); + } catch (Exception e2) { + e.printStackTrace(); + } e.printStackTrace(); } 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 1f3e9742..3f6b5bf9 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 @@ -145,7 +145,7 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. @Override public void onResume() { super.onResume(); - if (slug.compareTo(slugOfFirstFragment) != 0 && !isViewInitialized) { + if (slug != null && slugOfFirstFragment != null && slug.compareTo(slugOfFirstFragment) != 0 && !isViewInitialized) { isViewInitialized = true; initializeStatusesCommonView(initialStatuses); } @@ -220,7 +220,7 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. canBeFederated = false; } } - + isViewInitialized = getArguments().getBoolean(Helper.ARG_INITIALIZE_VIEW, true); tagTimeline = (TagTimeline) getArguments().getSerializable(Helper.ARG_TAG_TIMELINE); accountTimeline = (Account) getArguments().getSerializable(Helper.ARG_ACCOUNT); exclude_replies = !getArguments().getBoolean(Helper.ARG_SHOW_REPLIES, true); @@ -252,8 +252,15 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. if (timelineType != null) { slug = timelineType.getValue() + (ident != null ? "|" + ident : ""); } - //Only the first fragment will initialize its view - isViewInitialized = slug.compareTo(slugOfFirstFragment) == 0; + //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 { + isViewInitialized = timelineType.compareTo(Timeline.TimeLineEnum.HOME) == 0; + } + } SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity()); //Retrieve the max_id to keep position @@ -541,7 +548,7 @@ 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) { + if (direction == null && !isViewInitialized) { isViewInitialized = slug.compareTo(slugOfFirstFragment) == 0; } if (binding == null || getActivity() == null || !isAdded()) { @@ -626,7 +633,10 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. if (isAdded()) { storeMarker(); } - LocalBroadcastManager.getInstance(requireActivity()).unregisterReceiver(receive_action); + try { + LocalBroadcastManager.getInstance(requireActivity()).unregisterReceiver(receive_action); + } catch (Exception ignored) { + } super.onDestroyView(); } diff --git a/app/src/main/java/app/fedilab/android/ui/pageadapter/FedilabPageAdapter.java b/app/src/main/java/app/fedilab/android/ui/pageadapter/FedilabPageAdapter.java index e8f740f1..4f1fe6b9 100644 --- a/app/src/main/java/app/fedilab/android/ui/pageadapter/FedilabPageAdapter.java +++ b/app/src/main/java/app/fedilab/android/ui/pageadapter/FedilabPageAdapter.java @@ -90,7 +90,7 @@ public class FedilabPageAdapter extends FragmentStatePagerAdapter { FragmentMastodonTimeline fragment = new FragmentMastodonTimeline(); fragment.update = activity; Bundle bundle = new Bundle(); - //Position 3 is for notifications + bundle.putBoolean(Helper.ARG_INITIALIZE_VIEW, false); if (position < (BOTTOM_TIMELINE_COUNT - toRemove) && !singleBar) { if (bottomMenu != null) { BottomMenu.ItemMenuType type = BottomMenu.getType(bottomMenu, position); @@ -98,9 +98,13 @@ public class FedilabPageAdapter extends FragmentStatePagerAdapter { return fragment; } if (type == BottomMenu.ItemMenuType.NOTIFICATION) { - return new FragmentNotificationContainer(); + FragmentNotificationContainer fragmentNotificationContainer = new FragmentNotificationContainer(); + fragmentNotificationContainer.setArguments(bundle); + return fragmentNotificationContainer; } else if (type == BottomMenu.ItemMenuType.DIRECT) { - return new FragmentMastodonConversation(); + FragmentMastodonConversation fragmentMastodonConversation = new FragmentMastodonConversation(); + fragmentMastodonConversation.setArguments(bundle); + return fragmentMastodonConversation; } if (type == BottomMenu.ItemMenuType.HOME) { //Home timeline bundle.putSerializable(Helper.ARG_TIMELINE_TYPE, Timeline.TimeLineEnum.HOME); @@ -120,12 +124,13 @@ public class FedilabPageAdapter extends FragmentStatePagerAdapter { if (pinnedTimeline.type == Timeline.TimeLineEnum.NOTIFICATION) { FragmentNotificationContainer fragmentNotificationContainer = new FragmentNotificationContainer(); FragmentNotificationContainer.update = activity; + fragmentNotificationContainer.setArguments(bundle); return fragmentNotificationContainer; } else if (pinnedTimeline.type == Timeline.TimeLineEnum.DIRECT) { FragmentMastodonConversation fragmentMastodonConversation = new FragmentMastodonConversation(); fragmentMastodonConversation.update = activity; + fragmentMastodonConversation.setArguments(bundle); return fragmentMastodonConversation; - } else if (pinnedTimeline.type == Timeline.TimeLineEnum.LIST) { bundle.putString(Helper.ARG_LIST_ID, pinnedTimeline.mastodonList.id); } else if (pinnedTimeline.type == Timeline.TimeLineEnum.TAG) {