From 0e1abee883469a6345485499013c97c252361f33 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 24 Jan 2019 23:24:14 +0800 Subject: Fixed vote status cannot be shown on posts loaded from specific subreddits or users. --- .../infinityforreddit/PostDataSource.java | 11 ++--- .../infinityforreddit/PostDataSourceFactory.java | 5 ++- .../infinityforreddit/PostFragment.java | 51 ++++++++++------------ .../infinityforreddit/PostViewModel.java | 9 ++-- .../infinityforreddit/RedditAPI.java | 6 ++- .../infinityforreddit/RedditUtils.java | 2 +- 6 files changed, 43 insertions(+), 41 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java index 615ceb9f..9ff04e03 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java @@ -47,8 +47,9 @@ class PostDataSource extends PageKeyedDataSource { 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 { private void loadSubredditPostsInitial(@NonNull final LoadInitialCallback callback) { RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.getSubredditBestPosts(name, null); + Call getPost = api.getSubredditBestPosts(name, null, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback() { @Override public void onResponse(Call call, retrofit2.Response response) { @@ -233,7 +234,7 @@ class PostDataSource extends PageKeyedDataSource { private void loadSubredditPostsAfter(@NonNull LoadParams params, @NonNull final LoadCallback callback) { RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.getSubredditBestPosts(name, params.key); + Call getPost = api.getSubredditBestPosts(name, params.key, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback() { @Override public void onResponse(Call call, retrofit2.Response response) { @@ -267,7 +268,7 @@ class PostDataSource extends PageKeyedDataSource { private void loadUserPostsInitial(@NonNull final LoadInitialCallback callback, String lastItem) { RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.getUserBestPosts(name, lastItem); + Call getPost = api.getUserBestPosts(name, lastItem, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback() { @Override public void onResponse(Call call, retrofit2.Response response) { @@ -311,7 +312,7 @@ class PostDataSource extends PageKeyedDataSource { private void loadUserPostsAfter(@NonNull LoadParams params, @NonNull final LoadCallback callback) { RedditAPI api = retrofit.create(RedditAPI.class); - Call getPost = api.getUserBestPosts(name, params.key); + Call getPost = api.getUserBestPosts(name, params.key, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback() { @Override public void onResponse(Call call, retrofit2.Response response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java index 848799bf..58f67d42 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java @@ -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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index e4023fd0..28ad4cef 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -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,26 +96,19 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mPostType = getArguments().getInt(POST_TYPE_KEY); + 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) { 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) { factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, - getResources().getConfiguration().locale, mPostType, new PostDataSource.OnPostFetchedCallback() { + getResources().getConfiguration().locale, mName, mPostType, new PostDataSource.OnPostFetchedCallback() { @Override public void hasPost() { mFetchPostInfoLinearLayout.setVisibility(View.GONE); @@ -131,8 +123,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }); } else { - factory = new PostViewModel.Factory(mRetrofit, - getResources().getConfiguration().locale, mName, mPostType, new PostDataSource.OnPostFetchedCallback() { + mAdapter = new PostRecyclerViewAdapter(getActivity(), mOauthRetrofit, + mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore()); + + factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, + getResources().getConfiguration().locale, mPostType, new PostDataSource.OnPostFetchedCallback() { @Override public void hasPost() { mFetchPostInfoLinearLayout.setVisibility(View.GONE); @@ -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)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java index 69df991f..8bfc4560 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java @@ -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); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index 41fe1567..3a697832 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -39,10 +39,12 @@ public interface RedditAPI { Call getBestPosts(@Query("after") String lastItem, @HeaderMap Map headers); @GET("r/{subredditName}.json?raw_json=1&limit=25") - Call getSubredditBestPosts(@Path("subredditName") String subredditName, @Query("after") String lastItem); + Call getSubredditBestPosts(@Path("subredditName") String subredditName, @Query("after") String lastItem, + @HeaderMap Map headers); @GET("user/{userName}.json?raw_json=1&limit=25") - Call getUserBestPosts(@Path("userName") String userName, @Query("after") String lastItem); + Call getUserBestPosts(@Path("userName") String userName, @Query("after") String lastItem, + @HeaderMap Map headers); @GET("user/{username}/about.json?raw_json=1") Call getUserData(@Path("username") String username); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java index d598f684..aa9f0ab3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java @@ -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 = ""; -- cgit v1.2.3