Fix app crashes when opening a very long private message.

This commit is contained in:
Docile-Alligator 2022-05-15 22:12:26 +08:00
parent 67d30b90e4
commit 955329cb87
6 changed files with 91 additions and 8 deletions

View File

@ -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) {

View File

@ -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,9 +133,16 @@ 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)));
}
}
private void bindView() {
if (privateMessage != null) {
@ -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);
}

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -0,0 +1,9 @@
package ml.docilealligator.infinityforreddit.events;
public class PassPrivateMessageIndexEvent {
public int privateMessageIndex;
public PassPrivateMessageIndexEvent(int privateMessageIndex) {
this.privateMessageIndex = privateMessageIndex;
}
}

View File

@ -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();