From 28efeea30b56a7db4e1364753da2a5af11449394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sat, 17 Nov 2018 16:45:42 +0100 Subject: [PATCH] message delete problems --- .../lib/collections/CollectionHelper.java | 5 + .../model/CMessageList.java | 158 ++++++++++++------ .../view/MessageAdapter.java | 9 +- .../view/NotificationsFragment.java | 4 +- 4 files changed, 114 insertions(+), 62 deletions(-) diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/lib/collections/CollectionHelper.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/lib/collections/CollectionHelper.java index 4fd2eae..38a923a 100644 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/lib/collections/CollectionHelper.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/lib/collections/CollectionHelper.java @@ -24,6 +24,11 @@ public final class CollectionHelper return output; } + public static void sort_inplace(List input, Comparator comparator) + { + Collections.sort(input, comparator); + } + public static > List sort(List input, Func1to1 mapper) { return sort(input, mapper, 1); diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/CMessageList.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/CMessageList.java index 2f1c13a..e384282 100644 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/CMessageList.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/CMessageList.java @@ -3,17 +3,21 @@ package com.blackforestbytes.simplecloudnotifier.model; import android.content.Context; import android.content.SharedPreferences; +import com.blackforestbytes.simplecloudnotifier.lib.collections.CollectionHelper; import com.blackforestbytes.simplecloudnotifier.lib.string.Str; import com.blackforestbytes.simplecloudnotifier.view.MessageAdapter; import com.blackforestbytes.simplecloudnotifier.SCNApp; import java.lang.ref.WeakReference; import java.util.ArrayList; +import java.util.Collections; import java.util.HashSet; import java.util.Set; public class CMessageList { + private final Object msg_lock = new Object(); + public ArrayList Messages; public Set AllAcks; @@ -32,23 +36,26 @@ public class CMessageList private CMessageList() { - Messages = new ArrayList<>(); - AllAcks = new HashSet<>(); - - SharedPreferences sharedPref = SCNApp.getContext().getSharedPreferences("CMessageList", Context.MODE_PRIVATE); - int count = sharedPref.getInt("message_count", 0); - for (int i=0; i < count; i++) + synchronized (msg_lock) { - long time = sharedPref.getLong("message["+i+"].timestamp", 0); - String title = sharedPref.getString("message["+i+"].title", ""); - String content = sharedPref.getString("message["+i+"].content", ""); - PriorityEnum prio = PriorityEnum.parseAPI(sharedPref.getInt("message["+i+"].priority", 1)); - long scnid = sharedPref.getLong("message["+i+"].scnid", 0); + Messages = new ArrayList<>(); + AllAcks = new HashSet<>(); - Messages.add(new CMessage(scnid, time, title, content, prio)); + SharedPreferences sharedPref = SCNApp.getContext().getSharedPreferences("CMessageList", Context.MODE_PRIVATE); + int count = sharedPref.getInt("message_count", 0); + for (int i=0; i < count; i++) + { + long time = sharedPref.getLong("message["+i+"].timestamp", 0); + String title = sharedPref.getString("message["+i+"].title", ""); + String content = sharedPref.getString("message["+i+"].content", ""); + PriorityEnum prio = PriorityEnum.parseAPI(sharedPref.getInt("message["+i+"].priority", 1)); + long scnid = sharedPref.getLong("message["+i+"].scnid", 0); + + Messages.add(new CMessage(scnid, time, title, content, prio)); + } + + AllAcks = sharedPref.getStringSet("acks", new HashSet<>()); } - - AllAcks = sharedPref.getStringSet("acks", new HashSet<>()); } public CMessage add(final long scnid, final long time, final String title, final String content, final PriorityEnum pe) @@ -60,23 +67,38 @@ public class CMessageList SharedPreferences sharedPref = SCNApp.getContext().getSharedPreferences("CMessageList", Context.MODE_PRIVATE); int count = sharedPref.getInt("message_count", 0); - SharedPreferences.Editor e = sharedPref.edit(); + synchronized (msg_lock) + { + Messages.add(msg); + AllAcks.add(Long.toHexString(msg.SCN_ID)); - Messages.add(msg); - AllAcks.add(Long.toHexString(msg.SCN_ID)); + while (Messages.size()>SCNSettings.inst().LocalCacheSize) Messages.remove(0); + } - while (Messages.size()>SCNSettings.inst().LocalCacheSize) Messages.remove(0); + if (Messages.size()>1 && Messages.get(Messages.size()-2).Timestamp < msg.Timestamp) + { + // quick save - e.putInt( "message_count", count+1); - e.putLong( "message["+count+"].timestamp", time); - e.putString("message["+count+"].title", title); - e.putString("message["+count+"].content", content); - e.putInt( "message["+count+"].priority", pe.ID); - e.putLong( "message["+count+"].scnid", scnid); + SharedPreferences.Editor e = sharedPref.edit(); - e.putStringSet("acks", AllAcks); + e.putInt( "message_count", count+1); + e.putLong( "message["+count+"].timestamp", time); + e.putString("message["+count+"].title", title); + e.putString("message["+count+"].content", content); + e.putInt( "message["+count+"].priority", pe.ID); + e.putLong( "message["+count+"].scnid", scnid); + + e.putStringSet("acks", AllAcks); + + e.apply(); + } + else + { + // full save + + fullSave(); // does sort in here + } - e.apply(); for (WeakReference ref : _listener) { @@ -90,9 +112,12 @@ public class CMessageList if (!run) { - Messages.add(new CMessage(scnid, time, title, content, pe)); - AllAcks.add(Long.toHexString(msg.SCN_ID)); - fullSave(); + synchronized (msg_lock) + { + Messages.add(new CMessage(scnid, time, title, content, pe)); + AllAcks.add(Long.toHexString(msg.SCN_ID)); + } + fullSave(); // does sort in here } return msg; @@ -114,31 +139,39 @@ public class CMessageList public void fullSave() { - SharedPreferences sharedPref = SCNApp.getContext().getSharedPreferences("CMessageList", Context.MODE_PRIVATE); - SharedPreferences.Editor e = sharedPref.edit(); - - e.clear(); - - e.putInt("message_count", Messages.size()); - - for (int i = 0; i < Messages.size(); i++) + synchronized (msg_lock) { - e.putLong( "message["+i+"].timestamp", Messages.get(i).Timestamp); - e.putString("message["+i+"].title", Messages.get(i).Title); - e.putString("message["+i+"].content", Messages.get(i).Content); - e.putInt( "message["+i+"].priority", Messages.get(i).Priority.ID); - e.putLong( "message["+i+"].scnid", Messages.get(i).SCN_ID); + CollectionHelper.sort_inplace(Messages, (a,b) -> Long.compare(a.Timestamp, b.Timestamp)); + + SharedPreferences sharedPref = SCNApp.getContext().getSharedPreferences("CMessageList", Context.MODE_PRIVATE); + SharedPreferences.Editor e = sharedPref.edit(); + + e.clear(); + + e.putInt("message_count", Messages.size()); + + for (int i = 0; i < Messages.size(); i++) + { + e.putLong( "message["+i+"].timestamp", Messages.get(i).Timestamp); + e.putString("message["+i+"].title", Messages.get(i).Title); + e.putString("message["+i+"].content", Messages.get(i).Content); + e.putInt( "message["+i+"].priority", Messages.get(i).Priority.ID); + e.putLong( "message["+i+"].scnid", Messages.get(i).SCN_ID); + } + + e.putStringSet("acks", AllAcks); + + e.apply(); } - - e.putStringSet("acks", AllAcks); - - e.apply(); } public CMessage tryGet(int pos) { - if (pos < 0 || pos >= Messages.size()) return null; - return Messages.get(pos); + synchronized (msg_lock) + { + if (pos < 0 || pos >= Messages.size()) return null; + return Messages.get(pos); + } } public CMessage tryGetFromBack(int pos) @@ -148,7 +181,10 @@ public class CMessageList public int size() { - return Messages.size(); + synchronized (msg_lock) + { + return Messages.size(); + } } public void register(MessageAdapter adp) @@ -167,18 +203,30 @@ public class CMessageList public boolean isAck(long id) { - return AllAcks.contains(Long.toHexString(id)); + synchronized (msg_lock) + { + return AllAcks.contains(Long.toHexString(id)); + } } - public void remove(int index) + public CMessage removeFromBack(int pos) { - Messages.remove(index); - fullSave(); + CMessage r; + synchronized (msg_lock) + { + int index = Messages.size() - pos - 1; + r = Messages.remove(index); + } + fullSave(); // does sort in here + return r; } public void insert(int index, CMessage item) { - Messages.add(index, item); - fullSave(); + synchronized (msg_lock) + { + Messages.add(index, item); + } + fullSave(); // does sort in here } } diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/view/MessageAdapter.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/view/MessageAdapter.java index e83ea5e..e73bd54 100644 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/view/MessageAdapter.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/view/MessageAdapter.java @@ -86,16 +86,17 @@ public class MessageAdapter extends RecyclerView.Adapter manLayout.smoothScrollToPosition(viewRecycler, null, 0); } - public void removeItem(int position) + public CMessage removeItem(int position) { - CMessageList.inst().remove(position); - notifyItemRemoved(position); + CMessage i = CMessageList.inst().removeFromBack(position); + notifyDataSetChanged(); + return i; } public void restoreItem(CMessage item, int position) { CMessageList.inst().insert(position, item); - notifyItemInserted(position); + notifyDataSetChanged(); } public class MessagePresenter extends RecyclerView.ViewHolder implements View.OnClickListener diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/view/NotificationsFragment.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/view/NotificationsFragment.java index dc39167..554162c 100644 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/view/NotificationsFragment.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/view/NotificationsFragment.java @@ -65,13 +65,11 @@ public class NotificationsFragment extends Fragment implements MessageAdapterTou { if (viewHolder instanceof MessageAdapter.MessagePresenter) { - final CMessage deletedItem = CMessageList.inst().tryGet(viewHolder.getAdapterPosition()); final int deletedIndex = viewHolder.getAdapterPosition(); + final CMessage deletedItem = adpMessages.removeItem(viewHolder.getAdapterPosition()); String name = deletedItem.Title; - adpMessages.removeItem(viewHolder.getAdapterPosition()); - Snackbar snackbar = Snackbar.make(SCNApp.getMainActivity().layoutRoot, name + " removed", Snackbar.LENGTH_LONG); snackbar.setAction("UNDO", view -> adpMessages.restoreItem(deletedItem, deletedIndex)); snackbar.setActionTextColor(Color.YELLOW);