diff options
author | Docile-Alligator <chineseperson5@gmail.com> | 2022-05-15 14:12:26 +0000 |
---|---|---|
committer | Docile-Alligator <chineseperson5@gmail.com> | 2022-05-15 14:12:26 +0000 |
commit | 955329cb875cc9b4e03e08c811345df05142e2a9 (patch) | |
tree | 4005e5ea185122218b9107418d5c1d7648904381 /app/src/main | |
parent | 67d30b90e445163b09e325f8fc28154037f93266 (diff) | |
download | infinity-for-reddit-955329cb875cc9b4e03e08c811345df05142e2a9.tar infinity-for-reddit-955329cb875cc9b4e03e08c811345df05142e2a9.tar.gz infinity-for-reddit-955329cb875cc9b4e03e08c811345df05142e2a9.tar.bz2 infinity-for-reddit-955329cb875cc9b4e03e08c811345df05142e2a9.tar.lz infinity-for-reddit-955329cb875cc9b4e03e08c811345df05142e2a9.tar.xz infinity-for-reddit-955329cb875cc9b4e03e08c811345df05142e2a9.tar.zst infinity-for-reddit-955329cb875cc9b4e03e08c811345df05142e2a9.zip |
Fix app crashes when opening a very long private message.
Diffstat (limited to '')
6 files changed, 91 insertions, 8 deletions
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<Message, Recycl private static final int VIEW_TYPE_DATA = 0; private static final int VIEW_TYPE_ERROR = 1; private static final int VIEW_TYPE_LOADING = 2; - private static final DiffUtil.ItemCallback<Message> DIFF_CALLBACK = new DiffUtil.ItemCallback<Message>() { + private static final DiffUtil.ItemCallback<Message> 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<Message, Recycl mActivity.startActivity(intent); } else if (mMessageType == FetchMessage.MESSAGE_TYPE_PRIVATE_MESSAGE) { Intent intent = new Intent(mActivity, ViewPrivateMessagesActivity.class); - intent.putExtra(ViewPrivateMessagesActivity.EXTRA_PRIVATE_MESSAGE, message); + intent.putExtra(ViewPrivateMessagesActivity.EXTRA_PRIVATE_MESSAGE_INDEX, holder.getBindingAdapterPosition()); intent.putExtra(ViewPrivateMessagesActivity.EXTRA_MESSAGE_POSITION, holder.getBindingAdapterPosition()); mActivity.startActivity(intent); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/events/PassPrivateMessageEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/events/PassPrivateMessageEvent.java new file mode 100644 index 00000000..670dc19c --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/events/PassPrivateMessageEvent.java @@ -0,0 +1,11 @@ +package ml.docilealligator.infinityforreddit.events; + +import ml.docilealligator.infinityforreddit.message.Message; + +public class PassPrivateMessageEvent { + public Message message; + + public PassPrivateMessageEvent(Message message) { + this.message = message; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/events/PassPrivateMessageIndexEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/events/PassPrivateMessageIndexEvent.java new file mode 100644 index 00000000..7e9f4d8e --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/events/PassPrivateMessageIndexEvent.java @@ -0,0 +1,9 @@ +package ml.docilealligator.infinityforreddit.events; + +public class PassPrivateMessageIndexEvent { + public int privateMessageIndex; + + public PassPrivateMessageIndexEvent(int privateMessageIndex) { + this.privateMessageIndex = privateMessageIndex; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/InboxFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/InboxFragment.java index 0ff23330..cc1ef512 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/InboxFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/InboxFragment.java @@ -13,6 +13,7 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; +import androidx.paging.PagedList; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.RecyclerView; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; @@ -31,8 +32,8 @@ import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.NetworkState; -import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.adapters.MessageRecyclerViewAdapter; @@ -40,6 +41,7 @@ import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed; import ml.docilealligator.infinityforreddit.events.RepliedToPrivateMessageEvent; import ml.docilealligator.infinityforreddit.message.FetchMessage; +import ml.docilealligator.infinityforreddit.message.Message; import ml.docilealligator.infinityforreddit.message.MessageViewModel; import retrofit2.Retrofit; @@ -214,6 +216,21 @@ public class InboxFragment extends Fragment implements FragmentCommunicator { mAdapter.setNetworkState(null); } + public Message getMessageByIndex(int index) { + if (mMessageViewModel == null || index < 0) { + return null; + } + PagedList<Message> 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(); |