FairEmail/app/src/main/java/eu/faircode/email/FragmentDialogSelectAccount...

205 lines
7.2 KiB
Java
Raw Normal View History

2021-09-28 17:14:27 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2024-01-01 07:50:49 +00:00
Copyright 2018-2024 by Marcel Bokhorst (M66B)
2021-09-28 17:14:27 +00:00
*/
import static android.app.Activity.RESULT_OK;
import android.app.Dialog;
import android.content.Context;
2022-02-09 14:33:24 +00:00
import android.graphics.Color;
2021-09-28 17:14:27 +00:00
import android.os.Bundle;
import android.view.LayoutInflater;
2022-02-09 14:33:24 +00:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
2021-09-28 17:14:27 +00:00
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;
2021-09-28 17:14:27 +00:00
2023-06-12 12:34:20 +00:00
import java.util.ArrayList;
2021-09-28 17:14:27 +00:00
import java.util.List;
public class FragmentDialogSelectAccount extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
2022-02-09 14:33:24 +00:00
final Context context = getContext();
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);
2022-02-09 14:33:24 +00:00
rvSelect.setHasFixedSize(false);
rvSelect.setLayoutManager(new LinearLayoutManager(context));
2022-02-09 14:33:24 +00:00
Dialog dialog = new AlertDialog.Builder(context)
.setIcon(R.drawable.twotone_account_circle_24)
.setTitle(R.string.title_list_accounts)
.setView(dview)
.setNegativeButton(android.R.string.cancel, null)
.create();
2023-08-22 15:50:01 +00:00
new SimpleTask<List<EntityAccount>>() {
@Override
protected void onPreExecute(Bundle args) {
pbWait.setVisibility(View.VISIBLE);
grpReady.setVisibility(View.GONE);
}
2022-02-09 14:33:24 +00:00
@Override
protected void onPostExecute(Bundle args) {
pbWait.setVisibility(View.GONE);
grpReady.setVisibility(View.VISIBLE);
2022-02-09 14:33:24 +00:00
}
2021-09-28 17:14:27 +00:00
@Override
protected List<EntityAccount> onExecute(Context context, Bundle args) {
2022-02-08 11:39:24 +00:00
boolean all = (args != null && args.getBoolean("all"));
2022-03-25 10:04:57 +00:00
Integer type = (args == null || !args.containsKey("type") ? null : args.getInt("type"));
2022-02-08 11:39:24 +00:00
2021-09-28 17:14:27 +00:00
DB db = DB.getInstance(context);
2022-02-08 11:39:24 +00:00
return (all
? db.account().getAccounts()
2022-03-25 10:04:57 +00:00
: db.account().getSynchronizingAccounts(type));
2021-09-28 17:14:27 +00:00
}
@Override
protected void onExecuted(Bundle args, List<EntityAccount> accounts) {
2023-06-12 12:34:20 +00:00
if ("outlook".equals(args.getString("filter")))
for (EntityAccount account : new ArrayList<>(accounts))
if (!account.isOutlook())
accounts.remove(account);
2021-09-28 17:14:27 +00:00
AdapterAccount adapter = new AdapterAccount(context, accounts, new AdapterAccount.IListener() {
2021-09-28 17:14:27 +00:00
@Override
public void onSelected(EntityAccount account) {
2022-02-08 11:39:24 +00:00
Bundle args = getArguments();
args.putLong("account", account.id);
2022-03-25 09:48:12 +00:00
args.putInt("protocol", account.protocol);
2022-02-08 11:39:24 +00:00
args.putString("name", account.name);
2023-06-12 12:34:20 +00:00
args.putString("user", account.user);
2021-09-28 17:14:27 +00:00
sendResult(RESULT_OK);
dialog.dismiss();
2021-09-28 17:14:27 +00:00
}
});
rvSelect.setAdapter(adapter);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, getArguments(), "select:account");
return dialog;
}
public static class AdapterAccount extends RecyclerView.Adapter<AdapterAccount.ViewHolder> {
private Context context;
private LayoutInflater inflater;
private int dp6;
private int dp12;
private List<EntityAccount> 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(EntityAccount account) {
int vpad = (getItemCount() > 10 ? dp6 : dp12);
tv.setPadding(0, vpad, 0, vpad);
vwColor.setBackgroundColor(account.color == null ? Color.TRANSPARENT : account.color);
tv.setText(account.name);
}
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
EntityAccount account = items.get(pos);
listener.onSelected(account);
}
}
AdapterAccount(Context context, List<EntityAccount> accounts, 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 = accounts;
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) {
2023-08-23 07:02:52 +00:00
return new ViewHolder(inflater.inflate(R.layout.item_account_select, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.unwire();
EntityAccount account = items.get(position);
holder.bindTo(account);
holder.wire();
}
public interface IListener {
void onSelected(EntityAccount account);
}
2021-09-28 17:14:27 +00:00
}
}