mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-11 18:57:11 +01:00
Fixed no posts, no comments, no users and no subreddits message cannot be retained after orientation change in PostFragment, CommentsListingFragment, UserListingFragment and SubredditListingFragment respectively. Minor bugs fixed.
This commit is contained in:
parent
85597a82d0
commit
d408a47dba
@ -2,12 +2,11 @@ package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import SubscribedSubredditDatabase.SubscribedSubredditDao;
|
||||
import SubscribedSubredditDatabase.SubscribedSubredditData;
|
||||
|
||||
class CheckIsSubscribedToSubredditAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||
|
||||
private SubscribedSubredditDao subscribedSubredditDao;
|
||||
private RedditDataRoomDatabase redditDataRoomDatabase;
|
||||
private String subredditName;
|
||||
private String accountName;
|
||||
private SubscribedSubredditData subscribedSubredditData;
|
||||
@ -18,10 +17,10 @@ class CheckIsSubscribedToSubredditAsyncTask extends AsyncTask<Void, Void, Void>
|
||||
void isNotSubscribed();
|
||||
}
|
||||
|
||||
CheckIsSubscribedToSubredditAsyncTask(SubscribedSubredditDao subscribedSubredditDao,
|
||||
CheckIsSubscribedToSubredditAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase,
|
||||
String subredditName, String accountName,
|
||||
CheckIsSubscribedToSubredditListener checkIsSubscribedToSubredditListener) {
|
||||
this.subscribedSubredditDao = subscribedSubredditDao;
|
||||
this.redditDataRoomDatabase = redditDataRoomDatabase;
|
||||
this.subredditName =subredditName;
|
||||
this.accountName = accountName;
|
||||
this.checkIsSubscribedToSubredditListener = checkIsSubscribedToSubredditListener;
|
||||
@ -29,7 +28,7 @@ class CheckIsSubscribedToSubredditAsyncTask extends AsyncTask<Void, Void, Void>
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
subscribedSubredditData = subscribedSubredditDao.getSubscribedSubreddit(subredditName, accountName);
|
||||
subscribedSubredditData = redditDataRoomDatabase.subscribedSubredditDao().getSubscribedSubreddit(subredditName, accountName);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -20,41 +20,41 @@ import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class CommentDataSource extends PageKeyedDataSource<String, CommentData> {
|
||||
interface OnCommentFetchedCallback {
|
||||
void hasComment();
|
||||
void noComment();
|
||||
}
|
||||
|
||||
private Retrofit retrofit;
|
||||
private Locale locale;
|
||||
private String username;
|
||||
private OnCommentFetchedCallback onCommentFetchedCallback;
|
||||
|
||||
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
|
||||
private MutableLiveData<NetworkState> initialLoadStateLiveData;
|
||||
private MutableLiveData<Boolean> hasPostLiveData;
|
||||
|
||||
private LoadInitialParams<String> initialParams;
|
||||
private LoadInitialCallback<String, CommentData> initialCallback;
|
||||
private LoadParams<String> params;
|
||||
private LoadCallback<String, CommentData> callback;
|
||||
|
||||
CommentDataSource(Retrofit retrofit, Locale locale, String username, OnCommentFetchedCallback onCommentFetchedCallback) {
|
||||
CommentDataSource(Retrofit retrofit, Locale locale, String username) {
|
||||
this.retrofit = retrofit;
|
||||
this.locale = locale;
|
||||
this.username = username;
|
||||
paginationNetworkStateLiveData = new MutableLiveData();
|
||||
initialLoadStateLiveData = new MutableLiveData();
|
||||
this.onCommentFetchedCallback = onCommentFetchedCallback;
|
||||
paginationNetworkStateLiveData = new MutableLiveData<>();
|
||||
initialLoadStateLiveData = new MutableLiveData<>();
|
||||
hasPostLiveData = new MutableLiveData<>();
|
||||
}
|
||||
|
||||
MutableLiveData getPaginationNetworkStateLiveData() {
|
||||
MutableLiveData<NetworkState> getPaginationNetworkStateLiveData() {
|
||||
return paginationNetworkStateLiveData;
|
||||
}
|
||||
|
||||
MutableLiveData getInitialLoadStateLiveData() {
|
||||
MutableLiveData<NetworkState> getInitialLoadStateLiveData() {
|
||||
return initialLoadStateLiveData;
|
||||
}
|
||||
|
||||
MutableLiveData<Boolean> hasPostLiveData() {
|
||||
return hasPostLiveData;
|
||||
}
|
||||
|
||||
void retry() {
|
||||
loadInitial(initialParams, initialCallback);
|
||||
}
|
||||
@ -80,9 +80,9 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData>
|
||||
@Override
|
||||
public void parseSuccessful(ArrayList<CommentData> comments, String after) {
|
||||
if(comments.size() == 0) {
|
||||
onCommentFetchedCallback.noComment();
|
||||
hasPostLiveData.postValue(false);
|
||||
} else {
|
||||
onCommentFetchedCallback.hasComment();
|
||||
hasPostLiveData.postValue(true);
|
||||
}
|
||||
|
||||
callback.onResult(comments, null, after);
|
||||
|
@ -12,23 +12,21 @@ class CommentDataSourceFactory extends DataSource.Factory {
|
||||
private Retrofit retrofit;
|
||||
private Locale locale;
|
||||
private String username;
|
||||
private CommentDataSource.OnCommentFetchedCallback onCommentFetchedCallback;
|
||||
|
||||
private CommentDataSource commentDataSource;
|
||||
private MutableLiveData<CommentDataSource> commentDataSourceLiveData;
|
||||
|
||||
CommentDataSourceFactory(Retrofit retrofit, Locale locale, String username, CommentDataSource.OnCommentFetchedCallback onCommentFetchedCallback) {
|
||||
CommentDataSourceFactory(Retrofit retrofit, Locale locale, String username) {
|
||||
this.retrofit = retrofit;
|
||||
this.locale = locale;
|
||||
this.username = username;
|
||||
commentDataSourceLiveData = new MutableLiveData<>();
|
||||
this.onCommentFetchedCallback = onCommentFetchedCallback;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public DataSource create() {
|
||||
commentDataSource = new CommentDataSource(retrofit, locale, username, onCommentFetchedCallback);
|
||||
commentDataSource = new CommentDataSource(retrofit, locale, username);
|
||||
commentDataSourceLiveData.postValue(commentDataSource);
|
||||
return commentDataSource;
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.arch.core.util.Function;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.Transformations;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
@ -17,16 +16,18 @@ public class CommentViewModel extends ViewModel {
|
||||
private CommentDataSourceFactory commentDataSourceFactory;
|
||||
private LiveData<NetworkState> paginationNetworkState;
|
||||
private LiveData<NetworkState> initialLoadingState;
|
||||
private LiveData<Boolean> hasCommentLiveData;
|
||||
private LiveData<PagedList<CommentData>> comments;
|
||||
|
||||
public CommentViewModel(Retrofit retrofit, Locale locale, String username,
|
||||
CommentDataSource.OnCommentFetchedCallback onCommentFetchedCallback) {
|
||||
commentDataSourceFactory = new CommentDataSourceFactory(retrofit, locale, username, onCommentFetchedCallback);
|
||||
public CommentViewModel(Retrofit retrofit, Locale locale, String username) {
|
||||
commentDataSourceFactory = new CommentDataSourceFactory(retrofit, locale, username);
|
||||
|
||||
initialLoadingState = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
|
||||
(Function<CommentDataSource, LiveData<NetworkState>>) CommentDataSource::getInitialLoadStateLiveData);
|
||||
CommentDataSource::getInitialLoadStateLiveData);
|
||||
paginationNetworkState = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
|
||||
(Function<CommentDataSource, LiveData<NetworkState>>) CommentDataSource::getPaginationNetworkStateLiveData);
|
||||
CommentDataSource::getPaginationNetworkStateLiveData);
|
||||
hasCommentLiveData = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
|
||||
CommentDataSource::hasPostLiveData);
|
||||
PagedList.Config pagedListConfig =
|
||||
(new PagedList.Config.Builder())
|
||||
.setEnablePlaceholders(false)
|
||||
@ -48,6 +49,10 @@ public class CommentViewModel extends ViewModel {
|
||||
return initialLoadingState;
|
||||
}
|
||||
|
||||
LiveData<Boolean> hasComment() {
|
||||
return hasCommentLiveData;
|
||||
}
|
||||
|
||||
void refresh() {
|
||||
commentDataSourceFactory.getCommentDataSource().invalidate();
|
||||
}
|
||||
@ -64,20 +69,17 @@ public class CommentViewModel extends ViewModel {
|
||||
private Retrofit retrofit;
|
||||
private Locale locale;
|
||||
private String username;
|
||||
private CommentDataSource.OnCommentFetchedCallback onCommentFetchedCallback;
|
||||
|
||||
public Factory(Retrofit retrofit, Locale locale, String username,
|
||||
CommentDataSource.OnCommentFetchedCallback onCommentFetchedCallback) {
|
||||
public Factory(Retrofit retrofit, Locale locale, String username) {
|
||||
this.retrofit = retrofit;
|
||||
this.locale = locale;
|
||||
this.username = username;
|
||||
this.onCommentFetchedCallback = onCommentFetchedCallback;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
|
||||
return (T) new CommentViewModel(retrofit, locale, username, onCommentFetchedCallback);
|
||||
return (T) new CommentViewModel(retrofit, locale, username);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -78,30 +78,14 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
|
||||
|
||||
mCommentRecyclerView.setLayoutManager(new LinearLayoutManager(activity));
|
||||
|
||||
CommentViewModel.Factory factory;
|
||||
mAdapter = new CommentsListingRecyclerViewAdapter(activity, mOauthRetrofit,
|
||||
getArguments().getString(EXTRA_ACCESS_TOKEN), () -> mCommentViewModel.retryLoadingMore());
|
||||
|
||||
String username = getArguments().getString(EXTRA_USERNAME_KEY);
|
||||
|
||||
factory = new CommentViewModel.Factory(mRetrofit, getResources().getConfiguration().locale,
|
||||
username, new CommentDataSource.OnCommentFetchedCallback() {
|
||||
@Override
|
||||
public void hasComment() {
|
||||
mFetchCommentInfoLinearLayout.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void noComment() {
|
||||
mFetchCommentInfoLinearLayout.setOnClickListener(view -> {
|
||||
//Do nothing
|
||||
});
|
||||
showErrorView(R.string.no_posts);
|
||||
}
|
||||
});
|
||||
|
||||
mCommentRecyclerView.setAdapter(mAdapter);
|
||||
|
||||
CommentViewModel.Factory factory = new CommentViewModel.Factory(mRetrofit, getResources().getConfiguration().locale, username);
|
||||
mCommentViewModel = ViewModelProviders.of(this, factory).get(CommentViewModel.class);
|
||||
mCommentViewModel.getComments().observe(this, comments -> mAdapter.submitList(comments));
|
||||
|
||||
@ -117,6 +101,17 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
|
||||
}
|
||||
});
|
||||
|
||||
mCommentViewModel.hasComment().observe(this, hasComment -> {
|
||||
if(hasComment) {
|
||||
mFetchCommentInfoLinearLayout.setVisibility(View.GONE);
|
||||
} else {
|
||||
mFetchCommentInfoLinearLayout.setOnClickListener(view -> {
|
||||
//Do nothing
|
||||
});
|
||||
showErrorView(R.string.no_comments);
|
||||
}
|
||||
});
|
||||
|
||||
mCommentViewModel.getPaginationNetworkState().observe(this, networkState -> {
|
||||
mAdapter.setNetworkState(networkState);
|
||||
});
|
||||
|
@ -150,6 +150,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
||||
mProfileImageUrl = savedInstanceState.getString(ACCOUNT_PROFILE_IMAGE_URL_STATE);
|
||||
mBannerImageUrl = savedInstanceState.getString(ACCOUNT_BANNER_IMAGE_URL_STATE);
|
||||
mKarma = savedInstanceState.getInt(ACCOUNT_KARMA_STATE);
|
||||
|
||||
if(!mNullAccessToken && mAccessToken == null) {
|
||||
getCurrentAccountAndBindView();
|
||||
} else {
|
||||
|
@ -14,10 +14,6 @@ import retrofit2.Callback;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
interface OnPostFetchedCallback {
|
||||
void hasPost();
|
||||
void noPost();
|
||||
}
|
||||
|
||||
static final int TYPE_FRONT_PAGE = 0;
|
||||
static final int TYPE_SUBREDDIT = 1;
|
||||
@ -42,10 +38,10 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
private int postType;
|
||||
private String sortType;
|
||||
private int filter;
|
||||
private OnPostFetchedCallback onPostFetchedCallback;
|
||||
|
||||
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
|
||||
private MutableLiveData<NetworkState> initialLoadStateLiveData;
|
||||
private MutableLiveData<Boolean> hasPostLiveData;
|
||||
|
||||
private LoadInitialParams<String> initialParams;
|
||||
private LoadInitialCallback<String, Post> initialCallback;
|
||||
@ -53,68 +49,72 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
private LoadCallback<String, Post> callback;
|
||||
|
||||
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType,
|
||||
int filter, OnPostFetchedCallback onPostFetchedCallback) {
|
||||
int filter) {
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.locale = locale;
|
||||
paginationNetworkStateLiveData = new MutableLiveData();
|
||||
initialLoadStateLiveData = new MutableLiveData();
|
||||
hasPostLiveData = new MutableLiveData<>();
|
||||
this.postType = postType;
|
||||
this.sortType = sortType;
|
||||
this.filter = filter;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, int postType,
|
||||
String sortType, int filter, OnPostFetchedCallback onPostFetchedCallback) {
|
||||
String sortType, int filter) {
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.locale = locale;
|
||||
this.subredditOrUserName = subredditOrUserName;
|
||||
paginationNetworkStateLiveData = new MutableLiveData();
|
||||
initialLoadStateLiveData = new MutableLiveData();
|
||||
paginationNetworkStateLiveData = new MutableLiveData<>();
|
||||
initialLoadStateLiveData = new MutableLiveData<>();
|
||||
hasPostLiveData = new MutableLiveData<>();
|
||||
this.postType = postType;
|
||||
this.sortType = sortType;
|
||||
this.filter = filter;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, int postType,
|
||||
int filter, OnPostFetchedCallback onPostFetchedCallback) {
|
||||
int filter) {
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.locale = locale;
|
||||
this.subredditOrUserName = subredditOrUserName;
|
||||
paginationNetworkStateLiveData = new MutableLiveData();
|
||||
initialLoadStateLiveData = new MutableLiveData();
|
||||
paginationNetworkStateLiveData = new MutableLiveData<>();
|
||||
initialLoadStateLiveData = new MutableLiveData<>();
|
||||
hasPostLiveData = new MutableLiveData<>();
|
||||
this.postType = postType;
|
||||
this.filter = filter;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, String query,
|
||||
int postType, String sortType, int filter, OnPostFetchedCallback onPostFetchedCallback) {
|
||||
int postType, String sortType, int filter) {
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.locale = locale;
|
||||
this.subredditOrUserName = subredditOrUserName;
|
||||
this.query = query;
|
||||
paginationNetworkStateLiveData = new MutableLiveData();
|
||||
initialLoadStateLiveData = new MutableLiveData();
|
||||
paginationNetworkStateLiveData = new MutableLiveData<>();
|
||||
initialLoadStateLiveData = new MutableLiveData<>();
|
||||
hasPostLiveData = new MutableLiveData<>();
|
||||
this.postType = postType;
|
||||
this.sortType = sortType;
|
||||
this.filter = filter;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
MutableLiveData getPaginationNetworkStateLiveData() {
|
||||
MutableLiveData<NetworkState> getPaginationNetworkStateLiveData() {
|
||||
return paginationNetworkStateLiveData;
|
||||
}
|
||||
|
||||
MutableLiveData getInitialLoadStateLiveData() {
|
||||
MutableLiveData<NetworkState> getInitialLoadStateLiveData() {
|
||||
return initialLoadStateLiveData;
|
||||
}
|
||||
|
||||
MutableLiveData<Boolean> hasPostLiveData() {
|
||||
return hasPostLiveData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull final LoadInitialCallback<String, Post> callback) {
|
||||
initialParams = params;
|
||||
@ -184,7 +184,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
public void onParsePostSuccess(Post post) {
|
||||
ArrayList<Post> singlePostList = new ArrayList<>();
|
||||
singlePostList.add(post);
|
||||
onPostFetchedCallback.hasPost();
|
||||
hasPostLiveData.postValue(true);
|
||||
callback.onResult(singlePostList, null, null);
|
||||
initialLoadStateLiveData.postValue(NetworkState.LOADED);
|
||||
}
|
||||
@ -208,12 +208,12 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
}
|
||||
|
||||
if(newPosts.size() != 0) {
|
||||
onPostFetchedCallback.hasPost();
|
||||
hasPostLiveData.postValue(true);
|
||||
} else if(nextPageKey != null) {
|
||||
loadBestPostsInitial(callback, nextPageKey);
|
||||
return;
|
||||
} else {
|
||||
onPostFetchedCallback.noPost();
|
||||
hasPostLiveData.postValue(false);
|
||||
}
|
||||
|
||||
callback.onResult(newPosts, null, nextPageKey);
|
||||
@ -295,7 +295,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
public void onParsePostSuccess(Post post) {
|
||||
ArrayList<Post> singlePostList = new ArrayList<>();
|
||||
singlePostList.add(post);
|
||||
onPostFetchedCallback.hasPost();
|
||||
hasPostLiveData.postValue(true);
|
||||
callback.onResult(singlePostList, null, null);
|
||||
initialLoadStateLiveData.postValue(NetworkState.LOADED);
|
||||
}
|
||||
@ -319,12 +319,12 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
}
|
||||
|
||||
if(newPosts.size() != 0) {
|
||||
onPostFetchedCallback.hasPost();
|
||||
hasPostLiveData.postValue(true);
|
||||
} else if(nextPageKey != null) {
|
||||
loadSubredditPostsInitial(callback, nextPageKey);
|
||||
return;
|
||||
} else {
|
||||
onPostFetchedCallback.noPost();
|
||||
hasPostLiveData.postValue(false);
|
||||
}
|
||||
|
||||
callback.onResult(newPosts, null, nextPageKey);
|
||||
@ -411,12 +411,12 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
}
|
||||
|
||||
if(newPosts.size() != 0) {
|
||||
onPostFetchedCallback.hasPost();
|
||||
hasPostLiveData.postValue(true);
|
||||
} else if(nextPageKey != null) {
|
||||
loadUserPostsInitial(callback, nextPageKey);
|
||||
return;
|
||||
} else {
|
||||
onPostFetchedCallback.noPost();
|
||||
hasPostLiveData.postValue(false);
|
||||
}
|
||||
|
||||
callback.onResult(newPosts, null, nextPageKey);
|
||||
@ -509,12 +509,12 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
}
|
||||
|
||||
if(newPosts.size() != 0) {
|
||||
onPostFetchedCallback.hasPost();
|
||||
hasPostLiveData.postValue(true);
|
||||
} else if(nextPageKey != null) {
|
||||
loadSearchPostsInitial(callback, nextPageKey);
|
||||
return;
|
||||
} else {
|
||||
onPostFetchedCallback.noPost();
|
||||
hasPostLiveData.postValue(false);
|
||||
}
|
||||
|
||||
callback.onResult(newPosts, null, nextPageKey);
|
||||
|
@ -16,13 +16,12 @@ class PostDataSourceFactory extends DataSource.Factory {
|
||||
private int postType;
|
||||
private String sortType;
|
||||
private int filter;
|
||||
private PostDataSource.OnPostFetchedCallback onPostFetchedCallback;
|
||||
|
||||
private PostDataSource postDataSource;
|
||||
private MutableLiveData<PostDataSource> postDataSourceLiveData;
|
||||
|
||||
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType,
|
||||
int filter, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
int filter) {
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.locale = locale;
|
||||
@ -30,12 +29,10 @@ class PostDataSourceFactory extends DataSource.Factory {
|
||||
this.postType = postType;
|
||||
this.sortType = sortType;
|
||||
this.filter = filter;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName,
|
||||
int postType, String sortType, int filter,
|
||||
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
int postType, String sortType, int filter) {
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.locale = locale;
|
||||
@ -44,12 +41,10 @@ class PostDataSourceFactory extends DataSource.Factory {
|
||||
this.postType = postType;
|
||||
this.sortType = sortType;
|
||||
this.filter = filter;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName,
|
||||
int postType, int filter,
|
||||
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
int postType, int filter) {
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.locale = locale;
|
||||
@ -57,12 +52,10 @@ class PostDataSourceFactory extends DataSource.Factory {
|
||||
postDataSourceLiveData = new MutableLiveData<>();
|
||||
this.postType = postType;
|
||||
this.filter = filter;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName,
|
||||
String query, int postType, String sortType, int filter,
|
||||
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
String query, int postType, String sortType, int filter) {
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.locale = locale;
|
||||
@ -72,23 +65,22 @@ class PostDataSourceFactory extends DataSource.Factory {
|
||||
this.postType = postType;
|
||||
this.sortType = sortType;
|
||||
this.filter = filter;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource create() {
|
||||
if(postType == PostDataSource.TYPE_FRONT_PAGE) {
|
||||
postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, sortType,
|
||||
filter, onPostFetchedCallback);
|
||||
filter);
|
||||
} else if(postType == PostDataSource.TYPE_SEARCH) {
|
||||
postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, query,
|
||||
postType, sortType, filter, onPostFetchedCallback);
|
||||
postType, sortType, filter);
|
||||
} else if(postType == PostDataSource.TYPE_SUBREDDIT) {
|
||||
postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType,
|
||||
sortType, filter, onPostFetchedCallback);
|
||||
sortType, filter);
|
||||
} else {
|
||||
postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType,
|
||||
filter, onPostFetchedCallback);
|
||||
filter);
|
||||
}
|
||||
|
||||
postDataSourceLiveData.postValue(postDataSource);
|
||||
|
@ -201,20 +201,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
||||
});
|
||||
|
||||
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
|
||||
getResources().getConfiguration().locale, subredditName, query, postType, sortType, filter, 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);
|
||||
}
|
||||
});
|
||||
getResources().getConfiguration().locale, subredditName, query, postType, sortType, filter);
|
||||
} else if(postType == PostDataSource.TYPE_SUBREDDIT) {
|
||||
String subredditName = getArguments().getString(EXTRA_NAME);
|
||||
|
||||
@ -238,20 +225,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
||||
});
|
||||
|
||||
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
|
||||
getResources().getConfiguration().locale, subredditName, postType, sortType, filter, 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);
|
||||
}
|
||||
});
|
||||
getResources().getConfiguration().locale, subredditName, postType, sortType, filter);
|
||||
} else if(postType == PostDataSource.TYPE_USER) {
|
||||
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mFetchPostInfoLinearLayout.getLayoutParams();
|
||||
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
|
||||
@ -278,21 +252,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
||||
});
|
||||
|
||||
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
|
||||
getResources().getConfiguration().locale, username, postType, sortType, filter,
|
||||
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);
|
||||
}
|
||||
});
|
||||
getResources().getConfiguration().locale, username, postType, sortType, filter);
|
||||
} else {
|
||||
mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, redditDataRoomDatabase,
|
||||
accessToken, postType, true, new PostRecyclerViewAdapter.Callback() {
|
||||
@ -313,20 +273,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
||||
});
|
||||
|
||||
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
|
||||
getResources().getConfiguration().locale, postType, sortType, filter, 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);
|
||||
}
|
||||
});
|
||||
getResources().getConfiguration().locale, postType, sortType, filter);
|
||||
}
|
||||
|
||||
mPostRecyclerView.setAdapter(mAdapter);
|
||||
@ -346,6 +293,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
||||
}
|
||||
});
|
||||
|
||||
mPostViewModel.hasPost().observe(this, hasPost -> {
|
||||
if(hasPost) {
|
||||
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
|
||||
} else {
|
||||
showErrorView(R.string.no_posts);
|
||||
}
|
||||
});
|
||||
|
||||
mPostViewModel.getPaginationNetworkState().observe(this, networkState -> {
|
||||
mAdapter.setNetworkState(networkState);
|
||||
});
|
||||
|
@ -1,7 +1,6 @@
|
||||
package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.arch.core.util.Function;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.Transformations;
|
||||
@ -18,17 +17,20 @@ public class PostViewModel extends ViewModel {
|
||||
private PostDataSourceFactory postDataSourceFactory;
|
||||
private LiveData<NetworkState> paginationNetworkState;
|
||||
private LiveData<NetworkState> initialLoadingState;
|
||||
private LiveData<Boolean> hasPostLiveData;
|
||||
private LiveData<PagedList<Post>> posts;
|
||||
private MutableLiveData<String> sortTypeLiveData;
|
||||
|
||||
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType,
|
||||
int filter, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType, sortType, filter, onPostFetchedCallback);
|
||||
int filter) {
|
||||
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType, sortType, filter);
|
||||
|
||||
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||
(Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getInitialLoadStateLiveData);
|
||||
PostDataSource::getInitialLoadStateLiveData);
|
||||
paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||
(Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getPaginationNetworkStateLiveData);
|
||||
PostDataSource::getPaginationNetworkStateLiveData);
|
||||
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||
PostDataSource::hasPostLiveData);
|
||||
|
||||
sortTypeLiveData = new MutableLiveData<>();
|
||||
sortTypeLiveData.postValue(sortType);
|
||||
@ -46,14 +48,16 @@ public class PostViewModel extends ViewModel {
|
||||
}
|
||||
|
||||
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType,
|
||||
String sortType, int filter, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
String sortType, int filter) {
|
||||
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName,
|
||||
postType, sortType, filter, onPostFetchedCallback);
|
||||
postType, sortType, filter);
|
||||
|
||||
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||
(Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getInitialLoadStateLiveData);
|
||||
PostDataSource::getInitialLoadStateLiveData);
|
||||
paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||
(Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getPaginationNetworkStateLiveData);
|
||||
PostDataSource::getPaginationNetworkStateLiveData);
|
||||
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||
PostDataSource::hasPostLiveData);
|
||||
|
||||
sortTypeLiveData = new MutableLiveData<>();
|
||||
sortTypeLiveData.postValue(sortType);
|
||||
@ -71,14 +75,16 @@ public class PostViewModel extends ViewModel {
|
||||
}
|
||||
|
||||
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType,
|
||||
int filter, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
int filter) {
|
||||
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName,
|
||||
postType, filter, onPostFetchedCallback);
|
||||
postType, filter);
|
||||
|
||||
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||
dataSource -> dataSource.getInitialLoadStateLiveData());
|
||||
PostDataSource::getInitialLoadStateLiveData);
|
||||
paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||
(Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getPaginationNetworkStateLiveData);
|
||||
PostDataSource::getPaginationNetworkStateLiveData);
|
||||
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||
PostDataSource::hasPostLiveData);
|
||||
|
||||
PagedList.Config pagedListConfig =
|
||||
(new PagedList.Config.Builder())
|
||||
@ -90,14 +96,16 @@ public class PostViewModel extends ViewModel {
|
||||
}
|
||||
|
||||
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query,
|
||||
int postType, String sortType, int filter, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
int postType, String sortType, int filter) {
|
||||
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName,
|
||||
query, postType, sortType, filter, onPostFetchedCallback);
|
||||
query, postType, sortType, filter);
|
||||
|
||||
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||
dataSource -> dataSource.getInitialLoadStateLiveData());
|
||||
PostDataSource::getInitialLoadStateLiveData);
|
||||
paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||
dataSource -> dataSource.getPaginationNetworkStateLiveData());
|
||||
PostDataSource::getPaginationNetworkStateLiveData);
|
||||
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
|
||||
PostDataSource::hasPostLiveData);
|
||||
|
||||
sortTypeLiveData = new MutableLiveData<>();
|
||||
sortTypeLiveData.postValue(sortType);
|
||||
@ -126,6 +134,10 @@ public class PostViewModel extends ViewModel {
|
||||
return initialLoadingState;
|
||||
}
|
||||
|
||||
LiveData<Boolean> hasPost() {
|
||||
return hasPostLiveData;
|
||||
}
|
||||
|
||||
void refresh() {
|
||||
postDataSourceFactory.getPostDataSource().invalidate();
|
||||
}
|
||||
@ -151,21 +163,19 @@ public class PostViewModel extends ViewModel {
|
||||
private int postType;
|
||||
private String sortType;
|
||||
private int filter;
|
||||
private PostDataSource.OnPostFetchedCallback onPostFetchedCallback;
|
||||
|
||||
public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType,
|
||||
int filter, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
int filter) {
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.locale = locale;
|
||||
this.postType = postType;
|
||||
this.sortType = sortType;
|
||||
this.filter = filter;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType,
|
||||
String sortType, int filter, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
String sortType, int filter) {
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.locale = locale;
|
||||
@ -173,22 +183,20 @@ public class PostViewModel extends ViewModel {
|
||||
this.postType = postType;
|
||||
this.sortType = sortType;
|
||||
this.filter = filter;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType,
|
||||
int filter, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
int filter) {
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.locale = locale;
|
||||
this.subredditName = subredditName;
|
||||
this.postType = postType;
|
||||
this.filter = filter;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query,
|
||||
int postType, String sortType, int filter, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
|
||||
int postType, String sortType, int filter) {
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.locale = locale;
|
||||
@ -197,20 +205,19 @@ public class PostViewModel extends ViewModel {
|
||||
this.postType = postType;
|
||||
this.sortType = sortType;
|
||||
this.filter = filter;
|
||||
this.onPostFetchedCallback = onPostFetchedCallback;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
|
||||
if(postType == PostDataSource.TYPE_FRONT_PAGE) {
|
||||
return (T) new PostViewModel(retrofit, accessToken, locale, postType, sortType, filter, onPostFetchedCallback);
|
||||
return (T) new PostViewModel(retrofit, accessToken, locale, postType, sortType, filter);
|
||||
} else if(postType == PostDataSource.TYPE_SEARCH){
|
||||
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, query, postType, sortType, filter, onPostFetchedCallback);
|
||||
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, query, postType, sortType, filter);
|
||||
} else if(postType == PostDataSource.TYPE_SUBREDDIT) {
|
||||
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, sortType, filter, onPostFetchedCallback);
|
||||
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, sortType, filter);
|
||||
} else {
|
||||
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, filter, onPostFetchedCallback);
|
||||
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, filter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,41 +10,41 @@ import SubredditDatabase.SubredditData;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class SubredditListingDataSource extends PageKeyedDataSource<String, SubredditData> {
|
||||
interface OnSubredditListingDataFetchedCallback {
|
||||
void hasSubreddit();
|
||||
void noSubreddit();
|
||||
}
|
||||
|
||||
private Retrofit retrofit;
|
||||
private String query;
|
||||
private String sortType;
|
||||
private OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback;
|
||||
|
||||
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
|
||||
private MutableLiveData<NetworkState> initialLoadStateLiveData;
|
||||
private MutableLiveData<Boolean> hasSubredditLiveData;
|
||||
|
||||
private LoadInitialParams<String> initialParams;
|
||||
private LoadInitialCallback<String, SubredditData> initialCallback;
|
||||
private LoadParams<String> params;
|
||||
private LoadCallback<String, SubredditData> callback;
|
||||
|
||||
SubredditListingDataSource(Retrofit retrofit, String query, String sortType,
|
||||
OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback) {
|
||||
SubredditListingDataSource(Retrofit retrofit, String query, String sortType) {
|
||||
this.retrofit = retrofit;
|
||||
this.query = query;
|
||||
this.sortType = sortType;
|
||||
this.onSubredditListingDataFetchedCallback = onSubredditListingDataFetchedCallback;
|
||||
paginationNetworkStateLiveData = new MutableLiveData();
|
||||
initialLoadStateLiveData = new MutableLiveData();
|
||||
paginationNetworkStateLiveData = new MutableLiveData<>();
|
||||
initialLoadStateLiveData = new MutableLiveData<>();
|
||||
hasSubredditLiveData = new MutableLiveData<>();
|
||||
}
|
||||
|
||||
MutableLiveData getPaginationNetworkStateLiveData() {
|
||||
MutableLiveData<NetworkState> getPaginationNetworkStateLiveData() {
|
||||
return paginationNetworkStateLiveData;
|
||||
}
|
||||
|
||||
MutableLiveData getInitialLoadStateLiveData() {
|
||||
MutableLiveData<NetworkState> getInitialLoadStateLiveData() {
|
||||
return initialLoadStateLiveData;
|
||||
}
|
||||
|
||||
MutableLiveData<Boolean> hasSubredditLiveData() {
|
||||
return hasSubredditLiveData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<String, SubredditData> callback) {
|
||||
initialParams = params;
|
||||
@ -56,9 +56,9 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
|
||||
@Override
|
||||
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
|
||||
if(subredditData.size() == 0) {
|
||||
onSubredditListingDataFetchedCallback.noSubreddit();
|
||||
hasSubredditLiveData.postValue(false);
|
||||
} else {
|
||||
onSubredditListingDataFetchedCallback.hasSubreddit();
|
||||
hasSubredditLiveData.postValue(true);
|
||||
}
|
||||
|
||||
callback.onResult(subredditData, null, after);
|
||||
@ -82,7 +82,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
|
||||
this.params = params;
|
||||
this.callback = callback;
|
||||
|
||||
if(params.key.equals("null")) {
|
||||
if(params.key.equals("") || params.key.equals("null")) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -9,25 +9,21 @@ public class SubredditListingDataSourceFactory extends DataSource.Factory {
|
||||
private Retrofit retrofit;
|
||||
private String query;
|
||||
private String sortType;
|
||||
private SubredditListingDataSource.OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback;
|
||||
|
||||
private SubredditListingDataSource subredditListingDataSource;
|
||||
private MutableLiveData<SubredditListingDataSource> subredditListingDataSourceMutableLiveData;
|
||||
|
||||
SubredditListingDataSourceFactory(Retrofit retrofit, String query, String sortType,
|
||||
SubredditListingDataSource.OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback) {
|
||||
SubredditListingDataSourceFactory(Retrofit retrofit, String query, String sortType) {
|
||||
this.retrofit = retrofit;
|
||||
this.query = query;
|
||||
this.sortType = sortType;
|
||||
this.onSubredditListingDataFetchedCallback = onSubredditListingDataFetchedCallback;
|
||||
subredditListingDataSourceMutableLiveData = new MutableLiveData<>();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public DataSource create() {
|
||||
subredditListingDataSource = new SubredditListingDataSource(retrofit,
|
||||
query, sortType, onSubredditListingDataFetchedCallback);
|
||||
subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType);
|
||||
subredditListingDataSourceMutableLiveData.postValue(subredditListingDataSource);
|
||||
return subredditListingDataSource;
|
||||
}
|
||||
|
@ -86,24 +86,8 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
|
||||
String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN);
|
||||
String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME);
|
||||
|
||||
SubredditListingViewModel.Factory factory = new SubredditListingViewModel.Factory(mRetrofit, query,
|
||||
PostDataSource.SORT_TYPE_RELEVANCE, new SubredditListingDataSource.OnSubredditListingDataFetchedCallback() {
|
||||
@Override
|
||||
public void hasSubreddit() {
|
||||
mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void noSubreddit() {
|
||||
mFetchSubredditListingInfoLinearLayout.setOnClickListener(view -> {
|
||||
//Do nothing
|
||||
});
|
||||
showErrorView(R.string.no_subreddits);
|
||||
}
|
||||
});
|
||||
|
||||
mAdapter = new SubredditListingRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit,
|
||||
accessToken, accountName, redditDataRoomDatabase.subscribedSubredditDao(),
|
||||
accessToken, accountName, redditDataRoomDatabase,
|
||||
new SubredditListingRecyclerViewAdapter.Callback() {
|
||||
@Override
|
||||
public void retryLoadingMore() {
|
||||
@ -124,6 +108,8 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
|
||||
|
||||
mSubredditListingRecyclerView.setAdapter(mAdapter);
|
||||
|
||||
SubredditListingViewModel.Factory factory = new SubredditListingViewModel.Factory(mRetrofit, query,
|
||||
PostDataSource.SORT_TYPE_RELEVANCE);
|
||||
mSubredditListingViewModel = ViewModelProviders.of(this, factory).get(SubredditListingViewModel.class);
|
||||
mSubredditListingViewModel.getSubreddits().observe(this, subredditData -> mAdapter.submitList(subredditData));
|
||||
|
||||
@ -139,6 +125,17 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
|
||||
}
|
||||
});
|
||||
|
||||
mSubredditListingViewModel.hasSubredditLiveData().observe(this, hasSubreddit -> {
|
||||
if(hasSubreddit) {
|
||||
mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE);
|
||||
} else {
|
||||
mFetchSubredditListingInfoLinearLayout.setOnClickListener(view -> {
|
||||
//Do nothing
|
||||
});
|
||||
showErrorView(R.string.no_subreddits);
|
||||
}
|
||||
});
|
||||
|
||||
mSubredditListingViewModel.getPaginationNetworkState().observe(this, networkState -> {
|
||||
mAdapter.setNetworkState(networkState);
|
||||
});
|
||||
|
@ -21,7 +21,6 @@ import com.bumptech.glide.RequestManager;
|
||||
import com.bumptech.glide.request.RequestOptions;
|
||||
|
||||
import SubredditDatabase.SubredditData;
|
||||
import SubscribedSubredditDatabase.SubscribedSubredditDao;
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
|
||||
@ -45,14 +44,14 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
|
||||
private Retrofit retrofit;
|
||||
private String accessToken;
|
||||
private String accountName;
|
||||
private SubscribedSubredditDao subscribedSubredditDao;
|
||||
private RedditDataRoomDatabase redditDataRoomDatabase;
|
||||
|
||||
private NetworkState networkState;
|
||||
private Callback callback;
|
||||
|
||||
SubredditListingRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, Retrofit retrofit,
|
||||
String accessToken, String accountName,
|
||||
SubscribedSubredditDao subscribedSubredditDao,
|
||||
RedditDataRoomDatabase redditDataRoomDatabase,
|
||||
Callback callback) {
|
||||
super(DIFF_CALLBACK);
|
||||
this.context = context;
|
||||
@ -60,7 +59,7 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
|
||||
this.retrofit = retrofit;
|
||||
this.accessToken = accessToken;
|
||||
this.accountName = accountName;
|
||||
this.subscribedSubredditDao = subscribedSubredditDao;
|
||||
this.redditDataRoomDatabase = redditDataRoomDatabase;
|
||||
this.callback = callback;
|
||||
glide = Glide.with(context.getApplicationContext());
|
||||
}
|
||||
@ -113,7 +112,7 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
|
||||
|
||||
((DataViewHolder) holder).subredditNameTextView.setText(subredditData.getName());
|
||||
|
||||
new CheckIsSubscribedToSubredditAsyncTask(subscribedSubredditDao, subredditData.getName(), accountName,
|
||||
new CheckIsSubscribedToSubredditAsyncTask(redditDataRoomDatabase, subredditData.getName(), accountName,
|
||||
new CheckIsSubscribedToSubredditAsyncTask.CheckIsSubscribedToSubredditListener() {
|
||||
@Override
|
||||
public void isSubscribed() {
|
||||
@ -125,7 +124,7 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
|
||||
((DataViewHolder) holder).subscribeButton.setVisibility(View.VISIBLE);
|
||||
((DataViewHolder) holder).subscribeButton.setOnClickListener(view -> {
|
||||
SubredditSubscription.subscribeToSubreddit(oauthRetrofit, retrofit,
|
||||
accessToken, accountName, subredditData.getName(), subscribedSubredditDao,
|
||||
accessToken, accountName, subredditData.getName(), redditDataRoomDatabase,
|
||||
new SubredditSubscription.SubredditSubscriptionListener() {
|
||||
@Override
|
||||
public void onSubredditSubscriptionSuccess() {
|
||||
|
@ -1,7 +1,6 @@
|
||||
package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.arch.core.util.Function;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.Transformations;
|
||||
@ -17,17 +16,19 @@ public class SubredditListingViewModel extends ViewModel {
|
||||
private SubredditListingDataSourceFactory subredditListingDataSourceFactory;
|
||||
private LiveData<NetworkState> paginationNetworkState;
|
||||
private LiveData<NetworkState> initialLoadingState;
|
||||
private LiveData<Boolean> hasSubredditLiveData;
|
||||
private LiveData<PagedList<SubredditData>> subreddits;
|
||||
private MutableLiveData<String> sortTypeLiveData;
|
||||
|
||||
SubredditListingViewModel(Retrofit retrofit, String query, String sortType,
|
||||
SubredditListingDataSource.OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback) {
|
||||
subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType, onSubredditListingDataFetchedCallback);
|
||||
SubredditListingViewModel(Retrofit retrofit, String query, String sortType) {
|
||||
subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType);
|
||||
|
||||
initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
|
||||
(Function<SubredditListingDataSource, LiveData<NetworkState>>) SubredditListingDataSource::getInitialLoadStateLiveData);
|
||||
SubredditListingDataSource::getInitialLoadStateLiveData);
|
||||
paginationNetworkState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
|
||||
(Function<SubredditListingDataSource, LiveData<NetworkState>>) SubredditListingDataSource::getPaginationNetworkStateLiveData);
|
||||
SubredditListingDataSource::getPaginationNetworkStateLiveData);
|
||||
hasSubredditLiveData = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
|
||||
SubredditListingDataSource::hasSubredditLiveData);
|
||||
|
||||
sortTypeLiveData = new MutableLiveData<>();
|
||||
sortTypeLiveData.postValue(sortType);
|
||||
@ -56,6 +57,10 @@ public class SubredditListingViewModel extends ViewModel {
|
||||
return initialLoadingState;
|
||||
}
|
||||
|
||||
LiveData<Boolean> hasSubredditLiveData() {
|
||||
return hasSubredditLiveData;
|
||||
}
|
||||
|
||||
void refresh() {
|
||||
subredditListingDataSourceFactory.getSubredditListingDataSource().invalidate();
|
||||
}
|
||||
@ -76,20 +81,17 @@ public class SubredditListingViewModel extends ViewModel {
|
||||
private Retrofit retrofit;
|
||||
private String query;
|
||||
private String sortType;
|
||||
private SubredditListingDataSource.OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback;
|
||||
|
||||
public Factory(Retrofit retrofit, String query, String sortType,
|
||||
SubredditListingDataSource.OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback) {
|
||||
public Factory(Retrofit retrofit, String query, String sortType) {
|
||||
this.retrofit = retrofit;
|
||||
this.query = query;
|
||||
this.sortType = sortType;
|
||||
this.onSubredditListingDataFetchedCallback = onSubredditListingDataFetchedCallback;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
|
||||
return (T) new SubredditListingViewModel(retrofit, query, sortType, onSubredditListingDataFetchedCallback);
|
||||
return (T) new SubredditListingViewModel(retrofit, query, sortType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,6 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import SubredditDatabase.SubredditData;
|
||||
import SubscribedSubredditDatabase.SubscribedSubredditDao;
|
||||
import SubscribedSubredditDatabase.SubscribedSubredditData;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
@ -23,23 +22,23 @@ class SubredditSubscription {
|
||||
|
||||
static void subscribeToSubreddit(Retrofit oauthRetrofit, Retrofit retrofit,
|
||||
String accessToken, String subredditName, String accountName,
|
||||
SubscribedSubredditDao subscribedSubredditDao,
|
||||
RedditDataRoomDatabase redditDataRoomDatabase,
|
||||
SubredditSubscriptionListener subredditSubscriptionListener) {
|
||||
subredditSubscription(oauthRetrofit, retrofit, accessToken, subredditName, accountName, "sub",
|
||||
subscribedSubredditDao, subredditSubscriptionListener);
|
||||
redditDataRoomDatabase, subredditSubscriptionListener);
|
||||
}
|
||||
|
||||
static void unsubscribeToSubreddit(Retrofit oauthRetrofit, String accessToken,
|
||||
String subredditName, String accountName,
|
||||
SubscribedSubredditDao subscribedSubredditDao,
|
||||
RedditDataRoomDatabase redditDataRoomDatabase,
|
||||
SubredditSubscriptionListener subredditSubscriptionListener) {
|
||||
subredditSubscription(oauthRetrofit, null, accessToken, subredditName, accountName, "unsub",
|
||||
subscribedSubredditDao,subredditSubscriptionListener);
|
||||
redditDataRoomDatabase,subredditSubscriptionListener);
|
||||
}
|
||||
|
||||
private static void subredditSubscription(Retrofit oauthRetrofit, Retrofit retrofit, String accessToken,
|
||||
String subredditName, String accountName, String action,
|
||||
SubscribedSubredditDao subscribedSubredditDao,
|
||||
RedditDataRoomDatabase redditDataRoomDatabase,
|
||||
SubredditSubscriptionListener subredditSubscriptionListener) {
|
||||
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
|
||||
|
||||
@ -56,7 +55,7 @@ class SubredditSubscription {
|
||||
FetchSubredditData.fetchSubredditData(retrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() {
|
||||
@Override
|
||||
public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) {
|
||||
new UpdateSubscriptionAsyncTask(subscribedSubredditDao,
|
||||
new UpdateSubscriptionAsyncTask(redditDataRoomDatabase,
|
||||
subredditData, accountName, true).execute();
|
||||
}
|
||||
|
||||
@ -66,7 +65,7 @@ class SubredditSubscription {
|
||||
}
|
||||
});
|
||||
} else {
|
||||
new UpdateSubscriptionAsyncTask(subscribedSubredditDao, subredditName, accountName, false).execute();
|
||||
new UpdateSubscriptionAsyncTask(redditDataRoomDatabase, subredditName, accountName, false).execute();
|
||||
}
|
||||
subredditSubscriptionListener.onSubredditSubscriptionSuccess();
|
||||
} else {
|
||||
@ -85,23 +84,23 @@ class SubredditSubscription {
|
||||
|
||||
private static class UpdateSubscriptionAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||
|
||||
private SubscribedSubredditDao subscribedSubredditDao;
|
||||
private RedditDataRoomDatabase redditDataRoomDatabase;
|
||||
private String subredditName;
|
||||
private String accountName;
|
||||
private SubscribedSubredditData subscribedSubredditData;
|
||||
private boolean isSubscribing;
|
||||
|
||||
UpdateSubscriptionAsyncTask(SubscribedSubredditDao subscribedSubredditDao, String subredditName,
|
||||
UpdateSubscriptionAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String subredditName,
|
||||
String accountName, boolean isSubscribing) {
|
||||
this.subscribedSubredditDao = subscribedSubredditDao;
|
||||
this.redditDataRoomDatabase = redditDataRoomDatabase;
|
||||
this.subredditName = subredditName;
|
||||
this.accountName = accountName;
|
||||
this.isSubscribing = isSubscribing;
|
||||
}
|
||||
|
||||
UpdateSubscriptionAsyncTask(SubscribedSubredditDao subscribedSubredditDao, SubredditData subredditData,
|
||||
UpdateSubscriptionAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, SubredditData subredditData,
|
||||
String accountName, boolean isSubscribing) {
|
||||
this.subscribedSubredditDao = subscribedSubredditDao;
|
||||
this.redditDataRoomDatabase = redditDataRoomDatabase;
|
||||
this.subscribedSubredditData = new SubscribedSubredditData(subredditData.getId(), subredditData.getName(),
|
||||
subredditData.getIconUrl(), accountName);
|
||||
this.accountName = accountName;
|
||||
@ -111,9 +110,9 @@ class SubredditSubscription {
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
if(isSubscribing) {
|
||||
subscribedSubredditDao.insert(subscribedSubredditData);
|
||||
redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData);
|
||||
} else {
|
||||
subscribedSubredditDao.deleteSubscribedSubreddit(subredditName, accountName);
|
||||
redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -10,42 +10,41 @@ import User.UserData;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class UserListingDataSource extends PageKeyedDataSource<String, UserData> {
|
||||
interface OnUserListingDataFetchedCallback {
|
||||
void hasUser();
|
||||
void noUser();
|
||||
}
|
||||
|
||||
private Retrofit retrofit;
|
||||
private String query;
|
||||
private String sortType;
|
||||
private UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback;
|
||||
|
||||
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
|
||||
private MutableLiveData<NetworkState> initialLoadStateLiveData;
|
||||
private MutableLiveData<Boolean> hasUserLiveData;
|
||||
|
||||
private PageKeyedDataSource.LoadInitialParams<String> initialParams;
|
||||
private PageKeyedDataSource.LoadInitialCallback<String, UserData> initialCallback;
|
||||
private PageKeyedDataSource.LoadParams<String> params;
|
||||
private PageKeyedDataSource.LoadCallback<String, UserData> callback;
|
||||
|
||||
UserListingDataSource(Retrofit retrofit, String query, String sortType,
|
||||
UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback) {
|
||||
UserListingDataSource(Retrofit retrofit, String query, String sortType) {
|
||||
this.retrofit = retrofit;
|
||||
this.query = query;
|
||||
this.sortType = sortType;
|
||||
this.onUserListingDataFetchedCallback = onUserListingDataFetchedCallback;
|
||||
paginationNetworkStateLiveData = new MutableLiveData();
|
||||
initialLoadStateLiveData = new MutableLiveData();
|
||||
paginationNetworkStateLiveData = new MutableLiveData<>();
|
||||
initialLoadStateLiveData = new MutableLiveData<>();
|
||||
hasUserLiveData = new MutableLiveData<>();
|
||||
}
|
||||
|
||||
MutableLiveData getPaginationNetworkStateLiveData() {
|
||||
MutableLiveData<NetworkState> getPaginationNetworkStateLiveData() {
|
||||
return paginationNetworkStateLiveData;
|
||||
}
|
||||
|
||||
MutableLiveData getInitialLoadStateLiveData() {
|
||||
MutableLiveData<NetworkState> getInitialLoadStateLiveData() {
|
||||
return initialLoadStateLiveData;
|
||||
}
|
||||
|
||||
MutableLiveData<Boolean> hasUserLiveData() {
|
||||
return hasUserLiveData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadInitial(@NonNull PageKeyedDataSource.LoadInitialParams<String> params, @NonNull PageKeyedDataSource.LoadInitialCallback<String, UserData> callback) {
|
||||
initialParams = params;
|
||||
@ -57,9 +56,9 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
|
||||
@Override
|
||||
public void onFetchUserListingDataSuccess(ArrayList<UserData> UserData, String after) {
|
||||
if(UserData.size() == 0) {
|
||||
onUserListingDataFetchedCallback.noUser();
|
||||
hasUserLiveData.postValue(false);
|
||||
} else {
|
||||
onUserListingDataFetchedCallback.hasUser();
|
||||
hasUserLiveData.postValue(true);
|
||||
}
|
||||
|
||||
callback.onResult(UserData, null, after);
|
||||
|
@ -9,25 +9,21 @@ public class UserListingDataSourceFactory extends DataSource.Factory {
|
||||
private Retrofit retrofit;
|
||||
private String query;
|
||||
private String sortType;
|
||||
private UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback;
|
||||
|
||||
private UserListingDataSource userListingDataSource;
|
||||
private MutableLiveData<UserListingDataSource> userListingDataSourceMutableLiveData;
|
||||
|
||||
UserListingDataSourceFactory(Retrofit retrofit, String query, String sortType,
|
||||
UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback) {
|
||||
UserListingDataSourceFactory(Retrofit retrofit, String query, String sortType) {
|
||||
this.retrofit = retrofit;
|
||||
this.query = query;
|
||||
this.sortType = sortType;
|
||||
this.onUserListingDataFetchedCallback = onUserListingDataFetchedCallback;
|
||||
userListingDataSourceMutableLiveData = new MutableLiveData<>();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public DataSource create() {
|
||||
userListingDataSource = new UserListingDataSource(retrofit,
|
||||
query, sortType, onUserListingDataFetchedCallback);
|
||||
userListingDataSource = new UserListingDataSource(retrofit, query, sortType);
|
||||
userListingDataSourceMutableLiveData.postValue(userListingDataSource);
|
||||
return userListingDataSource;
|
||||
}
|
||||
|
@ -81,28 +81,14 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
|
||||
String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN);
|
||||
String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME);
|
||||
|
||||
UserListingViewModel.Factory factory = new UserListingViewModel.Factory(mRetrofit, mQuery,
|
||||
PostDataSource.SORT_TYPE_RELEVANCE, new UserListingDataSource.OnUserListingDataFetchedCallback() {
|
||||
@Override
|
||||
public void hasUser() {
|
||||
mFetchUserListingInfoLinearLayout.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void noUser() {
|
||||
mFetchUserListingInfoLinearLayout.setOnClickListener(view -> {
|
||||
//Do nothing
|
||||
});
|
||||
showErrorView(R.string.no_users);
|
||||
}
|
||||
});
|
||||
|
||||
mAdapter = new UserListingRecyclerViewAdapter(getActivity(), mOauthRetrofit, mRetrofit,
|
||||
accessToken, accountName, redditDataRoomDatabase.subscribedUserDao(),
|
||||
() -> mUserListingViewModel.retryLoadingMore());
|
||||
|
||||
mUserListingRecyclerView.setAdapter(mAdapter);
|
||||
|
||||
UserListingViewModel.Factory factory = new UserListingViewModel.Factory(mRetrofit, mQuery,
|
||||
PostDataSource.SORT_TYPE_RELEVANCE);
|
||||
mUserListingViewModel = ViewModelProviders.of(this, factory).get(UserListingViewModel.class);
|
||||
mUserListingViewModel.getUsers().observe(this, UserData -> mAdapter.submitList(UserData));
|
||||
|
||||
@ -118,6 +104,17 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
|
||||
}
|
||||
});
|
||||
|
||||
mUserListingViewModel.hasUser().observe(this, hasUser -> {
|
||||
if(hasUser) {
|
||||
mFetchUserListingInfoLinearLayout.setVisibility(View.GONE);
|
||||
} else {
|
||||
mFetchUserListingInfoLinearLayout.setOnClickListener(view -> {
|
||||
//Do nothing
|
||||
});
|
||||
showErrorView(R.string.no_users);
|
||||
}
|
||||
});
|
||||
|
||||
mUserListingViewModel.getPaginationNetworkState().observe(this, networkState -> {
|
||||
mAdapter.setNetworkState(networkState);
|
||||
});
|
||||
|
@ -1,7 +1,6 @@
|
||||
package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.arch.core.util.Function;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.Transformations;
|
||||
@ -14,20 +13,22 @@ import User.UserData;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class UserListingViewModel extends ViewModel {
|
||||
private UserListingDataSourceFactory UserListingDataSourceFactory;
|
||||
private UserListingDataSourceFactory userListingDataSourceFactory;
|
||||
private LiveData<NetworkState> paginationNetworkState;
|
||||
private LiveData<NetworkState> initialLoadingState;
|
||||
private LiveData<Boolean> hasUserLiveData;
|
||||
private LiveData<PagedList<UserData>> users;
|
||||
private MutableLiveData<String> sortTypeLiveData;
|
||||
|
||||
UserListingViewModel(Retrofit retrofit, String query, String sortType,
|
||||
UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback) {
|
||||
UserListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType, onUserListingDataFetchedCallback);
|
||||
UserListingViewModel(Retrofit retrofit, String query, String sortType) {
|
||||
userListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType);
|
||||
|
||||
initialLoadingState = Transformations.switchMap(UserListingDataSourceFactory.getUserListingDataSourceMutableLiveData(),
|
||||
(Function<UserListingDataSource, LiveData<NetworkState>>) UserListingDataSource::getInitialLoadStateLiveData);
|
||||
paginationNetworkState = Transformations.switchMap(UserListingDataSourceFactory.getUserListingDataSourceMutableLiveData(),
|
||||
(Function<UserListingDataSource, LiveData<NetworkState>>) UserListingDataSource::getPaginationNetworkStateLiveData);
|
||||
initialLoadingState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(),
|
||||
UserListingDataSource::getInitialLoadStateLiveData);
|
||||
paginationNetworkState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(),
|
||||
UserListingDataSource::getPaginationNetworkStateLiveData);
|
||||
hasUserLiveData = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(),
|
||||
UserListingDataSource::hasUserLiveData);
|
||||
|
||||
sortTypeLiveData = new MutableLiveData<>();
|
||||
sortTypeLiveData.postValue(sortType);
|
||||
@ -39,8 +40,8 @@ public class UserListingViewModel extends ViewModel {
|
||||
.build();
|
||||
|
||||
users = Transformations.switchMap(sortTypeLiveData, sort -> {
|
||||
UserListingDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
|
||||
return (new LivePagedListBuilder(UserListingDataSourceFactory, pagedListConfig)).build();
|
||||
userListingDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
|
||||
return (new LivePagedListBuilder(userListingDataSourceFactory, pagedListConfig)).build();
|
||||
});
|
||||
}
|
||||
|
||||
@ -56,16 +57,20 @@ public class UserListingViewModel extends ViewModel {
|
||||
return initialLoadingState;
|
||||
}
|
||||
|
||||
LiveData<Boolean> hasUser() {
|
||||
return hasUserLiveData;
|
||||
}
|
||||
|
||||
void refresh() {
|
||||
UserListingDataSourceFactory.getUserListingDataSource().invalidate();
|
||||
userListingDataSourceFactory.getUserListingDataSource().invalidate();
|
||||
}
|
||||
|
||||
void retry() {
|
||||
UserListingDataSourceFactory.getUserListingDataSource().retry();
|
||||
userListingDataSourceFactory.getUserListingDataSource().retry();
|
||||
}
|
||||
|
||||
void retryLoadingMore() {
|
||||
UserListingDataSourceFactory.getUserListingDataSource().retryLoadingMore();
|
||||
userListingDataSourceFactory.getUserListingDataSource().retryLoadingMore();
|
||||
}
|
||||
|
||||
void changeSortType(String sortType) {
|
||||
@ -76,20 +81,17 @@ public class UserListingViewModel extends ViewModel {
|
||||
private Retrofit retrofit;
|
||||
private String query;
|
||||
private String sortType;
|
||||
private UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback;
|
||||
|
||||
public Factory(Retrofit retrofit, String query, String sortType,
|
||||
UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback) {
|
||||
public Factory(Retrofit retrofit, String query, String sortType) {
|
||||
this.retrofit = retrofit;
|
||||
this.query = query;
|
||||
this.sortType = sortType;
|
||||
this.onUserListingDataFetchedCallback = onUserListingDataFetchedCallback;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
|
||||
return (T) new UserListingViewModel(retrofit, query, sortType, onUserListingDataFetchedCallback);
|
||||
return (T) new UserListingViewModel(retrofit, query, sortType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,6 @@ import javax.inject.Named;
|
||||
import SubredditDatabase.SubredditDao;
|
||||
import SubredditDatabase.SubredditData;
|
||||
import SubredditDatabase.SubredditViewModel;
|
||||
import SubscribedSubredditDatabase.SubscribedSubredditDao;
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
|
||||
@ -76,7 +75,6 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
|
||||
private PostTypeBottomSheetFragment postTypeBottomSheetFragment;
|
||||
private SortTypeBottomSheetFragment sortTypeBottomSheetFragment;
|
||||
|
||||
private SubscribedSubredditDao subscribedSubredditDao;
|
||||
private SubredditViewModel mSubredditViewModel;
|
||||
|
||||
@Inject
|
||||
@ -142,7 +140,6 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
|
||||
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams();
|
||||
params.topMargin = statusBarHeight;
|
||||
|
||||
subscribedSubredditDao = mRedditDataRoomDatabase.subscribedSubredditDao();
|
||||
glide = Glide.with(this);
|
||||
|
||||
mSubredditViewModel = ViewModelProviders.of(this,
|
||||
@ -239,7 +236,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
|
||||
subscriptionReady = false;
|
||||
if(subscribeSubredditChip.getText().equals(getResources().getString(R.string.subscribe))) {
|
||||
SubredditSubscription.subscribeToSubreddit(mOauthRetrofit, mRetrofit, mAccessToken,
|
||||
subredditName, mAccountName, subscribedSubredditDao,
|
||||
subredditName, mAccountName, mRedditDataRoomDatabase,
|
||||
new SubredditSubscription.SubredditSubscriptionListener() {
|
||||
@Override
|
||||
public void onSubredditSubscriptionSuccess() {
|
||||
@ -257,7 +254,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
|
||||
});
|
||||
} else {
|
||||
SubredditSubscription.unsubscribeToSubreddit(mOauthRetrofit, mAccessToken,
|
||||
subredditName, mAccountName, subscribedSubredditDao,
|
||||
subredditName, mAccountName, mRedditDataRoomDatabase,
|
||||
new SubredditSubscription.SubredditSubscriptionListener() {
|
||||
@Override
|
||||
public void onSubredditSubscriptionSuccess() {
|
||||
@ -277,7 +274,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
|
||||
}
|
||||
});
|
||||
|
||||
new CheckIsSubscribedToSubredditAsyncTask(subscribedSubredditDao, subredditName, mAccountName,
|
||||
new CheckIsSubscribedToSubredditAsyncTask(mRedditDataRoomDatabase, subredditName, mAccountName,
|
||||
new CheckIsSubscribedToSubredditAsyncTask.CheckIsSubscribedToSubredditListener() {
|
||||
@Override
|
||||
public void isSubscribed() {
|
||||
|
@ -489,7 +489,6 @@ public class ViewUserDetailActivity extends AppCompatActivity {
|
||||
break;
|
||||
case 1:
|
||||
commentsListingFragment = (CommentsListingFragment) fragment;
|
||||
break;
|
||||
}
|
||||
return fragment;
|
||||
}
|
||||
|
@ -27,7 +27,8 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:gravity="center" />
|
||||
android:gravity="center"
|
||||
android:text="@string/no_users" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
@ -28,7 +28,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:gravity="center"
|
||||
android:text="@string/no_subreddits"/>
|
||||
android:text="@string/no_subreddits" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
@ -35,9 +35,10 @@
|
||||
<string name="load_posts_error">Error loading posts.\nTap to retry.</string>
|
||||
<string name="search_subreddits_error">Error searching subreddits.\nTap to retry.</string>
|
||||
<string name="search_users_error">Error searching users.\nTap to retry.</string>
|
||||
<string name="no_posts">No posts found.</string>
|
||||
<string name="no_subreddits">No subreddits found.</string>
|
||||
<string name="no_users">No users found.</string>
|
||||
<string name="no_posts">No posts found</string>
|
||||
<string name="no_comments">No comments found</string>
|
||||
<string name="no_subreddits">No subreddits found</string>
|
||||
<string name="no_users">No users found</string>
|
||||
<string name="no_storage_permission">No storage permission to save this file</string>
|
||||
|
||||
<string name="load_comments_failed">Error loading comments.</string>
|
||||
|
Loading…
Reference in New Issue
Block a user