From 32b005599a99879c51c7aeb3cccd69e8ffd30282 Mon Sep 17 00:00:00 2001 From: "Daniele Verducci (Slimpenguin)" Date: Sat, 24 Feb 2024 07:19:55 +0100 Subject: [PATCH] User badge --- app/build.gradle | 3 ++ .../activity/main/MainActivity.java | 2 +- .../fragments/GeofavoritesFragment.java | 44 +++++++++++++++++++ app/src/main/res/drawable/user_badge_mask.xml | 15 +++++++ app/src/main/res/layout/app_toolbar.xml | 16 +++++++ app/src/main/res/values-it/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + 7 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 app/src/main/res/drawable/user_badge_mask.xml diff --git a/app/build.gradle b/app/build.gradle index c90c6ba..69a5d26 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -85,6 +85,9 @@ dependencies { // https://mvnrepository.com/artifact/commons-io/commons-io implementation 'commons-io:commons-io:2.11.0' + // Picasso (image loader) + implementation 'com.squareup.picasso:picasso:2.8' + configurations.all { resolutionStrategy { force 'commons-io:commons-io:2.11.0' 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 49d6087..63261d4 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 @@ -195,7 +195,7 @@ public class MainActivity extends NextcloudMapsStyledActivity { startActivity(new Intent(this, AboutActivity.class)); } - private void switch_account() { + public void switch_account() { ApiProvider.logout(); SingleAccountHelper.applyCurrentAccount(this, null); Intent intent = new Intent(MainActivity.this, LoginActivity.class); diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoritesFragment.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoritesFragment.java index 08fb599..4204ae1 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoritesFragment.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoritesFragment.java @@ -3,6 +3,8 @@ package it.danieleverducci.nextcloudmaps.fragments; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; +import android.os.Handler; +import android.util.Log; import android.view.View; import android.widget.Toast; @@ -10,10 +12,19 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.AppCompatImageButton; +import androidx.appcompat.widget.AppCompatImageView; import androidx.fragment.app.Fragment; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; +import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException; +import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException; +import com.nextcloud.android.sso.helper.SingleAccountHelper; +import com.nextcloud.android.sso.model.SingleSignOnAccount; +import com.squareup.picasso.Picasso; + +import java.io.File; + import it.danieleverducci.nextcloudmaps.R; import it.danieleverducci.nextcloudmaps.activity.detail.GeofavoriteDetailActivity; import it.danieleverducci.nextcloudmaps.activity.main.GeofavoritesFragmentViewModel; @@ -26,6 +37,7 @@ import it.danieleverducci.nextcloudmaps.utils.IntentGenerator; * to communicate with the activity */ public abstract class GeofavoritesFragment extends Fragment { + private final String TAG = "GeofavoritesFragment"; protected GeofavoritesFragmentViewModel mGeofavoritesFragmentViewModel; @@ -54,6 +66,24 @@ public abstract class GeofavoritesFragment extends Fragment { // Set views AppCompatImageButton menuButton = view.findViewById(R.id.menu_button); menuButton.setOnClickListener(v -> ((MainActivity)requireActivity()).openDrawer()); + + View userBadgeContainer = view.findViewById(R.id.user_badge_container); + userBadgeContainer.setOnClickListener(v -> showSwitchAccountDialog()); + + // Set user badge (async) + Handler h = new Handler(); + h.post(() -> { + try { + SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(requireContext()); + String userBadgePath = ssoAccount.url + "/index.php/avatar/" + ssoAccount.userId + "/64"; + if (getActivity() != null) + getActivity().runOnUiThread( + () -> Picasso.get().load(userBadgePath).into((AppCompatImageView)userBadgeContainer.findViewById(R.id.user_badge)) + ); + } catch (Exception e) { + Log.e(TAG, "Unable to load user image: " + e.toString()); + } + }); } @Override @@ -110,4 +140,18 @@ public abstract class GeofavoritesFragment extends Fragment { ad.show(); } + private void showSwitchAccountDialog() { + AlertDialog.Builder builder = new AlertDialog.Builder(requireContext()); + builder.setMessage(R.string.dialog_logout_message) + .setTitle(R.string.dialog_logout_title) + .setPositiveButton(android.R.string.yes, (dialog, id) -> { + if (getActivity() != null) + ((MainActivity) getActivity()).switch_account(); + dialog.dismiss(); + }) + .setNegativeButton(android.R.string.no, (dialog, id) -> dialog.dismiss()); + AlertDialog ad = builder.create(); + ad.show(); + } + } diff --git a/app/src/main/res/drawable/user_badge_mask.xml b/app/src/main/res/drawable/user_badge_mask.xml new file mode 100644 index 0000000..0d36cbd --- /dev/null +++ b/app/src/main/res/drawable/user_badge_mask.xml @@ -0,0 +1,15 @@ + + + + + + + + + + diff --git a/app/src/main/res/layout/app_toolbar.xml b/app/src/main/res/layout/app_toolbar.xml index 1279674..ef84b90 100644 --- a/app/src/main/res/layout/app_toolbar.xml +++ b/app/src/main/res/layout/app_toolbar.xml @@ -67,6 +67,22 @@ android:lines="1" android:textSize="16sp" android:text="@string/search_in_all"/> + + + + + + diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index b0f6ac7..ca707f7 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -39,6 +39,8 @@ Stai per eliminare il geosegnalibro {name}. Procedere? Elimina Mantieni + Cambia account + Vuoi cambiare account? Geosegnalibro eliminato Impossibile ottenere la lista dei geosegnalibri diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 54e2c87..85685cd 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -38,6 +38,8 @@ You are about to delete geofavorite {name}. Proceed? Delete Maintain + Switch account + Do you want to switch account? Geofavorite deleted Unable to obtain geofavorites list