Current user's upvoted, downvoted, hidden, saved, gilded posts are now available to see.

This commit is contained in:
Alex Ning
2019-08-14 15:54:08 +08:00
parent eaef58037c
commit 7cdf5d2e4c
21 changed files with 466 additions and 24 deletions

View File

@@ -0,0 +1,173 @@
package ml.docilealligator.infinityforreddit;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import com.google.android.material.appbar.AppBarLayout;
import javax.inject.Inject;
import butterknife.BindView;
import butterknife.ButterKnife;
public class AccountPostsActivity extends AppCompatActivity {
static final String EXTRA_USER_WHERE = "EUW";
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
private static final String ACCESS_TOKEN_STATE = "ATS";
private static final String ACCOUNT_NAME_STATE = "ANS";
private static final String FRAGMENT_OUT_STATE = "FOS";
@BindView(R.id.appbar_layout_account_posts_activity) AppBarLayout appBarLayout;
@BindView(R.id.toolbar_account_posts_activity) Toolbar toolbar;
private boolean mNullAccessToken = false;
private String mAccessToken;
private String mAccountName;
private String mUserWhere;
private Fragment mFragment;
@Inject
RedditDataRoomDatabase mRedditDataRoomDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account_posts);
ButterKnife.bind(this);
((Infinity) getApplication()).getAppComponent().inject(this);
Resources resources = getResources();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1
&& (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT
|| resources.getBoolean(R.bool.isTablet))) {
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
boolean lightNavBar = false;
if((resources.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) {
lightNavBar = true;
}
boolean finalLightNavBar = lightNavBar;
View decorView = window.getDecorView();
if(finalLightNavBar) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
@Override
void onStateChanged(AppBarLayout appBarLayout, State state) {
if (state == State.COLLAPSED) {
if(finalLightNavBar) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
} else if (state == State.EXPANDED) {
if(finalLightNavBar) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
}
}
});
int statusBarResourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (statusBarResourceId > 0) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams();
params.topMargin = getResources().getDimensionPixelSize(statusBarResourceId);
toolbar.setLayoutParams(params);
}
}
mUserWhere = getIntent().getExtras().getString(EXTRA_USER_WHERE);
if(mUserWhere.equals(PostDataSource.USER_WHERE_UPVOTED)) {
toolbar.setTitle(R.string.upvoted);
} else if(mUserWhere.equals(PostDataSource.USER_WHERE_DOWNVOTED)) {
toolbar.setTitle(R.string.downvoted);
} else if(mUserWhere.equals(PostDataSource.USER_WHERE_SAVED)) {
toolbar.setTitle(R.string.saved);
} else if(mUserWhere.equals(PostDataSource.USER_WHERE_HIDDEN)) {
toolbar.setTitle(R.string.hidden);
} else if(mUserWhere.equals(PostDataSource.USER_WHERE_GILDED)){
toolbar.setTitle(R.string.gilded);
}
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if(savedInstanceState != null) {
mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE);
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
if(!mNullAccessToken && mAccessToken == null) {
getCurrentAccountAndBindView();
} else {
mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_account_posts_activity, mFragment).commit();
initializeFragment();
}
} else {
getCurrentAccountAndBindView();
}
}
private void getCurrentAccountAndBindView() {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) {
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
}
initializeFragment();
}).execute();
}
private void initializeFragment() {
mFragment = new PostFragment();
Bundle bundle = new Bundle();
bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_USER);
bundle.putString(PostFragment.EXTRA_USER_NAME, mAccountName);
bundle.putString(PostFragment.EXTRA_USER_WHERE, mUserWhere);
bundle.putString(PostFragment.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_NEW);
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
mFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_account_posts_activity, mFragment).commit();
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(item.getItemId() == android.R.id.home) {
finish();
return true;
}
return false;
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
if (mFragment != null) {
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment);
}
outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
outState.putString(ACCOUNT_NAME_STATE, mAccountName);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
}
}

View File

@@ -33,4 +33,5 @@ interface AppComponent {
void inject(SubredditSelectionActivity subredditSelectionActivity);
void inject(EditPostActivity editPostActivity);
void inject(EditCommentActivity editCommentActivity);
void inject(AccountPostsActivity accountPostsActivity);
}

View File

@@ -279,7 +279,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
((PostDetailViewHolder) holder).mGildedImageView.setVisibility(View.VISIBLE);
mGlide.load(R.drawable.gold).into(((PostDetailViewHolder) holder).mGildedImageView);
((PostDetailViewHolder) holder).mGildedNumberTextView.setVisibility(View.VISIBLE);
String gildedNumber = mActivity.getResources().getString(R.string.gilded, mPost.getGilded());
String gildedNumber = mActivity.getResources().getString(R.string.gilded_count, mPost.getGilded());
((PostDetailViewHolder) holder).mGildedNumberTextView.setText(gildedNumber);
}

View File

@@ -258,6 +258,7 @@ public class FilteredPostsActivity extends AppCompatActivity implements SortType
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment);
}
outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
}
@Override

View File

@@ -76,6 +76,11 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
@BindView(R.id.all_drawer_items_linear_layout_main_activity) LinearLayout allDrawerItemsLinearLayout;
@BindView(R.id.profile_linear_layout_main_activity) LinearLayout profileLinearLayout;
@BindView(R.id.subscriptions_linear_layout_main_activity) LinearLayout subscriptionLinearLayout;
@BindView(R.id.upvoted_linear_layout_main_activity) LinearLayout upvotedLinearLayout;
@BindView(R.id.downvoted_linear_layout_main_activity) LinearLayout downvotedLinearLayout;
@BindView(R.id.hidden_linear_layout_main_activity) LinearLayout hiddenLinearLayout;
@BindView(R.id.saved_linear_layout_main_activity) LinearLayout savedLinearLayout;
@BindView(R.id.gilded_linear_layout_main_activity) LinearLayout gildedLinearLayout;
@BindView(R.id.divider_main_activity) View divider;
@BindView(R.id.settings_linear_layout_main_activity) LinearLayout settingsLinearLayout;
@BindView(R.id.account_recycler_view_main_activity) RecyclerView accountRecyclerView;
@@ -386,6 +391,41 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
drawer.closeDrawers();
});
upvotedLinearLayout.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, AccountPostsActivity.class);
intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_UPVOTED);
startActivity(intent);
drawer.closeDrawers();
});
downvotedLinearLayout.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, AccountPostsActivity.class);
intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_DOWNVOTED);
startActivity(intent);
drawer.closeDrawers();
});
hiddenLinearLayout.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, AccountPostsActivity.class);
intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_HIDDEN);
startActivity(intent);
drawer.closeDrawers();
});
savedLinearLayout.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, AccountPostsActivity.class);
intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SAVED);
startActivity(intent);
drawer.closeDrawers();
});
gildedLinearLayout.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, AccountPostsActivity.class);
intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_GILDED);
startActivity(intent);
drawer.closeDrawers();
});
settingsLinearLayout.setOnClickListener(view -> {
drawer.closeDrawers();
});

View File

@@ -30,6 +30,13 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
static final String SORT_TYPE_RELEVANCE = "relevance";
static final String SORT_TYPE_COMMENTS = "comments";
static final String USER_WHERE_SUBMITTED = "submitted";
static final String USER_WHERE_UPVOTED = "upvoted";
static final String USER_WHERE_DOWNVOTED = "downvoted";
static final String USER_WHERE_HIDDEN = "hidden";
static final String USER_WHERE_SAVED = "saved";
static final String USER_WHERE_GILDED = "gilded";
private Retrofit retrofit;
private String accessToken;
private Locale locale;
@@ -38,6 +45,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
private int postType;
private String sortType;
private int filter;
private String userWhere;
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
private MutableLiveData<NetworkState> initialLoadStateLiveData;
@@ -76,7 +84,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
}
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, int postType,
int filter) {
String sortType, String where, int filter) {
this.retrofit = retrofit;
this.accessToken = accessToken;
this.locale = locale;
@@ -85,6 +93,8 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
initialLoadStateLiveData = new MutableLiveData<>();
hasPostLiveData = new MutableLiveData<>();
this.postType = postType;
this.sortType = sortType;
userWhere = where;
this.filter = filter;
}
@@ -410,9 +420,10 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
Call<String> getPost;
if(accessToken == null) {
getPost = api.getUserBestPosts(subredditOrUserName, lastItem);
getPost = api.getUserBestPosts(subredditOrUserName, lastItem, sortType);
} else {
getPost = api.getUserBestPostsOauth(subredditOrUserName, lastItem, RedditUtils.getOAuthHeader(accessToken));
getPost = api.getUserBestPostsOauth(subredditOrUserName, userWhere, lastItem, sortType,
RedditUtils.getOAuthHeader(accessToken));
}
getPost.enqueue(new Callback<String>() {
@Override
@@ -469,9 +480,10 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
Call<String> getPost;
if(accessToken == null) {
getPost = api.getUserBestPosts(subredditOrUserName, after);
getPost = api.getUserBestPosts(subredditOrUserName, after, sortType);
} else {
getPost = api.getUserBestPostsOauth(subredditOrUserName, after, RedditUtils.getOAuthHeader(accessToken));
getPost = api.getUserBestPostsOauth(subredditOrUserName, userWhere, after, sortType,
RedditUtils.getOAuthHeader(accessToken));
}
getPost.enqueue(new Callback<String>() {
@Override

View File

@@ -15,6 +15,7 @@ class PostDataSourceFactory extends DataSource.Factory {
private String query;
private int postType;
private String sortType;
private String userWhere;
private int filter;
private PostDataSource postDataSource;
@@ -44,13 +45,15 @@ class PostDataSourceFactory extends DataSource.Factory {
}
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName,
int postType, int filter) {
int postType, String sortType, String where, int filter) {
this.retrofit = retrofit;
this.accessToken = accessToken;
this.locale = locale;
this.subredditName = subredditName;
postDataSourceLiveData = new MutableLiveData<>();
this.postType = postType;
this.sortType = sortType;
userWhere = where;
this.filter = filter;
}
@@ -80,7 +83,7 @@ class PostDataSourceFactory extends DataSource.Factory {
sortType, filter);
} else {
postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType,
filter);
sortType, userWhere, filter);
}
postDataSourceLiveData.postValue(postDataSource);

View File

@@ -23,7 +23,7 @@ import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;
@@ -50,6 +50,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
static final String EXTRA_NAME = "EN";
static final String EXTRA_USER_NAME = "EN";
static final String EXTRA_USER_WHERE = "EUW";
static final String EXTRA_QUERY = "EQ";
static final String EXTRA_POST_TYPE = "EPT";
static final String EXTRA_SORT_TYPE = "EST";
@@ -256,6 +257,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mFetchPostInfoLinearLayout.setLayoutParams(params);
String username = getArguments().getString(EXTRA_USER_NAME);
String where = getArguments().getString(EXTRA_USER_WHERE);
mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, redditDataRoomDatabase,
accessToken, postType, true, new PostRecyclerViewAdapter.Callback() {
@@ -277,10 +279,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
if(accessToken == null) {
factory = new PostViewModel.Factory(mRetrofit, accessToken,
getResources().getConfiguration().locale, username, postType, sortType, filter);
getResources().getConfiguration().locale, username, postType, sortType, where, filter);
} else {
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
getResources().getConfiguration().locale, username, postType, sortType, filter);
getResources().getConfiguration().locale, username, postType, sortType, where, filter);
}
} else {
mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, redditDataRoomDatabase,
@@ -307,7 +309,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mPostRecyclerView.setAdapter(mAdapter);
mPostViewModel = ViewModelProviders.of(this, factory).get(PostViewModel.class);
mPostViewModel = new ViewModelProvider(this, factory).get(PostViewModel.class);
mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts));
mPostViewModel.getInitialLoadingState().observe(this, networkState -> {

View File

@@ -300,7 +300,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
((DataViewHolder) holder).gildedImageView.setVisibility(View.VISIBLE);
mGlide.load(R.drawable.gold).into(((DataViewHolder) holder).gildedImageView);
((DataViewHolder) holder).gildedNumberTextView.setVisibility(View.VISIBLE);
String gildedNumber = mContext.getResources().getString(R.string.gilded, gilded);
String gildedNumber = mContext.getResources().getString(R.string.gilded_count, gilded);
((DataViewHolder) holder).gildedNumberTextView.setText(gildedNumber);
}

View File

@@ -87,9 +87,9 @@ public class PostViewModel extends ViewModel {
}
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType,
int filter) {
String sortType, String where, int filter) {
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName,
postType, filter);
postType, sortType, where, filter);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
PostDataSource::getInitialLoadStateLiveData);
@@ -190,6 +190,7 @@ public class PostViewModel extends ViewModel {
private String query;
private int postType;
private String sortType;
private String userWhere;
private int filter;
public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType,
@@ -214,12 +215,14 @@ public class PostViewModel extends ViewModel {
}
public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType,
int filter) {
String sortType, String where, int filter) {
this.retrofit = retrofit;
this.accessToken = accessToken;
this.locale = locale;
this.subredditName = subredditName;
this.postType = postType;
this.sortType = sortType;
userWhere = where;
this.filter = filter;
}
@@ -245,7 +248,7 @@ public class PostViewModel extends ViewModel {
} else if(postType == PostDataSource.TYPE_SUBREDDIT) {
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, sortType, filter);
} else {
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, filter);
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, sortType, userWhere, filter);
}
}
}

View File

@@ -55,12 +55,13 @@ public interface RedditAPI {
Call<String> getSubredditBestPosts(@Path("subredditName") String subredditName, @Path("sortType") String sortType,
@Query("after") String lastItem);
@GET("user/{username}/submitted.json?raw_json=1&limit=25")
Call<String> getUserBestPostsOauth(@Path("username") String username, @Query("after") String lastItem,
@HeaderMap Map<String, String> headers);
@GET("user/{username}/{where}.json?&raw_json=1&limit=25")
Call<String> getUserBestPostsOauth(@Path("username") String username, @Path("where") String where,
@Query("after") String lastItem, @Query("sort") String sortType, @HeaderMap Map<String, String> headers);
@GET("user/{username}/submitted.json?raw_json=1&limit=25")
Call<String> getUserBestPosts(@Path("username") String username, @Query("after") String lastItem);
Call<String> getUserBestPosts(@Path("username") String username, @Query("after") String lastItem,
@Query("sort") String sortType);
@GET("user/{username}/about.json?raw_json=1")
Call<String> getUserData(@Path("username") String username);

View File

@@ -531,6 +531,8 @@ public class ViewUserDetailActivity extends AppCompatActivity {
Bundle bundle = new Bundle();
bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_USER);
bundle.putString(PostFragment.EXTRA_USER_NAME, username);
bundle.putString(PostFragment.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_SUBMITTED);
bundle.putString(PostFragment.EXTRA_SORT_TYPE, PostDataSource.SORT_TYPE_NEW);
bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER);
bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
fragment.setArguments(bundle);