Add another tab for PM in ViewMessageActivity.

This commit is contained in:
Alex Ning 2020-06-27 14:00:24 +08:00
parent f988416769
commit 3c482c63ec
7 changed files with 189 additions and 157 deletions

View File

@ -1054,9 +1054,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
if (mAccessToken == null) { if (mAccessToken == null) {
switch (position) { switch (position) {
case 0: case 0:
return "Popular"; return getString(R.string.popular);
case 1: case 1:
return "All"; return getString(R.string.all);
} }
} else { } else {
switch (position) { switch (position) {

View File

@ -5,27 +5,23 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.lifecycle.ViewModelProvider; import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration; import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.RecyclerView; import androidx.viewpager.widget.ViewPager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.tabs.TabLayout;
import com.r0adkll.slidr.Slidr; import com.r0adkll.slidr.Slidr;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
@ -37,15 +33,13 @@ import javax.inject.Named;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; import ml.docilealligator.infinityforreddit.ActivityToolbarInterface;
import ml.docilealligator.infinityforreddit.Adapter.MessageRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask;
import ml.docilealligator.infinityforreddit.AsyncTask.SwitchAccountAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.SwitchAccountAsyncTask;
import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent;
import ml.docilealligator.infinityforreddit.FetchMessages; import ml.docilealligator.infinityforreddit.FetchMessages;
import ml.docilealligator.infinityforreddit.Fragment.ViewMessagesFragment;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.MessageViewModel;
import ml.docilealligator.infinityforreddit.NetworkState;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
@ -67,17 +61,10 @@ public class ViewMessageActivity extends BaseActivity implements ActivityToolbar
AppBarLayout mAppBarLayout; AppBarLayout mAppBarLayout;
@BindView(R.id.toolbar_view_message_activity) @BindView(R.id.toolbar_view_message_activity)
Toolbar mToolbar; Toolbar mToolbar;
@BindView(R.id.swipe_refresh_layout_view_message_activity) @BindView(R.id.tab_layout_view_message_activity)
SwipeRefreshLayout mSwipeRefreshLayout; TabLayout tabLayout;
@BindView(R.id.recycler_view_view_message_activity) @BindView(R.id.view_pager_view_message_activity)
RecyclerView mRecyclerView; ViewPager viewPager;
@BindView(R.id.fetch_messages_info_linear_layout_view_message_activity)
LinearLayout mFetchMessageInfoLinearLayout;
@BindView(R.id.fetch_messages_info_image_view_view_message_activity)
ImageView mFetchMessageInfoImageView;
@BindView(R.id.fetch_messages_info_text_view_view_message_activity)
TextView mFetchMessageInfoTextView;
MessageViewModel mMessageViewModel;
@Inject @Inject
@Named("oauth") @Named("oauth")
Retrofit mOauthRetrofit; Retrofit mOauthRetrofit;
@ -88,12 +75,10 @@ public class ViewMessageActivity extends BaseActivity implements ActivityToolbar
SharedPreferences mSharedPreferences; SharedPreferences mSharedPreferences;
@Inject @Inject
CustomThemeWrapper mCustomThemeWrapper; CustomThemeWrapper mCustomThemeWrapper;
private SectionsPagerAdapter sectionsPagerAdapter;
private boolean mNullAccessToken = false; private boolean mNullAccessToken = false;
private String mAccessToken; private String mAccessToken;
private String mNewAccountName; private String mNewAccountName;
private MessageRecyclerViewAdapter mAdapter;
private RequestManager mGlide;
private LinearLayoutManager mLinearLayoutManager;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -113,8 +98,6 @@ public class ViewMessageActivity extends BaseActivity implements ActivityToolbar
Slidr.attach(this); Slidr.attach(this);
} }
mGlide = Glide.with(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Window window = getWindow(); Window window = getWindow();
@ -125,11 +108,6 @@ public class ViewMessageActivity extends BaseActivity implements ActivityToolbar
if (isImmersiveInterface()) { if (isImmersiveInterface()) {
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
adjustToolbar(mToolbar); adjustToolbar(mToolbar);
int navBarHeight = getNavBarHeight();
if (navBarHeight > 0) {
mRecyclerView.setPadding(0, 0, 0, navBarHeight);
}
} }
} }
@ -167,9 +145,7 @@ public class ViewMessageActivity extends BaseActivity implements ActivityToolbar
protected void applyCustomTheme() { protected void applyCustomTheme() {
mCoordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); mCoordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor());
applyAppBarLayoutAndToolbarTheme(mAppBarLayout, mToolbar); applyAppBarLayoutAndToolbarTheme(mAppBarLayout, mToolbar);
mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(mCustomThemeWrapper.getCircularProgressBarBackground()); applyTabLayoutTheme(tabLayout);
mSwipeRefreshLayout.setColorSchemeColors(mCustomThemeWrapper.getColorAccent());
mFetchMessageInfoTextView.setTextColor(mCustomThemeWrapper.getSecondaryTextColor());
} }
private void getCurrentAccountAndFetchMessage() { private void getCurrentAccountAndFetchMessage() {
@ -206,59 +182,10 @@ public class ViewMessageActivity extends BaseActivity implements ActivityToolbar
} }
private void bindView() { private void bindView() {
mAdapter = new MessageRecyclerViewAdapter(this, mOauthRetrofit, mCustomThemeWrapper, sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mAccessToken, () -> mMessageViewModel.retryLoadingMore()); viewPager.setAdapter(sectionsPagerAdapter);
mLinearLayoutManager = new LinearLayoutManager(this); viewPager.setOffscreenPageLimit(2);
mRecyclerView.setLayoutManager(mLinearLayoutManager); tabLayout.setupWithViewPager(viewPager);
mRecyclerView.setAdapter(mAdapter);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, mLinearLayoutManager.getOrientation());
mRecyclerView.addItemDecoration(dividerItemDecoration);
MessageViewModel.Factory factory = new MessageViewModel.Factory(mOauthRetrofit,
getResources().getConfiguration().locale, mAccessToken, FetchMessages.WHERE_INBOX);
mMessageViewModel = new ViewModelProvider(this, factory).get(MessageViewModel.class);
mMessageViewModel.getMessages().observe(this, messages -> mAdapter.submitList(messages));
mMessageViewModel.hasMessage().observe(this, hasMessage -> {
mSwipeRefreshLayout.setRefreshing(false);
if (hasMessage) {
mFetchMessageInfoLinearLayout.setVisibility(View.GONE);
} else {
mFetchMessageInfoLinearLayout.setOnClickListener(view -> {
//Do nothing
});
showErrorView(R.string.no_messages);
}
});
mMessageViewModel.getInitialLoadingState().observe(this, networkState -> {
if (networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mSwipeRefreshLayout.setRefreshing(false);
} else if (networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mSwipeRefreshLayout.setRefreshing(false);
mFetchMessageInfoLinearLayout.setOnClickListener(view -> {
mFetchMessageInfoLinearLayout.setVisibility(View.GONE);
mMessageViewModel.refresh();
mAdapter.setNetworkState(null);
});
showErrorView(R.string.load_messages_failed);
} else {
mSwipeRefreshLayout.setRefreshing(true);
}
});
mMessageViewModel.getPaginationNetworkState().observe(this, networkState -> {
mAdapter.setNetworkState(networkState);
});
mSwipeRefreshLayout.setOnRefreshListener(this::onRefresh);
}
private void showErrorView(int stringResId) {
mSwipeRefreshLayout.setRefreshing(false);
mFetchMessageInfoLinearLayout.setVisibility(View.VISIBLE);
mFetchMessageInfoTextView.setText(stringResId);
mGlide.load(R.drawable.error_image).into(mFetchMessageInfoImageView);
} }
@Override @Override
@ -271,8 +198,9 @@ public class ViewMessageActivity extends BaseActivity implements ActivityToolbar
@Override @Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) { public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.action_refresh_view_message_activity) { if (item.getItemId() == R.id.action_refresh_view_message_activity) {
mMessageViewModel.refresh(); if (sectionsPagerAdapter != null) {
mAdapter.setNetworkState(null); sectionsPagerAdapter.refresh();
}
return true; return true;
} else if (item.getItemId() == android.R.id.home) { } else if (item.getItemId() == android.R.id.home) {
finish(); finish();
@ -302,14 +230,89 @@ public class ViewMessageActivity extends BaseActivity implements ActivityToolbar
} }
} }
private void onRefresh() {
mMessageViewModel.refresh();
}
@Override @Override
public void onLongPress() { public void onLongPress() {
if (mLinearLayoutManager != null) { if (sectionsPagerAdapter != null) {
mLinearLayoutManager.scrollToPositionWithOffset(0, 0); sectionsPagerAdapter.goBackToTop();
}
}
private class SectionsPagerAdapter extends FragmentPagerAdapter {
private ViewMessagesFragment tab1;
private ViewMessagesFragment tab2;
public SectionsPagerAdapter(@NonNull FragmentManager fm) {
super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
@NonNull
@Override
public Fragment getItem(int position) {
if (position == 0) {
ViewMessagesFragment fragment = new ViewMessagesFragment();
Bundle bundle = new Bundle();
bundle.putString(ViewMessagesFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(ViewMessagesFragment.EXTRA_MESSAGE_WHERE, FetchMessages.WHERE_INBOX);
fragment.setArguments(bundle);
return fragment;
} else {
ViewMessagesFragment fragment = new ViewMessagesFragment();
Bundle bundle = new Bundle();
bundle.putString(ViewMessagesFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(ViewMessagesFragment.EXTRA_MESSAGE_WHERE, FetchMessages.WHERE_MESSAGES);
fragment.setArguments(bundle);
return fragment;
}
}
@Override
public int getCount() {
return 2;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return getString(R.string.notifications);
}
return getString(R.string.messages);
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
if (position == 0) {
tab1 = (ViewMessagesFragment) fragment;
} else if (position == 1) {
tab2 = (ViewMessagesFragment) fragment;
}
return fragment;
}
void refresh() {
if (viewPager.getCurrentItem() == 0) {
if (tab1 != null) {
tab1.refresh();
}
} else if (viewPager.getCurrentItem() == 1 && tab2 != null) {
tab2.refresh();
}
}
void goBackToTop() {
if (viewPager.getCurrentItem() == 0) {
if (tab1 != null) {
tab1.goBackToTop();
}
} else if (viewPager.getCurrentItem() == 1) {
if (tab2 != null) {
tab2.goBackToTop();
}
}
} }
} }
} }

View File

@ -12,6 +12,7 @@ import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.paging.PagedListAdapter; import androidx.paging.PagedListAdapter;
@ -29,6 +30,7 @@ import io.noties.markwon.simple.ext.SimpleExtPlugin;
import ml.docilealligator.infinityforreddit.Activity.LinkResolverActivity; import ml.docilealligator.infinityforreddit.Activity.LinkResolverActivity;
import ml.docilealligator.infinityforreddit.Activity.ViewUserDetailActivity; import ml.docilealligator.infinityforreddit.Activity.ViewUserDetailActivity;
import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.FetchMessages;
import ml.docilealligator.infinityforreddit.Message; import ml.docilealligator.infinityforreddit.Message;
import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.NetworkState;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
@ -54,6 +56,7 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
private Retrofit mOauthRetrofit; private Retrofit mOauthRetrofit;
private Markwon mMarkwon; private Markwon mMarkwon;
private String mAccessToken; private String mAccessToken;
private int mMessageType;
private NetworkState networkState; private NetworkState networkState;
private RetryLoadingMoreCallback mRetryLoadingMoreCallback; private RetryLoadingMoreCallback mRetryLoadingMoreCallback;
private int mColorAccent; private int mColorAccent;
@ -67,7 +70,8 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
public MessageRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, public MessageRecyclerViewAdapter(Context context, Retrofit oauthRetrofit,
CustomThemeWrapper customThemeWrapper, CustomThemeWrapper customThemeWrapper,
String accessToken, RetryLoadingMoreCallback retryLoadingMoreCallback) { String accessToken, String where,
RetryLoadingMoreCallback retryLoadingMoreCallback) {
super(DIFF_CALLBACK); super(DIFF_CALLBACK);
mContext = context; mContext = context;
mOauthRetrofit = oauthRetrofit; mOauthRetrofit = oauthRetrofit;
@ -98,6 +102,8 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
) )
.build(); .build();
mAccessToken = accessToken; mAccessToken = accessToken;
mMessageType = where.equals(FetchMessages.WHERE_MESSAGES) ?
FetchMessages.MESSAGE_TYPE_PRIVATE_MESSAGE : FetchMessages.MESSAGE_TYPE_NOTIFICATION;
mColorAccent = customThemeWrapper.getColorAccent(); mColorAccent = customThemeWrapper.getColorAccent();
mMessageBackgroundColor = customThemeWrapper.getCardViewBackgroundColor(); mMessageBackgroundColor = customThemeWrapper.getCardViewBackgroundColor();
@ -132,7 +138,6 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
} }
if (message.wasComment()) { if (message.wasComment()) {
((DataViewHolder) holder).authorTextView.setTextColor(mUsernameColor);
((DataViewHolder) holder).titleTextView.setText(message.getTitle()); ((DataViewHolder) holder).titleTextView.setText(message.getTitle());
} else { } else {
((DataViewHolder) holder).titleTextView.setVisibility(View.GONE); ((DataViewHolder) holder).titleTextView.setVisibility(View.GONE);
@ -149,6 +154,8 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
Intent intent = new Intent(mContext, LinkResolverActivity.class); Intent intent = new Intent(mContext, LinkResolverActivity.class);
intent.setData(uri); intent.setData(uri);
mContext.startActivity(intent); mContext.startActivity(intent);
} else if (mMessageType == FetchMessages.MESSAGE_TYPE_PRIVATE_MESSAGE) {
Toast.makeText(mContext, "To be implemented", Toast.LENGTH_SHORT).show();
} }
if (message.isNew()) { if (message.isNew()) {
@ -171,11 +178,9 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
}); });
((DataViewHolder) holder).authorTextView.setOnClickListener(view -> { ((DataViewHolder) holder).authorTextView.setOnClickListener(view -> {
if (message.wasComment()) { Intent intent = new Intent(mContext, ViewUserDetailActivity.class);
Intent intent = new Intent(mContext, ViewUserDetailActivity.class); intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, message.getAuthor());
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, message.getAuthor()); mContext.startActivity(intent);
mContext.startActivity(intent);
}
}); });
((DataViewHolder) holder).contentCustomMarkwonView.setOnClickListener(view -> ((DataViewHolder) holder).itemView.performClick()); ((DataViewHolder) holder).contentCustomMarkwonView.setOnClickListener(view -> ((DataViewHolder) holder).itemView.performClick());
@ -211,7 +216,6 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
if (holder instanceof DataViewHolder) { if (holder instanceof DataViewHolder) {
((DataViewHolder) holder).itemView.setBackgroundColor(mMessageBackgroundColor); ((DataViewHolder) holder).itemView.setBackgroundColor(mMessageBackgroundColor);
((DataViewHolder) holder).titleTextView.setVisibility(View.VISIBLE); ((DataViewHolder) holder).titleTextView.setVisibility(View.VISIBLE);
((DataViewHolder) holder).authorTextView.setTextColor(mPrimaryTextColor);
} }
} }

View File

@ -50,6 +50,7 @@ import ml.docilealligator.infinityforreddit.Fragment.SubredditListingFragment;
import ml.docilealligator.infinityforreddit.Fragment.SubscribedSubredditsListingFragment; import ml.docilealligator.infinityforreddit.Fragment.SubscribedSubredditsListingFragment;
import ml.docilealligator.infinityforreddit.Fragment.UserListingFragment; import ml.docilealligator.infinityforreddit.Fragment.UserListingFragment;
import ml.docilealligator.infinityforreddit.Fragment.ViewImgurVideoFragment; import ml.docilealligator.infinityforreddit.Fragment.ViewImgurVideoFragment;
import ml.docilealligator.infinityforreddit.Fragment.ViewMessagesFragment;
import ml.docilealligator.infinityforreddit.Service.DownloadRedditVideoService; import ml.docilealligator.infinityforreddit.Service.DownloadRedditVideoService;
import ml.docilealligator.infinityforreddit.Service.SubmitPostService; import ml.docilealligator.infinityforreddit.Service.SubmitPostService;
import ml.docilealligator.infinityforreddit.Settings.AdvancedPreferenceFragment; import ml.docilealligator.infinityforreddit.Settings.AdvancedPreferenceFragment;
@ -173,4 +174,6 @@ public interface AppComponent {
void inject(DownloadRedditVideoService downloadRedditVideoService); void inject(DownloadRedditVideoService downloadRedditVideoService);
void inject(MultiRedditListingFragment multiRedditListingFragment); void inject(MultiRedditListingFragment multiRedditListingFragment);
void inject(ViewMessagesFragment viewMessagesFragment);
} }

View File

@ -1,5 +1,6 @@
package ml.docilealligator.infinityforreddit.Fragment; package ml.docilealligator.infinityforreddit.Fragment;
import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -9,22 +10,27 @@ import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager; import com.bumptech.glide.RequestManager;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.Activity.BaseActivity;
import ml.docilealligator.infinityforreddit.Adapter.MessageRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.Adapter.MessageRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.FetchMessages;
import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.FragmentCommunicator;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.MessageViewModel; import ml.docilealligator.infinityforreddit.MessageViewModel;
import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.NetworkState;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
@ -33,6 +39,8 @@ import retrofit2.Retrofit;
public class ViewMessagesFragment extends Fragment implements FragmentCommunicator { public class ViewMessagesFragment extends Fragment implements FragmentCommunicator {
public static final String EXTRA_ACCESS_TOKEN = "EAT";
public static final String EXTRA_MESSAGE_WHERE = "EMT";
@BindView(R.id.swipe_refresh_layout_view_messages_fragment) @BindView(R.id.swipe_refresh_layout_view_messages_fragment)
SwipeRefreshLayout mSwipeRefreshLayout; SwipeRefreshLayout mSwipeRefreshLayout;
@BindView(R.id.recycler_view_view_messages_fragment) @BindView(R.id.recycler_view_view_messages_fragment)
@ -58,6 +66,7 @@ public class ViewMessagesFragment extends Fragment implements FragmentCommunicat
private MessageRecyclerViewAdapter mAdapter; private MessageRecyclerViewAdapter mAdapter;
private RequestManager mGlide; private RequestManager mGlide;
private LinearLayoutManager mLinearLayoutManager; private LinearLayoutManager mLinearLayoutManager;
private BaseActivity mActivity;
public ViewMessagesFragment() { public ViewMessagesFragment() {
// Required empty public constructor // Required empty public constructor
@ -68,8 +77,34 @@ public class ViewMessagesFragment extends Fragment implements FragmentCommunicat
Bundle savedInstanceState) { Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_view_messages, container, false); View rootView = inflater.inflate(R.layout.fragment_view_messages, container, false);
((Infinity) mActivity.getApplication()).getAppComponent().inject(this);
ButterKnife.bind(this, rootView);
applyTheme();
Bundle arguments = getArguments();
if (arguments == null) {
return rootView;
}
mAccessToken = getArguments().getString(EXTRA_ACCESS_TOKEN);
mGlide = Glide.with(this);
if (mActivity.isImmersiveInterface()) {
mRecyclerView.setPadding(0, 0, 0, mActivity.getNavBarHeight());
}
String where = arguments.getString(EXTRA_MESSAGE_WHERE);
mAdapter = new MessageRecyclerViewAdapter(mActivity, mOauthRetrofit, mCustomThemeWrapper,
mAccessToken, where, () -> mMessageViewModel.retryLoadingMore());
mLinearLayoutManager = new LinearLayoutManager(mActivity);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setAdapter(mAdapter);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mActivity, mLinearLayoutManager.getOrientation());
mRecyclerView.addItemDecoration(dividerItemDecoration);
MessageViewModel.Factory factory = new MessageViewModel.Factory(mOauthRetrofit, MessageViewModel.Factory factory = new MessageViewModel.Factory(mOauthRetrofit,
getResources().getConfiguration().locale, mAccessToken, FetchMessages.WHERE_INBOX); getResources().getConfiguration().locale, mAccessToken, where);
mMessageViewModel = new ViewModelProvider(this, factory).get(MessageViewModel.class); mMessageViewModel = new ViewModelProvider(this, factory).get(MessageViewModel.class);
mMessageViewModel.getMessages().observe(getViewLifecycleOwner(), messages -> mAdapter.submitList(messages)); mMessageViewModel.getMessages().observe(getViewLifecycleOwner(), messages -> mAdapter.submitList(messages));
@ -78,9 +113,7 @@ public class ViewMessagesFragment extends Fragment implements FragmentCommunicat
if (hasMessage) { if (hasMessage) {
mFetchMessageInfoLinearLayout.setVisibility(View.GONE); mFetchMessageInfoLinearLayout.setVisibility(View.GONE);
} else { } else {
mFetchMessageInfoLinearLayout.setOnClickListener(view -> { mFetchMessageInfoLinearLayout.setOnClickListener(null);
//Do nothing
});
showErrorView(R.string.no_messages); showErrorView(R.string.no_messages);
} }
}); });
@ -119,10 +152,25 @@ public class ViewMessagesFragment extends Fragment implements FragmentCommunicat
@Override @Override
public void applyTheme() { public void applyTheme() {
mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(mCustomThemeWrapper.getCircularProgressBarBackground());
mSwipeRefreshLayout.setColorSchemeColors(mCustomThemeWrapper.getColorAccent());
mFetchMessageInfoTextView.setTextColor(mCustomThemeWrapper.getSecondaryTextColor());
}
public void goBackToTop() {
if (mLinearLayoutManager != null) {
mLinearLayoutManager.scrollToPositionWithOffset(0, 0);
}
} }
private void onRefresh() { private void onRefresh() {
mMessageViewModel.refresh(); mMessageViewModel.refresh();
mAdapter.setNetworkState(null);
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
mActivity = (BaseActivity) context;
} }
} }

View File

@ -31,52 +31,24 @@
</com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout> <com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout_view_message_activity"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" >
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout_view_message_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_view_message_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<LinearLayout
android:id="@+id/fetch_messages_info_linear_layout_view_message_activity"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerInParent="true" android:layout_gravity="bottom"
android:gravity="center" app:layout_scrollFlags="scroll|enterAlways"
android:orientation="vertical" app:tabGravity="fill"
android:visibility="gone"> app:tabIndicatorHeight="3dp"
app:tabMode="fixed"
app:tabRippleColor="?attr/colorControlHighlight"
app:tabUnboundedRipple="false" />
<ImageView </com.google.android.material.appbar.AppBarLayout>
android:id="@+id/fetch_messages_info_image_view_view_message_activity"
android:layout_width="150dp"
android:layout_height="wrap_content" />
<TextView <androidx.viewpager.widget.ViewPager
android:id="@+id/fetch_messages_info_text_view_view_message_activity" android:id="@+id/view_pager_view_message_activity"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_marginTop="16dp" app:layout_behavior="@string/appbar_scrolling_view_behavior" />
android:gravity="center"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family" />
</LinearLayout>
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -760,6 +760,8 @@
<string name="home">Home</string> <string name="home">Home</string>
<string name="popular">Popular</string> <string name="popular">Popular</string>
<string name="notifications">Notifications</string>
<string name="messages">Messages</string>
<string name="fetch_gfycat_video_failed">Fetch Gfycat video failed</string> <string name="fetch_gfycat_video_failed">Fetch Gfycat video failed</string>
<string name="fetch_redgifs_video_failed">Fetch Redgifs video failed</string> <string name="fetch_redgifs_video_failed">Fetch Redgifs video failed</string>