From 3858220937f16f0da2843bb86ef17bac9ba1bd37 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 26 May 2023 12:05:18 +0200 Subject: [PATCH] Add a fallback when remote profiles are not working --- app/build.gradle | 3 +- .../mastodon/helper/CrossActionHelper.java | 2 +- .../timeline/FragmentMastodonAccount.java | 5 +- .../timeline/FragmentMastodonTimeline.java | 97 +++++++++++-------- 4 files changed, 61 insertions(+), 46 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 37c31f3e..1adde255 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -25,6 +25,7 @@ android { } debug { applicationIdSuffix '.debug' + pseudoLocalesEnabled true } } compileOptions { @@ -99,7 +100,7 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' - implementation 'com.google.android.material:material:1.9.0-beta01' + implementation 'com.google.android.material:material:1.9.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' diff --git a/app/src/main/java/app/fedilab/android/mastodon/helper/CrossActionHelper.java b/app/src/main/java/app/fedilab/android/mastodon/helper/CrossActionHelper.java index b3a8867e..c5633512 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/helper/CrossActionHelper.java +++ b/app/src/main/java/app/fedilab/android/mastodon/helper/CrossActionHelper.java @@ -508,7 +508,7 @@ public class CrossActionHelper { } } } - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonAccount.java b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonAccount.java index 3acf80e2..55879749 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonAccount.java +++ b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonAccount.java @@ -133,8 +133,9 @@ public class FragmentMastodonAccount extends Fragment { if (results != null && results.accounts.size() > 0) { remoteAccountId = results.accounts.get(0).id; fetchAccount(firstLoad, remoteAccountId); - } else { - Toasty.error(requireActivity(), getString(R.string.toast_error), Toasty.LENGTH_SHORT).show(); + } else { //FallBack the app failed to find remote accounts + checkRemotely = false; + fetchAccount(firstLoad, accountTimeline != null ? accountTimeline.id : null); } }); } else { diff --git a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonTimeline.java b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonTimeline.java index ad1e04ba..b0b1a0ae 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonTimeline.java +++ b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/timeline/FragmentMastodonTimeline.java @@ -1036,15 +1036,15 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. } else if (timelineType == Timeline.TimeLineEnum.TAG || timelineType == Timeline.TimeLineEnum.ART) { //TAG TIMELINE routeCommon(direction, fetchingMissing, fetchStatus); } else if (timelineType == Timeline.TimeLineEnum.ACCOUNT_TIMELINE) { //PROFILE TIMELINES - String tempToken; - String tempInstance; - String accountId; + final String[] tempToken = new String[1]; + final String[] tempInstance = new String[1]; + final String[] accountId = new String[1]; if (checkRemotely) { - tempToken = null; - tempInstance = remoteInstance; - accountId = accountIDInRemoteInstance; + tempToken[0] = null; + tempInstance[0] = remoteInstance; + accountId[0] = accountIDInRemoteInstance; if (accountIDInRemoteInstance == null) { - CrossActionHelper.fetchAccountInRemoteInstance(requireActivity(), accountTimeline.acct, tempInstance, new CrossActionHelper.Callback() { + CrossActionHelper.fetchAccountInRemoteInstance(requireActivity(), accountTimeline.acct, tempInstance[0], new CrossActionHelper.Callback() { @Override public void federatedStatus(Status status) { } @@ -1053,8 +1053,8 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. public void federatedAccount(Account account) { if (account != null && isAdded() && !requireActivity().isFinishing()) { accountIDInRemoteInstance = account.id; - accountsVM.getAccountStatuses(tempInstance, null, accountIDInRemoteInstance, null, null, null, null, null, false, true, MastodonHelper.statusesPerCall(requireActivity())) - .observe(getViewLifecycleOwner(), pinnedStatuses -> accountsVM.getAccountStatuses(tempInstance, null, accountIDInRemoteInstance, null, null, null, exclude_replies, exclude_reblogs, media_only, false, MastodonHelper.statusesPerCall(requireActivity())) + accountsVM.getAccountStatuses(tempInstance[0], null, accountIDInRemoteInstance, null, null, null, null, null, false, true, MastodonHelper.statusesPerCall(requireActivity())) + .observe(getViewLifecycleOwner(), pinnedStatuses -> accountsVM.getAccountStatuses(tempInstance[0], null, accountIDInRemoteInstance, null, null, null, exclude_replies, exclude_reblogs, media_only, false, MastodonHelper.statusesPerCall(requireActivity())) .observe(getViewLifecycleOwner(), otherStatuses -> { if (otherStatuses != null && otherStatuses.statuses != null) { if (pinnedStatuses != null && pinnedStatuses.statuses != null) { @@ -1067,47 +1067,25 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. initializeStatusesCommonView(otherStatuses); })); } else { - if (isAdded() && !requireActivity().isFinishing()) { - Toasty.error(requireActivity(), getString(R.string.toast_fetch_error), Toasty.LENGTH_LONG).show(); - } + tempToken[0] = MainActivity.currentToken; + tempInstance[0] = currentInstance; + accountId[0] = accountTimeline.id; + displayStatuses(direction, accountId[0], tempInstance[0], tempToken[0], fetchStatus); } } }); } else { - accountId = accountIDInRemoteInstance; + accountId[0] = accountIDInRemoteInstance; } } else { - tempToken = MainActivity.currentToken; - tempInstance = currentInstance; - accountId = accountTimeline.id; + tempToken[0] = MainActivity.currentToken; + tempInstance[0] = currentInstance; + accountId[0] = accountTimeline.id; } - if (accountId == null) { - accountId = accountTimeline.id; - } - if (direction == null && !checkRemotely) { - if (show_pinned) { - //Fetch pinned statuses to display them at the top - accountsVM.getAccountStatuses(currentInstance, MainActivity.currentToken, accountId, null, null, null, null, null, false, true, MastodonHelper.statusesPerCall(requireActivity())) - .observe(getViewLifecycleOwner(), pinnedStatuses -> accountsVM.getAccountStatuses(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, accountTimeline.id, null, null, null, exclude_replies, exclude_reblogs, media_only, false, MastodonHelper.statusesPerCall(requireActivity())) - .observe(getViewLifecycleOwner(), otherStatuses -> { - if (otherStatuses != null && otherStatuses.statuses != null && pinnedStatuses != null && pinnedStatuses.statuses != null) { - for (Status status : pinnedStatuses.statuses) { - status.pinned = true; - } - otherStatuses.statuses.addAll(0, pinnedStatuses.statuses); - initializeStatusesCommonView(otherStatuses); - } - })); - } else { - accountsVM.getAccountStatuses(tempInstance, tempToken, accountId, null, null, null, exclude_replies, exclude_reblogs, media_only, false, MastodonHelper.statusesPerCall(requireActivity())) - .observe(getViewLifecycleOwner(), this::initializeStatusesCommonView); - } - } else if (direction == DIRECTION.BOTTOM) { - accountsVM.getAccountStatuses(tempInstance, tempToken, accountId, max_id, null, null, exclude_replies, exclude_reblogs, media_only, false, MastodonHelper.statusesPerCall(requireActivity())) - .observe(getViewLifecycleOwner(), statusesBottom -> dealWithPagination(statusesBottom, DIRECTION.BOTTOM, false, true, fetchStatus)); - } else { - flagLoading = false; + if (accountId[0] == null) { + accountId[0] = accountTimeline.id; } + displayStatuses(direction, accountId[0], tempInstance[0], tempToken[0], fetchStatus); } else if (search != null) { SearchVM searchVM = new ViewModelProvider(FragmentMastodonTimeline.this).get(viewModelKey, SearchVM.class); if (direction == null) { @@ -1189,7 +1167,42 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. flagLoading = false; } } + } + /** + * FallBack when remote profile is enabled + * + * @param direction - DIRECTION + * @param accountId - String account id + * @param tempInstance - Used instance + * @param tempToken - Token + * @param fetchStatus - Status fetched + */ + private void displayStatuses(DIRECTION direction, String accountId, String tempInstance, String tempToken, Status fetchStatus) { + if (direction == null && !checkRemotely) { + if (show_pinned) { + //Fetch pinned statuses to display them at the top + accountsVM.getAccountStatuses(currentInstance, MainActivity.currentToken, accountId, null, null, null, null, null, false, true, MastodonHelper.statusesPerCall(requireActivity())) + .observe(getViewLifecycleOwner(), pinnedStatuses -> accountsVM.getAccountStatuses(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, accountTimeline.id, null, null, null, exclude_replies, exclude_reblogs, media_only, false, MastodonHelper.statusesPerCall(requireActivity())) + .observe(getViewLifecycleOwner(), otherStatuses -> { + if (otherStatuses != null && otherStatuses.statuses != null && pinnedStatuses != null && pinnedStatuses.statuses != null) { + for (Status status : pinnedStatuses.statuses) { + status.pinned = true; + } + otherStatuses.statuses.addAll(0, pinnedStatuses.statuses); + initializeStatusesCommonView(otherStatuses); + } + })); + } else { + accountsVM.getAccountStatuses(tempInstance, tempToken, accountId, null, null, null, exclude_replies, exclude_reblogs, media_only, false, MastodonHelper.statusesPerCall(requireActivity())) + .observe(getViewLifecycleOwner(), this::initializeStatusesCommonView); + } + } else if (direction == DIRECTION.BOTTOM) { + accountsVM.getAccountStatuses(tempInstance, tempToken, accountId, max_id, null, null, exclude_replies, exclude_reblogs, media_only, false, MastodonHelper.statusesPerCall(requireActivity())) + .observe(getViewLifecycleOwner(), statusesBottom -> dealWithPagination(statusesBottom, DIRECTION.BOTTOM, false, true, fetchStatus)); + } else { + flagLoading = false; + } } /**