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.asynctasks.SwitchAccount;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.events.ChangeInboxCountEvent; 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.events.SwitchAccountEvent;
import ml.docilealligator.infinityforreddit.fragments.InboxFragment; import ml.docilealligator.infinityforreddit.fragments.InboxFragment;
import ml.docilealligator.infinityforreddit.message.FetchMessage; import ml.docilealligator.infinityforreddit.message.FetchMessage;
import ml.docilealligator.infinityforreddit.message.Message;
import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.utils.Utils; 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 @Override
public void onLongPress() { public void onLongPress() {
if (sectionsPagerAdapter != null) { 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 @NonNull
@Override @Override
public Fragment createFragment(int position) { 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 com.google.android.material.snackbar.Snackbar;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.Executor; 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.asynctasks.LoadUserData;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed; 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.events.RepliedToPrivateMessageEvent;
import ml.docilealligator.infinityforreddit.message.Message; import ml.docilealligator.infinityforreddit.message.Message;
import ml.docilealligator.infinityforreddit.message.ReadMessage; import ml.docilealligator.infinityforreddit.message.ReadMessage;
@ -48,9 +51,10 @@ import retrofit2.Retrofit;
public class ViewPrivateMessagesActivity extends BaseActivity implements ActivityToolbarInterface { 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"; public static final String EXTRA_MESSAGE_POSITION = "EMP";
private static final String USER_AVATAR_STATE = "UAS"; 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) @BindView(R.id.linear_layout_view_private_messages_activity)
LinearLayout mLinearLayout; LinearLayout mLinearLayout;
@BindView(R.id.coordinator_layout_view_private_messages_activity) @BindView(R.id.coordinator_layout_view_private_messages_activity)
@ -111,15 +115,14 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit
ButterKnife.bind(this); ButterKnife.bind(this);
EventBus.getDefault().register(this);
applyCustomTheme(); applyCustomTheme();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isChangeStatusBarIconColor()) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isChangeStatusBarIconColor()) {
addOnOffsetChangedListener(mAppBarLayout); addOnOffsetChangedListener(mAppBarLayout);
} }
Intent intent = getIntent();
privateMessage = intent.getParcelableExtra(EXTRA_PRIVATE_MESSAGE);
setSupportActionBar(mToolbar); setSupportActionBar(mToolbar);
setToolbarGoToTop(mToolbar); setToolbarGoToTop(mToolbar);
@ -130,9 +133,16 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit
if (savedInstanceState != null) { if (savedInstanceState != null) {
mUserAvatar = savedInstanceState.getString(USER_AVATAR_STATE); 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(); bindView();
} }
} else {
EventBus.getDefault().post(new PassPrivateMessageIndexEvent(getIntent().getIntExtra(EXTRA_PRIVATE_MESSAGE_INDEX, -1)));
}
}
private void bindView() { private void bindView() {
if (privateMessage != null) { if (privateMessage != null) {
@ -275,6 +285,13 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit
protected void onSaveInstanceState(@NonNull Bundle outState) { protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putString(USER_AVATAR_STATE, mUserAvatar); outState.putString(USER_AVATAR_STATE, mUserAvatar);
outState.putParcelable(PRIVATE_MESSAGES_STATE, privateMessage);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
} }
@Override @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 { public interface ProvideUserAvatarCallback {
void fetchAvatarSuccess(String userAvatarUrl); 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_DATA = 0;
private static final int VIEW_TYPE_ERROR = 1; private static final int VIEW_TYPE_ERROR = 1;
private static final int VIEW_TYPE_LOADING = 2; 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 @Override
public boolean areItemsTheSame(@NonNull Message message, @NonNull Message t1) { public boolean areItemsTheSame(@NonNull Message message, @NonNull Message t1) {
return message.getId().equals(t1.getId()); return message.getId().equals(t1.getId());
@ -199,7 +199,7 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
mActivity.startActivity(intent); mActivity.startActivity(intent);
} else if (mMessageType == FetchMessage.MESSAGE_TYPE_PRIVATE_MESSAGE) { } else if (mMessageType == FetchMessage.MESSAGE_TYPE_PRIVATE_MESSAGE) {
Intent intent = new Intent(mActivity, ViewPrivateMessagesActivity.class); 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()); intent.putExtra(ViewPrivateMessagesActivity.EXTRA_MESSAGE_POSITION, holder.getBindingAdapterPosition());
mActivity.startActivity(intent); 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.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
@ -31,8 +32,8 @@ import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.FragmentCommunicator;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.NetworkState;
import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.adapters.MessageRecyclerViewAdapter; 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.customviews.LinearLayoutManagerBugFixed;
import ml.docilealligator.infinityforreddit.events.RepliedToPrivateMessageEvent; import ml.docilealligator.infinityforreddit.events.RepliedToPrivateMessageEvent;
import ml.docilealligator.infinityforreddit.message.FetchMessage; import ml.docilealligator.infinityforreddit.message.FetchMessage;
import ml.docilealligator.infinityforreddit.message.Message;
import ml.docilealligator.infinityforreddit.message.MessageViewModel; import ml.docilealligator.infinityforreddit.message.MessageViewModel;
import retrofit2.Retrofit; import retrofit2.Retrofit;
@ -214,6 +216,21 @@ public class InboxFragment extends Fragment implements FragmentCommunicator {
mAdapter.setNetworkState(null); 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 @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();