mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-11-10 12:47:26 +01:00
Fixed vote status cannot be shown on posts loaded from specific subreddits or users.
This commit is contained in:
parent
86570d4959
commit
0e1abee883
@ -47,8 +47,9 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
|||||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
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.retrofit = retrofit;
|
||||||
|
this.accessToken = accessToken;
|
||||||
this.locale = locale;
|
this.locale = locale;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
paginationNetworkStateLiveData = new MutableLiveData();
|
paginationNetworkStateLiveData = new MutableLiveData();
|
||||||
@ -192,7 +193,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
|||||||
|
|
||||||
private void loadSubredditPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback) {
|
private void loadSubredditPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback) {
|
||||||
RedditAPI api = retrofit.create(RedditAPI.class);
|
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>() {
|
getPost.enqueue(new Callback<String>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<String> call, retrofit2.Response<String> response) {
|
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) {
|
private void loadSubredditPostsAfter(@NonNull LoadParams<String> params, @NonNull final LoadCallback<String, Post> callback) {
|
||||||
RedditAPI api = retrofit.create(RedditAPI.class);
|
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>() {
|
getPost.enqueue(new Callback<String>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<String> call, retrofit2.Response<String> response) {
|
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) {
|
private void loadUserPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback, String lastItem) {
|
||||||
RedditAPI api = retrofit.create(RedditAPI.class);
|
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>() {
|
getPost.enqueue(new Callback<String>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<String> call, retrofit2.Response<String> response) {
|
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) {
|
private void loadUserPostsAfter(@NonNull LoadParams<String> params, @NonNull final LoadCallback<String, Post> callback) {
|
||||||
RedditAPI api = retrofit.create(RedditAPI.class);
|
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>() {
|
getPost.enqueue(new Callback<String>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<String> call, retrofit2.Response<String> response) {
|
public void onResponse(Call<String> call, retrofit2.Response<String> response) {
|
||||||
|
@ -28,9 +28,10 @@ class PostDataSourceFactory extends DataSource.Factory {
|
|||||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
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) {
|
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||||
this.retrofit = retrofit;
|
this.retrofit = retrofit;
|
||||||
|
this.accessToken = accessToken;
|
||||||
this.locale = locale;
|
this.locale = locale;
|
||||||
this.subredditName = subredditName;
|
this.subredditName = subredditName;
|
||||||
postDataSourceLiveData = new MutableLiveData<>();
|
postDataSourceLiveData = new MutableLiveData<>();
|
||||||
@ -43,7 +44,7 @@ class PostDataSourceFactory extends DataSource.Factory {
|
|||||||
if(postType == PostDataSource.TYPE_FRONT_PAGE) {
|
if(postType == PostDataSource.TYPE_FRONT_PAGE) {
|
||||||
postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, onPostFetchedCallback);
|
postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, onPostFetchedCallback);
|
||||||
} else {
|
} else {
|
||||||
postDataSource = new PostDataSource(retrofit, locale, subredditName, postType, onPostFetchedCallback);
|
postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, onPostFetchedCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
postDataSourceLiveData.postValue(postDataSource);
|
postDataSourceLiveData.postValue(postDataSource);
|
||||||
|
@ -23,6 +23,8 @@ import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
|
|
||||||
|
import butterknife.BindView;
|
||||||
|
import butterknife.ButterKnife;
|
||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
|
|
||||||
@ -34,13 +36,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
|||||||
static final String NAME_KEY = "NK";
|
static final String NAME_KEY = "NK";
|
||||||
static final String POST_TYPE_KEY = "PTK";
|
static final String POST_TYPE_KEY = "PTK";
|
||||||
|
|
||||||
private CoordinatorLayout mCoordinatorLayout;
|
@BindView(R.id.coordinator_layout_post_fragment) CoordinatorLayout mCoordinatorLayout;
|
||||||
private RecyclerView mPostRecyclerView;
|
@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 LinearLayoutManager mLinearLayoutManager;
|
||||||
private CircleProgressBar mProgressBar;
|
|
||||||
private LinearLayout mFetchPostInfoLinearLayout;
|
|
||||||
private ImageView mFetchPostInfoImageView;
|
|
||||||
private TextView mFetchPostInfoTextView;
|
|
||||||
|
|
||||||
private String mName;
|
private String mName;
|
||||||
private int mPostType;
|
private int mPostType;
|
||||||
@ -78,14 +81,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
|||||||
|
|
||||||
((Infinity) getActivity().getApplication()).getmNetworkComponent().inject(this);
|
((Infinity) getActivity().getApplication()).getmNetworkComponent().inject(this);
|
||||||
|
|
||||||
mCoordinatorLayout = rootView.findViewById(R.id.coordinator_layout_post_fragment);
|
ButterKnife.bind(this, rootView);
|
||||||
mPostRecyclerView = rootView.findViewById(R.id.recycler_view_post_fragment);
|
|
||||||
mLinearLayoutManager = new LinearLayoutManager(getActivity());
|
mLinearLayoutManager = new LinearLayoutManager(getActivity());
|
||||||
mPostRecyclerView.setLayoutManager(mLinearLayoutManager);
|
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);
|
/*FloatingActionButton fab = rootView.findViewById(R.id.fab_post_fragment);
|
||||||
fab.setOnClickListener(new View.OnClickListener() {
|
fab.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
@ -97,24 +96,36 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
|||||||
|
|
||||||
mPostType = getArguments().getInt(POST_TYPE_KEY);
|
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)
|
String accessToken = getActivity().getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE)
|
||||||
.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
|
.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
|
||||||
|
|
||||||
PostViewModel.Factory factory;
|
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,
|
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
|
||||||
getResources().getConfiguration().locale, mPostType, new PostDataSource.OnPostFetchedCallback() {
|
getResources().getConfiguration().locale, mPostType, new PostDataSource.OnPostFetchedCallback() {
|
||||||
@Override
|
@Override
|
||||||
@ -130,23 +141,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
|||||||
showErrorView(R.string.no_posts);
|
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
|
mPostRecyclerView.setAdapter(mAdapter);
|
||||||
public void noPost() {
|
|
||||||
mFetchPostInfoLinearLayout.setOnClickListener(view -> {
|
|
||||||
//Do nothing
|
|
||||||
});
|
|
||||||
showErrorView(R.string.no_posts);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
mPostViewModel = ViewModelProviders.of(this, factory).get(PostViewModel.class);
|
mPostViewModel = ViewModelProviders.of(this, factory).get(PostViewModel.class);
|
||||||
mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts));
|
mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts));
|
||||||
|
@ -35,9 +35,9 @@ public class PostViewModel extends ViewModel {
|
|||||||
posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
|
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) {
|
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(),
|
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||||
dataSource -> dataSource.getInitialLoadStateLiveData());
|
dataSource -> dataSource.getInitialLoadStateLiveData());
|
||||||
@ -94,9 +94,10 @@ public class PostViewModel extends ViewModel {
|
|||||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
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) {
|
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||||
this.retrofit = retrofit;
|
this.retrofit = retrofit;
|
||||||
|
this.accessToken = accessToken;
|
||||||
this.locale = locale;
|
this.locale = locale;
|
||||||
this.subredditName = subredditName;
|
this.subredditName = subredditName;
|
||||||
this.postType = postType;
|
this.postType = postType;
|
||||||
@ -109,7 +110,7 @@ public class PostViewModel extends ViewModel {
|
|||||||
if(postType == PostDataSource.TYPE_FRONT_PAGE) {
|
if(postType == PostDataSource.TYPE_FRONT_PAGE) {
|
||||||
return (T) new PostViewModel(retrofit, accessToken, locale, postType, onPostFetchedCallback);
|
return (T) new PostViewModel(retrofit, accessToken, locale, postType, onPostFetchedCallback);
|
||||||
} else {
|
} else {
|
||||||
return (T) new PostViewModel(retrofit, locale, subredditName, postType, onPostFetchedCallback);
|
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, onPostFetchedCallback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,10 +39,12 @@ public interface RedditAPI {
|
|||||||
Call<String> getBestPosts(@Query("after") String lastItem, @HeaderMap Map<String, String> headers);
|
Call<String> getBestPosts(@Query("after") String lastItem, @HeaderMap Map<String, String> headers);
|
||||||
|
|
||||||
@GET("r/{subredditName}.json?raw_json=1&limit=25")
|
@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")
|
@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")
|
@GET("user/{username}/about.json?raw_json=1")
|
||||||
Call<String> getUserData(@Path("username") String username);
|
Call<String> getUserData(@Path("username") String username);
|
||||||
|
@ -12,7 +12,7 @@ import java.util.Map;
|
|||||||
public class RedditUtils {
|
public class RedditUtils {
|
||||||
static final String OAUTH_URL ="https://www.reddit.com/api/v1/authorize.compact";
|
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 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_KEY = "client_id";
|
||||||
static final String CLIENT_ID = "";
|
static final String CLIENT_ID = "";
|
||||||
|
Loading…
Reference in New Issue
Block a user