From 9298f22b5d159de01d0ecdeaf2674b499868d394 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 19 Nov 2022 11:58:58 +0100 Subject: [PATCH] Fix issue #482 - Warn when the app fails to add account into a list --- .../android/activities/ProfileActivity.java | 12 ++++++++++-- .../endpoints/MastodonTimelinesService.java | 2 +- .../android/viewmodel/mastodon/TimelinesVM.java | 15 ++++++++++++--- app/src/main/res/values/strings.xml | 1 + 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java b/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java index e62e6fd1b..4a1537991 100644 --- a/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java @@ -857,14 +857,22 @@ public class ProfileActivity extends BaseActivity { relationship = newRelationShip; updateAccount(); if (isChecked) { - timelinesVM.addAccountsList(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, listsId[which], userIds); + timelinesVM.addAccountsList(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, listsId[which], userIds).observe(ProfileActivity.this, success -> { + if (!success) { + Toasty.error(ProfileActivity.this, getString(R.string.toast_error_add_to_list), Toast.LENGTH_LONG).show(); + } + }); } else { timelinesVM.deleteAccountsList(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, listsId[which], userIds); } }); } else { if (isChecked) { - timelinesVM.addAccountsList(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, listsId[which], userIds); + timelinesVM.addAccountsList(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, listsId[which], userIds).observe(ProfileActivity.this, success -> { + if (!success) { + Toasty.error(ProfileActivity.this, getString(R.string.toast_error_add_to_list), Toast.LENGTH_LONG).show(); + } + }); } else { timelinesVM.deleteAccountsList(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, listsId[which], userIds); } diff --git a/app/src/main/java/app/fedilab/android/client/endpoints/MastodonTimelinesService.java b/app/src/main/java/app/fedilab/android/client/endpoints/MastodonTimelinesService.java index 7fdf07447..e09c70c63 100644 --- a/app/src/main/java/app/fedilab/android/client/endpoints/MastodonTimelinesService.java +++ b/app/src/main/java/app/fedilab/android/client/endpoints/MastodonTimelinesService.java @@ -179,7 +179,7 @@ public interface MastodonTimelinesService { //Add account in a list @FormUrlEncoded @POST("lists/{id}/accounts") - Call addAccountsList( + Call addAccountsList( @Header("Authorization") String token, @Path("id") String id, @Field("account_ids[]") List account_ids diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java index 124bb8ec0..19e5b498f 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java @@ -74,6 +74,7 @@ public class TimelinesVM extends AndroidViewModel { private MutableLiveData> accountListMutableLiveData; + private MutableLiveData booleanMutableLiveData; private MutableLiveData> statusDraftListMutableLiveData; private MutableLiveData statusMutableLiveData; private MutableLiveData statusesMutableLiveData; @@ -838,18 +839,26 @@ public class TimelinesVM extends AndroidViewModel { * @param listId ID of the list * @param accountIds Array of account IDs to add to the list. */ - public void addAccountsList(@NonNull String instance, String token, @NonNull String listId, @NonNull List accountIds) { + public LiveData addAccountsList(@NonNull String instance, String token, @NonNull String listId, @NonNull List accountIds) { MastodonTimelinesService mastodonTimelinesService = init(instance); + booleanMutableLiveData = new MutableLiveData<>(); new Thread(() -> { - Call addAccountsListCall = mastodonTimelinesService.addAccountsList(token, listId, accountIds); + Call addAccountsListCall = mastodonTimelinesService.addAccountsList(token, listId, accountIds); + Boolean reply = null; if (addAccountsListCall != null) { try { - addAccountsListCall.execute(); + Response response = addAccountsListCall.execute(); + reply = response.isSuccessful(); } catch (Exception e) { e.printStackTrace(); } } + Handler mainHandler = new Handler(Looper.getMainLooper()); + Boolean finalReply = reply; + Runnable myRunnable = () -> booleanMutableLiveData.setValue(finalReply); + mainHandler.post(myRunnable); }).start(); + return booleanMutableLiveData; } /** diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cb7d66ad2..43ff53b15 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1898,4 +1898,5 @@ Add keyword Show anyway Filtered: %1$s + The app failed to add the account into the list! \ No newline at end of file