From 251228dcc43b30abe86f0ef62cf0e903988f913d Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Thu, 31 Dec 2020 19:35:33 -0500 Subject: [PATCH] add many fetch failure checks, close #486 --- .../instagrabber/asyncs/CommentsFetcher.java | 2 ++ .../instagrabber/asyncs/HashtagFetcher.java | 1 + .../fragments/CommentsViewerFragment.java | 7 ++++ .../fragments/FollowViewerFragment.java | 3 ++ .../fragments/HashTagFragment.java | 35 ++++++++++++------- .../fragments/LikesViewerFragment.java | 4 +-- .../NotificationsViewerFragment.java | 18 +++++++--- 7 files changed, 51 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/asyncs/CommentsFetcher.java b/app/src/main/java/awais/instagrabber/asyncs/CommentsFetcher.java index b845bf24..d9f3cb65 100755 --- a/app/src/main/java/awais/instagrabber/asyncs/CommentsFetcher.java +++ b/app/src/main/java/awais/instagrabber/asyncs/CommentsFetcher.java @@ -145,6 +145,7 @@ public final class CommentsFetcher extends AsyncTask("commentModels.size", commentModels.size())); if (BuildConfig.DEBUG) Log.e(TAG, "", e); + if (fetchListener != null) fetchListener.onFailure(e); break; } } @@ -284,6 +285,7 @@ public final class CommentsFetcher extends AsyncTask("commentModelsList.size", commentModels.size())); if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e); + if (fetchListener != null) fetchListener.onFailure(e); return null; } return commentModels; diff --git a/app/src/main/java/awais/instagrabber/asyncs/HashtagFetcher.java b/app/src/main/java/awais/instagrabber/asyncs/HashtagFetcher.java index 96ff320d..db2f5df3 100644 --- a/app/src/main/java/awais/instagrabber/asyncs/HashtagFetcher.java +++ b/app/src/main/java/awais/instagrabber/asyncs/HashtagFetcher.java @@ -89,6 +89,7 @@ public final class HashtagFetcher extends AsyncTask { if (logCollector != null) logCollector.appendException(e, LogCollector.LogFile.ASYNC_HASHTAG_FETCHER, "doInBackground"); if (BuildConfig.DEBUG) Log.e(TAG, "", e); + if (fetchListener != null) fetchListener.onFailure(e); } return result; diff --git a/app/src/main/java/awais/instagrabber/fragments/CommentsViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/CommentsViewerFragment.java index d467ef03..500ad96b 100644 --- a/app/src/main/java/awais/instagrabber/fragments/CommentsViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/CommentsViewerFragment.java @@ -97,6 +97,13 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment impl binding.swipeRefreshLayout.setRefreshing(false); stopCurrentExecutor(); } + + @Override + public void onFailure(Throwable t) { + Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show(); + binding.swipeRefreshLayout.setRefreshing(false); + stopCurrentExecutor(); + } }; private final CommentsAdapter.CommentCallback commentCallback = new CommentsAdapter.CommentCallback() { diff --git a/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.java index 86ce599a..2512cca0 100644 --- a/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.java @@ -80,6 +80,7 @@ public final class FollowViewerFragment extends Fragment implements SwipeRefresh @Override public void onFailure(final Throwable t) { binding.swipeRefreshLayout.setRefreshing(false); + Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show(); Log.e(TAG, "Error fetching list (double, following)", t); } }; @@ -105,6 +106,7 @@ public final class FollowViewerFragment extends Fragment implements SwipeRefresh @Override public void onFailure(final Throwable t) { binding.swipeRefreshLayout.setRefreshing(false); + Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show(); Log.e(TAG, "Error fetching list (double, follower)", t); } }; @@ -219,6 +221,7 @@ public final class FollowViewerFragment extends Fragment implements SwipeRefresh @Override public void onFailure(final Throwable t) { binding.swipeRefreshLayout.setRefreshing(false); + Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show(); Log.e(TAG, "Error fetching list (single)", t); } }; diff --git a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java index ac6df39d..8e30f318 100644 --- a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java @@ -54,6 +54,7 @@ import awais.instagrabber.db.entities.Favorite; import awais.instagrabber.db.repositories.FavoriteRepository; import awais.instagrabber.db.repositories.RepositoryCallback; import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment; +import awais.instagrabber.interfaces.FetchListener; import awais.instagrabber.models.FeedModel; import awais.instagrabber.models.HashtagModel; import awais.instagrabber.models.PostsLayoutPreferences; @@ -363,19 +364,27 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe private void fetchHashtagModel() { stopCurrentExecutor(); binding.swipeRefreshLayout.setRefreshing(true); - currentlyExecuting = new HashtagFetcher(hashtag.substring(1), result -> { - hashtagModel = result; - binding.swipeRefreshLayout.setRefreshing(false); - final Context context = getContext(); - if (context == null) return; - if (hashtagModel == null) { - Toast.makeText(context, R.string.error_loading_profile, Toast.LENGTH_SHORT).show(); - return; + currentlyExecuting = new HashtagFetcher(hashtag.substring(1), new FetchListener() { + @Override + public void onResult(final HashtagModel result) { + hashtagModel = result; + binding.swipeRefreshLayout.setRefreshing(false); + final Context context = getContext(); + if (context == null) return; + if (hashtagModel == null) { + Toast.makeText(context, R.string.error_loading_profile, Toast.LENGTH_SHORT).show(); + return; + } + setTitle(); + setHashtagDetails(); + setupPosts(); + fetchStories(); + } + + @Override + public void onFailure(Throwable t) { + Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show(); } - setTitle(); - setHashtagDetails(); - setupPosts(); - fetchStories(); }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } @@ -524,7 +533,7 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe public void onSuccess(final Void result) { hashtagDetailsBinding.favChip.setText(R.string.favorite_short); hashtagDetailsBinding.favChip.setChipIconResource(R.drawable.ic_star_check_24); - showSnackbar(getString(R.string.added_to_favs)); + showSnackbar(getString(R.string.added_to_favs_short)); } @Override diff --git a/app/src/main/java/awais/instagrabber/fragments/LikesViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/LikesViewerFragment.java index 315bc038..157222b8 100644 --- a/app/src/main/java/awais/instagrabber/fragments/LikesViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/LikesViewerFragment.java @@ -97,9 +97,7 @@ public final class LikesViewerFragment extends BottomSheetDialogFragment impleme NavHostFragment.findNavController(LikesViewerFragment.this).navigate(R.id.action_global_profileFragment, bundle); } }); - layoutManager = new LinearLayoutManager(getContext()); binding.rvLikes.setAdapter(likesAdapter); - binding.rvLikes.setLayoutManager(layoutManager); binding.swipeRefreshLayout.setRefreshing(false); } @@ -158,6 +156,8 @@ public final class LikesViewerFragment extends BottomSheetDialogFragment impleme binding.swipeRefreshLayout.setRefreshing(true); resources = getResources(); if (isComment && !isLoggedIn) { + layoutManager = new LinearLayoutManager(getContext()); + binding.rvLikes.setLayoutManager(layoutManager); lazyLoader = new RecyclerLazyLoader(layoutManager, (page, totalItemsCount) -> { if (!TextUtils.isEmpty(endCursor)) graphQLService.fetchCommentLikers(postId, endCursor, acb); diff --git a/app/src/main/java/awais/instagrabber/fragments/NotificationsViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/NotificationsViewerFragment.java index 56895648..34a2ba39 100644 --- a/app/src/main/java/awais/instagrabber/fragments/NotificationsViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/NotificationsViewerFragment.java @@ -36,6 +36,7 @@ import awais.instagrabber.asyncs.PostFetcher; import awais.instagrabber.databinding.FragmentNotificationsViewerBinding; import awais.instagrabber.dialogs.ProfilePicDialogFragment; import awais.instagrabber.fragments.settings.MorePreferencesFragmentDirections; +import awais.instagrabber.interfaces.FetchListener; import awais.instagrabber.interfaces.MentionClickListener; import awais.instagrabber.models.FeedModel; import awais.instagrabber.models.NotificationModel; @@ -267,9 +268,18 @@ public final class NotificationsViewerFragment extends Fragment implements Swipe binding.swipeRefreshLayout.setRefreshing(true); switch (type) { case "notif": - new NotificationsFetcher(true, notificationModels -> { - binding.swipeRefreshLayout.setRefreshing(false); - notificationViewModel.getList().postValue(notificationModels); + new NotificationsFetcher(true, new FetchListener>() { + @Override + public void onResult(final List notificationModels) { + binding.swipeRefreshLayout.setRefreshing(false); + notificationViewModel.getList().postValue(notificationModels); + } + + @Override + public void onFailure(Throwable t) { + binding.swipeRefreshLayout.setRefreshing(false); + Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show(); + } }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); break; case "ayml": @@ -284,7 +294,7 @@ public final class NotificationsViewerFragment extends Fragment implements Swipe @Override public void onFailure(final Throwable t) { binding.swipeRefreshLayout.setRefreshing(false); - Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show(); } }); break;