Display indicator for currently active account in ManageAccountsActivity

This commit is contained in:
Stefan Niedermann 2020-06-06 11:28:10 +02:00
parent 49f373fd1e
commit 4c2a01c693
6 changed files with 85 additions and 23 deletions

View file

@ -3,14 +3,12 @@ package it.niedermann.owncloud.notes.accountswitcher;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable; import android.graphics.drawable.LayerDrawable;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
@ -19,14 +17,16 @@ import com.bumptech.glide.request.RequestOptions;
import java.util.List; import java.util.List;
import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandedDialogFragment;
import it.niedermann.owncloud.notes.databinding.DialogAccountSwitcherBinding; import it.niedermann.owncloud.notes.databinding.DialogAccountSwitcherBinding;
import it.niedermann.owncloud.notes.manageaccounts.ManageAccountsActivity; import it.niedermann.owncloud.notes.manageaccounts.ManageAccountsActivity;
import it.niedermann.owncloud.notes.model.LocalAccount; import it.niedermann.owncloud.notes.model.LocalAccount;
import it.niedermann.owncloud.notes.persistence.NotesDatabase; import it.niedermann.owncloud.notes.persistence.NotesDatabase;
import static it.niedermann.owncloud.notes.android.activity.NotesListViewActivity.manage_account; import static it.niedermann.owncloud.notes.android.activity.NotesListViewActivity.manage_account;
import static it.niedermann.owncloud.notes.branding.BrandingUtil.applyBrandToLayerDrawable;
public class AccountSwitcherDialog extends DialogFragment { public class AccountSwitcherDialog extends BrandedDialogFragment {
private static final String KEY_CURRENT_ACCOUNT_ID = "current_account_id"; private static final String KEY_CURRENT_ACCOUNT_ID = "current_account_id";
@ -69,10 +69,6 @@ public class AccountSwitcherDialog extends DialogFragment {
.into(binding.currentAccountItemAvatar); .into(binding.currentAccountItemAvatar);
binding.accountLayout.setOnClickListener((v) -> dismiss()); binding.accountLayout.setOnClickListener((v) -> dismiss());
LayerDrawable ld = (LayerDrawable) binding.check.getDrawable();
Drawable ldDrawable = ld.findDrawableByLayerId(R.id.area);
DrawableCompat.setTint(ldDrawable, currentLocalAccount.getColor());
AccountSwitcherAdapter adapter = new AccountSwitcherAdapter((localAccount -> { AccountSwitcherAdapter adapter = new AccountSwitcherAdapter((localAccount -> {
accountSwitcherListener.onAccountChosen(localAccount); accountSwitcherListener.onAccountChosen(localAccount);
dismiss(); dismiss();
@ -111,4 +107,9 @@ public class AccountSwitcherDialog extends DialogFragment {
return dialog; return dialog;
} }
@Override
public void applyBrand(int mainColor, int textColor) {
applyBrandToLayerDrawable((LayerDrawable) binding.check.getDrawable(), R.id.area, mainColor);
}
} }

View file

@ -5,11 +5,13 @@ import android.content.SharedPreferences;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.Log; import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.EditText; import android.widget.EditText;
import androidx.annotation.ColorInt; import androidx.annotation.ColorInt;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.graphics.drawable.DrawableCompat; import androidx.core.graphics.drawable.DrawableCompat;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
@ -120,4 +122,13 @@ public class BrandingUtil {
menuItem.setIcon(drawable); menuItem.setIcon(drawable);
} }
} }
public static void applyBrandToLayerDrawable(@NonNull LayerDrawable check, @IdRes int areaToColor, @ColorInt int mainColor) {
final Drawable drawable = check.findDrawableByLayerId(areaToColor);
if (drawable == null) {
Log.e(TAG, "Could not find areaToColor (" + areaToColor + "). Cannot apply brand.");
} else {
DrawableCompat.setTint(drawable, mainColor);
}
}
} }

View file

@ -16,6 +16,8 @@ import it.niedermann.owncloud.notes.model.LocalAccount;
public class ManageAccountAdapter extends RecyclerView.Adapter<ManageAccountViewHolder> { public class ManageAccountAdapter extends RecyclerView.Adapter<ManageAccountViewHolder> {
@Nullable
private LocalAccount currentLocalAccount = null;
@NonNull @NonNull
private final List<LocalAccount> localAccounts = new ArrayList<>(); private final List<LocalAccount> localAccounts = new ArrayList<>();
@NonNull @NonNull
@ -42,18 +44,22 @@ public class ManageAccountAdapter extends RecyclerView.Adapter<ManageAccountView
@Override @Override
public void onBindViewHolder(@NonNull ManageAccountViewHolder holder, int position) { public void onBindViewHolder(@NonNull ManageAccountViewHolder holder, int position) {
holder.bind(localAccounts.get(position), onAccountClick, (localAccount -> { final LocalAccount localAccount = localAccounts.get(position);
holder.bind(localAccount, (localAccountClicked) -> {
setCurrentLocalAccount(localAccountClicked);
onAccountClick.accept(localAccountClicked);
}, (localAccountToDelete -> {
if (onAccountDelete != null) { if (onAccountDelete != null) {
for (int i = 0; i < localAccounts.size(); i++) { for (int i = 0; i < localAccounts.size(); i++) {
if (localAccounts.get(i).getId() == localAccount.getId()) { if (localAccounts.get(i).getId() == localAccountToDelete.getId()) {
localAccounts.remove(i); localAccounts.remove(i);
notifyItemRemoved(i); notifyItemRemoved(i);
break; break;
} }
} }
onAccountDelete.accept(localAccount); onAccountDelete.accept(localAccountToDelete);
} }
})); }), currentLocalAccount != null && currentLocalAccount.getId() == localAccount.getId());
} }
@Override @Override
@ -66,4 +72,9 @@ public class ManageAccountAdapter extends RecyclerView.Adapter<ManageAccountView
this.localAccounts.addAll(localAccounts); this.localAccounts.addAll(localAccounts);
notifyDataSetChanged(); notifyDataSetChanged();
} }
public void setCurrentLocalAccount(@Nullable LocalAccount currentLocalAccount) {
this.currentLocalAccount = currentLocalAccount;
notifyDataSetChanged();
}
} }

View file

@ -1,5 +1,6 @@
package it.niedermann.owncloud.notes.manageaccounts; package it.niedermann.owncloud.notes.manageaccounts;
import android.graphics.drawable.LayerDrawable;
import android.net.Uri; import android.net.Uri;
import android.view.View; import android.view.View;
@ -15,16 +16,20 @@ import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.databinding.ItemAccountChooseBinding; import it.niedermann.owncloud.notes.databinding.ItemAccountChooseBinding;
import it.niedermann.owncloud.notes.model.LocalAccount; import it.niedermann.owncloud.notes.model.LocalAccount;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static it.niedermann.owncloud.notes.branding.BrandingUtil.applyBrandToLayerDrawable;
public class ManageAccountViewHolder extends RecyclerView.ViewHolder { public class ManageAccountViewHolder extends RecyclerView.ViewHolder {
ItemAccountChooseBinding binding; private ItemAccountChooseBinding binding;
public ManageAccountViewHolder(@NonNull View itemView) { public ManageAccountViewHolder(@NonNull View itemView) {
super(itemView); super(itemView);
binding = ItemAccountChooseBinding.bind(itemView); binding = ItemAccountChooseBinding.bind(itemView);
} }
public void bind(@NonNull LocalAccount localAccount, @NonNull Consumer<LocalAccount> onAccountClick, @Nullable Consumer<LocalAccount> onAccountDelete) { public void bind(@NonNull LocalAccount localAccount, @NonNull Consumer<LocalAccount> onAccountClick, @Nullable Consumer<LocalAccount> onAccountDelete, boolean isCurrentAccount) {
binding.accountItemLabel.setText(localAccount.getAccountName()); binding.accountItemLabel.setText(localAccount.getAccountName());
Glide.with(itemView.getContext()) Glide.with(itemView.getContext())
.load(localAccount.getUrl() + "/index.php/avatar/" + Uri.encode(localAccount.getUserName()) + "/64") .load(localAccount.getUrl() + "/index.php/avatar/" + Uri.encode(localAccount.getUserName()) + "/64")
@ -33,10 +38,16 @@ public class ManageAccountViewHolder extends RecyclerView.ViewHolder {
.into(binding.accountItemAvatar); .into(binding.accountItemAvatar);
itemView.setOnClickListener((v) -> onAccountClick.accept(localAccount)); itemView.setOnClickListener((v) -> onAccountClick.accept(localAccount));
if (onAccountDelete == null) { if (onAccountDelete == null) {
binding.delete.setVisibility(View.GONE); binding.delete.setVisibility(GONE);
} else { } else {
binding.delete.setVisibility(View.VISIBLE); binding.delete.setVisibility(VISIBLE);
binding.delete.setOnClickListener((v) -> onAccountDelete.accept(localAccount)); binding.delete.setOnClickListener((v) -> onAccountDelete.accept(localAccount));
} }
if (isCurrentAccount) {
binding.currentAccountIndicator.setVisibility(VISIBLE);
applyBrandToLayerDrawable((LayerDrawable) binding.currentAccountIndicator.getDrawable(), R.id.area, localAccount.getColor());
} else {
binding.currentAccountIndicator.setVisibility(GONE);
}
} }
} }

View file

@ -5,7 +5,10 @@ import android.os.Bundle;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
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.helper.SingleAccountHelper;
import com.nextcloud.android.sso.model.SingleSignOnAccount;
import java.util.List; import java.util.List;
@ -46,12 +49,21 @@ public class ManageAccountsActivity extends LockedActivity {
} }
if (localAccounts.size() > 0) { if (localAccounts.size() > 0) {
SingleAccountHelper.setCurrentAccount(getApplicationContext(), localAccounts.get(0).getAccountName()); SingleAccountHelper.setCurrentAccount(getApplicationContext(), localAccounts.get(0).getAccountName());
adapter.setCurrentLocalAccount(localAccounts.get(0));
} else { } else {
setResult(AppCompatActivity.RESULT_FIRST_USER); setResult(AppCompatActivity.RESULT_FIRST_USER);
finish(); finish();
} }
}); });
adapter.setLocalAccounts(localAccounts); adapter.setLocalAccounts(localAccounts);
try {
SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(this);
if (ssoAccount != null) {
adapter.setCurrentLocalAccount(db.getLocalAccountByAccountName(ssoAccount.name));
}
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
e.printStackTrace();
}
binding.accounts.setAdapter(adapter); binding.accounts.setAdapter(adapter);
} }

View file

@ -8,6 +8,12 @@
android:orientation="horizontal" android:orientation="horizontal"
android:padding="@dimen/spacer_3x"> android:padding="@dimen/spacer_3x">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/spacer_1hx"
android:layout_marginRight="@dimen/spacer_1hx">
<androidx.appcompat.widget.AppCompatImageView <androidx.appcompat.widget.AppCompatImageView
android:id="@+id/accountItemAvatar" android:id="@+id/accountItemAvatar"
android:layout_width="36dp" android:layout_width="36dp"
@ -17,6 +23,16 @@
android:scaleType="center" android:scaleType="center"
app:srcCompat="@drawable/ic_account_circle_grey_24dp" /> app:srcCompat="@drawable/ic_account_circle_grey_24dp" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/currentAccountIndicator"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_gravity="end|bottom"
android:visibility="gone"
app:srcCompat="@drawable/check"
tools:visibility="visible" />
</FrameLayout>
<TextView <TextView
android:id="@+id/accountItemLabel" android:id="@+id/accountItemLabel"
android:layout_width="0dp" android:layout_width="0dp"