mirror of
https://github.com/M66B/FairEmail.git
synced 2025-02-22 14:11:00 +00:00
Simplify compose
This commit is contained in:
parent
04e91b1954
commit
3754a55ab7
4 changed files with 31 additions and 129 deletions
|
@ -179,9 +179,4 @@ public class EntityAccount {
|
||||||
} else
|
} else
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return name + (primary ? " ★" : "");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -197,9 +197,4 @@ public class EntityIdentity {
|
||||||
String getDisplayName() {
|
String getDisplayName() {
|
||||||
return (display == null ? name : display);
|
return (display == null ? name : display);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return getDisplayName() + (primary ? " ★" : "");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,7 +108,6 @@ import androidx.core.content.ContextCompat;
|
||||||
import androidx.cursoradapter.widget.SimpleCursorAdapter;
|
import androidx.cursoradapter.widget.SimpleCursorAdapter;
|
||||||
import androidx.fragment.app.FragmentTransaction;
|
import androidx.fragment.app.FragmentTransaction;
|
||||||
import androidx.lifecycle.Lifecycle;
|
import androidx.lifecycle.Lifecycle;
|
||||||
import androidx.lifecycle.LiveData;
|
|
||||||
import androidx.lifecycle.Observer;
|
import androidx.lifecycle.Observer;
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
@ -119,7 +118,6 @@ public class FragmentCompose extends FragmentEx {
|
||||||
private enum State {NONE, LOADING, LOADED}
|
private enum State {NONE, LOADING, LOADED}
|
||||||
|
|
||||||
private ViewGroup view;
|
private ViewGroup view;
|
||||||
private Spinner spAccount;
|
|
||||||
private Spinner spIdentity;
|
private Spinner spIdentity;
|
||||||
private ImageView ivIdentityAdd;
|
private ImageView ivIdentityAdd;
|
||||||
private TextView tvExtraPrefix;
|
private TextView tvExtraPrefix;
|
||||||
|
@ -170,7 +168,6 @@ public class FragmentCompose extends FragmentEx {
|
||||||
view = (ViewGroup) inflater.inflate(R.layout.fragment_compose, container, false);
|
view = (ViewGroup) inflater.inflate(R.layout.fragment_compose, container, false);
|
||||||
|
|
||||||
// Get controls
|
// Get controls
|
||||||
spAccount = view.findViewById(R.id.spAccount);
|
|
||||||
spIdentity = view.findViewById(R.id.spIdentity);
|
spIdentity = view.findViewById(R.id.spIdentity);
|
||||||
ivIdentityAdd = view.findViewById(R.id.ivIdentityAdd);
|
ivIdentityAdd = view.findViewById(R.id.ivIdentityAdd);
|
||||||
tvExtraPrefix = view.findViewById(R.id.tvExtraPrefix);
|
tvExtraPrefix = view.findViewById(R.id.tvExtraPrefix);
|
||||||
|
@ -1414,85 +1411,42 @@ public class FragmentCompose extends FragmentEx {
|
||||||
|
|
||||||
final DB db = DB.getInstance(getContext());
|
final DB db = DB.getInstance(getContext());
|
||||||
|
|
||||||
db.account().liveAccounts(true).observe(getViewLifecycleOwner(), new Observer<List<EntityAccount>>() {
|
db.identity().liveIdentities().observe(getViewLifecycleOwner(), new Observer<List<TupleIdentityEx>>() {
|
||||||
private LiveData<List<EntityIdentity>> liveIdentities = null;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onChanged(List<EntityAccount> accounts) {
|
public void onChanged(List<TupleIdentityEx> identities) {
|
||||||
if (accounts == null)
|
if (identities == null)
|
||||||
accounts = new ArrayList<>();
|
identities = new ArrayList<>();
|
||||||
|
|
||||||
Log.i("Set accounts=" + accounts.size());
|
Log.i("Set identities=" + identities.size());
|
||||||
|
|
||||||
// Sort accounts
|
// Sort identities
|
||||||
Collections.sort(accounts, new Comparator<EntityAccount>() {
|
Collections.sort(identities, new Comparator<TupleIdentityEx>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(EntityAccount a1, EntityAccount a2) {
|
public int compare(TupleIdentityEx i1, TupleIdentityEx i2) {
|
||||||
return a1.name.compareTo(a2.name);
|
int a = i1.accountName.compareTo(i2.accountName);
|
||||||
|
if (a != 0)
|
||||||
|
return a;
|
||||||
|
int d = i1.getDisplayName().compareTo(i2.getDisplayName());
|
||||||
|
if (d != 0)
|
||||||
|
return d;
|
||||||
|
return i1.email.compareTo(i2.email);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Show accounts
|
// Show identities
|
||||||
AccountAdapter adapter = new AccountAdapter(getContext(), accounts);
|
IdentityAdapter adapter = new IdentityAdapter(getContext(), identities);
|
||||||
adapter.setDropDownViewResource(R.layout.spinner_item1_dropdown);
|
adapter.setDropDownViewResource(R.layout.spinner_item1_dropdown);
|
||||||
spAccount.setAdapter(adapter);
|
spIdentity.setAdapter(adapter);
|
||||||
|
|
||||||
spAccount.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
// Select identity
|
||||||
@Override
|
if (result.draft.identity != null)
|
||||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
for (int pos = 0; pos < identities.size(); pos++) {
|
||||||
EntityAccount account = (EntityAccount) parent.getAdapter().getItem(position);
|
if (identities.get(pos).id.equals(result.draft.identity)) {
|
||||||
|
spIdentity.setSelection(pos);
|
||||||
if (liveIdentities != null)
|
break;
|
||||||
liveIdentities.removeObservers(getViewLifecycleOwner());
|
}
|
||||||
liveIdentities = db.identity().liveIdentities(account.id, true);
|
|
||||||
|
|
||||||
liveIdentities.observe(getViewLifecycleOwner(), new Observer<List<EntityIdentity>>() {
|
|
||||||
@Override
|
|
||||||
public void onChanged(@Nullable List<EntityIdentity> identities) {
|
|
||||||
if (identities == null)
|
|
||||||
identities = new ArrayList<>();
|
|
||||||
|
|
||||||
Log.i("Set identities=" + identities.size());
|
|
||||||
|
|
||||||
// Sort identities
|
|
||||||
Collections.sort(identities, new Comparator<EntityIdentity>() {
|
|
||||||
@Override
|
|
||||||
public int compare(EntityIdentity i1, EntityIdentity i2) {
|
|
||||||
return i1.toString().compareTo(i2.toString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Show identities
|
|
||||||
IdentityAdapter adapter = new IdentityAdapter(getContext(), identities);
|
|
||||||
adapter.setDropDownViewResource(R.layout.spinner_item1_dropdown);
|
|
||||||
spIdentity.setAdapter(adapter);
|
|
||||||
|
|
||||||
// Select identity
|
|
||||||
if (result.draft.identity != null)
|
|
||||||
for (int pos = 0; pos < identities.size(); pos++) {
|
|
||||||
if (identities.get(pos).id.equals(result.draft.identity)) {
|
|
||||||
spIdentity.setSelection(pos);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onNothingSelected(AdapterView<?> parent) {
|
|
||||||
IdentityAdapter adapter = new IdentityAdapter(getContext(), new ArrayList<EntityIdentity>());
|
|
||||||
adapter.setDropDownViewResource(R.layout.spinner_item1_dropdown);
|
|
||||||
spIdentity.setAdapter(adapter);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Select account
|
|
||||||
for (int pos = 0; pos < accounts.size(); pos++)
|
|
||||||
if (accounts.get(pos).id.equals(result.draft.account)) {
|
|
||||||
spAccount.setSelection(pos);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1915,44 +1869,11 @@ public class FragmentCompose extends FragmentEx {
|
||||||
EntityAccount account;
|
EntityAccount account;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AccountAdapter extends ArrayAdapter<EntityAccount> {
|
public class IdentityAdapter extends ArrayAdapter<TupleIdentityEx> {
|
||||||
private Context context;
|
private Context context;
|
||||||
private List<EntityAccount> accounts;
|
private List<TupleIdentityEx> identities;
|
||||||
|
|
||||||
AccountAdapter(@NonNull Context context, List<EntityAccount> accounts) {
|
IdentityAdapter(@NonNull Context context, List<TupleIdentityEx> identities) {
|
||||||
super(context, 0, accounts);
|
|
||||||
this.context = context;
|
|
||||||
this.accounts = accounts;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
@Override
|
|
||||||
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
|
|
||||||
return getLayout(position, convertView, parent, R.layout.spinner_item1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public View getDropDownView(int position, View convertView, ViewGroup parent) {
|
|
||||||
return getLayout(position, convertView, parent, R.layout.spinner_item1_dropdown);
|
|
||||||
}
|
|
||||||
|
|
||||||
View getLayout(int position, View convertView, ViewGroup parent, int resid) {
|
|
||||||
View view = LayoutInflater.from(context).inflate(resid, parent, false);
|
|
||||||
|
|
||||||
EntityAccount account = accounts.get(position);
|
|
||||||
|
|
||||||
TextView text1 = view.findViewById(android.R.id.text1);
|
|
||||||
text1.setText(account.name);
|
|
||||||
|
|
||||||
return view;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class IdentityAdapter extends ArrayAdapter<EntityIdentity> {
|
|
||||||
private Context context;
|
|
||||||
private List<EntityIdentity> identities;
|
|
||||||
|
|
||||||
IdentityAdapter(@NonNull Context context, List<EntityIdentity> identities) {
|
|
||||||
super(context, 0, identities);
|
super(context, 0, identities);
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.identities = identities;
|
this.identities = identities;
|
||||||
|
@ -1972,10 +1893,10 @@ public class FragmentCompose extends FragmentEx {
|
||||||
View getLayout(int position, View convertView, ViewGroup parent, int resid) {
|
View getLayout(int position, View convertView, ViewGroup parent, int resid) {
|
||||||
View view = LayoutInflater.from(context).inflate(resid, parent, false);
|
View view = LayoutInflater.from(context).inflate(resid, parent, false);
|
||||||
|
|
||||||
EntityIdentity identity = identities.get(position);
|
TupleIdentityEx identity = identities.get(position);
|
||||||
|
|
||||||
TextView text1 = view.findViewById(android.R.id.text1);
|
TextView text1 = view.findViewById(android.R.id.text1);
|
||||||
text1.setText(identity.toString());
|
text1.setText(identity.accountName + "/" + identity.getDisplayName() + (identity.primary ? " ★" : ""));
|
||||||
|
|
||||||
TextView text2 = view.findViewById(android.R.id.text2);
|
TextView text2 = view.findViewById(android.R.id.text2);
|
||||||
text2.setText(identity.email);
|
text2.setText(identity.email);
|
||||||
|
|
|
@ -15,15 +15,6 @@
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content">
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
<Spinner
|
|
||||||
android:id="@+id/spAccount"
|
|
||||||
android:layout_width="0dp"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginTop="6dp"
|
|
||||||
app:layout_constraintEnd_toStartOf="@+id/ivIdentityAdd"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
|
||||||
|
|
||||||
<Spinner
|
<Spinner
|
||||||
android:id="@+id/spIdentity"
|
android:id="@+id/spIdentity"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
|
@ -31,7 +22,7 @@
|
||||||
android:layout_marginTop="6dp"
|
android:layout_marginTop="6dp"
|
||||||
app:layout_constraintEnd_toStartOf="@+id/ivIdentityAdd"
|
app:layout_constraintEnd_toStartOf="@+id/ivIdentityAdd"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@id/spAccount" />
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
<ImageView
|
<ImageView
|
||||||
android:id="@+id/ivIdentityAdd"
|
android:id="@+id/ivIdentityAdd"
|
||||||
|
|
Loading…
Reference in a new issue