From 1d08a823dd572f821b6f609777d501e77029dc71 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 14 Feb 2021 10:02:10 +0100 Subject: [PATCH] Simplify selecting identities --- .../email/FragmentDialogIdentity.java | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogIdentity.java b/app/src/main/java/eu/faircode/email/FragmentDialogIdentity.java index 650dccb059..dacd5543a5 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogIdentity.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogIdentity.java @@ -27,6 +27,7 @@ import android.content.SharedPreferences; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; +import android.widget.AdapterView; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; @@ -57,6 +58,27 @@ public class FragmentDialogIdentity extends FragmentDialogBase { final Group grpNoIdentities = dview.findViewById(R.id.grpNoIdentities); final ContentLoadingProgressBar pbWait = dview.findViewById(R.id.pbWait); + spIdentity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + Object tag = spIdentity.getTag(); + if (tag != null && !tag.equals(position)) { + TupleIdentityEx identity = (TupleIdentityEx) spIdentity.getAdapter().getItem(position); + startActivity(new Intent(getContext(), ActivityCompose.class) + .putExtra("action", "new") + .putExtra("account", identity.account) + .putExtra("identity", identity.id) + ); + dismiss(); + } + } + + @Override + public void onNothingSelected(AdapterView parent) { + // Do nothing + } + }); + cbNotAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { @@ -99,22 +121,27 @@ public class FragmentDialogIdentity extends FragmentDialogBase { AdapterIdentitySelect iadapter = new AdapterIdentitySelect(getContext(), identities); spIdentity.setAdapter(iadapter); - Integer fallback = null; + Integer selected = null; long account = getArguments().getLong("account"); for (int pos = 0; pos < identities.size(); pos++) { EntityIdentity identity = identities.get(pos); if (identity.account.equals(account)) { if (identity.primary) { - fallback = null; - spIdentity.setSelection(pos); + selected = pos; break; } - if (fallback == null) - fallback = pos; + if (selected == null) + selected = pos; } } - if (fallback != null) - spIdentity.setSelection(fallback); + + if (selected == null && identities.size() > 0) + selected = 0; + + if (selected != null) { + spIdentity.setTag(selected); + spIdentity.setSelection(selected); + } grpIdentities.setVisibility(identities.size() > 0 ? View.VISIBLE : View.GONE); grpNoIdentities.setVisibility(identities.size() > 0 ? View.GONE : View.VISIBLE);