2018-08-02 13:33:06 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
/*
|
2018-08-14 05:53:24 +00:00
|
|
|
This file is part of FairEmail.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-14 05:53:24 +00:00
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
2018-08-02 13:33:06 +00:00
|
|
|
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.
|
|
|
|
|
2018-10-29 10:46:49 +00:00
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
2018-08-02 13:33:06 +00:00
|
|
|
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
|
2018-10-29 10:46:49 +00:00
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-12-31 08:04:33 +00:00
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
2018-08-02 13:33:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2018-09-12 16:54:48 +00:00
|
|
|
import android.graphics.Color;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import java.text.Collator;
|
2019-01-23 09:16:33 +00:00
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
2018-08-02 13:33:06 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
import androidx.recyclerview.widget.DiffUtil;
|
|
|
|
import androidx.recyclerview.widget.ListUpdateCallback;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
public class AdapterAccount extends RecyclerView.Adapter<AdapterAccount.ViewHolder> {
|
|
|
|
private Context context;
|
2018-12-24 13:41:49 +00:00
|
|
|
private LayoutInflater inflater;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
private List<EntityAccount> all = new ArrayList<>();
|
|
|
|
private List<EntityAccount> filtered = new ArrayList<>();
|
|
|
|
|
2019-01-23 09:16:33 +00:00
|
|
|
private static final DateFormat df = SimpleDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
|
|
|
View itemView;
|
2018-09-12 16:54:48 +00:00
|
|
|
View vwColor;
|
2018-08-02 13:33:06 +00:00
|
|
|
ImageView ivPrimary;
|
|
|
|
TextView tvName;
|
|
|
|
ImageView ivSync;
|
|
|
|
TextView tvUser;
|
2018-08-15 10:32:51 +00:00
|
|
|
TextView tvHost;
|
2018-08-13 14:44:47 +00:00
|
|
|
ImageView ivState;
|
2018-11-28 16:43:46 +00:00
|
|
|
TextView tvLast;
|
2018-08-11 08:28:54 +00:00
|
|
|
TextView tvError;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
ViewHolder(View itemView) {
|
|
|
|
super(itemView);
|
|
|
|
|
2018-12-06 12:43:00 +00:00
|
|
|
this.itemView = itemView.findViewById(R.id.clItem);
|
2018-09-12 16:54:48 +00:00
|
|
|
vwColor = itemView.findViewById(R.id.vwColor);
|
2018-08-02 13:33:06 +00:00
|
|
|
ivPrimary = itemView.findViewById(R.id.ivPrimary);
|
|
|
|
tvName = itemView.findViewById(R.id.tvName);
|
|
|
|
ivSync = itemView.findViewById(R.id.ivSync);
|
|
|
|
tvUser = itemView.findViewById(R.id.tvUser);
|
2018-08-15 10:32:51 +00:00
|
|
|
tvHost = itemView.findViewById(R.id.tvHost);
|
2018-08-13 14:44:47 +00:00
|
|
|
ivState = itemView.findViewById(R.id.ivState);
|
2018-11-28 16:43:46 +00:00
|
|
|
tvLast = itemView.findViewById(R.id.tvLast);
|
2018-08-11 08:28:54 +00:00
|
|
|
tvError = itemView.findViewById(R.id.tvError);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void wire() {
|
|
|
|
itemView.setOnClickListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void unwire() {
|
|
|
|
itemView.setOnClickListener(null);
|
|
|
|
}
|
|
|
|
|
2018-08-07 06:51:49 +00:00
|
|
|
private void bindTo(EntityAccount account) {
|
2018-12-06 12:43:00 +00:00
|
|
|
itemView.setActivated(account.tbd != null);
|
2018-09-12 16:54:48 +00:00
|
|
|
vwColor.setBackgroundColor(account.color == null ? Color.TRANSPARENT : account.color);
|
2018-08-15 10:32:51 +00:00
|
|
|
ivPrimary.setVisibility(account.primary ? View.VISIBLE : View.INVISIBLE);
|
2018-08-07 06:51:49 +00:00
|
|
|
tvName.setText(account.name);
|
2018-09-09 07:41:53 +00:00
|
|
|
ivSync.setImageResource(account.synchronize ? R.drawable.baseline_sync_24 : R.drawable.baseline_sync_disabled_24);
|
2018-08-07 06:51:49 +00:00
|
|
|
tvUser.setText(account.user);
|
2018-08-15 10:32:51 +00:00
|
|
|
tvHost.setText(String.format("%s:%d", account.host, account.port));
|
|
|
|
|
2018-08-13 14:44:47 +00:00
|
|
|
if ("connected".equals(account.state))
|
|
|
|
ivState.setImageResource(R.drawable.baseline_cloud_24);
|
|
|
|
else if ("connecting".equals(account.state))
|
|
|
|
ivState.setImageResource(R.drawable.baseline_cloud_queue_24);
|
2018-08-15 19:34:22 +00:00
|
|
|
else if ("closing".equals(account.state))
|
|
|
|
ivState.setImageResource(R.drawable.baseline_close_24);
|
2018-08-13 14:44:47 +00:00
|
|
|
else
|
|
|
|
ivState.setImageResource(R.drawable.baseline_cloud_off_24);
|
|
|
|
ivState.setVisibility(account.synchronize ? View.VISIBLE : View.INVISIBLE);
|
|
|
|
|
2019-01-23 09:16:33 +00:00
|
|
|
tvLast.setText(context.getString(R.string.title_last_connected,
|
2019-01-25 06:57:26 +00:00
|
|
|
account.last_connected == null ? "-" : df.format(account.last_connected)));
|
2018-11-28 16:43:46 +00:00
|
|
|
|
2018-08-11 08:28:54 +00:00
|
|
|
tvError.setText(account.error);
|
|
|
|
tvError.setVisibility(account.error == null ? View.GONE : View.VISIBLE);
|
2018-08-07 06:51:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
2018-08-05 18:36:39 +00:00
|
|
|
int pos = getAdapterPosition();
|
|
|
|
if (pos == RecyclerView.NO_POSITION)
|
|
|
|
return;
|
2019-01-12 11:02:26 +00:00
|
|
|
|
2018-08-05 18:36:39 +00:00
|
|
|
EntityAccount account = filtered.get(pos);
|
2019-01-12 11:02:26 +00:00
|
|
|
if (account.tbd != null)
|
|
|
|
return;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
|
|
|
|
lbm.sendBroadcast(
|
2018-08-04 15:46:22 +00:00
|
|
|
new Intent(ActivitySetup.ACTION_EDIT_ACCOUNT)
|
2018-08-08 06:55:47 +00:00
|
|
|
.putExtra("id", account.id));
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AdapterAccount(Context context) {
|
|
|
|
this.context = context;
|
2018-12-24 13:41:49 +00:00
|
|
|
this.inflater = LayoutInflater.from(context);
|
2018-11-28 16:43:46 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
setHasStableIds(true);
|
|
|
|
}
|
|
|
|
|
2018-08-05 16:14:43 +00:00
|
|
|
public void set(@NonNull List<EntityAccount> accounts) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Set accounts=" + accounts.size());
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
final Collator collator = Collator.getInstance(Locale.getDefault());
|
|
|
|
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
|
|
|
|
|
|
|
|
Collections.sort(accounts, new Comparator<EntityAccount>() {
|
|
|
|
@Override
|
|
|
|
public int compare(EntityAccount a1, EntityAccount a2) {
|
|
|
|
return collator.compare(a1.host, a2.host);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-21 15:36:24 +00:00
|
|
|
all = accounts;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new MessageDiffCallback(filtered, all));
|
|
|
|
|
|
|
|
filtered.clear();
|
|
|
|
filtered.addAll(all);
|
|
|
|
|
|
|
|
diff.dispatchUpdatesTo(new ListUpdateCallback() {
|
|
|
|
@Override
|
|
|
|
public void onInserted(int position, int count) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Inserted @" + position + " #" + count);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRemoved(int position, int count) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Removed @" + position + " #" + count);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMoved(int fromPosition, int toPosition) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Moved " + fromPosition + ">" + toPosition);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChanged(int position, int count, Object payload) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Changed @" + position + " #" + count);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
});
|
2018-08-27 07:06:03 +00:00
|
|
|
diff.dispatchUpdatesTo(this);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private class MessageDiffCallback extends DiffUtil.Callback {
|
|
|
|
private List<EntityAccount> prev;
|
|
|
|
private List<EntityAccount> next;
|
|
|
|
|
|
|
|
MessageDiffCallback(List<EntityAccount> prev, List<EntityAccount> next) {
|
|
|
|
this.prev = prev;
|
|
|
|
this.next = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getOldListSize() {
|
|
|
|
return prev.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getNewListSize() {
|
|
|
|
return next.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
|
|
|
|
EntityAccount f1 = prev.get(oldItemPosition);
|
|
|
|
EntityAccount f2 = next.get(newItemPosition);
|
|
|
|
return f1.id.equals(f2.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
|
|
|
|
EntityAccount f1 = prev.get(oldItemPosition);
|
|
|
|
EntityAccount f2 = next.get(newItemPosition);
|
|
|
|
return f1.equals(f2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getItemId(int position) {
|
|
|
|
return filtered.get(position).id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return filtered.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@NonNull
|
|
|
|
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
2018-12-24 13:41:49 +00:00
|
|
|
return new ViewHolder(inflater.inflate(R.layout.item_account, parent, false));
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
|
|
|
holder.unwire();
|
|
|
|
|
|
|
|
EntityAccount account = filtered.get(position);
|
2018-08-07 06:51:49 +00:00
|
|
|
holder.bindTo(account);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
holder.wire();
|
|
|
|
}
|
|
|
|
}
|