Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
6.3 KiB

  1. package eu.faircode.email;
  2. /*
  3. This file is part of FairEmail.
  4. FairEmail is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. NetGuard is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
  14. Copyright 2018 by Marcel Bokhorst (M66B)
  15. */
  16. import android.content.Context;
  17. import android.os.Bundle;
  18. import android.util.Log;
  19. import android.view.LayoutInflater;
  20. import android.view.View;
  21. import android.view.ViewGroup;
  22. import android.widget.TextView;
  23. import java.text.DateFormat;
  24. import java.text.SimpleDateFormat;
  25. import java.util.ArrayList;
  26. import java.util.Date;
  27. import java.util.List;
  28. import androidx.annotation.NonNull;
  29. import androidx.lifecycle.LifecycleOwner;
  30. import androidx.recyclerview.widget.DiffUtil;
  31. import androidx.recyclerview.widget.ListUpdateCallback;
  32. import androidx.recyclerview.widget.RecyclerView;
  33. public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.ViewHolder> {
  34. private Context context;
  35. private LifecycleOwner owner;
  36. private List<EntityOperation> all = new ArrayList<>();
  37. private List<EntityOperation> filtered = new ArrayList<>();
  38. private DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.LONG);
  39. public class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
  40. View itemView;
  41. TextView tvMessage;
  42. TextView tvName;
  43. TextView tvTime;
  44. ViewHolder(View itemView) {
  45. super(itemView);
  46. this.itemView = itemView;
  47. tvMessage = itemView.findViewById(R.id.tvMessage);
  48. tvName = itemView.findViewById(R.id.tvName);
  49. tvTime = itemView.findViewById(R.id.tvTime);
  50. }
  51. private void wire() {
  52. itemView.setOnLongClickListener(this);
  53. }
  54. private void unwire() {
  55. itemView.setOnLongClickListener(null);
  56. }
  57. private void bindTo(EntityOperation operation) {
  58. tvMessage.setText(Long.toString(operation.message));
  59. tvName.setText(operation.name);
  60. tvTime.setText(df.format(new Date(operation.created)));
  61. }
  62. @Override
  63. public boolean onLongClick(View view) {
  64. int pos = getAdapterPosition();
  65. if (pos == RecyclerView.NO_POSITION)
  66. return false;
  67. EntityOperation operation = filtered.get(pos);
  68. Bundle args = new Bundle();
  69. args.putLong("id", operation.id);
  70. new SimpleTask<Void>() {
  71. @Override
  72. protected Void onLoad(Context context, Bundle args) throws Throwable {
  73. DB.getInstance(context).operation().deleteOperation(args.getLong("id"));
  74. EntityOperation.process(context);
  75. return null;
  76. }
  77. }.load(context, owner, args);
  78. return true;
  79. }
  80. }
  81. AdapterOperation(Context context, LifecycleOwner owner) {
  82. this.context = context;
  83. this.owner = owner;
  84. setHasStableIds(true);
  85. }
  86. public void set(@NonNull List<EntityOperation> operations) {
  87. Log.i(Helper.TAG, "Set operations=" + operations.size());
  88. all.clear();
  89. all.addAll(operations);
  90. DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new MessageDiffCallback(filtered, all));
  91. filtered.clear();
  92. filtered.addAll(all);
  93. diff.dispatchUpdatesTo(new ListUpdateCallback() {
  94. @Override
  95. public void onInserted(int position, int count) {
  96. Log.i(Helper.TAG, "Inserted @" + position + " #" + count);
  97. }
  98. @Override
  99. public void onRemoved(int position, int count) {
  100. Log.i(Helper.TAG, "Removed @" + position + " #" + count);
  101. }
  102. @Override
  103. public void onMoved(int fromPosition, int toPosition) {
  104. Log.i(Helper.TAG, "Moved " + fromPosition + ">" + toPosition);
  105. }
  106. @Override
  107. public void onChanged(int position, int count, Object payload) {
  108. Log.i(Helper.TAG, "Changed @" + position + " #" + count);
  109. }
  110. });
  111. diff.dispatchUpdatesTo(this);
  112. }
  113. private class MessageDiffCallback extends DiffUtil.Callback {
  114. private List<EntityOperation> prev;
  115. private List<EntityOperation> next;
  116. MessageDiffCallback(List<EntityOperation> prev, List<EntityOperation> next) {
  117. this.prev = prev;
  118. this.next = next;
  119. }
  120. @Override
  121. public int getOldListSize() {
  122. return prev.size();
  123. }
  124. @Override
  125. public int getNewListSize() {
  126. return next.size();
  127. }
  128. @Override
  129. public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
  130. EntityOperation a1 = prev.get(oldItemPosition);
  131. EntityOperation a2 = next.get(newItemPosition);
  132. return a1.id.equals(a2.id);
  133. }
  134. @Override
  135. public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
  136. EntityOperation a1 = prev.get(oldItemPosition);
  137. EntityOperation a2 = next.get(newItemPosition);
  138. return a1.equals(a2);
  139. }
  140. }
  141. @Override
  142. public long getItemId(int position) {
  143. return filtered.get(position).id;
  144. }
  145. @Override
  146. public int getItemCount() {
  147. return filtered.size();
  148. }
  149. @Override
  150. @NonNull
  151. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  152. return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_operation, parent, false));
  153. }
  154. @Override
  155. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  156. holder.unwire();
  157. EntityOperation operation = filtered.get(position);
  158. holder.bindTo(operation);
  159. holder.wire();
  160. }
  161. }