Open message tab in InboxActivity when opening a message notification.

This commit is contained in:
Alex Ning 2020-07-02 13:12:36 +08:00
parent 1958ad77e3
commit 3a72c5a1f8
7 changed files with 21 additions and 18 deletions

View File

@ -49,6 +49,7 @@ import retrofit2.Retrofit;
public class InboxActivity extends BaseActivity implements ActivityToolbarInterface {
public static final String EXTRA_NEW_ACCOUNT_NAME = "ENAN";
public static final String EXTRA_VIEW_MESSAGE = "EVM";
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
private static final String ACCESS_TOKEN_STATE = "ATS";
@ -123,13 +124,13 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf
mNewAccountName = savedInstanceState.getString(NEW_ACCOUNT_NAME_STATE);
if (!mNullAccessToken && mAccessToken == null) {
getCurrentAccountAndFetchMessage();
getCurrentAccountAndFetchMessage(savedInstanceState);
} else {
bindView();
bindView(savedInstanceState);
}
} else {
mNewAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME);
getCurrentAccountAndFetchMessage();
getCurrentAccountAndFetchMessage(savedInstanceState);
}
}
@ -150,7 +151,7 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf
applyTabLayoutTheme(tabLayout);
}
private void getCurrentAccountAndFetchMessage() {
private void getCurrentAccountAndFetchMessage(Bundle savedInstanceState) {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if (mNewAccountName != null) {
if (account == null || !account.getUsername().equals(mNewAccountName)) {
@ -165,11 +166,11 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf
mAccessToken = newAccount.getAccessToken();
}
bindView();
bindView(savedInstanceState);
}).execute();
} else {
mAccessToken = account.getAccessToken();
bindView();
bindView(savedInstanceState);
}
} else {
if (account == null) {
@ -178,12 +179,12 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf
mAccessToken = account.getAccessToken();
}
bindView();
bindView(savedInstanceState);
}
}).execute();
}
private void bindView() {
private void bindView(Bundle savedInstanceState) {
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
@ -198,6 +199,9 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf
viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setOffscreenPageLimit(2);
tabLayout.setupWithViewPager(viewPager);
if (savedInstanceState == null && getIntent().getBooleanExtra(EXTRA_VIEW_MESSAGE, false)) {
viewPager.setCurrentItem(1);
}
}
@Override

View File

@ -106,10 +106,8 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
mAccessToken = accessToken;
if (where.equals(FetchMessages.WHERE_MESSAGES)) {
mMessageType = FetchMessages.MESSAGE_TYPE_PRIVATE_MESSAGE;
} else if (where.equals(FetchMessages.WHERE_MESSAGES_DETAIL)) {
mMessageType = FetchMessages.MESSAGE_TYPE_PRIVATE_MESSAGE_DETAIL;
} else {
mMessageType = FetchMessages.MESSAGE_TYPE_NOTIFICATION;
mMessageType = FetchMessages.MESSAGE_TYPE_INBOX;
}
mColorAccent = customThemeWrapper.getColorAccent();
@ -163,7 +161,7 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
mMarkwon.setMarkdown(((DataViewHolder) holder).contentCustomMarkwonView, displayedMessage.getBody());
((DataViewHolder) holder).itemView.setOnClickListener(view -> {
if (mMessageType == FetchMessages.MESSAGE_TYPE_NOTIFICATION
if (mMessageType == FetchMessages.MESSAGE_TYPE_INBOX
&& message.getContext() != null && !message.getContext().equals("")) {
Uri uri = LinkResolverActivity.getRedditUriByPath(message.getContext());
Intent intent = new Intent(mContext, LinkResolverActivity.class);

View File

@ -21,9 +21,9 @@ public class FetchMessages {
public static final String WHERE_COMMENTS = "comments";
public static final String WHERE_MESSAGES = "messages";
public static final String WHERE_MESSAGES_DETAIL = "messages_detail";
public static final int MESSAGE_TYPE_NOTIFICATION = 0;
public static final int MESSAGE_TYPE_INBOX = 0;
public static final int MESSAGE_TYPE_PRIVATE_MESSAGE = 1;
public static final int MESSAGE_TYPE_PRIVATE_MESSAGE_DETAIL = 2;
public static final int MESSAGE_TYPE_NOTIFICATION = 2;
static void fetchInbox(Retrofit oauthRetrofit, Locale locale, String accessToken, String where,
String after, int messageType, FetchMessagesListener fetchMessagesListener) {

View File

@ -102,7 +102,7 @@ public class InboxFragment extends Fragment implements FragmentCommunicator {
mRecyclerView.setPadding(0, 0, 0, mActivity.getNavBarHeight());
}
mWhere = arguments.getString(EXTRA_MESSAGE_WHERE);
mWhere = arguments.getString(EXTRA_MESSAGE_WHERE, FetchMessages.WHERE_INBOX);
mAdapter = new MessageRecyclerViewAdapter(mActivity, mOauthRetrofit, mCustomThemeWrapper,
mAccessToken, mWhere, () -> mMessageViewModel.retryLoadingMore());
mLinearLayoutManager = new LinearLayoutManager(mActivity);

View File

@ -32,7 +32,7 @@ class MessageDataSource extends PageKeyedDataSource<String, Message> {
if (where.equals(FetchMessages.WHERE_MESSAGES)) {
messageType = FetchMessages.MESSAGE_TYPE_PRIVATE_MESSAGE;
} else {
messageType = FetchMessages.MESSAGE_TYPE_NOTIFICATION;
messageType = FetchMessages.MESSAGE_TYPE_INBOX;
}
paginationNetworkStateLiveData = new MutableLiveData<>();
initialLoadStateLiveData = new MutableLiveData<>();

View File

@ -44,7 +44,7 @@ public class ParseMessage {
@Nullable
private static Message parseSingleMessage(JSONObject messageJSON, Locale locale, int messageType) throws JSONException {
String kind = messageJSON.getString(JSONUtils.KIND_KEY);
if ((messageType == FetchMessages.MESSAGE_TYPE_NOTIFICATION && kind.equals("t4")) ||
if ((messageType == FetchMessages.MESSAGE_TYPE_INBOX && kind.equals("t4")) ||
(messageType == FetchMessages.MESSAGE_TYPE_PRIVATE_MESSAGE && !kind.equals("t4"))) {
return null;
}

View File

@ -98,7 +98,7 @@ public class PullNotificationWorker extends Worker {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
int messageSize = Math.min(messages.size(), 5);
int messageSize = Math.min(messages.size(), 20);
long lastNotificationTime = mSharedPreferences.getLong(SharedPreferencesUtils.PULL_NOTIFICATION_TIME, -1L);
boolean hasValidMessage = false;
@ -164,6 +164,7 @@ public class PullNotificationWorker extends Worker {
} else if (kind.equals(Message.TYPE_MESSAGE)) {
Intent intent = new Intent(context, InboxActivity.class);
intent.putExtra(InboxActivity.EXTRA_NEW_ACCOUNT_NAME, accountName);
intent.putExtra(InboxActivity.EXTRA_VIEW_MESSAGE, true);
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, accountIndex * 6 + 3, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(summaryPendingIntent);
} else if (kind.equals(Message.TYPE_SUBREDDIT)) {