mirror of
https://github.com/M66B/FairEmail.git
synced 2026-03-30 05:44:51 +02:00
Use recyclerview for account/identity selection
This commit is contained in:
@@ -23,17 +23,19 @@ import static android.app.Activity.RESULT_OK;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.constraintlayout.widget.Group;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -43,37 +45,34 @@ public class FragmentDialogSelectIdentity extends FragmentDialogBase {
|
||||
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
|
||||
final Context context = getContext();
|
||||
|
||||
final int dp6 = Helper.dp2pixels(context, 6);
|
||||
final int dp12 = Helper.dp2pixels(context, 12);
|
||||
final View dview = LayoutInflater.from(context).inflate(R.layout.dialog_account_select, null);
|
||||
RecyclerView rvSelect = dview.findViewById(R.id.rvSelect);
|
||||
final ContentLoadingProgressBar pbWait = dview.findViewById(R.id.pbWait);
|
||||
final Group grpReady = dview.findViewById(R.id.grpReady);
|
||||
|
||||
final ArrayAdapter<TupleIdentityEx> adapter = new ArrayAdapter<TupleIdentityEx>(context, R.layout.spinner_account, android.R.id.text1) {
|
||||
@NonNull
|
||||
@Override
|
||||
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
|
||||
View view = super.getView(position, convertView, parent);
|
||||
rvSelect.setHasFixedSize(false);
|
||||
rvSelect.setLayoutManager(new LinearLayoutManager(context));
|
||||
|
||||
try {
|
||||
TupleIdentityEx identity = getItem(position);
|
||||
Dialog dialog = new AlertDialog.Builder(context)
|
||||
.setIcon(R.drawable.twotone_person_24)
|
||||
.setTitle(R.string.title_list_identities)
|
||||
.setView(dview)
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
.create();
|
||||
|
||||
View vwColor = view.findViewById(R.id.vwColor);
|
||||
TextView tv = view.findViewById(android.R.id.text1);
|
||||
|
||||
int vpad = (getCount() > 10 ? dp6 : dp12);
|
||||
tv.setPadding(0, vpad, 0, vpad);
|
||||
|
||||
Integer color = (identity.color == null ? identity.accountColor : identity.color);
|
||||
vwColor.setBackgroundColor(color == null ? Color.TRANSPARENT : color);
|
||||
tv.setText(identity.getDisplayName());
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
|
||||
return view;
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: spinner
|
||||
new SimpleTask<List<TupleIdentityEx>>() {
|
||||
@Override
|
||||
protected void onPreExecute(Bundle args) {
|
||||
pbWait.setVisibility(View.VISIBLE);
|
||||
grpReady.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Bundle args) {
|
||||
pbWait.setVisibility(View.GONE);
|
||||
grpReady.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<TupleIdentityEx> onExecute(Context context, Bundle args) {
|
||||
DB db = DB.getInstance(context);
|
||||
@@ -82,8 +81,17 @@ public class FragmentDialogSelectIdentity extends FragmentDialogBase {
|
||||
|
||||
@Override
|
||||
protected void onExecuted(Bundle args, List<TupleIdentityEx> identities) {
|
||||
EntityLog.log(context, "Composable identities=" + (identities == null ? null : identities.size()));
|
||||
adapter.addAll(identities);
|
||||
AdapterIdentity adapter = new AdapterIdentity(context, identities, new AdapterIdentity.IListener() {
|
||||
@Override
|
||||
public void onSelected(TupleIdentityEx identity) {
|
||||
Bundle args = getArguments();
|
||||
args.putLong("id", identity.id);
|
||||
args.putString("html", identity.signature);
|
||||
sendResult(RESULT_OK);
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
rvSelect.setAdapter(adapter);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -92,20 +100,93 @@ public class FragmentDialogSelectIdentity extends FragmentDialogBase {
|
||||
}
|
||||
}.execute(this, getArguments(), "select:identity");
|
||||
|
||||
return new AlertDialog.Builder(context)
|
||||
.setIcon(R.drawable.twotone_person_24)
|
||||
.setTitle(R.string.title_list_identities)
|
||||
.setAdapter(adapter, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
TupleIdentityEx identity = adapter.getItem(which);
|
||||
Bundle args = getArguments();
|
||||
args.putLong("id", identity.id);
|
||||
args.putString("html", identity.signature);
|
||||
sendResult(RESULT_OK);
|
||||
}
|
||||
})
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
.create();
|
||||
return dialog;
|
||||
}
|
||||
}
|
||||
|
||||
public static class AdapterIdentity extends RecyclerView.Adapter<AdapterIdentity.ViewHolder> {
|
||||
private Context context;
|
||||
private LayoutInflater inflater;
|
||||
|
||||
private int dp6;
|
||||
private int dp12;
|
||||
private List<TupleIdentityEx> items;
|
||||
private IListener listener;
|
||||
|
||||
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
||||
private View vwColor;
|
||||
private TextView tv;
|
||||
|
||||
ViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
vwColor = itemView.findViewById(R.id.vwColor);
|
||||
tv = itemView.findViewById(android.R.id.text1);
|
||||
}
|
||||
|
||||
private void wire() {
|
||||
itemView.setOnClickListener(this);
|
||||
}
|
||||
|
||||
private void unwire() {
|
||||
itemView.setOnClickListener(null);
|
||||
}
|
||||
|
||||
private void bindTo(TupleIdentityEx identity) {
|
||||
int vpad = (getItemCount() > 10 ? dp6 : dp12);
|
||||
tv.setPadding(0, vpad, 0, vpad);
|
||||
|
||||
Integer color = (identity.color == null ? identity.accountColor : identity.color);
|
||||
vwColor.setBackgroundColor(color == null ? Color.TRANSPARENT : color);
|
||||
tv.setText(identity.getDisplayName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
int pos = getAdapterPosition();
|
||||
if (pos == RecyclerView.NO_POSITION)
|
||||
return;
|
||||
|
||||
TupleIdentityEx identity = items.get(pos);
|
||||
listener.onSelected(identity);
|
||||
}
|
||||
}
|
||||
|
||||
AdapterIdentity(Context context, List<TupleIdentityEx> identities, IListener listener) {
|
||||
this.context = context;
|
||||
this.inflater = LayoutInflater.from(context);
|
||||
this.dp6 = Helper.dp2pixels(context, 6);
|
||||
this.dp12 = Helper.dp2pixels(context, 12);
|
||||
|
||||
setHasStableIds(true);
|
||||
this.items = identities;
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return items.get(position).id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return items.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
return new ViewHolder(inflater.inflate(R.layout.spinner_account, parent, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||
holder.unwire();
|
||||
TupleIdentityEx identity = items.get(position);
|
||||
holder.bindTo(identity);
|
||||
holder.wire();
|
||||
}
|
||||
|
||||
public interface IListener {
|
||||
void onSelected(TupleIdentityEx identity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user