From 955329cb875cc9b4e03e08c811345df05142e2a9 Mon Sep 17 00:00:00 2001 From: Docile-Alligator Date: Sun, 15 May 2022 22:12:26 +0800 Subject: Fix app crashes when opening a very long private message. --- .../activities/InboxActivity.java | 21 +++++++++++++ .../activities/ViewPrivateMessagesActivity.java | 35 ++++++++++++++++++---- .../adapters/MessageRecyclerViewAdapter.java | 4 +-- .../events/PassPrivateMessageEvent.java | 11 +++++++ .../events/PassPrivateMessageIndexEvent.java | 9 ++++++ .../infinityforreddit/fragments/InboxFragment.java | 19 +++++++++++- 6 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/events/PassPrivateMessageEvent.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/events/PassPrivateMessageIndexEvent.java (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java index c6c98e61..d978e42a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java @@ -54,9 +54,12 @@ import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.ChangeInboxCountEvent; +import ml.docilealligator.infinityforreddit.events.PassPrivateMessageEvent; +import ml.docilealligator.infinityforreddit.events.PassPrivateMessageIndexEvent; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.InboxFragment; import ml.docilealligator.infinityforreddit.message.FetchMessage; +import ml.docilealligator.infinityforreddit.message.Message; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -378,6 +381,13 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf } } + @Subscribe + public void onPassPrivateMessageIndexEvent(PassPrivateMessageIndexEvent event) { + if (sectionsPagerAdapter != null) { + EventBus.getDefault().post(new PassPrivateMessageEvent(sectionsPagerAdapter.getPrivateMessage(event.privateMessageIndex))); + } + } + @Override public void onLongPress() { if (sectionsPagerAdapter != null) { @@ -442,6 +452,17 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf } } + Message getPrivateMessage(int index) { + if (viewPager2 == null || fragmentManager == null) { + return null; + } + Fragment fragment = fragmentManager.findFragmentByTag("f" + viewPager2.getCurrentItem()); + if (fragment instanceof InboxFragment) { + return ((InboxFragment) fragment).getMessageByIndex(index); + } + return null; + } + @NonNull @Override public Fragment createFragment(int position) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java index 40b858c6..d8269c4b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java @@ -22,6 +22,7 @@ import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.snackbar.Snackbar; import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; import java.util.ArrayList; import java.util.concurrent.Executor; @@ -39,6 +40,8 @@ import ml.docilealligator.infinityforreddit.adapters.PrivateMessagesDetailRecycl import ml.docilealligator.infinityforreddit.asynctasks.LoadUserData; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed; +import ml.docilealligator.infinityforreddit.events.PassPrivateMessageEvent; +import ml.docilealligator.infinityforreddit.events.PassPrivateMessageIndexEvent; import ml.docilealligator.infinityforreddit.events.RepliedToPrivateMessageEvent; import ml.docilealligator.infinityforreddit.message.Message; import ml.docilealligator.infinityforreddit.message.ReadMessage; @@ -48,9 +51,10 @@ import retrofit2.Retrofit; public class ViewPrivateMessagesActivity extends BaseActivity implements ActivityToolbarInterface { - public static final String EXTRA_PRIVATE_MESSAGE = "EPM"; + public static final String EXTRA_PRIVATE_MESSAGE_INDEX = "EPM"; public static final String EXTRA_MESSAGE_POSITION = "EMP"; private static final String USER_AVATAR_STATE = "UAS"; + private static final String PRIVATE_MESSAGES_STATE = "PMS"; @BindView(R.id.linear_layout_view_private_messages_activity) LinearLayout mLinearLayout; @BindView(R.id.coordinator_layout_view_private_messages_activity) @@ -111,15 +115,14 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit ButterKnife.bind(this); + EventBus.getDefault().register(this); + applyCustomTheme(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isChangeStatusBarIconColor()) { addOnOffsetChangedListener(mAppBarLayout); } - Intent intent = getIntent(); - privateMessage = intent.getParcelableExtra(EXTRA_PRIVATE_MESSAGE); - setSupportActionBar(mToolbar); setToolbarGoToTop(mToolbar); @@ -130,8 +133,15 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit if (savedInstanceState != null) { mUserAvatar = savedInstanceState.getString(USER_AVATAR_STATE); + privateMessage = savedInstanceState.getParcelable(PRIVATE_MESSAGES_STATE); + if (privateMessage == null) { + EventBus.getDefault().post(new PassPrivateMessageIndexEvent(getIntent().getIntExtra(EXTRA_PRIVATE_MESSAGE_INDEX, -1))); + } else { + bindView(); + } + } else { + EventBus.getDefault().post(new PassPrivateMessageIndexEvent(getIntent().getIntExtra(EXTRA_PRIVATE_MESSAGE_INDEX, -1))); } - bindView(); } private void bindView() { @@ -275,6 +285,13 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); outState.putString(USER_AVATAR_STATE, mUserAvatar); + outState.putParcelable(PRIVATE_MESSAGES_STATE, privateMessage); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); } @Override @@ -310,6 +327,14 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit } } + @Subscribe + public void onPassPrivateMessageEvent(PassPrivateMessageEvent passPrivateMessageEvent) { + privateMessage = passPrivateMessageEvent.message; + if (privateMessage != null) { + bindView(); + } + } + public interface ProvideUserAvatarCallback { void fetchAvatarSuccess(String userAvatarUrl); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java index 1fd5f7f0..822625b3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java @@ -55,7 +55,7 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter DIFF_CALLBACK = new DiffUtil.ItemCallback() { + private static final DiffUtil.ItemCallback DIFF_CALLBACK = new DiffUtil.ItemCallback<>() { @Override public boolean areItemsTheSame(@NonNull Message message, @NonNull Message t1) { return message.getId().equals(t1.getId()); @@ -199,7 +199,7 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter messages = mMessageViewModel.getMessages().getValue(); + if (messages == null) { + return null; + } + if (index >= messages.size()) { + return null; + } + + return messages.get(index); + } + @Override public void onDestroy() { super.onDestroy(); -- cgit v1.2.3