From fbb5c244aee2c1e394a0be247c340547226353c5 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Wed, 17 Mar 2021 15:09:00 -0400 Subject: [PATCH] patrol certain error strings --- .../GifPickerBottomDialogFragment.java | 5 ++- .../fragments/UserSearchFragment.java | 3 ++ .../DirectMessageInboxFragment.java | 3 ++ .../DirectMessageSettingsFragment.java | 15 +++++++++ .../DirectMessageThreadFragment.java | 9 ++++++ .../instagrabber/managers/InboxManager.java | 10 +++--- .../instagrabber/managers/ThreadManager.java | 31 +++++++++---------- .../awais/instagrabber/models/Resource.java | 16 +++++++--- .../viewmodels/GifPickerViewModel.java | 5 +-- .../viewmodels/UserSearchViewModel.java | 7 +++-- app/src/main/res/values/strings.xml | 7 +++++ 11 files changed, 79 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/dialogs/GifPickerBottomDialogFragment.java b/app/src/main/java/awais/instagrabber/dialogs/GifPickerBottomDialogFragment.java index a7ab61e4..3d322081 100644 --- a/app/src/main/java/awais/instagrabber/dialogs/GifPickerBottomDialogFragment.java +++ b/app/src/main/java/awais/instagrabber/dialogs/GifPickerBottomDialogFragment.java @@ -131,7 +131,10 @@ public class GifPickerBottomDialogFragment extends BottomSheetDialogFragment { case ERROR: final Context context = getContext(); if (context != null && imagesResource.message != null) { - Snackbar.make(context, binding.getRoot(), imagesResource.message, Snackbar.LENGTH_LONG); + Snackbar.make(context, binding.getRoot(), imagesResource.message, Snackbar.LENGTH_LONG).show(); + } + if (context != null && imagesResource.resId != 0) { + Snackbar.make(context, binding.getRoot(), getString(imagesResource.resId), Snackbar.LENGTH_LONG).show(); } break; case LOADING: diff --git a/app/src/main/java/awais/instagrabber/fragments/UserSearchFragment.java b/app/src/main/java/awais/instagrabber/fragments/UserSearchFragment.java index 1513edbe..4fa33649 100644 --- a/app/src/main/java/awais/instagrabber/fragments/UserSearchFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/UserSearchFragment.java @@ -204,6 +204,9 @@ public class UserSearchFragment extends Fragment { if (results.message != null) { Snackbar.make(binding.getRoot(), results.message, Snackbar.LENGTH_LONG).show(); } + if (results.resId != 0) { + Snackbar.make(binding.getRoot(), results.resId, Snackbar.LENGTH_LONG).show(); + } if (results.data != null) { resultsAdapter.submitList(results.data); } diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java index fb4d2768..5c62d592 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java @@ -193,6 +193,9 @@ public class DirectMessageInboxFragment extends Fragment implements SwipeRefresh if (inboxResource.message != null) { Snackbar.make(binding.getRoot(), inboxResource.message, Snackbar.LENGTH_LONG).show(); } + if (inboxResource.resId != 0) { + Snackbar.make(binding.getRoot(), inboxResource.resId, Snackbar.LENGTH_LONG).show(); + } binding.swipeRefreshLayout.setRefreshing(false); break; case LOADING: diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.java index 13e28c3d..1ca1552a 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.java @@ -329,6 +329,9 @@ public class DirectMessageSettingsFragment extends Fragment implements ConfirmDi if (resource.message != null) { Snackbar.make(binding.getRoot(), resource.message, Snackbar.LENGTH_LONG).show(); } + if (resource.resId != 0) { + Snackbar.make(binding.getRoot(), resource.resId, Snackbar.LENGTH_LONG).show(); + } break; case LOADING: buttonView.setEnabled(false); @@ -427,6 +430,9 @@ public class DirectMessageSettingsFragment extends Fragment implements ConfirmDi if (resource.message != null) { Snackbar.make(binding.getRoot(), resource.message, Snackbar.LENGTH_LONG).show(); } + if (resource.resId != 0) { + Snackbar.make(binding.getRoot(), resource.resId, Snackbar.LENGTH_LONG).show(); + } break; } }); @@ -450,6 +456,9 @@ public class DirectMessageSettingsFragment extends Fragment implements ConfirmDi if (resource.message != null) { Snackbar.make(binding.getRoot(), resource.message, Snackbar.LENGTH_LONG).show(); } + if (resource.resId != 0) { + Snackbar.make(binding.getRoot(), resource.resId, Snackbar.LENGTH_LONG).show(); + } break; } pendingUsersAdapter.notifyItemChanged(position); @@ -477,6 +486,9 @@ public class DirectMessageSettingsFragment extends Fragment implements ConfirmDi if (resource.message != null) { Snackbar.make(binding.getRoot(), resource.message, Snackbar.LENGTH_LONG).show(); } + if (resource.resId != 0) { + Snackbar.make(binding.getRoot(), resource.resId, Snackbar.LENGTH_LONG).show(); + } break; case LOADING: binding.leave.setEnabled(false); @@ -497,6 +509,9 @@ public class DirectMessageSettingsFragment extends Fragment implements ConfirmDi if (resource.message != null) { Snackbar.make(binding.getRoot(), resource.message, Snackbar.LENGTH_LONG).show(); } + if (resource.resId != 0) { + Snackbar.make(binding.getRoot(), resource.resId, Snackbar.LENGTH_LONG).show(); + } break; case LOADING: binding.end.setEnabled(false); diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java index 82cb2d6f..7edbe49c 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java @@ -612,6 +612,9 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact if (fetchingResource.message != null) { Snackbar.make(binding.getRoot(), fetchingResource.message, Snackbar.LENGTH_LONG).show(); } + if (fetchingResource.resId != 0) { + Snackbar.make(binding.getRoot(), fetchingResource.resId, Snackbar.LENGTH_LONG).show(); + } break; case LOADING: setTitle(getString(R.string.dms_thread_updating)); @@ -750,6 +753,9 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact if (resource.message != null) { Snackbar.make(binding.getRoot(), resource.message, Snackbar.LENGTH_LONG).show(); } + if (resource.resId != 0) { + Snackbar.make(binding.getRoot(), resource.resId, Snackbar.LENGTH_LONG).show(); + } resourceLiveData.removeObservers(getViewLifecycleOwner()); break; } @@ -1141,6 +1147,9 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact if (resource.message != null) { Snackbar.make(binding.getRoot(), resource.message, Snackbar.LENGTH_LONG).show(); } + if (resource.resId != 0) { + Snackbar.make(binding.getRoot(), resource.resId, Snackbar.LENGTH_LONG).show(); + } resourceLiveData.removeObservers(getViewLifecycleOwner()); break; } diff --git a/app/src/main/java/awais/instagrabber/managers/InboxManager.java b/app/src/main/java/awais/instagrabber/managers/InboxManager.java index a41f0fc9..70457e03 100644 --- a/app/src/main/java/awais/instagrabber/managers/InboxManager.java +++ b/app/src/main/java/awais/instagrabber/managers/InboxManager.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import awais.instagrabber.R; import awais.instagrabber.models.Resource; import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.directmessages.DirectBadgeCount; @@ -156,7 +157,7 @@ public final class InboxManager { final DirectBadgeCount directBadgeCount = response.body(); if (directBadgeCount == null) { Log.e(TAG, "onResponse: directBadgeCount Response is null"); - unseenCount.postValue(Resource.error("Unseen count response is null", getCurrentUnseenCount())); + unseenCount.postValue(Resource.error(R.string.dms_inbox_error_null_count, getCurrentUnseenCount())); return; } unseenCount.postValue(Resource.success(directBadgeCount.getBadgeCount())); @@ -188,14 +189,13 @@ public final class InboxManager { private void parseInboxResponse(final DirectInboxResponse response) { if (response == null) { Log.e(TAG, "parseInboxResponse: Response is null"); - inbox.postValue(Resource.error("Response is null", getCurrentDirectInbox())); + inbox.postValue(Resource.error(R.string.generic_null_response, getCurrentDirectInbox())); hasOlder = false; return; } if (!response.getStatus().equals("ok")) { - final String msg = "DM inbox fetch response: status not ok"; - Log.e(TAG, msg); - inbox.postValue(Resource.error(msg, getCurrentDirectInbox())); + Log.e(TAG, "DM inbox fetch response: status not ok"); + inbox.postValue(Resource.error(R.string.generic_not_ok_response, getCurrentDirectInbox())); hasOlder = false; return; } diff --git a/app/src/main/java/awais/instagrabber/managers/ThreadManager.java b/app/src/main/java/awais/instagrabber/managers/ThreadManager.java index 0f8678e2..6e7de7ab 100644 --- a/app/src/main/java/awais/instagrabber/managers/ThreadManager.java +++ b/app/src/main/java/awais/instagrabber/managers/ThreadManager.java @@ -32,6 +32,7 @@ import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; +import awais.instagrabber.R; import awais.instagrabber.customviews.emoji.Emoji; import awais.instagrabber.models.Resource; import awais.instagrabber.models.Resource.Status; @@ -83,10 +84,6 @@ import static awais.instagrabber.utils.Utils.settingsHelper; public final class ThreadManager { private static final String TAG = ThreadManager.class.getSimpleName(); private static final Object LOCK = new Object(); - private static final String ERROR_INVALID_USER = "Invalid user"; - private static final String ERROR_RESPONSE_NOT_OK = "Response status from server was not ok"; - private static final String ERROR_VIDEO_TOO_LONG = "Instagram does not allow uploading videos longer than 60 secs for Direct messages"; - private static final String ERROR_AUDIO_TOO_LONG = "Instagram does not allow uploading audio longer than 60 secs"; private static final Map INSTANCE_MAP = new ConcurrentHashMap<>(); private final MutableLiveData> fetching = new MutableLiveData<>(); @@ -363,12 +360,12 @@ public final class ThreadManager { public void onResponse(@NonNull final Call call, @NonNull final Response response) { final DirectThreadFeedResponse feedResponse = response.body(); if (feedResponse == null) { - fetching.postValue(Resource.error("response was null!", null)); + fetching.postValue(Resource.error(R.string.generic_null_response, null)); Log.e(TAG, "onResponse: response was null!"); return; } if (!feedResponse.getStatus().equals("ok")) { - fetching.postValue(Resource.error("response was not ok", null)); + fetching.postValue(Resource.error(R.string.generic_not_ok_response, null)); return; } final DirectThread thread = feedResponse.getThread(); @@ -674,7 +671,7 @@ public final class ThreadManager { final long byteLength) { if (duration > 60000) { // instagram does not allow uploading audio longer than 60 secs for Direct messages - data.postValue(Resource.error(ERROR_AUDIO_TOO_LONG, null)); + data.postValue(Resource.error(R.string.dms_ERROR_AUDIO_TOO_LONG, null)); return; } final Long userId = getCurrentUserId(data); @@ -794,7 +791,7 @@ public final class ThreadManager { handleErrorBody(call, response, data); return; } - data.postValue(Resource.error("request was not successful and response error body was null", item)); + data.postValue(Resource.error(R.string.generic_failed_request, item)); } @Override @@ -1056,7 +1053,7 @@ public final class ThreadManager { final int height) { if (duration > 60000) { // instagram does not allow uploading videos longer than 60 secs for Direct messages - data.postValue(Resource.error(ERROR_VIDEO_TOO_LONG, null)); + data.postValue(Resource.error(R.string.dms_ERROR_VIDEO_TOO_LONG, null)); return; } final Long userId = getCurrentUserId(data); @@ -1125,7 +1122,7 @@ public final class ThreadManager { if (response.isSuccessful()) { final DirectThreadBroadcastResponse broadcastResponse = response.body(); if (broadcastResponse == null) { - data.postValue(Resource.error("Response was null from server", directItem)); + data.postValue(Resource.error(R.string.generic_null_response, directItem)); Log.e(TAG, "enqueueRequest: onResponse: response body is null"); return; } @@ -1155,7 +1152,7 @@ public final class ThreadManager { if (response.errorBody() != null) { handleErrorBody(call, response, data); } - data.postValue(Resource.error("request was not successful and response error body was null", directItem)); + data.postValue(Resource.error(R.string.generic_failed_request, directItem)); } @Override @@ -1215,12 +1212,12 @@ public final class ThreadManager { @NonNull final MediaUploader.MediaUploadResponse response) { final JSONObject responseJson = response.getResponse(); if (responseJson == null || response.getResponseCode() != HttpURLConnection.HTTP_OK) { - data.postValue(Resource.error(ERROR_RESPONSE_NOT_OK, null)); + data.postValue(Resource.error(R.string.generic_not_ok_response, null)); return true; } final String status = responseJson.optString("status"); if (TextUtils.isEmpty(status) || !status.equals("ok")) { - data.postValue(Resource.error(ERROR_RESPONSE_NOT_OK, null)); + data.postValue(Resource.error(R.string.generic_not_ok_response, null)); return true; } return false; @@ -1253,12 +1250,12 @@ public final class ThreadManager { handleErrorBody(call, response, data); return; } - data.postValue(Resource.error("request was not successful and response error body was null", item)); + data.postValue(Resource.error(R.string.generic_failed_request, item)); return; } final DirectThreadBroadcastResponse body = response.body(); if (body == null) { - data.postValue(Resource.error("Response is null!", item)); + data.postValue(Resource.error(R.string.generic_null_response, item)); } // otherwise nothing to do? maybe update the timestamp in the emoji? } @@ -1282,7 +1279,7 @@ public final class ThreadManager { @Nullable private Long getCurrentUserId(final MutableLiveData> data) { if (currentUser == null || currentUser.getPk() <= 0) { - data.postValue(Resource.error(ERROR_INVALID_USER, null)); + data.postValue(Resource.error(R.string.dms_ERROR_INVALID_USER, null)); return null; } return currentUser.getPk(); @@ -1777,7 +1774,7 @@ public final class ThreadManager { } final DirectThreadDetailsChangeResponse changeResponse = response.body(); if (changeResponse == null) { - data.postValue(Resource.error("Response is null", null)); + data.postValue(Resource.error(R.string.generic_null_response, null)); return; } data.postValue(Resource.success(new Object())); diff --git a/app/src/main/java/awais/instagrabber/models/Resource.java b/app/src/main/java/awais/instagrabber/models/Resource.java index f5dc5e7b..da93a0ea 100644 --- a/app/src/main/java/awais/instagrabber/models/Resource.java +++ b/app/src/main/java/awais/instagrabber/models/Resource.java @@ -9,28 +9,36 @@ public class Resource { public final Status status; public final T data; public final String message; + public final int resId; private Resource(@NonNull Status status, @Nullable T data, - @Nullable String message) { + @Nullable String message, + int resId) { this.status = status; this.data = data; this.message = message; + this.resId = resId; } @NonNull public static Resource success(@NonNull T data) { - return new Resource<>(Status.SUCCESS, data, null); + return new Resource<>(Status.SUCCESS, data, null, 0); } @NonNull public static Resource error(String msg, @Nullable T data) { - return new Resource<>(Status.ERROR, data, msg); + return new Resource<>(Status.ERROR, data, msg, 0); + } + + @NonNull + public static Resource error(int resId, @Nullable T data) { + return new Resource<>(Status.ERROR, data, null, resId); } @NonNull public static Resource loading(@Nullable T data) { - return new Resource<>(Status.LOADING, data, null); + return new Resource<>(Status.LOADING, data, null, 0); } @Override diff --git a/app/src/main/java/awais/instagrabber/viewmodels/GifPickerViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/GifPickerViewModel.java index e78b19db..dd014204 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/GifPickerViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/GifPickerViewModel.java @@ -14,6 +14,7 @@ import java.util.Collections; import java.util.List; import java.util.Locale; +import awais.instagrabber.R; import awais.instagrabber.models.Resource; import awais.instagrabber.repositories.responses.giphy.GiphyGif; import awais.instagrabber.repositories.responses.giphy.GiphyGifResponse; @@ -70,7 +71,7 @@ public class GifPickerViewModel extends ViewModel { Log.e(TAG, "onResponse: ", e); } } - images.postValue(Resource.error("request was not successful and response error body was null", getCurrentImages())); + images.postValue(Resource.error(R.string.generic_failed_request, getCurrentImages())); } @Override @@ -85,7 +86,7 @@ public class GifPickerViewModel extends ViewModel { private void parseResponse(final Response response) { final GiphyGifResponse giphyGifResponse = response.body(); if (giphyGifResponse == null) { - images.postValue(Resource.error("Response body was null", getCurrentImages())); + images.postValue(Resource.error(R.string.generic_null_response, getCurrentImages())); return; } final GiphyGifResults results = giphyGifResponse.getResults(); diff --git a/app/src/main/java/awais/instagrabber/viewmodels/UserSearchViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/UserSearchViewModel.java index e74c36c6..1e77c960 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/UserSearchViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/UserSearchViewModel.java @@ -20,6 +20,7 @@ import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; +import awais.instagrabber.R; import awais.instagrabber.fragments.UserSearchFragment; import awais.instagrabber.models.Resource; import awais.instagrabber.repositories.responses.User; @@ -205,7 +206,7 @@ public class UserSearchViewModel extends ViewModel { } final RankedRecipientsResponse rankedRecipientsResponse = response.body(); if (rankedRecipientsResponse == null) { - recipients.postValue(Resource.error("Response is null!", getCachedRecipients())); + recipients.postValue(Resource.error(R.string.generic_null_response, getCachedRecipients())); searchRequest = null; return; } @@ -234,7 +235,7 @@ public class UserSearchViewModel extends ViewModel { } final UserSearchResponse userSearchResponse = response.body(); if (userSearchResponse == null) { - recipients.postValue(Resource.error("Response is null!", getCachedRecipients())); + recipients.postValue(Resource.error(R.string.generic_null_response, getCachedRecipients())); searchRequest = null; return; } @@ -286,7 +287,7 @@ public class UserSearchViewModel extends ViewModel { final ResponseBody errorBody = response.errorBody(); if (errorBody == null) { if (updateResource) { - recipients.postValue(Resource.error("Request failed!", getCachedRecipients())); + recipients.postValue(Resource.error(R.string.generic_failed_request, getCachedRecipients())); } return; } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5404c0fe..8296c069 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -211,6 +211,7 @@ Suggested Screenshotted Cannot deliver + Unseen count response is null! Message... Press and hold to record audio Updating... @@ -219,6 +220,9 @@ Leave this chat? Kick Left users + Invalid user + Instagram does not allow uploading videos longer than 60 secs for DM. + Instagram does not allow uploading audio longer than 60 secs. Download directly Downloads posts directly to the phone! Fetching post(s) @@ -494,4 +498,7 @@ secs mins Search GIPHY + Response is null! + Response status is not ok! + Request failed!