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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -226,7 +226,6 @@ class ParsePost {
if(!data.has(JSONUtils.PREVIEW_KEY) && previewUrl.equals("")) {
if(url.contains(permalink)) {
//Text post
Log.i("text", Integer.toString(i));
int postType = Post.TEXT_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
title, permalink, score, postType, voteType, gilded, flair, spoiler, nsfw,
@ -238,7 +237,6 @@ class ParsePost {
}
} else {
//No preview link post
Log.i("no preview link", Integer.toString(i));
int postType = Post.NO_PREVIEW_LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
title, previewUrl, url, permalink, score, postType,
@ -257,7 +255,6 @@ class ParsePost {
if(isVideo) {
//Video post
Log.i("video", Integer.toString(i));
JSONObject redditVideoObject = data.getJSONObject(JSONUtils.MEDIA_KEY).getJSONObject(JSONUtils.REDDIT_VIDEO_KEY);
int postType = Post.VIDEO_TYPE;
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);
if (variations.has(JSONUtils.VARIANTS_KEY) && variations.getJSONObject(JSONUtils.VARIANTS_KEY).has(JSONUtils.MP4_KEY)) {
//Gif video post (MP4)
Log.i("gif video mp4", Integer.toString(i));
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 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);
} else if(data.getJSONObject(JSONUtils.PREVIEW_KEY).has(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY)) {
//Gif video post (Dash)
Log.i("gif video dash", Integer.toString(i));
int postType = Post.GIF_VIDEO_TYPE;
String videoUrl = Html.fromHtml(data.getJSONObject(JSONUtils.PREVIEW_KEY)
.getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.DASH_URL_KEY)).toString();
@ -304,7 +299,6 @@ class ParsePost {
} else {
if (url.endsWith("jpg") || url.endsWith("png")) {
//Image post
Log.i("image", Integer.toString(i));
int postType = Post.IMAGE_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
@ -316,7 +310,6 @@ class ParsePost {
} else {
if (url.contains(permalink)) {
//Text post but with a preview
Log.i("text with image", Integer.toString(i));
int postType = Post.TEXT_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
@ -333,7 +326,6 @@ class ParsePost {
}
} else {
//Link post
Log.i("link", Integer.toString(i));
int postType = Post.LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
@ -353,7 +345,6 @@ class ParsePost {
} else {
if (url.endsWith("jpg") || url.endsWith("png")) {
//Image post
Log.i("CP image", Integer.toString(i));
int postType = Post.IMAGE_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
@ -363,7 +354,6 @@ class ParsePost {
post.setPreviewHeight(previewHeight);
} else {
//CP No Preview Link post
Log.i("CP no preview link", Integer.toString(i));
int postType = Post.NO_PREVIEW_LINK_TYPE;
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<Boolean> hasPostLiveData;
private LoadInitialParams<String> initialParams;
private LoadInitialCallback<String, Post> initialCallback;
private LoadParams<String> params;
private LoadCallback<String, Post> callback;
@ -132,9 +130,6 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void loadInitial(@NonNull LoadInitialParams<String> params, @NonNull final LoadInitialCallback<String, Post> callback) {
initialParams = params;
initialCallback = callback;
initialLoadStateLiveData.postValue(NetworkState.LOADING);
switch (postType) {
@ -652,10 +647,6 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
});
}
void retry() {
loadInitial(initialParams, initialCallback);
}
void retryLoadingMore() {
loadAfter(params, callback);
}

View File

@ -296,7 +296,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
} else if(postType == PostDataSource.TYPE_SUBREDDIT) {
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,
accessToken, postType, displaySubredditName, new PostRecyclerViewAdapter.Callback() {
@Override
@ -392,8 +392,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mPostViewModel.getInitialLoadingState().observe(this, networkState -> {
if(networkState.getStatus().equals(NetworkState.Status.SUCCESS)) {
mProgressBar.setVisibility(View.GONE);
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
} 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);
} else {
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
@ -403,14 +405,15 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mPostViewModel.hasPost().observe(this, hasPost -> {
this.hasPost = hasPost;
mProgressBar.setVisibility(View.GONE);
if(hasPost) {
mFetchPostInfoLinearLayout.setVisibility(View.GONE);
mProgressBar.setVisibility(View.GONE);
} else {
if(isInLazyMode) {
stopLazyMode();
}
mFetchPostInfoLinearLayout.setOnClickListener(view -> {});
showErrorView(R.string.no_posts);
}
});
@ -451,6 +454,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
hasPost = false;
mPostViewModel.refresh();
mAdapter.setNetworkState(null);
}
private void showErrorView(int stringResId) {

View File

@ -169,10 +169,6 @@ public class PostViewModel extends ViewModel {
postDataSourceFactory.getPostDataSource().invalidate();
}
void retry() {
postDataSourceFactory.getPostDataSource().retry();
}
void 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) {
if(postFragment != null) {
postFragment.changeNSFW(nsfw);

View File

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

View File

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

View File

@ -20,7 +20,7 @@ public class SubredditListingViewModel extends ViewModel {
private LiveData<PagedList<SubredditData>> subreddits;
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);
initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
@ -65,10 +65,6 @@ public class SubredditListingViewModel extends ViewModel {
subredditListingDataSourceFactory.getSubredditListingDataSource().invalidate();
}
void retry() {
subredditListingDataSourceFactory.getSubredditListingDataSource().retry();
}
void retryLoadingMore() {
subredditListingDataSourceFactory.getSubredditListingDataSource().retryLoadingMore();
}

View File

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

View File

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

View File

@ -20,7 +20,7 @@ public class UserListingViewModel extends ViewModel {
private LiveData<PagedList<UserData>> users;
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);
initialLoadingState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(),
@ -65,10 +65,6 @@ public class UserListingViewModel extends ViewModel {
userListingDataSourceFactory.getUserListingDataSource().invalidate();
}
void retry() {
userListingDataSourceFactory.getUserListingDataSource().retry();
}
void retryLoadingMore() {
userListingDataSourceFactory.getUserListingDataSource().retryLoadingMore();
}

View File

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