message delete problems

This commit is contained in:
Mike Schwörer 2018-11-17 16:45:42 +01:00
parent a8a074907b
commit 28efeea30b
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
4 changed files with 114 additions and 62 deletions

View File

@ -24,6 +24,11 @@ public final class CollectionHelper
return output; return output;
} }
public static <T> void sort_inplace(List<T> input, Comparator<T> comparator)
{
Collections.sort(input, comparator);
}
public static <T, U extends Comparable<U>> List<T> sort(List<T> input, Func1to1<T, U> mapper) public static <T, U extends Comparable<U>> List<T> sort(List<T> input, Func1to1<T, U> mapper)
{ {
return sort(input, mapper, 1); return sort(input, mapper, 1);

View File

@ -3,17 +3,21 @@ package com.blackforestbytes.simplecloudnotifier.model;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import com.blackforestbytes.simplecloudnotifier.lib.collections.CollectionHelper;
import com.blackforestbytes.simplecloudnotifier.lib.string.Str; import com.blackforestbytes.simplecloudnotifier.lib.string.Str;
import com.blackforestbytes.simplecloudnotifier.view.MessageAdapter; import com.blackforestbytes.simplecloudnotifier.view.MessageAdapter;
import com.blackforestbytes.simplecloudnotifier.SCNApp; import com.blackforestbytes.simplecloudnotifier.SCNApp;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
public class CMessageList public class CMessageList
{ {
private final Object msg_lock = new Object();
public ArrayList<CMessage> Messages; public ArrayList<CMessage> Messages;
public Set<String> AllAcks; public Set<String> AllAcks;
@ -32,23 +36,26 @@ public class CMessageList
private CMessageList() private CMessageList()
{ {
Messages = new ArrayList<>(); synchronized (msg_lock)
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++)
{ {
long time = sharedPref.getLong("message["+i+"].timestamp", 0); Messages = new ArrayList<>();
String title = sharedPref.getString("message["+i+"].title", ""); AllAcks = new HashSet<>();
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)); 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) 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); SharedPreferences sharedPref = SCNApp.getContext().getSharedPreferences("CMessageList", Context.MODE_PRIVATE);
int count = sharedPref.getInt("message_count", 0); 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); while (Messages.size()>SCNSettings.inst().LocalCacheSize) Messages.remove(0);
AllAcks.add(Long.toHexString(msg.SCN_ID)); }
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); SharedPreferences.Editor e = sharedPref.edit();
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.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<MessageAdapter> ref : _listener) for (WeakReference<MessageAdapter> ref : _listener)
{ {
@ -90,9 +112,12 @@ public class CMessageList
if (!run) if (!run)
{ {
Messages.add(new CMessage(scnid, time, title, content, pe)); synchronized (msg_lock)
AllAcks.add(Long.toHexString(msg.SCN_ID)); {
fullSave(); Messages.add(new CMessage(scnid, time, title, content, pe));
AllAcks.add(Long.toHexString(msg.SCN_ID));
}
fullSave(); // does sort in here
} }
return msg; return msg;
@ -114,31 +139,39 @@ public class CMessageList
public void fullSave() public void fullSave()
{ {
SharedPreferences sharedPref = SCNApp.getContext().getSharedPreferences("CMessageList", Context.MODE_PRIVATE); synchronized (msg_lock)
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); CollectionHelper.sort_inplace(Messages, (a,b) -> Long.compare(a.Timestamp, b.Timestamp));
e.putString("message["+i+"].title", Messages.get(i).Title);
e.putString("message["+i+"].content", Messages.get(i).Content); SharedPreferences sharedPref = SCNApp.getContext().getSharedPreferences("CMessageList", Context.MODE_PRIVATE);
e.putInt( "message["+i+"].priority", Messages.get(i).Priority.ID); SharedPreferences.Editor e = sharedPref.edit();
e.putLong( "message["+i+"].scnid", Messages.get(i).SCN_ID);
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) public CMessage tryGet(int pos)
{ {
if (pos < 0 || pos >= Messages.size()) return null; synchronized (msg_lock)
return Messages.get(pos); {
if (pos < 0 || pos >= Messages.size()) return null;
return Messages.get(pos);
}
} }
public CMessage tryGetFromBack(int pos) public CMessage tryGetFromBack(int pos)
@ -148,7 +181,10 @@ public class CMessageList
public int size() public int size()
{ {
return Messages.size(); synchronized (msg_lock)
{
return Messages.size();
}
} }
public void register(MessageAdapter adp) public void register(MessageAdapter adp)
@ -167,18 +203,30 @@ public class CMessageList
public boolean isAck(long id) 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); CMessage r;
fullSave(); 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) public void insert(int index, CMessage item)
{ {
Messages.add(index, item); synchronized (msg_lock)
fullSave(); {
Messages.add(index, item);
}
fullSave(); // does sort in here
} }
} }

View File

@ -86,16 +86,17 @@ public class MessageAdapter extends RecyclerView.Adapter
manLayout.smoothScrollToPosition(viewRecycler, null, 0); manLayout.smoothScrollToPosition(viewRecycler, null, 0);
} }
public void removeItem(int position) public CMessage removeItem(int position)
{ {
CMessageList.inst().remove(position); CMessage i = CMessageList.inst().removeFromBack(position);
notifyItemRemoved(position); notifyDataSetChanged();
return i;
} }
public void restoreItem(CMessage item, int position) public void restoreItem(CMessage item, int position)
{ {
CMessageList.inst().insert(position, item); CMessageList.inst().insert(position, item);
notifyItemInserted(position); notifyDataSetChanged();
} }
public class MessagePresenter extends RecyclerView.ViewHolder implements View.OnClickListener public class MessagePresenter extends RecyclerView.ViewHolder implements View.OnClickListener

View File

@ -65,13 +65,11 @@ public class NotificationsFragment extends Fragment implements MessageAdapterTou
{ {
if (viewHolder instanceof MessageAdapter.MessagePresenter) if (viewHolder instanceof MessageAdapter.MessagePresenter)
{ {
final CMessage deletedItem = CMessageList.inst().tryGet(viewHolder.getAdapterPosition());
final int deletedIndex = viewHolder.getAdapterPosition(); final int deletedIndex = viewHolder.getAdapterPosition();
final CMessage deletedItem = adpMessages.removeItem(viewHolder.getAdapterPosition());
String name = deletedItem.Title; String name = deletedItem.Title;
adpMessages.removeItem(viewHolder.getAdapterPosition());
Snackbar snackbar = Snackbar.make(SCNApp.getMainActivity().layoutRoot, name + " removed", Snackbar.LENGTH_LONG); Snackbar snackbar = Snackbar.make(SCNApp.getMainActivity().layoutRoot, name + " removed", Snackbar.LENGTH_LONG);
snackbar.setAction("UNDO", view -> adpMessages.restoreItem(deletedItem, deletedIndex)); snackbar.setAction("UNDO", view -> adpMessages.restoreItem(deletedItem, deletedIndex));
snackbar.setActionTextColor(Color.YELLOW); snackbar.setActionTextColor(Color.YELLOW);