From e586370fb8ca611f9737904b825a4fd184fce6b2 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 11 May 2019 07:32:21 +0200 Subject: [PATCH] Fixed resetting order --- .../java/eu/faircode/email/FragmentOrder.java | 90 ++++++++++--------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentOrder.java b/app/src/main/java/eu/faircode/email/FragmentOrder.java index d36c6ff888..06de210a8b 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOrder.java +++ b/app/src/main/java/eu/faircode/email/FragmentOrder.java @@ -153,47 +153,8 @@ public class FragmentOrder extends FragmentBase { public void onPause() { super.onPause(); - if (dirty) { - List items = adapter.getItems(); - - List order = new ArrayList<>(); - for (int i = 0; i < items.size(); i++) - order.add(items.get(i).getSortId()); - - Bundle args = new Bundle(); - args.putString("class", clazz); - args.putLongArray("order", Helper.toLongArray(order)); - - new SimpleTask() { - @Override - protected Void onExecute(Context context, Bundle args) { - final String clazz = args.getString("class"); - final long[] order = args.getLongArray("order"); - - final DB db = DB.getInstance(context); - db.runInTransaction(new Runnable() { - @Override - public void run() { - for (int i = 0; i < order.length; i++) - if (EntityAccount.class.getName().equals(clazz)) - db.account().setAccountOrder(order[i], i); - else if (TupleFolderSort.class.getName().equals(clazz)) - db.folder().setFolderOrder(order[i], i); - else - throw new IllegalArgumentException("Unknown class=" + clazz); - } - }); - - return null; - } - - @Override - protected void onException(Bundle args, Throwable ex) { - Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex); - - } - }.execute(getContext(), getViewLifecycleOwner(), args, "order:set"); - } + if (dirty) + update(false); } @Override @@ -215,6 +176,53 @@ public class FragmentOrder extends FragmentBase { private void onMenuResetOrder() { adapter.onReset(); + dirty = false; + update(true); + } + + private void update(boolean reset) { + List items = adapter.getItems(); + + List order = new ArrayList<>(); + for (int i = 0; i < items.size(); i++) + order.add(items.get(i).getSortId()); + + Bundle args = new Bundle(); + args.putString("class", clazz); + args.putBoolean("reset", reset); + args.putLongArray("ids", Helper.toLongArray(order)); + + new SimpleTask() { + @Override + protected Void onExecute(Context context, Bundle args) { + final String clazz = args.getString("class"); + final boolean reset = args.getBoolean("reset"); + final long[] ids = args.getLongArray("ids"); + + final DB db = DB.getInstance(context); + db.runInTransaction(new Runnable() { + @Override + public void run() { + for (int i = 0; i < ids.length; i++) + if (EntityAccount.class.getName().equals(clazz)) + db.account().setAccountOrder(ids[i], reset ? null : i); + else if (TupleFolderSort.class.getName().equals(clazz)) + db.folder().setFolderOrder(ids[i], reset ? null : i); + else + throw new IllegalArgumentException("Unknown class=" + clazz); + } + }); + + return null; + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex); + + } + }.execute(getContext(), getViewLifecycleOwner(), args, "order:set"); + } private ItemTouchHelper.Callback touchHelper = new ItemTouchHelper.Callback() {