User badge

This commit is contained in:
Daniele Verducci (Slimpenguin) 2024-02-24 07:19:55 +01:00
parent 49e291881a
commit 32b005599a
7 changed files with 83 additions and 1 deletions

View File

@ -85,6 +85,9 @@ dependencies {
// https://mvnrepository.com/artifact/commons-io/commons-io // https://mvnrepository.com/artifact/commons-io/commons-io
implementation 'commons-io:commons-io:2.11.0' implementation 'commons-io:commons-io:2.11.0'
// Picasso (image loader)
implementation 'com.squareup.picasso:picasso:2.8'
configurations.all { configurations.all {
resolutionStrategy { resolutionStrategy {
force 'commons-io:commons-io:2.11.0' force 'commons-io:commons-io:2.11.0'

View File

@ -195,7 +195,7 @@ public class MainActivity extends NextcloudMapsStyledActivity {
startActivity(new Intent(this, AboutActivity.class)); startActivity(new Intent(this, AboutActivity.class));
} }
private void switch_account() { public void switch_account() {
ApiProvider.logout(); ApiProvider.logout();
SingleAccountHelper.applyCurrentAccount(this, null); SingleAccountHelper.applyCurrentAccount(this, null);
Intent intent = new Intent(MainActivity.this, LoginActivity.class); Intent intent = new Intent(MainActivity.this, LoginActivity.class);

View File

@ -3,6 +3,8 @@ package it.danieleverducci.nextcloudmaps.fragments;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Toast; import android.widget.Toast;
@ -10,10 +12,19 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatImageButton; import androidx.appcompat.widget.AppCompatImageButton;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider; 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.R;
import it.danieleverducci.nextcloudmaps.activity.detail.GeofavoriteDetailActivity; import it.danieleverducci.nextcloudmaps.activity.detail.GeofavoriteDetailActivity;
import it.danieleverducci.nextcloudmaps.activity.main.GeofavoritesFragmentViewModel; import it.danieleverducci.nextcloudmaps.activity.main.GeofavoritesFragmentViewModel;
@ -26,6 +37,7 @@ import it.danieleverducci.nextcloudmaps.utils.IntentGenerator;
* to communicate with the activity * to communicate with the activity
*/ */
public abstract class GeofavoritesFragment extends Fragment { public abstract class GeofavoritesFragment extends Fragment {
private final String TAG = "GeofavoritesFragment";
protected GeofavoritesFragmentViewModel mGeofavoritesFragmentViewModel; protected GeofavoritesFragmentViewModel mGeofavoritesFragmentViewModel;
@ -54,6 +66,24 @@ public abstract class GeofavoritesFragment extends Fragment {
// Set views // Set views
AppCompatImageButton menuButton = view.findViewById(R.id.menu_button); AppCompatImageButton menuButton = view.findViewById(R.id.menu_button);
menuButton.setOnClickListener(v -> ((MainActivity)requireActivity()).openDrawer()); 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 @Override
@ -110,4 +140,18 @@ public abstract class GeofavoritesFragment extends Fragment {
ad.show(); 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();
}
} }

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:innerRadius="0dp"
android:shape="oval"
android:useLevel="false">
<solid android:color="@android:color/transparent" />
<stroke
android:width="10dp"
android:color="#fff" />
</shape>
</item>
</layer-list>

View File

@ -67,6 +67,22 @@
android:lines="1" android:lines="1"
android:textSize="16sp" android:textSize="16sp"
android:text="@string/search_in_all"/> android:text="@string/search_in_all"/>
<!-- User badge -->
<FrameLayout
android:id="@+id/user_badge_container"
android:layout_width="?android:attr/actionBarSize"
android:layout_height="?android:attr/actionBarSize">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/user_badge"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/user_badge_mask"/>
</FrameLayout>
</LinearLayout> </LinearLayout>
</com.google.android.material.card.MaterialCardView> </com.google.android.material.card.MaterialCardView>

View File

@ -39,6 +39,8 @@
<string name="dialog_delete_message">Stai per eliminare il geosegnalibro {name}. Procedere?</string> <string name="dialog_delete_message">Stai per eliminare il geosegnalibro {name}. Procedere?</string>
<string name="dialog_delete_delete">Elimina</string> <string name="dialog_delete_delete">Elimina</string>
<string name="dialog_delete_cancel">Mantieni</string> <string name="dialog_delete_cancel">Mantieni</string>
<string name="dialog_logout_title">Cambia account</string>
<string name="dialog_logout_message">Vuoi cambiare account?</string>
<string name="list_geofavorite_deleted">Geosegnalibro eliminato</string> <string name="list_geofavorite_deleted">Geosegnalibro eliminato</string>
<string name="list_geofavorite_connection_error">Impossibile ottenere la lista dei geosegnalibri</string> <string name="list_geofavorite_connection_error">Impossibile ottenere la lista dei geosegnalibri</string>

View File

@ -38,6 +38,8 @@
<string name="dialog_delete_message">You are about to delete geofavorite {name}. Proceed?</string> <string name="dialog_delete_message">You are about to delete geofavorite {name}. Proceed?</string>
<string name="dialog_delete_delete">Delete</string> <string name="dialog_delete_delete">Delete</string>
<string name="dialog_delete_cancel">Maintain</string> <string name="dialog_delete_cancel">Maintain</string>
<string name="dialog_logout_title">Switch account</string>
<string name="dialog_logout_message">Do you want to switch account?</string>
<string name="list_geofavorite_deleted">Geofavorite deleted</string> <string name="list_geofavorite_deleted">Geofavorite deleted</string>
<string name="list_geofavorite_connection_error">Unable to obtain geofavorites list</string> <string name="list_geofavorite_connection_error">Unable to obtain geofavorites list</string>