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:
Alex Ning 2019-08-09 10:38:25 +08:00
parent 85597a82d0
commit d408a47dba
25 changed files with 255 additions and 321 deletions

View File

@ -2,12 +2,11 @@ package ml.docilealligator.infinityforreddit;
import android.os.AsyncTask; import android.os.AsyncTask;
import SubscribedSubredditDatabase.SubscribedSubredditDao;
import SubscribedSubredditDatabase.SubscribedSubredditData; import SubscribedSubredditDatabase.SubscribedSubredditData;
class CheckIsSubscribedToSubredditAsyncTask extends AsyncTask<Void, Void, Void> { class CheckIsSubscribedToSubredditAsyncTask extends AsyncTask<Void, Void, Void> {
private SubscribedSubredditDao subscribedSubredditDao; private RedditDataRoomDatabase redditDataRoomDatabase;
private String subredditName; private String subredditName;
private String accountName; private String accountName;
private SubscribedSubredditData subscribedSubredditData; private SubscribedSubredditData subscribedSubredditData;
@ -18,10 +17,10 @@ class CheckIsSubscribedToSubredditAsyncTask extends AsyncTask<Void, Void, Void>
void isNotSubscribed(); void isNotSubscribed();
} }
CheckIsSubscribedToSubredditAsyncTask(SubscribedSubredditDao subscribedSubredditDao, CheckIsSubscribedToSubredditAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase,
String subredditName, String accountName, String subredditName, String accountName,
CheckIsSubscribedToSubredditListener checkIsSubscribedToSubredditListener) { CheckIsSubscribedToSubredditListener checkIsSubscribedToSubredditListener) {
this.subscribedSubredditDao = subscribedSubredditDao; this.redditDataRoomDatabase = redditDataRoomDatabase;
this.subredditName =subredditName; this.subredditName =subredditName;
this.accountName = accountName; this.accountName = accountName;
this.checkIsSubscribedToSubredditListener = checkIsSubscribedToSubredditListener; this.checkIsSubscribedToSubredditListener = checkIsSubscribedToSubredditListener;
@ -29,7 +28,7 @@ class CheckIsSubscribedToSubredditAsyncTask extends AsyncTask<Void, Void, Void>
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
subscribedSubredditData = subscribedSubredditDao.getSubscribedSubreddit(subredditName, accountName); subscribedSubredditData = redditDataRoomDatabase.subscribedSubredditDao().getSubscribedSubreddit(subredditName, accountName);
return null; return null;
} }

View File

@ -20,41 +20,41 @@ import retrofit2.Response;
import retrofit2.Retrofit; import retrofit2.Retrofit;
public class CommentDataSource extends PageKeyedDataSource<String, CommentData> { public class CommentDataSource extends PageKeyedDataSource<String, CommentData> {
interface OnCommentFetchedCallback {
void hasComment();
void noComment();
}
private Retrofit retrofit; private Retrofit retrofit;
private Locale locale; private Locale locale;
private String username; private String username;
private OnCommentFetchedCallback onCommentFetchedCallback;
private MutableLiveData<NetworkState> paginationNetworkStateLiveData; private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
private MutableLiveData<NetworkState> initialLoadStateLiveData; private MutableLiveData<NetworkState> initialLoadStateLiveData;
private MutableLiveData<Boolean> hasPostLiveData;
private LoadInitialParams<String> initialParams; private LoadInitialParams<String> initialParams;
private LoadInitialCallback<String, CommentData> initialCallback; private LoadInitialCallback<String, CommentData> initialCallback;
private LoadParams<String> params; private LoadParams<String> params;
private LoadCallback<String, CommentData> callback; 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.retrofit = retrofit;
this.locale = locale; this.locale = locale;
this.username = username; this.username = username;
paginationNetworkStateLiveData = new MutableLiveData(); paginationNetworkStateLiveData = new MutableLiveData<>();
initialLoadStateLiveData = new MutableLiveData(); initialLoadStateLiveData = new MutableLiveData<>();
this.onCommentFetchedCallback = onCommentFetchedCallback; hasPostLiveData = new MutableLiveData<>();
} }
MutableLiveData getPaginationNetworkStateLiveData() { MutableLiveData<NetworkState> getPaginationNetworkStateLiveData() {
return paginationNetworkStateLiveData; return paginationNetworkStateLiveData;
} }
MutableLiveData getInitialLoadStateLiveData() { MutableLiveData<NetworkState> getInitialLoadStateLiveData() {
return initialLoadStateLiveData; return initialLoadStateLiveData;
} }
MutableLiveData<Boolean> hasPostLiveData() {
return hasPostLiveData;
}
void retry() { void retry() {
loadInitial(initialParams, initialCallback); loadInitial(initialParams, initialCallback);
} }
@ -80,9 +80,9 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData>
@Override @Override
public void parseSuccessful(ArrayList<CommentData> comments, String after) { public void parseSuccessful(ArrayList<CommentData> comments, String after) {
if(comments.size() == 0) { if(comments.size() == 0) {
onCommentFetchedCallback.noComment(); hasPostLiveData.postValue(false);
} else { } else {
onCommentFetchedCallback.hasComment(); hasPostLiveData.postValue(true);
} }
callback.onResult(comments, null, after); callback.onResult(comments, null, after);

View File

@ -12,23 +12,21 @@ class CommentDataSourceFactory extends DataSource.Factory {
private Retrofit retrofit; private Retrofit retrofit;
private Locale locale; private Locale locale;
private String username; private String username;
private CommentDataSource.OnCommentFetchedCallback onCommentFetchedCallback;
private CommentDataSource commentDataSource; private CommentDataSource commentDataSource;
private MutableLiveData<CommentDataSource> commentDataSourceLiveData; 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.retrofit = retrofit;
this.locale = locale; this.locale = locale;
this.username = username; this.username = username;
commentDataSourceLiveData = new MutableLiveData<>(); commentDataSourceLiveData = new MutableLiveData<>();
this.onCommentFetchedCallback = onCommentFetchedCallback;
} }
@NonNull @NonNull
@Override @Override
public DataSource create() { public DataSource create() {
commentDataSource = new CommentDataSource(retrofit, locale, username, onCommentFetchedCallback); commentDataSource = new CommentDataSource(retrofit, locale, username);
commentDataSourceLiveData.postValue(commentDataSource); commentDataSourceLiveData.postValue(commentDataSource);
return commentDataSource; return commentDataSource;
} }

View File

@ -1,7 +1,6 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.Transformations; import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModel;
@ -17,16 +16,18 @@ public class CommentViewModel extends ViewModel {
private CommentDataSourceFactory commentDataSourceFactory; private CommentDataSourceFactory commentDataSourceFactory;
private LiveData<NetworkState> paginationNetworkState; private LiveData<NetworkState> paginationNetworkState;
private LiveData<NetworkState> initialLoadingState; private LiveData<NetworkState> initialLoadingState;
private LiveData<Boolean> hasCommentLiveData;
private LiveData<PagedList<CommentData>> comments; private LiveData<PagedList<CommentData>> comments;
public CommentViewModel(Retrofit retrofit, Locale locale, String username, public CommentViewModel(Retrofit retrofit, Locale locale, String username) {
CommentDataSource.OnCommentFetchedCallback onCommentFetchedCallback) { commentDataSourceFactory = new CommentDataSourceFactory(retrofit, locale, username);
commentDataSourceFactory = new CommentDataSourceFactory(retrofit, locale, username, onCommentFetchedCallback);
initialLoadingState = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(), initialLoadingState = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
(Function<CommentDataSource, LiveData<NetworkState>>) CommentDataSource::getInitialLoadStateLiveData); CommentDataSource::getInitialLoadStateLiveData);
paginationNetworkState = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(), paginationNetworkState = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
(Function<CommentDataSource, LiveData<NetworkState>>) CommentDataSource::getPaginationNetworkStateLiveData); CommentDataSource::getPaginationNetworkStateLiveData);
hasCommentLiveData = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
CommentDataSource::hasPostLiveData);
PagedList.Config pagedListConfig = PagedList.Config pagedListConfig =
(new PagedList.Config.Builder()) (new PagedList.Config.Builder())
.setEnablePlaceholders(false) .setEnablePlaceholders(false)
@ -48,6 +49,10 @@ public class CommentViewModel extends ViewModel {
return initialLoadingState; return initialLoadingState;
} }
LiveData<Boolean> hasComment() {
return hasCommentLiveData;
}
void refresh() { void refresh() {
commentDataSourceFactory.getCommentDataSource().invalidate(); commentDataSourceFactory.getCommentDataSource().invalidate();
} }
@ -64,20 +69,17 @@ public class CommentViewModel extends ViewModel {
private Retrofit retrofit; private Retrofit retrofit;
private Locale locale; private Locale locale;
private String username; private String username;
private CommentDataSource.OnCommentFetchedCallback onCommentFetchedCallback;
public Factory(Retrofit retrofit, Locale locale, String username, public Factory(Retrofit retrofit, Locale locale, String username) {
CommentDataSource.OnCommentFetchedCallback onCommentFetchedCallback) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.locale = locale; this.locale = locale;
this.username = username; this.username = username;
this.onCommentFetchedCallback = onCommentFetchedCallback;
} }
@NonNull @NonNull
@Override @Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { 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);
} }
} }
} }

View File

@ -78,30 +78,14 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
mCommentRecyclerView.setLayoutManager(new LinearLayoutManager(activity)); mCommentRecyclerView.setLayoutManager(new LinearLayoutManager(activity));
CommentViewModel.Factory factory;
mAdapter = new CommentsListingRecyclerViewAdapter(activity, mOauthRetrofit, mAdapter = new CommentsListingRecyclerViewAdapter(activity, mOauthRetrofit,
getArguments().getString(EXTRA_ACCESS_TOKEN), () -> mCommentViewModel.retryLoadingMore()); getArguments().getString(EXTRA_ACCESS_TOKEN), () -> mCommentViewModel.retryLoadingMore());
String username = getArguments().getString(EXTRA_USERNAME_KEY); 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); mCommentRecyclerView.setAdapter(mAdapter);
CommentViewModel.Factory factory = new CommentViewModel.Factory(mRetrofit, getResources().getConfiguration().locale, username);
mCommentViewModel = ViewModelProviders.of(this, factory).get(CommentViewModel.class); mCommentViewModel = ViewModelProviders.of(this, factory).get(CommentViewModel.class);
mCommentViewModel.getComments().observe(this, comments -> mAdapter.submitList(comments)); 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 -> { mCommentViewModel.getPaginationNetworkState().observe(this, networkState -> {
mAdapter.setNetworkState(networkState); mAdapter.setNetworkState(networkState);
}); });

View File

@ -150,6 +150,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
mProfileImageUrl = savedInstanceState.getString(ACCOUNT_PROFILE_IMAGE_URL_STATE); mProfileImageUrl = savedInstanceState.getString(ACCOUNT_PROFILE_IMAGE_URL_STATE);
mBannerImageUrl = savedInstanceState.getString(ACCOUNT_BANNER_IMAGE_URL_STATE); mBannerImageUrl = savedInstanceState.getString(ACCOUNT_BANNER_IMAGE_URL_STATE);
mKarma = savedInstanceState.getInt(ACCOUNT_KARMA_STATE); mKarma = savedInstanceState.getInt(ACCOUNT_KARMA_STATE);
if(!mNullAccessToken && mAccessToken == null) { if(!mNullAccessToken && mAccessToken == null) {
getCurrentAccountAndBindView(); getCurrentAccountAndBindView();
} else { } else {

View File

@ -14,10 +14,6 @@ import retrofit2.Callback;
import retrofit2.Retrofit; import retrofit2.Retrofit;
class PostDataSource extends PageKeyedDataSource<String, Post> { class PostDataSource extends PageKeyedDataSource<String, Post> {
interface OnPostFetchedCallback {
void hasPost();
void noPost();
}
static final int TYPE_FRONT_PAGE = 0; static final int TYPE_FRONT_PAGE = 0;
static final int TYPE_SUBREDDIT = 1; static final int TYPE_SUBREDDIT = 1;
@ -42,10 +38,10 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
private int postType; private int postType;
private String sortType; private String sortType;
private int filter; private int filter;
private OnPostFetchedCallback onPostFetchedCallback;
private MutableLiveData<NetworkState> paginationNetworkStateLiveData; private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
private MutableLiveData<NetworkState> initialLoadStateLiveData; private MutableLiveData<NetworkState> initialLoadStateLiveData;
private MutableLiveData<Boolean> hasPostLiveData;
private LoadInitialParams<String> initialParams; private LoadInitialParams<String> initialParams;
private LoadInitialCallback<String, Post> initialCallback; private LoadInitialCallback<String, Post> initialCallback;
@ -53,68 +49,72 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
private LoadCallback<String, Post> callback; private LoadCallback<String, Post> callback;
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType, PostDataSource(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType,
int filter, OnPostFetchedCallback onPostFetchedCallback) { int filter) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.locale = locale; this.locale = locale;
paginationNetworkStateLiveData = new MutableLiveData(); paginationNetworkStateLiveData = new MutableLiveData();
initialLoadStateLiveData = new MutableLiveData(); initialLoadStateLiveData = new MutableLiveData();
hasPostLiveData = new MutableLiveData<>();
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
this.filter = filter; this.filter = filter;
this.onPostFetchedCallback = onPostFetchedCallback;
} }
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, int postType, 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.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.locale = locale; this.locale = locale;
this.subredditOrUserName = subredditOrUserName; this.subredditOrUserName = subredditOrUserName;
paginationNetworkStateLiveData = new MutableLiveData(); paginationNetworkStateLiveData = new MutableLiveData<>();
initialLoadStateLiveData = new MutableLiveData(); initialLoadStateLiveData = new MutableLiveData<>();
hasPostLiveData = new MutableLiveData<>();
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
this.filter = filter; this.filter = filter;
this.onPostFetchedCallback = onPostFetchedCallback;
} }
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, int postType, PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, int postType,
int filter, OnPostFetchedCallback onPostFetchedCallback) { int filter) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.locale = locale; this.locale = locale;
this.subredditOrUserName = subredditOrUserName; this.subredditOrUserName = subredditOrUserName;
paginationNetworkStateLiveData = new MutableLiveData(); paginationNetworkStateLiveData = new MutableLiveData<>();
initialLoadStateLiveData = new MutableLiveData(); initialLoadStateLiveData = new MutableLiveData<>();
hasPostLiveData = new MutableLiveData<>();
this.postType = postType; this.postType = postType;
this.filter = filter; this.filter = filter;
this.onPostFetchedCallback = onPostFetchedCallback;
} }
PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, String query, 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.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.locale = locale; this.locale = locale;
this.subredditOrUserName = subredditOrUserName; this.subredditOrUserName = subredditOrUserName;
this.query = query; this.query = query;
paginationNetworkStateLiveData = new MutableLiveData(); paginationNetworkStateLiveData = new MutableLiveData<>();
initialLoadStateLiveData = new MutableLiveData(); initialLoadStateLiveData = new MutableLiveData<>();
hasPostLiveData = new MutableLiveData<>();
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
this.filter = filter; this.filter = filter;
this.onPostFetchedCallback = onPostFetchedCallback;
} }
MutableLiveData getPaginationNetworkStateLiveData() { MutableLiveData<NetworkState> getPaginationNetworkStateLiveData() {
return paginationNetworkStateLiveData; return paginationNetworkStateLiveData;
} }
MutableLiveData getInitialLoadStateLiveData() { MutableLiveData<NetworkState> getInitialLoadStateLiveData() {
return initialLoadStateLiveData; return initialLoadStateLiveData;
} }
MutableLiveData<Boolean> hasPostLiveData() {
return hasPostLiveData;
}
@Override @Override
public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull final LoadInitialCallback<String, Post> callback) { public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull final LoadInitialCallback<String, Post> callback) {
initialParams = params; initialParams = params;
@ -184,7 +184,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
public void onParsePostSuccess(Post post) { public void onParsePostSuccess(Post post) {
ArrayList<Post> singlePostList = new ArrayList<>(); ArrayList<Post> singlePostList = new ArrayList<>();
singlePostList.add(post); singlePostList.add(post);
onPostFetchedCallback.hasPost(); hasPostLiveData.postValue(true);
callback.onResult(singlePostList, null, null); callback.onResult(singlePostList, null, null);
initialLoadStateLiveData.postValue(NetworkState.LOADED); initialLoadStateLiveData.postValue(NetworkState.LOADED);
} }
@ -208,12 +208,12 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
} }
if(newPosts.size() != 0) { if(newPosts.size() != 0) {
onPostFetchedCallback.hasPost(); hasPostLiveData.postValue(true);
} else if(nextPageKey != null) { } else if(nextPageKey != null) {
loadBestPostsInitial(callback, nextPageKey); loadBestPostsInitial(callback, nextPageKey);
return; return;
} else { } else {
onPostFetchedCallback.noPost(); hasPostLiveData.postValue(false);
} }
callback.onResult(newPosts, null, nextPageKey); callback.onResult(newPosts, null, nextPageKey);
@ -295,7 +295,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
public void onParsePostSuccess(Post post) { public void onParsePostSuccess(Post post) {
ArrayList<Post> singlePostList = new ArrayList<>(); ArrayList<Post> singlePostList = new ArrayList<>();
singlePostList.add(post); singlePostList.add(post);
onPostFetchedCallback.hasPost(); hasPostLiveData.postValue(true);
callback.onResult(singlePostList, null, null); callback.onResult(singlePostList, null, null);
initialLoadStateLiveData.postValue(NetworkState.LOADED); initialLoadStateLiveData.postValue(NetworkState.LOADED);
} }
@ -319,12 +319,12 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
} }
if(newPosts.size() != 0) { if(newPosts.size() != 0) {
onPostFetchedCallback.hasPost(); hasPostLiveData.postValue(true);
} else if(nextPageKey != null) { } else if(nextPageKey != null) {
loadSubredditPostsInitial(callback, nextPageKey); loadSubredditPostsInitial(callback, nextPageKey);
return; return;
} else { } else {
onPostFetchedCallback.noPost(); hasPostLiveData.postValue(false);
} }
callback.onResult(newPosts, null, nextPageKey); callback.onResult(newPosts, null, nextPageKey);
@ -411,12 +411,12 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
} }
if(newPosts.size() != 0) { if(newPosts.size() != 0) {
onPostFetchedCallback.hasPost(); hasPostLiveData.postValue(true);
} else if(nextPageKey != null) { } else if(nextPageKey != null) {
loadUserPostsInitial(callback, nextPageKey); loadUserPostsInitial(callback, nextPageKey);
return; return;
} else { } else {
onPostFetchedCallback.noPost(); hasPostLiveData.postValue(false);
} }
callback.onResult(newPosts, null, nextPageKey); callback.onResult(newPosts, null, nextPageKey);
@ -509,12 +509,12 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
} }
if(newPosts.size() != 0) { if(newPosts.size() != 0) {
onPostFetchedCallback.hasPost(); hasPostLiveData.postValue(true);
} else if(nextPageKey != null) { } else if(nextPageKey != null) {
loadSearchPostsInitial(callback, nextPageKey); loadSearchPostsInitial(callback, nextPageKey);
return; return;
} else { } else {
onPostFetchedCallback.noPost(); hasPostLiveData.postValue(false);
} }
callback.onResult(newPosts, null, nextPageKey); callback.onResult(newPosts, null, nextPageKey);

View File

@ -16,13 +16,12 @@ class PostDataSourceFactory extends DataSource.Factory {
private int postType; private int postType;
private String sortType; private String sortType;
private int filter; private int filter;
private PostDataSource.OnPostFetchedCallback onPostFetchedCallback;
private PostDataSource postDataSource; private PostDataSource postDataSource;
private MutableLiveData<PostDataSource> postDataSourceLiveData; private MutableLiveData<PostDataSource> postDataSourceLiveData;
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType, PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType,
int filter, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { int filter) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.locale = locale; this.locale = locale;
@ -30,12 +29,10 @@ class PostDataSourceFactory extends DataSource.Factory {
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
this.filter = filter; this.filter = filter;
this.onPostFetchedCallback = onPostFetchedCallback;
} }
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName,
int postType, String sortType, int filter, int postType, String sortType, int filter) {
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.locale = locale; this.locale = locale;
@ -44,12 +41,10 @@ class PostDataSourceFactory extends DataSource.Factory {
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
this.filter = filter; this.filter = filter;
this.onPostFetchedCallback = onPostFetchedCallback;
} }
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName,
int postType, int filter, int postType, int filter) {
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.locale = locale; this.locale = locale;
@ -57,12 +52,10 @@ class PostDataSourceFactory extends DataSource.Factory {
postDataSourceLiveData = new MutableLiveData<>(); postDataSourceLiveData = new MutableLiveData<>();
this.postType = postType; this.postType = postType;
this.filter = filter; this.filter = filter;
this.onPostFetchedCallback = onPostFetchedCallback;
} }
PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName,
String query, int postType, String sortType, int filter, String query, int postType, String sortType, int filter) {
PostDataSource.OnPostFetchedCallback onPostFetchedCallback) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.locale = locale; this.locale = locale;
@ -72,23 +65,22 @@ class PostDataSourceFactory extends DataSource.Factory {
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
this.filter = filter; this.filter = filter;
this.onPostFetchedCallback = onPostFetchedCallback;
} }
@Override @Override
public DataSource create() { public DataSource create() {
if(postType == PostDataSource.TYPE_FRONT_PAGE) { if(postType == PostDataSource.TYPE_FRONT_PAGE) {
postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, sortType, postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, sortType,
filter, onPostFetchedCallback); filter);
} else if(postType == PostDataSource.TYPE_SEARCH) { } else if(postType == PostDataSource.TYPE_SEARCH) {
postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, query, postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, query,
postType, sortType, filter, onPostFetchedCallback); postType, sortType, filter);
} else if(postType == PostDataSource.TYPE_SUBREDDIT) { } else if(postType == PostDataSource.TYPE_SUBREDDIT) {
postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType,
sortType, filter, onPostFetchedCallback); sortType, filter);
} else { } else {
postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType,
filter, onPostFetchedCallback); filter);
} }
postDataSourceLiveData.postValue(postDataSource); postDataSourceLiveData.postValue(postDataSource);

View File

@ -201,20 +201,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
}); });
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
getResources().getConfiguration().locale, subredditName, query, postType, sortType, filter, new PostDataSource.OnPostFetchedCallback() { getResources().getConfiguration().locale, subredditName, query, postType, sortType, filter);
@Override
public void hasPost() {
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
}
@Override
public void noPost() {
mFetchPostInfoLinearLayout.setOnClickListener(view -> {
//Do nothing
});
showErrorView(R.string.no_posts);
}
});
} else if(postType == PostDataSource.TYPE_SUBREDDIT) { } else if(postType == PostDataSource.TYPE_SUBREDDIT) {
String subredditName = getArguments().getString(EXTRA_NAME); String subredditName = getArguments().getString(EXTRA_NAME);
@ -238,20 +225,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
}); });
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
getResources().getConfiguration().locale, subredditName, postType, sortType, filter, new PostDataSource.OnPostFetchedCallback() { getResources().getConfiguration().locale, subredditName, postType, sortType, filter);
@Override
public void hasPost() {
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
}
@Override
public void noPost() {
mFetchPostInfoLinearLayout.setOnClickListener(view -> {
//Do nothing
});
showErrorView(R.string.no_posts);
}
});
} else if(postType == PostDataSource.TYPE_USER) { } else if(postType == PostDataSource.TYPE_USER) {
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mFetchPostInfoLinearLayout.getLayoutParams(); CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mFetchPostInfoLinearLayout.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT; params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
@ -278,21 +252,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
}); });
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
getResources().getConfiguration().locale, username, postType, sortType, filter, 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);
}
});
} else { } else {
mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, redditDataRoomDatabase, mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, redditDataRoomDatabase,
accessToken, postType, true, new PostRecyclerViewAdapter.Callback() { accessToken, postType, true, new PostRecyclerViewAdapter.Callback() {
@ -313,20 +273,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
}); });
factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, factory = new PostViewModel.Factory(mOauthRetrofit, accessToken,
getResources().getConfiguration().locale, postType, sortType, filter, new PostDataSource.OnPostFetchedCallback() { getResources().getConfiguration().locale, postType, sortType, filter);
@Override
public void hasPost() {
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
}
@Override
public void noPost() {
mFetchPostInfoLinearLayout.setOnClickListener(view -> {
//Do nothing
});
showErrorView(R.string.no_posts);
}
});
} }
mPostRecyclerView.setAdapter(mAdapter); 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 -> { mPostViewModel.getPaginationNetworkState().observe(this, networkState -> {
mAdapter.setNetworkState(networkState); mAdapter.setNetworkState(networkState);
}); });

View File

@ -1,7 +1,6 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations; import androidx.lifecycle.Transformations;
@ -18,17 +17,20 @@ public class PostViewModel extends ViewModel {
private PostDataSourceFactory postDataSourceFactory; private PostDataSourceFactory postDataSourceFactory;
private LiveData<NetworkState> paginationNetworkState; private LiveData<NetworkState> paginationNetworkState;
private LiveData<NetworkState> initialLoadingState; private LiveData<NetworkState> initialLoadingState;
private LiveData<Boolean> hasPostLiveData;
private LiveData<PagedList<Post>> posts; private LiveData<PagedList<Post>> posts;
private MutableLiveData<String> sortTypeLiveData; private MutableLiveData<String> sortTypeLiveData;
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType, public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType,
int filter, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { int filter) {
postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType, sortType, filter, onPostFetchedCallback); postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType, sortType, filter);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
(Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getInitialLoadStateLiveData); PostDataSource::getInitialLoadStateLiveData);
paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
(Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getPaginationNetworkStateLiveData); PostDataSource::getPaginationNetworkStateLiveData);
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
PostDataSource::hasPostLiveData);
sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData = new MutableLiveData<>();
sortTypeLiveData.postValue(sortType); sortTypeLiveData.postValue(sortType);
@ -46,14 +48,16 @@ public class PostViewModel extends ViewModel {
} }
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, 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, postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName,
postType, sortType, filter, onPostFetchedCallback); postType, sortType, filter);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
(Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getInitialLoadStateLiveData); PostDataSource::getInitialLoadStateLiveData);
paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
(Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getPaginationNetworkStateLiveData); PostDataSource::getPaginationNetworkStateLiveData);
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
PostDataSource::hasPostLiveData);
sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData = new MutableLiveData<>();
sortTypeLiveData.postValue(sortType); sortTypeLiveData.postValue(sortType);
@ -71,14 +75,16 @@ public class PostViewModel extends ViewModel {
} }
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, 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, postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName,
postType, filter, onPostFetchedCallback); postType, filter);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
dataSource -> dataSource.getInitialLoadStateLiveData()); PostDataSource::getInitialLoadStateLiveData);
paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
(Function<PostDataSource, LiveData<NetworkState>>) PostDataSource::getPaginationNetworkStateLiveData); PostDataSource::getPaginationNetworkStateLiveData);
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
PostDataSource::hasPostLiveData);
PagedList.Config pagedListConfig = PagedList.Config pagedListConfig =
(new PagedList.Config.Builder()) (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, 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, postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName,
query, postType, sortType, filter, onPostFetchedCallback); query, postType, sortType, filter);
initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
dataSource -> dataSource.getInitialLoadStateLiveData()); PostDataSource::getInitialLoadStateLiveData);
paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
dataSource -> dataSource.getPaginationNetworkStateLiveData()); PostDataSource::getPaginationNetworkStateLiveData);
hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
PostDataSource::hasPostLiveData);
sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData = new MutableLiveData<>();
sortTypeLiveData.postValue(sortType); sortTypeLiveData.postValue(sortType);
@ -126,6 +134,10 @@ public class PostViewModel extends ViewModel {
return initialLoadingState; return initialLoadingState;
} }
LiveData<Boolean> hasPost() {
return hasPostLiveData;
}
void refresh() { void refresh() {
postDataSourceFactory.getPostDataSource().invalidate(); postDataSourceFactory.getPostDataSource().invalidate();
} }
@ -151,21 +163,19 @@ public class PostViewModel extends ViewModel {
private int postType; private int postType;
private String sortType; private String sortType;
private int filter; private int filter;
private PostDataSource.OnPostFetchedCallback onPostFetchedCallback;
public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType, public Factory(Retrofit retrofit, String accessToken, Locale locale, int postType, String sortType,
int filter, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { int filter) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.locale = locale; this.locale = locale;
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
this.filter = filter; this.filter = filter;
this.onPostFetchedCallback = onPostFetchedCallback;
} }
public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, 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.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.locale = locale; this.locale = locale;
@ -173,22 +183,20 @@ public class PostViewModel extends ViewModel {
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
this.filter = filter; this.filter = filter;
this.onPostFetchedCallback = onPostFetchedCallback;
} }
public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType,
int filter, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { int filter) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.locale = locale; this.locale = locale;
this.subredditName = subredditName; this.subredditName = subredditName;
this.postType = postType; this.postType = postType;
this.filter = filter; this.filter = filter;
this.onPostFetchedCallback = onPostFetchedCallback;
} }
public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, String query, 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.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.locale = locale; this.locale = locale;
@ -197,20 +205,19 @@ public class PostViewModel extends ViewModel {
this.postType = postType; this.postType = postType;
this.sortType = sortType; this.sortType = sortType;
this.filter = filter; this.filter = filter;
this.onPostFetchedCallback = onPostFetchedCallback;
} }
@NonNull @NonNull
@Override @Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if(postType == PostDataSource.TYPE_FRONT_PAGE) { 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){ } 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) { } 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 { } else {
return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, filter, onPostFetchedCallback); return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, filter);
} }
} }
} }

View File

@ -10,41 +10,41 @@ import SubredditDatabase.SubredditData;
import retrofit2.Retrofit; import retrofit2.Retrofit;
public class SubredditListingDataSource extends PageKeyedDataSource<String, SubredditData> { public class SubredditListingDataSource extends PageKeyedDataSource<String, SubredditData> {
interface OnSubredditListingDataFetchedCallback {
void hasSubreddit();
void noSubreddit();
}
private Retrofit retrofit; private Retrofit retrofit;
private String query; private String query;
private String sortType; private String sortType;
private OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback;
private MutableLiveData<NetworkState> paginationNetworkStateLiveData; private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
private MutableLiveData<NetworkState> initialLoadStateLiveData; private MutableLiveData<NetworkState> initialLoadStateLiveData;
private MutableLiveData<Boolean> hasSubredditLiveData;
private LoadInitialParams<String> initialParams; private LoadInitialParams<String> initialParams;
private LoadInitialCallback<String, SubredditData> initialCallback; private LoadInitialCallback<String, SubredditData> initialCallback;
private LoadParams<String> params; private LoadParams<String> params;
private LoadCallback<String, SubredditData> callback; private LoadCallback<String, SubredditData> callback;
SubredditListingDataSource(Retrofit retrofit, String query, String sortType, SubredditListingDataSource(Retrofit retrofit, String query, String sortType) {
OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.query = query; this.query = query;
this.sortType = sortType; this.sortType = sortType;
this.onSubredditListingDataFetchedCallback = onSubredditListingDataFetchedCallback; paginationNetworkStateLiveData = new MutableLiveData<>();
paginationNetworkStateLiveData = new MutableLiveData(); initialLoadStateLiveData = new MutableLiveData<>();
initialLoadStateLiveData = new MutableLiveData(); hasSubredditLiveData = new MutableLiveData<>();
} }
MutableLiveData getPaginationNetworkStateLiveData() { MutableLiveData<NetworkState> getPaginationNetworkStateLiveData() {
return paginationNetworkStateLiveData; return paginationNetworkStateLiveData;
} }
MutableLiveData getInitialLoadStateLiveData() { MutableLiveData<NetworkState> getInitialLoadStateLiveData() {
return initialLoadStateLiveData; return initialLoadStateLiveData;
} }
MutableLiveData<Boolean> hasSubredditLiveData() {
return hasSubredditLiveData;
}
@Override @Override
public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<String, SubredditData> callback) { public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<String, SubredditData> callback) {
initialParams = params; initialParams = params;
@ -56,9 +56,9 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
@Override @Override
public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) { public void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
if(subredditData.size() == 0) { if(subredditData.size() == 0) {
onSubredditListingDataFetchedCallback.noSubreddit(); hasSubredditLiveData.postValue(false);
} else { } else {
onSubredditListingDataFetchedCallback.hasSubreddit(); hasSubredditLiveData.postValue(true);
} }
callback.onResult(subredditData, null, after); callback.onResult(subredditData, null, after);
@ -82,7 +82,7 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
this.params = params; this.params = params;
this.callback = callback; this.callback = callback;
if(params.key.equals("null")) { if(params.key.equals("") || params.key.equals("null")) {
return; return;
} }

View File

@ -9,25 +9,21 @@ public class SubredditListingDataSourceFactory extends DataSource.Factory {
private Retrofit retrofit; private Retrofit retrofit;
private String query; private String query;
private String sortType; private String sortType;
private SubredditListingDataSource.OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback;
private SubredditListingDataSource subredditListingDataSource; private SubredditListingDataSource subredditListingDataSource;
private MutableLiveData<SubredditListingDataSource> subredditListingDataSourceMutableLiveData; private MutableLiveData<SubredditListingDataSource> subredditListingDataSourceMutableLiveData;
SubredditListingDataSourceFactory(Retrofit retrofit, String query, String sortType, SubredditListingDataSourceFactory(Retrofit retrofit, String query, String sortType) {
SubredditListingDataSource.OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.query = query; this.query = query;
this.sortType = sortType; this.sortType = sortType;
this.onSubredditListingDataFetchedCallback = onSubredditListingDataFetchedCallback;
subredditListingDataSourceMutableLiveData = new MutableLiveData<>(); subredditListingDataSourceMutableLiveData = new MutableLiveData<>();
} }
@NonNull @NonNull
@Override @Override
public DataSource create() { public DataSource create() {
subredditListingDataSource = new SubredditListingDataSource(retrofit, subredditListingDataSource = new SubredditListingDataSource(retrofit, query, sortType);
query, sortType, onSubredditListingDataFetchedCallback);
subredditListingDataSourceMutableLiveData.postValue(subredditListingDataSource); subredditListingDataSourceMutableLiveData.postValue(subredditListingDataSource);
return subredditListingDataSource; return subredditListingDataSource;
} }

View File

@ -86,24 +86,8 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN);
String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); 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, mAdapter = new SubredditListingRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit,
accessToken, accountName, redditDataRoomDatabase.subscribedSubredditDao(), accessToken, accountName, redditDataRoomDatabase,
new SubredditListingRecyclerViewAdapter.Callback() { new SubredditListingRecyclerViewAdapter.Callback() {
@Override @Override
public void retryLoadingMore() { public void retryLoadingMore() {
@ -124,6 +108,8 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
mSubredditListingRecyclerView.setAdapter(mAdapter); mSubredditListingRecyclerView.setAdapter(mAdapter);
SubredditListingViewModel.Factory factory = new SubredditListingViewModel.Factory(mRetrofit, query,
PostDataSource.SORT_TYPE_RELEVANCE);
mSubredditListingViewModel = ViewModelProviders.of(this, factory).get(SubredditListingViewModel.class); mSubredditListingViewModel = ViewModelProviders.of(this, factory).get(SubredditListingViewModel.class);
mSubredditListingViewModel.getSubreddits().observe(this, subredditData -> mAdapter.submitList(subredditData)); 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 -> { mSubredditListingViewModel.getPaginationNetworkState().observe(this, networkState -> {
mAdapter.setNetworkState(networkState); mAdapter.setNetworkState(networkState);
}); });

View File

@ -21,7 +21,6 @@ import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.RequestOptions;
import SubredditDatabase.SubredditData; import SubredditDatabase.SubredditData;
import SubscribedSubredditDatabase.SubscribedSubredditDao;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
@ -45,14 +44,14 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
private Retrofit retrofit; private Retrofit retrofit;
private String accessToken; private String accessToken;
private String accountName; private String accountName;
private SubscribedSubredditDao subscribedSubredditDao; private RedditDataRoomDatabase redditDataRoomDatabase;
private NetworkState networkState; private NetworkState networkState;
private Callback callback; private Callback callback;
SubredditListingRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, Retrofit retrofit, SubredditListingRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, Retrofit retrofit,
String accessToken, String accountName, String accessToken, String accountName,
SubscribedSubredditDao subscribedSubredditDao, RedditDataRoomDatabase redditDataRoomDatabase,
Callback callback) { Callback callback) {
super(DIFF_CALLBACK); super(DIFF_CALLBACK);
this.context = context; this.context = context;
@ -60,7 +59,7 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
this.retrofit = retrofit; this.retrofit = retrofit;
this.accessToken = accessToken; this.accessToken = accessToken;
this.accountName = accountName; this.accountName = accountName;
this.subscribedSubredditDao = subscribedSubredditDao; this.redditDataRoomDatabase = redditDataRoomDatabase;
this.callback = callback; this.callback = callback;
glide = Glide.with(context.getApplicationContext()); glide = Glide.with(context.getApplicationContext());
} }
@ -113,7 +112,7 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
((DataViewHolder) holder).subredditNameTextView.setText(subredditData.getName()); ((DataViewHolder) holder).subredditNameTextView.setText(subredditData.getName());
new CheckIsSubscribedToSubredditAsyncTask(subscribedSubredditDao, subredditData.getName(), accountName, new CheckIsSubscribedToSubredditAsyncTask(redditDataRoomDatabase, subredditData.getName(), accountName,
new CheckIsSubscribedToSubredditAsyncTask.CheckIsSubscribedToSubredditListener() { new CheckIsSubscribedToSubredditAsyncTask.CheckIsSubscribedToSubredditListener() {
@Override @Override
public void isSubscribed() { public void isSubscribed() {
@ -125,7 +124,7 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
((DataViewHolder) holder).subscribeButton.setVisibility(View.VISIBLE); ((DataViewHolder) holder).subscribeButton.setVisibility(View.VISIBLE);
((DataViewHolder) holder).subscribeButton.setOnClickListener(view -> { ((DataViewHolder) holder).subscribeButton.setOnClickListener(view -> {
SubredditSubscription.subscribeToSubreddit(oauthRetrofit, retrofit, SubredditSubscription.subscribeToSubreddit(oauthRetrofit, retrofit,
accessToken, accountName, subredditData.getName(), subscribedSubredditDao, accessToken, accountName, subredditData.getName(), redditDataRoomDatabase,
new SubredditSubscription.SubredditSubscriptionListener() { new SubredditSubscription.SubredditSubscriptionListener() {
@Override @Override
public void onSubredditSubscriptionSuccess() { public void onSubredditSubscriptionSuccess() {

View File

@ -1,7 +1,6 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations; import androidx.lifecycle.Transformations;
@ -17,17 +16,19 @@ public class SubredditListingViewModel extends ViewModel {
private SubredditListingDataSourceFactory subredditListingDataSourceFactory; private SubredditListingDataSourceFactory subredditListingDataSourceFactory;
private LiveData<NetworkState> paginationNetworkState; private LiveData<NetworkState> paginationNetworkState;
private LiveData<NetworkState> initialLoadingState; private LiveData<NetworkState> initialLoadingState;
private LiveData<Boolean> hasSubredditLiveData;
private LiveData<PagedList<SubredditData>> subreddits; private LiveData<PagedList<SubredditData>> subreddits;
private MutableLiveData<String> sortTypeLiveData; private MutableLiveData<String> sortTypeLiveData;
SubredditListingViewModel(Retrofit retrofit, String query, String sortType, SubredditListingViewModel(Retrofit retrofit, String query, String sortType) {
SubredditListingDataSource.OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback) { subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType);
subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType, onSubredditListingDataFetchedCallback);
initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(), initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
(Function<SubredditListingDataSource, LiveData<NetworkState>>) SubredditListingDataSource::getInitialLoadStateLiveData); SubredditListingDataSource::getInitialLoadStateLiveData);
paginationNetworkState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(), paginationNetworkState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
(Function<SubredditListingDataSource, LiveData<NetworkState>>) SubredditListingDataSource::getPaginationNetworkStateLiveData); SubredditListingDataSource::getPaginationNetworkStateLiveData);
hasSubredditLiveData = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
SubredditListingDataSource::hasSubredditLiveData);
sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData = new MutableLiveData<>();
sortTypeLiveData.postValue(sortType); sortTypeLiveData.postValue(sortType);
@ -56,6 +57,10 @@ public class SubredditListingViewModel extends ViewModel {
return initialLoadingState; return initialLoadingState;
} }
LiveData<Boolean> hasSubredditLiveData() {
return hasSubredditLiveData;
}
void refresh() { void refresh() {
subredditListingDataSourceFactory.getSubredditListingDataSource().invalidate(); subredditListingDataSourceFactory.getSubredditListingDataSource().invalidate();
} }
@ -76,20 +81,17 @@ public class SubredditListingViewModel extends ViewModel {
private Retrofit retrofit; private Retrofit retrofit;
private String query; private String query;
private String sortType; private String sortType;
private SubredditListingDataSource.OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback;
public Factory(Retrofit retrofit, String query, String sortType, public Factory(Retrofit retrofit, String query, String sortType) {
SubredditListingDataSource.OnSubredditListingDataFetchedCallback onSubredditListingDataFetchedCallback) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.query = query; this.query = query;
this.sortType = sortType; this.sortType = sortType;
this.onSubredditListingDataFetchedCallback = onSubredditListingDataFetchedCallback;
} }
@NonNull @NonNull
@Override @Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { 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);
} }
} }
} }

View File

@ -9,7 +9,6 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import SubredditDatabase.SubredditData; import SubredditDatabase.SubredditData;
import SubscribedSubredditDatabase.SubscribedSubredditDao;
import SubscribedSubredditDatabase.SubscribedSubredditData; import SubscribedSubredditDatabase.SubscribedSubredditData;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
@ -23,23 +22,23 @@ class SubredditSubscription {
static void subscribeToSubreddit(Retrofit oauthRetrofit, Retrofit retrofit, static void subscribeToSubreddit(Retrofit oauthRetrofit, Retrofit retrofit,
String accessToken, String subredditName, String accountName, String accessToken, String subredditName, String accountName,
SubscribedSubredditDao subscribedSubredditDao, RedditDataRoomDatabase redditDataRoomDatabase,
SubredditSubscriptionListener subredditSubscriptionListener) { SubredditSubscriptionListener subredditSubscriptionListener) {
subredditSubscription(oauthRetrofit, retrofit, accessToken, subredditName, accountName, "sub", subredditSubscription(oauthRetrofit, retrofit, accessToken, subredditName, accountName, "sub",
subscribedSubredditDao, subredditSubscriptionListener); redditDataRoomDatabase, subredditSubscriptionListener);
} }
static void unsubscribeToSubreddit(Retrofit oauthRetrofit, String accessToken, static void unsubscribeToSubreddit(Retrofit oauthRetrofit, String accessToken,
String subredditName, String accountName, String subredditName, String accountName,
SubscribedSubredditDao subscribedSubredditDao, RedditDataRoomDatabase redditDataRoomDatabase,
SubredditSubscriptionListener subredditSubscriptionListener) { SubredditSubscriptionListener subredditSubscriptionListener) {
subredditSubscription(oauthRetrofit, null, accessToken, subredditName, accountName, "unsub", subredditSubscription(oauthRetrofit, null, accessToken, subredditName, accountName, "unsub",
subscribedSubredditDao,subredditSubscriptionListener); redditDataRoomDatabase,subredditSubscriptionListener);
} }
private static void subredditSubscription(Retrofit oauthRetrofit, Retrofit retrofit, String accessToken, private static void subredditSubscription(Retrofit oauthRetrofit, Retrofit retrofit, String accessToken,
String subredditName, String accountName, String action, String subredditName, String accountName, String action,
SubscribedSubredditDao subscribedSubredditDao, RedditDataRoomDatabase redditDataRoomDatabase,
SubredditSubscriptionListener subredditSubscriptionListener) { SubredditSubscriptionListener subredditSubscriptionListener) {
RedditAPI api = oauthRetrofit.create(RedditAPI.class); RedditAPI api = oauthRetrofit.create(RedditAPI.class);
@ -56,7 +55,7 @@ class SubredditSubscription {
FetchSubredditData.fetchSubredditData(retrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() { FetchSubredditData.fetchSubredditData(retrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() {
@Override @Override
public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) {
new UpdateSubscriptionAsyncTask(subscribedSubredditDao, new UpdateSubscriptionAsyncTask(redditDataRoomDatabase,
subredditData, accountName, true).execute(); subredditData, accountName, true).execute();
} }
@ -66,7 +65,7 @@ class SubredditSubscription {
} }
}); });
} else { } else {
new UpdateSubscriptionAsyncTask(subscribedSubredditDao, subredditName, accountName, false).execute(); new UpdateSubscriptionAsyncTask(redditDataRoomDatabase, subredditName, accountName, false).execute();
} }
subredditSubscriptionListener.onSubredditSubscriptionSuccess(); subredditSubscriptionListener.onSubredditSubscriptionSuccess();
} else { } else {
@ -85,23 +84,23 @@ class SubredditSubscription {
private static class UpdateSubscriptionAsyncTask extends AsyncTask<Void, Void, Void> { private static class UpdateSubscriptionAsyncTask extends AsyncTask<Void, Void, Void> {
private SubscribedSubredditDao subscribedSubredditDao; private RedditDataRoomDatabase redditDataRoomDatabase;
private String subredditName; private String subredditName;
private String accountName; private String accountName;
private SubscribedSubredditData subscribedSubredditData; private SubscribedSubredditData subscribedSubredditData;
private boolean isSubscribing; private boolean isSubscribing;
UpdateSubscriptionAsyncTask(SubscribedSubredditDao subscribedSubredditDao, String subredditName, UpdateSubscriptionAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String subredditName,
String accountName, boolean isSubscribing) { String accountName, boolean isSubscribing) {
this.subscribedSubredditDao = subscribedSubredditDao; this.redditDataRoomDatabase = redditDataRoomDatabase;
this.subredditName = subredditName; this.subredditName = subredditName;
this.accountName = accountName; this.accountName = accountName;
this.isSubscribing = isSubscribing; this.isSubscribing = isSubscribing;
} }
UpdateSubscriptionAsyncTask(SubscribedSubredditDao subscribedSubredditDao, SubredditData subredditData, UpdateSubscriptionAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, SubredditData subredditData,
String accountName, boolean isSubscribing) { String accountName, boolean isSubscribing) {
this.subscribedSubredditDao = subscribedSubredditDao; this.redditDataRoomDatabase = redditDataRoomDatabase;
this.subscribedSubredditData = new SubscribedSubredditData(subredditData.getId(), subredditData.getName(), this.subscribedSubredditData = new SubscribedSubredditData(subredditData.getId(), subredditData.getName(),
subredditData.getIconUrl(), accountName); subredditData.getIconUrl(), accountName);
this.accountName = accountName; this.accountName = accountName;
@ -111,9 +110,9 @@ class SubredditSubscription {
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
if(isSubscribing) { if(isSubscribing) {
subscribedSubredditDao.insert(subscribedSubredditData); redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData);
} else { } else {
subscribedSubredditDao.deleteSubscribedSubreddit(subredditName, accountName); redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName);
} }
return null; return null;
} }

View File

@ -10,42 +10,41 @@ import User.UserData;
import retrofit2.Retrofit; import retrofit2.Retrofit;
public class UserListingDataSource extends PageKeyedDataSource<String, UserData> { public class UserListingDataSource extends PageKeyedDataSource<String, UserData> {
interface OnUserListingDataFetchedCallback {
void hasUser();
void noUser();
}
private Retrofit retrofit; private Retrofit retrofit;
private String query; private String query;
private String sortType; private String sortType;
private UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback;
private MutableLiveData<NetworkState> paginationNetworkStateLiveData; private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
private MutableLiveData<NetworkState> initialLoadStateLiveData; private MutableLiveData<NetworkState> initialLoadStateLiveData;
private MutableLiveData<Boolean> hasUserLiveData;
private PageKeyedDataSource.LoadInitialParams<String> initialParams; private PageKeyedDataSource.LoadInitialParams<String> initialParams;
private PageKeyedDataSource.LoadInitialCallback<String, UserData> initialCallback; private PageKeyedDataSource.LoadInitialCallback<String, UserData> initialCallback;
private PageKeyedDataSource.LoadParams<String> params; private PageKeyedDataSource.LoadParams<String> params;
private PageKeyedDataSource.LoadCallback<String, UserData> callback; private PageKeyedDataSource.LoadCallback<String, UserData> callback;
UserListingDataSource(Retrofit retrofit, String query, String sortType, UserListingDataSource(Retrofit retrofit, String query, String sortType) {
UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.query = query; this.query = query;
this.sortType = sortType; this.sortType = sortType;
this.onUserListingDataFetchedCallback = onUserListingDataFetchedCallback; paginationNetworkStateLiveData = new MutableLiveData<>();
paginationNetworkStateLiveData = new MutableLiveData(); initialLoadStateLiveData = new MutableLiveData<>();
initialLoadStateLiveData = new MutableLiveData(); hasUserLiveData = new MutableLiveData<>();
} }
MutableLiveData getPaginationNetworkStateLiveData() { MutableLiveData<NetworkState> getPaginationNetworkStateLiveData() {
return paginationNetworkStateLiveData; return paginationNetworkStateLiveData;
} }
MutableLiveData getInitialLoadStateLiveData() { MutableLiveData<NetworkState> getInitialLoadStateLiveData() {
return initialLoadStateLiveData; return initialLoadStateLiveData;
} }
MutableLiveData<Boolean> hasUserLiveData() {
return hasUserLiveData;
}
@Override @Override
public void loadInitial(@NonNull PageKeyedDataSource.LoadInitialParams<String> params, @NonNull PageKeyedDataSource.LoadInitialCallback<String, UserData> callback) { public void loadInitial(@NonNull PageKeyedDataSource.LoadInitialParams<String> params, @NonNull PageKeyedDataSource.LoadInitialCallback<String, UserData> callback) {
initialParams = params; initialParams = params;
@ -57,9 +56,9 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
@Override @Override
public void onFetchUserListingDataSuccess(ArrayList<UserData> UserData, String after) { public void onFetchUserListingDataSuccess(ArrayList<UserData> UserData, String after) {
if(UserData.size() == 0) { if(UserData.size() == 0) {
onUserListingDataFetchedCallback.noUser(); hasUserLiveData.postValue(false);
} else { } else {
onUserListingDataFetchedCallback.hasUser(); hasUserLiveData.postValue(true);
} }
callback.onResult(UserData, null, after); callback.onResult(UserData, null, after);

View File

@ -9,25 +9,21 @@ public class UserListingDataSourceFactory extends DataSource.Factory {
private Retrofit retrofit; private Retrofit retrofit;
private String query; private String query;
private String sortType; private String sortType;
private UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback;
private UserListingDataSource userListingDataSource; private UserListingDataSource userListingDataSource;
private MutableLiveData<UserListingDataSource> userListingDataSourceMutableLiveData; private MutableLiveData<UserListingDataSource> userListingDataSourceMutableLiveData;
UserListingDataSourceFactory(Retrofit retrofit, String query, String sortType, UserListingDataSourceFactory(Retrofit retrofit, String query, String sortType) {
UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.query = query; this.query = query;
this.sortType = sortType; this.sortType = sortType;
this.onUserListingDataFetchedCallback = onUserListingDataFetchedCallback;
userListingDataSourceMutableLiveData = new MutableLiveData<>(); userListingDataSourceMutableLiveData = new MutableLiveData<>();
} }
@NonNull @NonNull
@Override @Override
public DataSource create() { public DataSource create() {
userListingDataSource = new UserListingDataSource(retrofit, userListingDataSource = new UserListingDataSource(retrofit, query, sortType);
query, sortType, onUserListingDataFetchedCallback);
userListingDataSourceMutableLiveData.postValue(userListingDataSource); userListingDataSourceMutableLiveData.postValue(userListingDataSource);
return userListingDataSource; return userListingDataSource;
} }

View File

@ -81,28 +81,14 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN);
String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); 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, mAdapter = new UserListingRecyclerViewAdapter(getActivity(), mOauthRetrofit, mRetrofit,
accessToken, accountName, redditDataRoomDatabase.subscribedUserDao(), accessToken, accountName, redditDataRoomDatabase.subscribedUserDao(),
() -> mUserListingViewModel.retryLoadingMore()); () -> mUserListingViewModel.retryLoadingMore());
mUserListingRecyclerView.setAdapter(mAdapter); mUserListingRecyclerView.setAdapter(mAdapter);
UserListingViewModel.Factory factory = new UserListingViewModel.Factory(mRetrofit, mQuery,
PostDataSource.SORT_TYPE_RELEVANCE);
mUserListingViewModel = ViewModelProviders.of(this, factory).get(UserListingViewModel.class); mUserListingViewModel = ViewModelProviders.of(this, factory).get(UserListingViewModel.class);
mUserListingViewModel.getUsers().observe(this, UserData -> mAdapter.submitList(UserData)); 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 -> { mUserListingViewModel.getPaginationNetworkState().observe(this, networkState -> {
mAdapter.setNetworkState(networkState); mAdapter.setNetworkState(networkState);
}); });

View File

@ -1,7 +1,6 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations; import androidx.lifecycle.Transformations;
@ -14,20 +13,22 @@ import User.UserData;
import retrofit2.Retrofit; import retrofit2.Retrofit;
public class UserListingViewModel extends ViewModel { public class UserListingViewModel extends ViewModel {
private UserListingDataSourceFactory UserListingDataSourceFactory; private UserListingDataSourceFactory userListingDataSourceFactory;
private LiveData<NetworkState> paginationNetworkState; private LiveData<NetworkState> paginationNetworkState;
private LiveData<NetworkState> initialLoadingState; private LiveData<NetworkState> initialLoadingState;
private LiveData<Boolean> hasUserLiveData;
private LiveData<PagedList<UserData>> users; private LiveData<PagedList<UserData>> users;
private MutableLiveData<String> sortTypeLiveData; private MutableLiveData<String> sortTypeLiveData;
UserListingViewModel(Retrofit retrofit, String query, String sortType, UserListingViewModel(Retrofit retrofit, String query, String sortType) {
UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback) { userListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType);
UserListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType, onUserListingDataFetchedCallback);
initialLoadingState = Transformations.switchMap(UserListingDataSourceFactory.getUserListingDataSourceMutableLiveData(), initialLoadingState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(),
(Function<UserListingDataSource, LiveData<NetworkState>>) UserListingDataSource::getInitialLoadStateLiveData); UserListingDataSource::getInitialLoadStateLiveData);
paginationNetworkState = Transformations.switchMap(UserListingDataSourceFactory.getUserListingDataSourceMutableLiveData(), paginationNetworkState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(),
(Function<UserListingDataSource, LiveData<NetworkState>>) UserListingDataSource::getPaginationNetworkStateLiveData); UserListingDataSource::getPaginationNetworkStateLiveData);
hasUserLiveData = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(),
UserListingDataSource::hasUserLiveData);
sortTypeLiveData = new MutableLiveData<>(); sortTypeLiveData = new MutableLiveData<>();
sortTypeLiveData.postValue(sortType); sortTypeLiveData.postValue(sortType);
@ -39,8 +40,8 @@ public class UserListingViewModel extends ViewModel {
.build(); .build();
users = Transformations.switchMap(sortTypeLiveData, sort -> { users = Transformations.switchMap(sortTypeLiveData, sort -> {
UserListingDataSourceFactory.changeSortType(sortTypeLiveData.getValue()); userListingDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
return (new LivePagedListBuilder(UserListingDataSourceFactory, pagedListConfig)).build(); return (new LivePagedListBuilder(userListingDataSourceFactory, pagedListConfig)).build();
}); });
} }
@ -56,16 +57,20 @@ public class UserListingViewModel extends ViewModel {
return initialLoadingState; return initialLoadingState;
} }
LiveData<Boolean> hasUser() {
return hasUserLiveData;
}
void refresh() { void refresh() {
UserListingDataSourceFactory.getUserListingDataSource().invalidate(); userListingDataSourceFactory.getUserListingDataSource().invalidate();
} }
void retry() { void retry() {
UserListingDataSourceFactory.getUserListingDataSource().retry(); userListingDataSourceFactory.getUserListingDataSource().retry();
} }
void retryLoadingMore() { void retryLoadingMore() {
UserListingDataSourceFactory.getUserListingDataSource().retryLoadingMore(); userListingDataSourceFactory.getUserListingDataSource().retryLoadingMore();
} }
void changeSortType(String sortType) { void changeSortType(String sortType) {
@ -76,20 +81,17 @@ public class UserListingViewModel extends ViewModel {
private Retrofit retrofit; private Retrofit retrofit;
private String query; private String query;
private String sortType; private String sortType;
private UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback;
public Factory(Retrofit retrofit, String query, String sortType, public Factory(Retrofit retrofit, String query, String sortType) {
UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback) {
this.retrofit = retrofit; this.retrofit = retrofit;
this.query = query; this.query = query;
this.sortType = sortType; this.sortType = sortType;
this.onUserListingDataFetchedCallback = onUserListingDataFetchedCallback;
} }
@NonNull @NonNull
@Override @Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { 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);
} }
} }
} }

View File

@ -31,7 +31,6 @@ import javax.inject.Named;
import SubredditDatabase.SubredditDao; import SubredditDatabase.SubredditDao;
import SubredditDatabase.SubredditData; import SubredditDatabase.SubredditData;
import SubredditDatabase.SubredditViewModel; import SubredditDatabase.SubredditViewModel;
import SubscribedSubredditDatabase.SubscribedSubredditDao;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
@ -76,7 +75,6 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
private PostTypeBottomSheetFragment postTypeBottomSheetFragment; private PostTypeBottomSheetFragment postTypeBottomSheetFragment;
private SortTypeBottomSheetFragment sortTypeBottomSheetFragment; private SortTypeBottomSheetFragment sortTypeBottomSheetFragment;
private SubscribedSubredditDao subscribedSubredditDao;
private SubredditViewModel mSubredditViewModel; private SubredditViewModel mSubredditViewModel;
@Inject @Inject
@ -142,7 +140,6 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams();
params.topMargin = statusBarHeight; params.topMargin = statusBarHeight;
subscribedSubredditDao = mRedditDataRoomDatabase.subscribedSubredditDao();
glide = Glide.with(this); glide = Glide.with(this);
mSubredditViewModel = ViewModelProviders.of(this, mSubredditViewModel = ViewModelProviders.of(this,
@ -239,7 +236,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
subscriptionReady = false; subscriptionReady = false;
if(subscribeSubredditChip.getText().equals(getResources().getString(R.string.subscribe))) { if(subscribeSubredditChip.getText().equals(getResources().getString(R.string.subscribe))) {
SubredditSubscription.subscribeToSubreddit(mOauthRetrofit, mRetrofit, mAccessToken, SubredditSubscription.subscribeToSubreddit(mOauthRetrofit, mRetrofit, mAccessToken,
subredditName, mAccountName, subscribedSubredditDao, subredditName, mAccountName, mRedditDataRoomDatabase,
new SubredditSubscription.SubredditSubscriptionListener() { new SubredditSubscription.SubredditSubscriptionListener() {
@Override @Override
public void onSubredditSubscriptionSuccess() { public void onSubredditSubscriptionSuccess() {
@ -257,7 +254,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
}); });
} else { } else {
SubredditSubscription.unsubscribeToSubreddit(mOauthRetrofit, mAccessToken, SubredditSubscription.unsubscribeToSubreddit(mOauthRetrofit, mAccessToken,
subredditName, mAccountName, subscribedSubredditDao, subredditName, mAccountName, mRedditDataRoomDatabase,
new SubredditSubscription.SubredditSubscriptionListener() { new SubredditSubscription.SubredditSubscriptionListener() {
@Override @Override
public void onSubredditSubscriptionSuccess() { 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() { new CheckIsSubscribedToSubredditAsyncTask.CheckIsSubscribedToSubredditListener() {
@Override @Override
public void isSubscribed() { public void isSubscribed() {

View File

@ -489,7 +489,6 @@ public class ViewUserDetailActivity extends AppCompatActivity {
break; break;
case 1: case 1:
commentsListingFragment = (CommentsListingFragment) fragment; commentsListingFragment = (CommentsListingFragment) fragment;
break;
} }
return fragment; return fragment;
} }

View File

@ -27,7 +27,8 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:gravity="center" /> android:gravity="center"
android:text="@string/no_users" />
</LinearLayout> </LinearLayout>

View File

@ -28,7 +28,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:gravity="center" android:gravity="center"
android:text="@string/no_subreddits"/> android:text="@string/no_subreddits" />
</LinearLayout> </LinearLayout>

View File

@ -35,9 +35,10 @@
<string name="load_posts_error">Error loading posts.\nTap to retry.</string> <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_subreddits_error">Error searching subreddits.\nTap to retry.</string>
<string name="search_users_error">Error searching users.\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_posts">No posts found</string>
<string name="no_subreddits">No subreddits found.</string> <string name="no_comments">No comments found</string>
<string name="no_users">No users 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="no_storage_permission">No storage permission to save this file</string>
<string name="load_comments_failed">Error loading comments.</string> <string name="load_comments_failed">Error loading comments.</string>