Fixed bugs related to several ViewModels.

This commit is contained in:
Alex Ning 2019-08-29 17:08:40 +08:00
parent eff076a270
commit 92762974ff
19 changed files with 30 additions and 164 deletions

View File

@ -1,73 +0,0 @@
package CustomView;
import android.content.Context;
import android.text.Spanned;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.commonmark.node.Node;
import java.util.List;
import io.noties.markwon.Markwon;
import io.noties.markwon.MarkwonPlugin;
public class CustomMarkwonView extends Markwon {
public void setMarkdown(@Nullable String markdown, Context context) {
}
@NonNull
@Override
public Node parse(@NonNull String input) {
return null;
}
@NonNull
@Override
public Spanned render(@NonNull Node node) {
return null;
}
@NonNull
@Override
public Spanned toMarkdown(@NonNull String input) {
return null;
}
@Override
public void setMarkdown(@NonNull TextView textView, @NonNull String markdown) {
}
@Override
public void setParsedMarkdown(@NonNull TextView textView, @NonNull Spanned markdown) {
}
@Override
public boolean hasPlugin(@NonNull Class<? extends MarkwonPlugin> plugin) {
return false;
}
@Nullable
@Override
public <P extends MarkwonPlugin> P getPlugin(@NonNull Class<P> type) {
return null;
}
@NonNull
@Override
public <P extends MarkwonPlugin> P requirePlugin(@NonNull Class<P> type) {
return null;
}
@NonNull
@Override
public List<? extends MarkwonPlugin> getPlugins() {
return null;
}
}

View File

@ -30,8 +30,6 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData>
private MutableLiveData<NetworkState> initialLoadStateLiveData; private MutableLiveData<NetworkState> initialLoadStateLiveData;
private MutableLiveData<Boolean> hasPostLiveData; private MutableLiveData<Boolean> hasPostLiveData;
private LoadInitialParams<String> initialParams;
private LoadInitialCallback<String, CommentData> initialCallback;
private LoadParams<String> params; private LoadParams<String> params;
private LoadCallback<String, CommentData> callback; private LoadCallback<String, CommentData> callback;
@ -57,19 +55,12 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData>
return hasPostLiveData; return hasPostLiveData;
} }
void retry() {
loadInitial(initialParams, initialCallback);
}
void retryLoadingMore() { void retryLoadingMore() {
loadAfter(params, callback); loadAfter(params, callback);
} }
@Override @Override
public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<String, CommentData> callback) { public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<String, CommentData> callback) {
initialParams = params;
initialCallback = callback;
initialLoadStateLiveData.postValue(NetworkState.LOADING); initialLoadStateLiveData.postValue(NetworkState.LOADING);
RedditAPI api = retrofit.create(RedditAPI.class); RedditAPI api = retrofit.create(RedditAPI.class);

View File

@ -66,10 +66,6 @@ public class CommentViewModel extends ViewModel {
commentDataSourceFactory.getCommentDataSource().invalidate(); commentDataSourceFactory.getCommentDataSource().invalidate();
} }
void retry() {
commentDataSourceFactory.getCommentDataSource().retry();
}
void retryLoadingMore() { void retryLoadingMore() {
commentDataSourceFactory.getCommentDataSource().retryLoadingMore(); commentDataSourceFactory.getCommentDataSource().retryLoadingMore();
} }

View File

@ -109,8 +109,10 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
mCommentViewModel.getInitialLoadingState().observe(this, networkState -> { mCommentViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) { if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE);
mFetchCommentInfoLinearLayout.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) { } else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mFetchCommentInfoLinearLayout.setOnClickListener(view -> mCommentViewModel.retry()); mProgressBar.setVisibility(View.GONE);
mFetchCommentInfoLinearLayout.setOnClickListener(view -> mCommentViewModel.refresh());
showErrorView(R.string.load_comments_failed); showErrorView(R.string.load_comments_failed);
} else { } else {
mFetchCommentInfoLinearLayout.setVisibility(View.GONE); mFetchCommentInfoLinearLayout.setVisibility(View.GONE);
@ -119,6 +121,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
}); });
mCommentViewModel.hasComment().observe(this, hasComment -> { mCommentViewModel.hasComment().observe(this, hasComment -> {
mProgressBar.setVisibility(View.GONE);
if(hasComment) { if(hasComment) {
mFetchCommentInfoLinearLayout.setVisibility(View.GONE); mFetchCommentInfoLinearLayout.setVisibility(View.GONE);
} else { } else {
@ -149,6 +152,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
@Override @Override
public void refresh() { public void refresh() {
mCommentViewModel.refresh(); mCommentViewModel.refresh();
mAdapter.setNetworkState(null);
} }
private void showErrorView(int stringResId) { private void showErrorView(int stringResId) {

View File

@ -390,7 +390,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(sectionsPagerAdapter); viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setOffscreenPageLimit(2); viewPager.setOffscreenPageLimit(3);
tabLayout.setupWithViewPager(viewPager); tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

View File

@ -22,8 +22,6 @@ class MessageDataSource extends PageKeyedDataSource<String, Message> {
private MutableLiveData<NetworkState> initialLoadStateLiveData; private MutableLiveData<NetworkState> initialLoadStateLiveData;
private MutableLiveData<Boolean> hasPostLiveData; private MutableLiveData<Boolean> hasPostLiveData;
private LoadInitialParams<String> initialParams;
private LoadInitialCallback<String, Message> initialCallback;
private LoadParams<String> params; private LoadParams<String> params;
private LoadCallback<String, Message> callback; private LoadCallback<String, Message> callback;
@ -49,19 +47,12 @@ class MessageDataSource extends PageKeyedDataSource<String, Message> {
return hasPostLiveData; return hasPostLiveData;
} }
void retry() {
loadInitial(initialParams, initialCallback);
}
void retryLoadingMore() { void retryLoadingMore() {
loadAfter(params, callback); loadAfter(params, callback);
} }
@Override @Override
public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<String, Message> callback) { public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull LoadInitialCallback<String, Message> callback) {
initialParams = params;
initialCallback = callback;
initialLoadStateLiveData.postValue(NetworkState.LOADING); initialLoadStateLiveData.postValue(NetworkState.LOADING);
FetchMessages.fetchMessagesAsync(oauthRetrofit, locale, accessToken, where, null, new FetchMessages.FetchMessagesListener() { FetchMessages.fetchMessagesAsync(oauthRetrofit, locale, accessToken, where, null, new FetchMessages.FetchMessagesListener() {

View File

@ -66,10 +66,6 @@ public class MessageViewModel extends ViewModel {
messageDataSourceFactory.getMessageDataSource().invalidate(); messageDataSourceFactory.getMessageDataSource().invalidate();
} }
void retry() {
messageDataSourceFactory.getMessageDataSource().retry();
}
void retryLoadingMore() { void retryLoadingMore() {
messageDataSourceFactory.getMessageDataSource().retryLoadingMore(); messageDataSourceFactory.getMessageDataSource().retryLoadingMore();
} }

View File

@ -226,7 +226,6 @@ class ParsePost {
if(!data.has(JSONUtils.PREVIEW_KEY) && previewUrl.equals("")) { if(!data.has(JSONUtils.PREVIEW_KEY) && previewUrl.equals("")) {
if(url.contains(permalink)) { if(url.contains(permalink)) {
//Text post //Text post
Log.i("text", Integer.toString(i));
int postType = Post.TEXT_TYPE; int postType = Post.TEXT_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
title, permalink, score, postType, voteType, gilded, flair, spoiler, nsfw, title, permalink, score, postType, voteType, gilded, flair, spoiler, nsfw,
@ -238,7 +237,6 @@ class ParsePost {
} }
} else { } else {
//No preview link post //No preview link post
Log.i("no preview link", Integer.toString(i));
int postType = Post.NO_PREVIEW_LINK_TYPE; int postType = Post.NO_PREVIEW_LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
title, previewUrl, url, permalink, score, postType, title, previewUrl, url, permalink, score, postType,
@ -257,7 +255,6 @@ class ParsePost {
if(isVideo) { if(isVideo) {
//Video post //Video post
Log.i("video", Integer.toString(i));
JSONObject redditVideoObject = data.getJSONObject(JSONUtils.MEDIA_KEY).getJSONObject(JSONUtils.REDDIT_VIDEO_KEY); JSONObject redditVideoObject = data.getJSONObject(JSONUtils.MEDIA_KEY).getJSONObject(JSONUtils.REDDIT_VIDEO_KEY);
int postType = Post.VIDEO_TYPE; int postType = Post.VIDEO_TYPE;
String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.DASH_URL_KEY)).toString(); String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.DASH_URL_KEY)).toString();
@ -274,7 +271,6 @@ class ParsePost {
JSONObject variations = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0); JSONObject variations = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0);
if (variations.has(JSONUtils.VARIANTS_KEY) && variations.getJSONObject(JSONUtils.VARIANTS_KEY).has(JSONUtils.MP4_KEY)) { if (variations.has(JSONUtils.VARIANTS_KEY) && variations.getJSONObject(JSONUtils.VARIANTS_KEY).has(JSONUtils.MP4_KEY)) {
//Gif video post (MP4) //Gif video post (MP4)
Log.i("gif video mp4", Integer.toString(i));
int postType = Post.GIF_VIDEO_TYPE; int postType = Post.GIF_VIDEO_TYPE;
String videoUrl = Html.fromHtml(variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.MP4_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY)).toString(); String videoUrl = Html.fromHtml(variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.MP4_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY)).toString();
String gifDownloadUrl = Html.fromHtml(variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.GIF_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY)).toString(); String gifDownloadUrl = Html.fromHtml(variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.GIF_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY)).toString();
@ -289,7 +285,6 @@ class ParsePost {
post.setGifOrVideoDownloadUrl(gifDownloadUrl); post.setGifOrVideoDownloadUrl(gifDownloadUrl);
} else if(data.getJSONObject(JSONUtils.PREVIEW_KEY).has(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY)) { } else if(data.getJSONObject(JSONUtils.PREVIEW_KEY).has(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY)) {
//Gif video post (Dash) //Gif video post (Dash)
Log.i("gif video dash", Integer.toString(i));
int postType = Post.GIF_VIDEO_TYPE; int postType = Post.GIF_VIDEO_TYPE;
String videoUrl = Html.fromHtml(data.getJSONObject(JSONUtils.PREVIEW_KEY) String videoUrl = Html.fromHtml(data.getJSONObject(JSONUtils.PREVIEW_KEY)
.getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.DASH_URL_KEY)).toString(); .getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.DASH_URL_KEY)).toString();
@ -304,7 +299,6 @@ class ParsePost {
} else { } else {
if (url.endsWith("jpg") || url.endsWith("png")) { if (url.endsWith("jpg") || url.endsWith("png")) {
//Image post //Image post
Log.i("image", Integer.toString(i));
int postType = Post.IMAGE_TYPE; int postType = Post.IMAGE_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
@ -316,7 +310,6 @@ class ParsePost {
} else { } else {
if (url.contains(permalink)) { if (url.contains(permalink)) {
//Text post but with a preview //Text post but with a preview
Log.i("text with image", Integer.toString(i));
int postType = Post.TEXT_TYPE; int postType = Post.TEXT_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
@ -333,7 +326,6 @@ class ParsePost {
} }
} else { } else {
//Link post //Link post
Log.i("link", Integer.toString(i));
int postType = Post.LINK_TYPE; int postType = Post.LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
@ -353,7 +345,6 @@ class ParsePost {
} else { } else {
if (url.endsWith("jpg") || url.endsWith("png")) { if (url.endsWith("jpg") || url.endsWith("png")) {
//Image post //Image post
Log.i("CP image", Integer.toString(i));
int postType = Post.IMAGE_TYPE; int postType = Post.IMAGE_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
@ -363,7 +354,6 @@ class ParsePost {
post.setPreviewHeight(previewHeight); post.setPreviewHeight(previewHeight);
} else { } else {
//CP No Preview Link post //CP No Preview Link post
Log.i("CP no preview link", Integer.toString(i));
int postType = Post.NO_PREVIEW_LINK_TYPE; int postType = Post.NO_PREVIEW_LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title,

View File

@ -52,8 +52,6 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
private MutableLiveData<NetworkState> initialLoadStateLiveData; private MutableLiveData<NetworkState> initialLoadStateLiveData;
private MutableLiveData<Boolean> hasPostLiveData; private MutableLiveData<Boolean> hasPostLiveData;
private LoadInitialParams<String> initialParams;
private LoadInitialCallback<String, Post> initialCallback;
private LoadParams<String> params; private LoadParams<String> params;
private LoadCallback<String, Post> callback; private LoadCallback<String, Post> callback;
@ -132,9 +130,6 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@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;
initialCallback = callback;
initialLoadStateLiveData.postValue(NetworkState.LOADING); initialLoadStateLiveData.postValue(NetworkState.LOADING);
switch (postType) { switch (postType) {
@ -652,10 +647,6 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
}); });
} }
void retry() {
loadInitial(initialParams, initialCallback);
}
void retryLoadingMore() { void retryLoadingMore() {
loadAfter(params, callback); loadAfter(params, callback);
} }

View File

@ -296,7 +296,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
} else if(postType == PostDataSource.TYPE_SUBREDDIT) { } else if(postType == PostDataSource.TYPE_SUBREDDIT) {
String subredditName = getArguments().getString(EXTRA_NAME); String subredditName = getArguments().getString(EXTRA_NAME);
boolean displaySubredditName = subredditName.equals("popular") || subredditName.equals("all"); boolean displaySubredditName = subredditName != null && (subredditName.equals("popular") || subredditName.equals("all"));
mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase,
accessToken, postType, displaySubredditName, new PostRecyclerViewAdapter.Callback() { accessToken, postType, displaySubredditName, new PostRecyclerViewAdapter.Callback() {
@Override @Override
@ -392,8 +392,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mPostViewModel.getInitialLoadingState().observe(this, networkState -> { mPostViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) { if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE);
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) { } else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mFetchPostInfoLinearLayout.setOnClickListener(view -> mPostViewModel.retry()); mProgressBar.setVisibility(View.GONE);
mFetchPostInfoLinearLayout.setOnClickListener(view -> mPostViewModel.refresh());
showErrorView(R.string.load_posts_error); showErrorView(R.string.load_posts_error);
} else { } else {
mFetchPostInfoLinearLayout.setVisibility(View.GONE); mFetchPostInfoLinearLayout.setVisibility(View.GONE);
@ -403,14 +405,15 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mPostViewModel.hasPost().observe(this, hasPost -> { mPostViewModel.hasPost().observe(this, hasPost -> {
this.hasPost = hasPost; this.hasPost = hasPost;
mProgressBar.setVisibility(View.GONE);
if(hasPost) { if(hasPost) {
mFetchPostInfoLinearLayout.setVisibility(View.GONE); mFetchPostInfoLinearLayout.setVisibility(View.GONE);
mProgressBar.setVisibility(View.GONE);
} else { } else {
if(isInLazyMode) { if(isInLazyMode) {
stopLazyMode(); stopLazyMode();
} }
mFetchPostInfoLinearLayout.setOnClickListener(view -> {});
showErrorView(R.string.no_posts); showErrorView(R.string.no_posts);
} }
}); });
@ -451,6 +454,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
hasPost = false; hasPost = false;
mPostViewModel.refresh(); mPostViewModel.refresh();
mAdapter.setNetworkState(null);
} }
private void showErrorView(int stringResId) { private void showErrorView(int stringResId) {

View File

@ -169,10 +169,6 @@ public class PostViewModel extends ViewModel {
postDataSourceFactory.getPostDataSource().invalidate(); postDataSourceFactory.getPostDataSource().invalidate();
} }
void retry() {
postDataSourceFactory.getPostDataSource().retry();
}
void retryLoadingMore() { void retryLoadingMore() {
postDataSourceFactory.getPostDataSource().retryLoadingMore(); postDataSourceFactory.getPostDataSource().retryLoadingMore();
} }

View File

@ -375,12 +375,6 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos
} }
} }
public void newSearch() {
getItem(0);
getItem(1);
getItem(2);
}
public void changeNSFW(boolean nsfw) { public void changeNSFW(boolean nsfw) {
if(postFragment != null) { if(postFragment != null) {
postFragment.changeNSFW(nsfw); postFragment.changeNSFW(nsfw);

View File

@ -19,8 +19,6 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
private MutableLiveData<NetworkState> initialLoadStateLiveData; private MutableLiveData<NetworkState> initialLoadStateLiveData;
private MutableLiveData<Boolean> hasSubredditLiveData; private MutableLiveData<Boolean> hasSubredditLiveData;
private LoadInitialParams<String> initialParams;
private LoadInitialCallback<String, SubredditData> initialCallback;
private LoadParams<String> params; private LoadParams<String> params;
private LoadCallback<String, SubredditData> callback; private LoadCallback<String, SubredditData> callback;
@ -47,9 +45,6 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
@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;
initialCallback = callback;
initialLoadStateLiveData.postValue(NetworkState.LOADING); initialLoadStateLiveData.postValue(NetworkState.LOADING);
FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType, FetchSubredditData.fetchSubredditListingData(retrofit, query, null, sortType,
@ -102,10 +97,6 @@ public class SubredditListingDataSource extends PageKeyedDataSource<String, Subr
}); });
} }
void retry() {
loadInitial(initialParams, initialCallback);
}
void retryLoadingMore() { void retryLoadingMore() {
loadAfter(params, callback); loadAfter(params, callback);
} }

View File

@ -130,8 +130,10 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
mSubredditListingViewModel.getInitialLoadingState().observe(this, networkState -> { mSubredditListingViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) { if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE);
mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) { } else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mFetchSubredditListingInfoLinearLayout.setOnClickListener(view -> mSubredditListingViewModel.retry()); mProgressBar.setVisibility(View.GONE);
mFetchSubredditListingInfoLinearLayout.setOnClickListener(view -> mSubredditListingViewModel.refresh());
showErrorView(R.string.search_subreddits_error); showErrorView(R.string.search_subreddits_error);
} else { } else {
mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE); mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE);
@ -140,6 +142,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
}); });
mSubredditListingViewModel.hasSubredditLiveData().observe(this, hasSubreddit -> { mSubredditListingViewModel.hasSubredditLiveData().observe(this, hasSubreddit -> {
mProgressBar.setVisibility(View.GONE);
if(hasSubreddit) { if(hasSubreddit) {
mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE); mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE);
} else { } else {
@ -173,5 +176,6 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
@Override @Override
public void refresh() { public void refresh() {
mSubredditListingViewModel.refresh(); mSubredditListingViewModel.refresh();
mAdapter.setNetworkState(null);
} }
} }

View File

@ -20,7 +20,7 @@ public class SubredditListingViewModel extends ViewModel {
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) { public SubredditListingViewModel(Retrofit retrofit, String query, String sortType) {
subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType); subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType);
initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(), initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
@ -65,10 +65,6 @@ public class SubredditListingViewModel extends ViewModel {
subredditListingDataSourceFactory.getSubredditListingDataSource().invalidate(); subredditListingDataSourceFactory.getSubredditListingDataSource().invalidate();
} }
void retry() {
subredditListingDataSourceFactory.getSubredditListingDataSource().retry();
}
void retryLoadingMore() { void retryLoadingMore() {
subredditListingDataSourceFactory.getSubredditListingDataSource().retryLoadingMore(); subredditListingDataSourceFactory.getSubredditListingDataSource().retryLoadingMore();
} }

View File

@ -19,8 +19,6 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
private MutableLiveData<NetworkState> initialLoadStateLiveData; private MutableLiveData<NetworkState> initialLoadStateLiveData;
private MutableLiveData<Boolean> hasUserLiveData; private MutableLiveData<Boolean> hasUserLiveData;
private PageKeyedDataSource.LoadInitialParams<String> initialParams;
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;
@ -47,9 +45,6 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
@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;
initialCallback = callback;
initialLoadStateLiveData.postValue(NetworkState.LOADING); initialLoadStateLiveData.postValue(NetworkState.LOADING);
FetchUserData.fetchUserListingData(retrofit, query, null, sortType, FetchUserData.fetchUserListingData(retrofit, query, null, sortType,
@ -102,10 +97,6 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData>
}); });
} }
void retry() {
loadInitial(initialParams, initialCallback);
}
void retryLoadingMore() { void retryLoadingMore() {
loadAfter(params, callback); loadAfter(params, callback);
} }

View File

@ -109,8 +109,10 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
mUserListingViewModel.getInitialLoadingState().observe(this, networkState -> { mUserListingViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) { if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE);
mFetchUserListingInfoLinearLayout.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) { } else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mFetchUserListingInfoLinearLayout.setOnClickListener(view -> mUserListingViewModel.retry()); mProgressBar.setVisibility(View.GONE);
mFetchUserListingInfoLinearLayout.setOnClickListener(view -> mUserListingViewModel.refresh());
showErrorView(R.string.search_users_error); showErrorView(R.string.search_users_error);
} else { } else {
mFetchUserListingInfoLinearLayout.setVisibility(View.GONE); mFetchUserListingInfoLinearLayout.setVisibility(View.GONE);
@ -119,6 +121,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
}); });
mUserListingViewModel.hasUser().observe(this, hasUser -> { mUserListingViewModel.hasUser().observe(this, hasUser -> {
mProgressBar.setVisibility(View.GONE);
if(hasUser) { if(hasUser) {
mFetchUserListingInfoLinearLayout.setVisibility(View.GONE); mFetchUserListingInfoLinearLayout.setVisibility(View.GONE);
} else { } else {
@ -152,5 +155,6 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
@Override @Override
public void refresh() { public void refresh() {
mUserListingViewModel.refresh(); mUserListingViewModel.refresh();
mAdapter.setNetworkState(null);
} }
} }

View File

@ -20,7 +20,7 @@ public class UserListingViewModel extends ViewModel {
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) { public UserListingViewModel(Retrofit retrofit, String query, String sortType) {
userListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType); userListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType);
initialLoadingState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(), initialLoadingState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(),
@ -65,10 +65,6 @@ public class UserListingViewModel extends ViewModel {
userListingDataSourceFactory.getUserListingDataSource().invalidate(); userListingDataSourceFactory.getUserListingDataSource().invalidate();
} }
void retry() {
userListingDataSourceFactory.getUserListingDataSource().retry();
}
void retryLoadingMore() { void retryLoadingMore() {
userListingDataSourceFactory.getUserListingDataSource().retryLoadingMore(); userListingDataSourceFactory.getUserListingDataSource().retryLoadingMore();
} }

View File

@ -229,8 +229,10 @@ public class ViewMessageActivity extends AppCompatActivity {
mMessageViewModel.getInitialLoadingState().observe(this, networkState -> { mMessageViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) { if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE);
mFetchMessageInfoLinearLayout.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) { } else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mFetchMessageInfoLinearLayout.setOnClickListener(view -> mMessageViewModel.retry()); mProgressBar.setVisibility(View.GONE);
mFetchMessageInfoLinearLayout.setOnClickListener(view -> mMessageViewModel.refresh());
showErrorView(R.string.load_messages_failed); showErrorView(R.string.load_messages_failed);
} else { } else {
mFetchMessageInfoLinearLayout.setVisibility(View.GONE); mFetchMessageInfoLinearLayout.setVisibility(View.GONE);
@ -239,6 +241,7 @@ public class ViewMessageActivity extends AppCompatActivity {
}); });
mMessageViewModel.hasMessage().observe(this, hasMessage -> { mMessageViewModel.hasMessage().observe(this, hasMessage -> {
mProgressBar.setVisibility(View.GONE);
if(hasMessage) { if(hasMessage) {
mFetchMessageInfoLinearLayout.setVisibility(View.GONE); mFetchMessageInfoLinearLayout.setVisibility(View.GONE);
} else { } else {
@ -271,6 +274,7 @@ public class ViewMessageActivity extends AppCompatActivity {
public boolean onOptionsItemSelected(@NonNull MenuItem item) { public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(item.getItemId() == R.id.action_refresh_view_message_activity) { if(item.getItemId() == R.id.action_refresh_view_message_activity) {
mMessageViewModel.refresh(); mMessageViewModel.refresh();
mAdapter.setNetworkState(null);
return true; return true;
} else if(item.getItemId() == android.R.id.home) { } else if(item.getItemId() == android.R.id.home) {
finish(); finish();