message delete problems
This commit is contained in:
parent
a8a074907b
commit
28efeea30b
@ -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);
|
||||||
|
@ -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;
|
||||||
|
|
||||||
@ -31,6 +35,8 @@ public class CMessageList
|
|||||||
}
|
}
|
||||||
|
|
||||||
private CMessageList()
|
private CMessageList()
|
||||||
|
{
|
||||||
|
synchronized (msg_lock)
|
||||||
{
|
{
|
||||||
Messages = new ArrayList<>();
|
Messages = new ArrayList<>();
|
||||||
AllAcks = new HashSet<>();
|
AllAcks = new HashSet<>();
|
||||||
@ -50,6 +56,7 @@ public class CMessageList
|
|||||||
|
|
||||||
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,12 +67,19 @@ 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);
|
Messages.add(msg);
|
||||||
AllAcks.add(Long.toHexString(msg.SCN_ID));
|
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
|
||||||
|
|
||||||
|
SharedPreferences.Editor e = sharedPref.edit();
|
||||||
|
|
||||||
e.putInt( "message_count", count+1);
|
e.putInt( "message_count", count+1);
|
||||||
e.putLong( "message["+count+"].timestamp", time);
|
e.putLong( "message["+count+"].timestamp", time);
|
||||||
@ -77,6 +91,14 @@ public class CMessageList
|
|||||||
e.putStringSet("acks", AllAcks);
|
e.putStringSet("acks", AllAcks);
|
||||||
|
|
||||||
e.apply();
|
e.apply();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// full save
|
||||||
|
|
||||||
|
fullSave(); // does sort in here
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
for (WeakReference<MessageAdapter> ref : _listener)
|
for (WeakReference<MessageAdapter> ref : _listener)
|
||||||
{
|
{
|
||||||
@ -89,10 +111,13 @@ public class CMessageList
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!run)
|
if (!run)
|
||||||
|
{
|
||||||
|
synchronized (msg_lock)
|
||||||
{
|
{
|
||||||
Messages.add(new CMessage(scnid, time, title, content, pe));
|
Messages.add(new CMessage(scnid, time, title, content, pe));
|
||||||
AllAcks.add(Long.toHexString(msg.SCN_ID));
|
AllAcks.add(Long.toHexString(msg.SCN_ID));
|
||||||
fullSave();
|
}
|
||||||
|
fullSave(); // does sort in here
|
||||||
}
|
}
|
||||||
|
|
||||||
return msg;
|
return msg;
|
||||||
@ -114,6 +139,10 @@ public class CMessageList
|
|||||||
|
|
||||||
public void fullSave()
|
public void fullSave()
|
||||||
{
|
{
|
||||||
|
synchronized (msg_lock)
|
||||||
|
{
|
||||||
|
CollectionHelper.sort_inplace(Messages, (a,b) -> Long.compare(a.Timestamp, b.Timestamp));
|
||||||
|
|
||||||
SharedPreferences sharedPref = SCNApp.getContext().getSharedPreferences("CMessageList", Context.MODE_PRIVATE);
|
SharedPreferences sharedPref = SCNApp.getContext().getSharedPreferences("CMessageList", Context.MODE_PRIVATE);
|
||||||
SharedPreferences.Editor e = sharedPref.edit();
|
SharedPreferences.Editor e = sharedPref.edit();
|
||||||
|
|
||||||
@ -134,12 +163,16 @@ public class CMessageList
|
|||||||
|
|
||||||
e.apply();
|
e.apply();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public CMessage tryGet(int pos)
|
public CMessage tryGet(int pos)
|
||||||
|
{
|
||||||
|
synchronized (msg_lock)
|
||||||
{
|
{
|
||||||
if (pos < 0 || pos >= Messages.size()) return null;
|
if (pos < 0 || pos >= Messages.size()) return null;
|
||||||
return Messages.get(pos);
|
return Messages.get(pos);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public CMessage tryGetFromBack(int pos)
|
public CMessage tryGetFromBack(int pos)
|
||||||
{
|
{
|
||||||
@ -147,9 +180,12 @@ public class CMessageList
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int size()
|
public int size()
|
||||||
|
{
|
||||||
|
synchronized (msg_lock)
|
||||||
{
|
{
|
||||||
return Messages.size();
|
return Messages.size();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void register(MessageAdapter adp)
|
public void register(MessageAdapter adp)
|
||||||
{
|
{
|
||||||
@ -166,19 +202,31 @@ public class CMessageList
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isAck(long id)
|
public boolean isAck(long id)
|
||||||
|
{
|
||||||
|
synchronized (msg_lock)
|
||||||
{
|
{
|
||||||
return AllAcks.contains(Long.toHexString(id));
|
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)
|
||||||
|
{
|
||||||
|
synchronized (msg_lock)
|
||||||
{
|
{
|
||||||
Messages.add(index, item);
|
Messages.add(index, item);
|
||||||
fullSave();
|
}
|
||||||
|
fullSave(); // does sort in here
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user