From 6bca4462a3f63b59c6ba4fb88f1e72398b4a4fac Mon Sep 17 00:00:00 2001 From: Daniele Date: Fri, 10 Sep 2021 19:02:28 +0200 Subject: [PATCH] Connection error management --- .../activity/detail/GeofavoriteDetailActivity.java | 13 +++++++++++-- .../nextcloudmaps/activity/main/MainActivity.java | 6 +++++- .../nextcloudmaps/activity/main/MainPresenter.java | 5 +++++ app/src/main/res/values/strings.xml | 1 + 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/detail/GeofavoriteDetailActivity.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/detail/GeofavoriteDetailActivity.java index b9bed57..5a6396d 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/detail/GeofavoriteDetailActivity.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/detail/GeofavoriteDetailActivity.java @@ -113,17 +113,26 @@ public class GeofavoriteDetailActivity extends AppCompatActivity implements Loca call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { - finish(); + if (response.isSuccessful()) + finish(); + else + onGeofavoriteSaveFailed(); } @Override public void onFailure(Call call, Throwable t) { - Toast.makeText(GeofavoriteDetailActivity.this, R.string.error_saving_geofavorite, Toast.LENGTH_SHORT).show(); + onGeofavoriteSaveFailed(); Log.e(TAG, "Unable to update geofavorite: " + t.getMessage()); } }); } + private void onGeofavoriteSaveFailed() { + runOnUiThread(() -> + Toast.makeText(GeofavoriteDetailActivity.this, R.string.error_saving_geofavorite, Toast.LENGTH_SHORT).show() + ); + } + /** * Obtains the current location (requesting user's permission, if necessary) * and calls updateLocationField() diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainActivity.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainActivity.java index 19e2912..577de6d 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainActivity.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainActivity.java @@ -22,6 +22,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.text.TextUtils; +import android.util.Log; import android.widget.Toast; import androidx.appcompat.app.AlertDialog; @@ -65,6 +66,8 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti private static final int INTENT_ADD = 100; private static final int INTENT_EDIT = 200; + private static final String TAG = "MainActivity"; + private static final String NAVIGATION_KEY_ADD_GEOFAVORITE = "add"; private static final String NAVIGATION_KEY_SHOW_ABOUT = "about"; private static final String NAVIGATION_KEY_SWITCH_ACCOUNT = "switch_account"; @@ -292,7 +295,8 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti @Override public void onErrorLoading(String message) { - Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show(); + Toast.makeText(MainActivity.this, R.string.list_geofavorite_connection_error, Toast.LENGTH_LONG).show(); + Log.e(TAG, "Unable to obtain geofavorites list: " + message); } @Override diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainPresenter.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainPresenter.java index 30d25b4..51112c9 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainPresenter.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainPresenter.java @@ -50,6 +50,11 @@ public class MainPresenter { view.hideLoading(); if (response.isSuccessful() && response.body() != null) { view.onGetResult(response.body()); + } else { + ((AppCompatActivity) view).runOnUiThread(() -> { + view.hideLoading(); + view.onErrorLoading(response.raw().message()); + }); } }); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 79994d1..e324793 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -38,6 +38,7 @@ Delete Maintain Geofavorite deleted + Unable to obtain geofavorites list Sort by