mirror of
https://github.com/M66B/FairEmail.git
synced 2025-02-24 15:11:03 +00:00
Added contact disabled state
This commit is contained in:
parent
4b4350e05f
commit
c5b90389ee
4 changed files with 132 additions and 118 deletions
|
@ -49,13 +49,12 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
|
|||
private int colorAccent;
|
||||
private int textColorSecondary;
|
||||
|
||||
private List<EntityContact> all = new ArrayList<>();
|
||||
private List<EntityContact> filtered = new ArrayList<>();
|
||||
private List<EntityContact> items = new ArrayList<>();
|
||||
|
||||
private static NumberFormat nf = NumberFormat.getNumberInstance();
|
||||
|
||||
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
|
||||
private View itemView;
|
||||
private View view;
|
||||
private ImageView ivType;
|
||||
private ImageView ivAvatar;
|
||||
private TextView tvName;
|
||||
|
@ -67,7 +66,7 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
|
|||
ViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
|
||||
this.itemView = itemView;
|
||||
view = itemView.findViewById(R.id.clItem);
|
||||
ivType = itemView.findViewById(R.id.ivType);
|
||||
ivAvatar = itemView.findViewById(R.id.ivAvatar);
|
||||
tvName = itemView.findViewById(R.id.tvName);
|
||||
|
@ -78,16 +77,18 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
|
|||
}
|
||||
|
||||
private void wire() {
|
||||
itemView.setOnClickListener(this);
|
||||
itemView.setOnLongClickListener(this);
|
||||
view.setOnClickListener(this);
|
||||
view.setOnLongClickListener(this);
|
||||
}
|
||||
|
||||
private void unwire() {
|
||||
itemView.setOnClickListener(null);
|
||||
itemView.setOnLongClickListener(null);
|
||||
view.setOnClickListener(null);
|
||||
view.setOnLongClickListener(null);
|
||||
}
|
||||
|
||||
private void bindTo(EntityContact contact) {
|
||||
view.setAlpha(contact.state == 2 ? Helper.LOW_LIGHT : 1.0f);
|
||||
|
||||
if (contact.type == EntityContact.TYPE_FROM)
|
||||
ivType.setImageResource(R.drawable.baseline_mail_24);
|
||||
else if (contact.type == EntityContact.TYPE_TO)
|
||||
|
@ -106,8 +107,10 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
|
|||
tvLast.setText(contact.last_contacted == null ? null
|
||||
: DateUtils.getRelativeTimeSpanString(context, contact.last_contacted));
|
||||
|
||||
ivFavorite.setImageResource(contact.favorite ? R.drawable.baseline_star_24 : R.drawable.baseline_star_border_24);
|
||||
ivFavorite.setImageTintList(ColorStateList.valueOf(contact.favorite ? colorAccent : textColorSecondary));
|
||||
ivFavorite.setImageResource(contact.state == 1 ? R.drawable.baseline_star_24 : R.drawable.baseline_star_border_24);
|
||||
ivFavorite.setImageTintList(ColorStateList.valueOf(contact.state == 1 ? colorAccent : textColorSecondary));
|
||||
|
||||
view.requestLayout();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -116,20 +119,22 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
|
|||
if (pos == RecyclerView.NO_POSITION)
|
||||
return;
|
||||
|
||||
EntityContact contact = filtered.get(pos);
|
||||
EntityContact contact = items.get(pos);
|
||||
contact.state = ++contact.state % 3;
|
||||
notifyItemChanged(pos);
|
||||
|
||||
Bundle args = new Bundle();
|
||||
args.putLong("id", contact.id);
|
||||
args.putBoolean("favorite", !contact.favorite);
|
||||
args.putInt("state", contact.state);
|
||||
|
||||
new SimpleTask<Void>() {
|
||||
@Override
|
||||
protected Void onExecute(Context context, Bundle args) {
|
||||
long id = args.getLong("id");
|
||||
boolean favorite = args.getBoolean("favorite");
|
||||
int state = args.getInt("state");
|
||||
|
||||
DB db = DB.getInstance(context);
|
||||
db.contact().setContactFavorite(id, favorite);
|
||||
db.contact().setContactState(id, state);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
@ -143,7 +148,7 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
|
|||
protected void onException(Bundle args, Throwable ex) {
|
||||
Helper.unexpectedError(context, owner, ex);
|
||||
}
|
||||
}.execute(context, owner, args, "contact:favorite");
|
||||
}.execute(context, owner, args, "contact:state");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -152,7 +157,7 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
|
|||
if (pos == RecyclerView.NO_POSITION)
|
||||
return false;
|
||||
|
||||
EntityContact contact = filtered.get(pos);
|
||||
EntityContact contact = items.get(pos);
|
||||
|
||||
Bundle args = new Bundle();
|
||||
args.putLong("id", contact.id);
|
||||
|
@ -196,13 +201,11 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
|
|||
public void set(@NonNull List<EntityContact> contacts) {
|
||||
Log.i("Set contacts=" + contacts.size());
|
||||
|
||||
all = contacts;
|
||||
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(items, contacts));
|
||||
|
||||
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(filtered, all));
|
||||
|
||||
filtered.clear();
|
||||
filtered.addAll(all);
|
||||
items = contacts;
|
||||
|
||||
diff.dispatchUpdatesTo(this);
|
||||
diff.dispatchUpdatesTo(new ListUpdateCallback() {
|
||||
@Override
|
||||
public void onInserted(int position, int count) {
|
||||
|
@ -224,16 +227,15 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
|
|||
Log.i("Changed @" + position + " #" + count);
|
||||
}
|
||||
});
|
||||
diff.dispatchUpdatesTo(this);
|
||||
}
|
||||
|
||||
private class DiffCallback extends DiffUtil.Callback {
|
||||
private List<EntityContact> prev;
|
||||
private List<EntityContact> next;
|
||||
private List<EntityContact> prev = new ArrayList<>();
|
||||
private List<EntityContact> next = new ArrayList<>();
|
||||
|
||||
DiffCallback(List<EntityContact> prev, List<EntityContact> next) {
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
this.prev.addAll(prev);
|
||||
this.next.addAll(next);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -263,12 +265,12 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
|
|||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return filtered.get(position).id;
|
||||
return items.get(position).id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return filtered.size();
|
||||
return items.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -280,7 +282,7 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
|
|||
@Override
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||
holder.unwire();
|
||||
EntityContact contact = filtered.get(position);
|
||||
EntityContact contact = items.get(position);
|
||||
holder.bindTo(contact);
|
||||
holder.wire();
|
||||
}
|
||||
|
|
|
@ -35,10 +35,14 @@ public interface DaoContact {
|
|||
List<EntityContact> getContacts();
|
||||
|
||||
@Query("SELECT * FROM contact" +
|
||||
" ORDER BY favorite DESC, times_contacted DESC, last_contacted DESC")
|
||||
" ORDER BY" +
|
||||
" CASE WHEN favorite = 1 THEN 0 ELSE 1 END" +
|
||||
", times_contacted DESC" +
|
||||
", last_contacted DESC")
|
||||
LiveData<List<EntityContact>> liveContacts();
|
||||
|
||||
@Query("SELECT * FROM contact" +
|
||||
" WHERE favorite <> 2" +
|
||||
" ORDER BY favorite DESC, times_contacted DESC, last_contacted DESC" +
|
||||
" LIMIT :count")
|
||||
List<EntityContact> getFrequentlyContacted(int count);
|
||||
|
@ -66,8 +70,8 @@ public interface DaoContact {
|
|||
@Update
|
||||
int updateContact(EntityContact contact);
|
||||
|
||||
@Query("UPDATE contact SET favorite = :favorite WHERE id = :id")
|
||||
int setContactFavorite(long id, boolean favorite);
|
||||
@Query("UPDATE contact SET favorite = :state WHERE id = :id")
|
||||
int setContactState(long id, int state);
|
||||
|
||||
@Query("DELETE FROM contact WHERE id= :id")
|
||||
int deleteContact(long id);
|
||||
|
|
|
@ -27,6 +27,7 @@ import java.util.Objects;
|
|||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.room.ColumnInfo;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.Index;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
@ -64,7 +65,8 @@ public class EntityContact implements Serializable {
|
|||
public Integer times_contacted;
|
||||
public Long last_contacted;
|
||||
@NonNull
|
||||
public Boolean favorite = false;
|
||||
@ColumnInfo(name = "favorite")
|
||||
public Integer state = 0;
|
||||
|
||||
public JSONObject toJSON() throws JSONException {
|
||||
JSONObject json = new JSONObject();
|
||||
|
@ -75,7 +77,7 @@ public class EntityContact implements Serializable {
|
|||
json.put("avatar", avatar);
|
||||
json.put("times_contacted", times_contacted);
|
||||
json.put("last_contacted", last_contacted);
|
||||
json.put("favorite", favorite);
|
||||
json.put("state", state);
|
||||
return json;
|
||||
}
|
||||
|
||||
|
@ -100,7 +102,9 @@ public class EntityContact implements Serializable {
|
|||
contact.last_contacted = json.getLong("last_contacted");
|
||||
|
||||
if (json.has("favorite"))
|
||||
contact.favorite = json.getBoolean("favorite");
|
||||
contact.state = (json.getBoolean("favorite") ? 1 : 0);
|
||||
if (json.has("state"))
|
||||
contact.state = json.getInt("state");
|
||||
|
||||
return contact;
|
||||
}
|
||||
|
@ -113,9 +117,9 @@ public class EntityContact implements Serializable {
|
|||
this.email.equals(other.email) &&
|
||||
Objects.equals(this.name, other.name) &&
|
||||
Objects.equals(this.avatar, other.avatar) &&
|
||||
this.times_contacted == other.times_contacted &&
|
||||
this.times_contacted.equals(other.times_contacted) &&
|
||||
Objects.equals(this.last_contacted, other.last_contacted) &&
|
||||
this.favorite == other.favorite);
|
||||
this.state.equals(other.state));
|
||||
|
||||
} else
|
||||
return false;
|
||||
|
|
|
@ -1,89 +1,93 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="3dp">
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/ivType"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:src="@drawable/baseline_mail_24"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/ivAvatar"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@+id/ivAvatar" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/ivAvatar"
|
||||
android:layout_width="42dp"
|
||||
android:layout_height="0dp"
|
||||
android:layout_marginStart="6dp"
|
||||
android:padding="3dp"
|
||||
android:src="@drawable/baseline_person_24"
|
||||
app:layout_constraintDimensionRatio="1:1"
|
||||
app:layout_constraintStart_toEndOf="@id/ivType"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvName"
|
||||
android:layout_width="0dp"
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:id="@+id/clItem"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="6dp"
|
||||
android:layout_marginEnd="6dp"
|
||||
android:ellipsize="middle"
|
||||
android:singleLine="true"
|
||||
android:text="Name"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintEnd_toStartOf="@+id/tvTimes"
|
||||
app:layout_constraintStart_toEndOf="@+id/ivAvatar"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
android:padding="3dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvEmail"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="6dp"
|
||||
android:layout_marginTop="3dp"
|
||||
android:layout_marginEnd="6dp"
|
||||
android:ellipsize="middle"
|
||||
android:singleLine="true"
|
||||
android:text="Email"
|
||||
android:textAppearance="@android:style/TextAppearance.Small"
|
||||
app:layout_constraintEnd_toStartOf="@+id/tvLast"
|
||||
app:layout_constraintStart_toEndOf="@id/ivAvatar"
|
||||
app:layout_constraintTop_toBottomOf="@id/tvName" />
|
||||
<ImageView
|
||||
android:id="@+id/ivType"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:src="@drawable/baseline_mail_24"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/ivAvatar"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@+id/ivAvatar" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvTimes"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="6dp"
|
||||
android:text="123"
|
||||
android:textAppearance="@android:style/TextAppearance.Small"
|
||||
app:layout_constraintEnd_toStartOf="@+id/ivFavorite"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
<ImageView
|
||||
android:id="@+id/ivAvatar"
|
||||
android:layout_width="42dp"
|
||||
android:layout_height="0dp"
|
||||
android:layout_marginStart="6dp"
|
||||
android:padding="3dp"
|
||||
android:src="@drawable/baseline_person_24"
|
||||
app:layout_constraintDimensionRatio="1:1"
|
||||
app:layout_constraintStart_toEndOf="@id/ivType"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvLast"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="3dp"
|
||||
android:layout_marginEnd="6dp"
|
||||
android:text="12:34:56"
|
||||
android:textAppearance="@android:style/TextAppearance.Small"
|
||||
app:layout_constraintEnd_toStartOf="@+id/ivFavorite"
|
||||
app:layout_constraintTop_toBottomOf="@+id/tvTimes" />
|
||||
<TextView
|
||||
android:id="@+id/tvName"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="6dp"
|
||||
android:layout_marginEnd="6dp"
|
||||
android:ellipsize="middle"
|
||||
android:singleLine="true"
|
||||
android:text="Name"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintEnd_toStartOf="@+id/tvTimes"
|
||||
app:layout_constraintStart_toEndOf="@+id/ivAvatar"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/ivFavorite"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:background="?android:attr/selectableItemBackgroundBorderless"
|
||||
android:src="@drawable/baseline_star_24"
|
||||
android:visibility="visible"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/ivAvatar"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@+id/ivAvatar" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
<TextView
|
||||
android:id="@+id/tvEmail"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="6dp"
|
||||
android:layout_marginTop="3dp"
|
||||
android:layout_marginEnd="6dp"
|
||||
android:ellipsize="middle"
|
||||
android:singleLine="true"
|
||||
android:text="Email"
|
||||
android:textAppearance="@android:style/TextAppearance.Small"
|
||||
app:layout_constraintEnd_toStartOf="@+id/tvLast"
|
||||
app:layout_constraintStart_toEndOf="@id/ivAvatar"
|
||||
app:layout_constraintTop_toBottomOf="@id/tvName" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvTimes"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="6dp"
|
||||
android:text="123"
|
||||
android:textAppearance="@android:style/TextAppearance.Small"
|
||||
app:layout_constraintEnd_toStartOf="@+id/ivFavorite"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvLast"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="3dp"
|
||||
android:layout_marginEnd="6dp"
|
||||
android:text="12:34:56"
|
||||
android:textAppearance="@android:style/TextAppearance.Small"
|
||||
app:layout_constraintEnd_toStartOf="@+id/ivFavorite"
|
||||
app:layout_constraintTop_toBottomOf="@+id/tvTimes" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/ivFavorite"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:src="@drawable/baseline_star_24"
|
||||
android:visibility="visible"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/ivAvatar"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@+id/ivAvatar" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</FrameLayout>
|
Loading…
Reference in a new issue