Fixed vote status cannot be shown on posts loaded from specific subreddits or users.

This commit is contained in:
Alex Ning 2019-01-24 23:24:14 +08:00
parent 86570d4959
commit 0e1abee883
6 changed files with 58 additions and 56 deletions

View File

@ -47,8 +47,9 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
this.onPostFetchedCallback = onPostFetchedCallback;
}
PostDataSource(Retrofit retrofit, Locale locale, String name, int postType, OnPostFetchedCallback onPostFetchedCallback) {
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String name, int postType, OnPostFetchedCallback onPostFetchedCallback) {
this.retrofit = retrofit;
this.accessToken = accessToken;
this.locale = locale;
this.name = name;
paginationNetworkStateLiveData = new MutableLiveData();
@ -192,7 +193,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
private void loadSubredditPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback) {
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> getPost = api.getSubredditBestPosts(name, null);
Call<String> getPost = api.getSubredditBestPosts(name, null, RedditUtils.getOAuthHeader(accessToken));
getPost.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, retrofit2.Response<String> response) {
@ -233,7 +234,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
private void loadSubredditPostsAfter(@NonNull LoadParams<String> params, @NonNull final LoadCallback<String, Post> callback) {
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> getPost = api.getSubredditBestPosts(name, params.key);
Call<String> getPost = api.getSubredditBestPosts(name, params.key, RedditUtils.getOAuthHeader(accessToken));
getPost.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, retrofit2.Response<String> response) {
@ -267,7 +268,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
private void loadUserPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback, String lastItem) {
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> getPost = api.getUserBestPosts(name, lastItem);
Call<String> getPost = api.getUserBestPosts(name, lastItem, RedditUtils.getOAuthHeader(accessToken));
getPost.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, retrofit2.Response<String> response) {
@ -311,7 +312,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
private void loadUserPostsAfter(@NonNull LoadParams<String> params, @NonNull final LoadCallback<String, Post> callback) {
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> getPost = api.getUserBestPosts(name, params.key);
Call<String> getPost = api.getUserBestPosts(name, params.key, RedditUtils.getOAuthHeader(accessToken));
getPost.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, retrofit2.Response<String> response) {

View File

@ -28,9 +28,10 @@ class PostDataSourceFactory extends DataSource.Factory {
this.onPostFetchedCallback = onPostFetchedCallback;
}
PostDataSourceFactory(Retrofit retrofit, Locale locale, String subredditName, int postType,
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType,
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
this.retrofit = retrofit;
this.accessToken = accessToken;
this.locale = locale;
this.subredditName = subredditName;
postDataSourceLiveData = new MutableLiveData<>();
@ -43,7 +44,7 @@ class PostDataSourceFactory extends DataSource.Factory {
if(postType == PostDataSource.TYPE_FRONT_PAGE) {
postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, onPostFetchedCallback);
} else {
postDataSource = new PostDataSource(retrofit, locale, subredditName, postType, onPostFetchedCallback);
postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, onPostFetchedCallback);
}
postDataSourceLiveData.postValue(postDataSource);

View File

@ -23,6 +23,8 @@ import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
import javax.inject.Inject;
import javax.inject.Named;
import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Retrofit;
@ -34,13 +36,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
static final String NAME_KEY = "NK";
static final String POST_TYPE_KEY = "PTK";
private CoordinatorLayout mCoordinatorLayout;
private RecyclerView mPostRecyclerView;
@BindView(R.id.coordinator_layout_post_fragment) CoordinatorLayout mCoordinatorLayout;
@BindView(R.id.recycler_view_post_fragment) RecyclerView mPostRecyclerView;
@BindView(R.id.progress_bar_post_fragment) CircleProgressBar mProgressBar;
@BindView(R.id.fetch_post_info_linear_layout_post_fragment) LinearLayout mFetchPostInfoLinearLayout;
@BindView(R.id.fetch_post_info_image_view_post_fragment) ImageView mFetchPostInfoImageView;
@BindView(R.id.fetch_post_info_text_view_post_fragment) TextView mFetchPostInfoTextView;
private LinearLayoutManager mLinearLayoutManager;
private CircleProgressBar mProgressBar;
private LinearLayout mFetchPostInfoLinearLayout;
private ImageView mFetchPostInfoImageView;
private TextView mFetchPostInfoTextView;
private String mName;
private int mPostType;
@ -78,14 +81,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
((Infinity) getActivity().getApplication()).getmNetworkComponent().inject(this);
mCoordinatorLayout = rootView.findViewById(R.id.coordinator_layout_post_fragment);
mPostRecyclerView = rootView.findViewById(R.id.recycler_view_post_fragment);
ButterKnife.bind(this, rootView);
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mPostRecyclerView.setLayoutManager(mLinearLayoutManager);
mProgressBar = rootView.findViewById(R.id.progress_bar_post_fragment);
mFetchPostInfoLinearLayout = rootView.findViewById(R.id.fetch_post_info_linear_layout_post_fragment);
mFetchPostInfoImageView = rootView.findViewById(R.id.fetch_post_info_image_view_post_fragment);
mFetchPostInfoTextView = rootView.findViewById(R.id.fetch_post_info_text_view_post_fragment);
/*FloatingActionButton fab = rootView.findViewById(R.id.fab_post_fragment);
fab.setOnClickListener(new View.OnClickListener() {
@Override
@ -97,24 +96,36 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mPostType = getArguments().getInt(POST_TYPE_KEY);
if(mPostType != PostDataSource.TYPE_FRONT_PAGE) {
mName = getArguments().getString(NAME_KEY);
}
if(mPostType == PostDataSource.TYPE_FRONT_PAGE) {
mAdapter = new PostRecyclerViewAdapter(getActivity(), mOauthRetrofit,
mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore());
} else {
mAdapter = new PostRecyclerViewAdapter(getActivity(), mRetrofit,
mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore());
}
mPostRecyclerView.setAdapter(mAdapter);
String accessToken = getActivity().getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE)
.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
PostViewModel.Factory factory;
if(mPostType == PostDataSource.TYPE_FRONT_PAGE) {
if(mPostType != PostDataSource.TYPE_FRONT_PAGE) {
mName = getArguments().getString(NAME_KEY);
mAdapter = new PostRecyclerViewAdapter(getActivity(), mRetrofit,
mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore());
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
getResources().getConfiguration().locale, mName, mPostType, new PostDataSource.OnPostFetchedCallback() {
@Override
public void hasPost() {
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
}
@Override
public void noPost() {
mFetchPostInfoLinearLayout.setOnClickListener(view -> {
//Do nothing
});
showErrorView(R.string.no_posts);
}
});
} else {
mAdapter = new PostRecyclerViewAdapter(getActivity(), mOauthRetrofit,
mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore());
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
getResources().getConfiguration().locale, mPostType, new PostDataSource.OnPostFetchedCallback() {
@Override
@ -122,22 +133,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
}
@Override
public void noPost() {
mFetchPostInfoLinearLayout.setOnClickListener(view -> {
//Do nothing
});
showErrorView(R.string.no_posts);
}
});
} else {
factory = new PostViewModel.Factory(mRetrofit,
getResources().getConfiguration().locale, mName, mPostType, new PostDataSource.OnPostFetchedCallback() {
@Override
public void hasPost() {
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
}
@Override
public void noPost() {
mFetchPostInfoLinearLayout.setOnClickListener(view -> {
@ -148,6 +143,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
});
}
mPostRecyclerView.setAdapter(mAdapter);
mPostViewModel = ViewModelProviders.of(this, factory).get(PostViewModel.class);
mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts));

View File

@ -35,9 +35,9 @@ public class PostViewModel extends ViewModel {
posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
}
public PostViewModel(Retrofit retrofit, Locale locale, String subredditName, int postType,
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType,
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
postDataSourceFactory = new PostDataSourceFactory(retrofit, locale, subredditName, postType, onPostFetchedCallback);
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, postType, onPostFetchedCallback);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
dataSource -> dataSource.getInitialLoadStateLiveData());
@ -94,9 +94,10 @@ public class PostViewModel extends ViewModel {
this.onPostFetchedCallback = onPostFetchedCallback;
}
public Factory(Retrofit retrofit, Locale locale, String subredditName, int postType,
public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType,
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
this.retrofit = retrofit;
this.accessToken = accessToken;
this.locale = locale;
this.subredditName = subredditName;
this.postType = postType;
@ -109,7 +110,7 @@ public class PostViewModel extends ViewModel {
if(postType == PostDataSource.TYPE_FRONT_PAGE) {
return (T) new PostViewModel(retrofit, accessToken, locale, postType, onPostFetchedCallback);
} else {
return (T) new PostViewModel(retrofit, locale, subredditName, postType, onPostFetchedCallback);
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, onPostFetchedCallback);
}
}
}

View File

@ -39,10 +39,12 @@ public interface RedditAPI {
Call<String> getBestPosts(@Query("after") String lastItem, @HeaderMap Map<String, String> headers);
@GET("r/{subredditName}.json?raw_json=1&limit=25")
Call<String> getSubredditBestPosts(@Path("subredditName") String subredditName, @Query("after") String lastItem);
Call<String> getSubredditBestPosts(@Path("subredditName") String subredditName, @Query("after") String lastItem,
@HeaderMap Map<String, String> headers);
@GET("user/{userName}.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,
@HeaderMap Map<String, String> headers);
@GET("user/{username}/about.json?raw_json=1")
Call<String> getUserData(@Path("username") String username);

View File

@ -12,7 +12,7 @@ import java.util.Map;
public class RedditUtils {
static final String OAUTH_URL ="https://www.reddit.com/api/v1/authorize.compact";
static final String OAUTH_API_BASE_URI = "https://oauth.reddit.com";
static final String API_BASE_URI = "https://www.reddit.com";
static final String API_BASE_URI = "https://api.reddit.com";
static final String CLIENT_ID_KEY = "client_id";
static final String CLIENT_ID = "";