diff --git a/app/src/main/java/eu/faircode/email/AdapterFolder.java b/app/src/main/java/eu/faircode/email/AdapterFolder.java index e00576b455..28aa3d630f 100644 --- a/app/src/main/java/eu/faircode/email/AdapterFolder.java +++ b/app/src/main/java/eu/faircode/email/AdapterFolder.java @@ -67,6 +67,7 @@ public class AdapterFolder extends RecyclerView.Adapter prev = new ArrayList<>(); private List next = new ArrayList<>(); @@ -738,6 +752,51 @@ public class AdapterFolder extends RecyclerView.Adapter= items.size() || + to < 0 || to >= items.size()) + return; + + if (from < to) + for (int i = from; i < to; i++) + Collections.swap(items, i, i + 1); + else + for (int i = from; i > to; i--) + Collections.swap(items, i, i - 1); + notifyItemMoved(from, to); + + List order = new ArrayList<>(); + for (int i = 0; i < items.size(); i++) + order.add(items.get(i).id); + + Bundle args = new Bundle(); + args.putLongArray("order", Helper.toLongArray(order)); + + new SimpleTask() { + @Override + protected Void onExecute(Context context, Bundle args) { + 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++) + db.folder().setFolderOrder(order[i], i); + } + }); + + return null; + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Helper.unexpectedError(context, owner, ex); + + } + }.execute(context, owner, args, "folders:order"); + } + @Override @NonNull public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { diff --git a/app/src/main/java/eu/faircode/email/DaoFolder.java b/app/src/main/java/eu/faircode/email/DaoFolder.java index fba2c90b52..23942fb3b8 100644 --- a/app/src/main/java/eu/faircode/email/DaoFolder.java +++ b/app/src/main/java/eu/faircode/email/DaoFolder.java @@ -222,6 +222,9 @@ public interface DaoFolder { @Query("UPDATE folder SET last_sync = :last_sync WHERE id = :id") int setFolderSync(long id, long last_sync); + @Query("UPDATE folder SET `order` = NULL") + int resetFolderOrder(); + @Query("UPDATE folder SET tbc = null WHERE id = :id") int resetFolderTbc(long id); diff --git a/app/src/main/java/eu/faircode/email/FragmentFolders.java b/app/src/main/java/eu/faircode/email/FragmentFolders.java index fc80ef569f..7004b43fa9 100644 --- a/app/src/main/java/eu/faircode/email/FragmentFolders.java +++ b/app/src/main/java/eu/faircode/email/FragmentFolders.java @@ -43,6 +43,7 @@ import androidx.lifecycle.Observer; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.preference.PreferenceManager; import androidx.recyclerview.widget.DividerItemDecoration; +import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; @@ -69,6 +70,7 @@ public class FragmentFolders extends FragmentBase { private long account; private boolean show_hidden = false; + private boolean reorder = false; private String searching = null; private AdapterFolder adapter; @@ -401,6 +403,9 @@ public class FragmentFolders extends FragmentBase { @Override public void onPrepareOptionsMenu(Menu menu) { menu.findItem(R.id.menu_show_hidden).setChecked(show_hidden); + menu.findItem(R.id.menu_reorder).setChecked(reorder); + menu.findItem(R.id.menu_reorder).setVisible(account < 0); // unified folders + menu.findItem(R.id.menu_reset_order).setVisible(account < 0); // unified folders super.onPrepareOptionsMenu(menu); } @@ -410,6 +415,12 @@ public class FragmentFolders extends FragmentBase { case R.id.menu_show_hidden: onMenuShowHidden(); return true; + case R.id.menu_reorder: + onReorder(); + return true; + case R.id.menu_reset_order: + onResetOrder(); + return true; default: return super.onOptionsItemSelected(item); } @@ -421,4 +432,60 @@ public class FragmentFolders extends FragmentBase { getActivity().invalidateOptionsMenu(); adapter.setShowHidden(show_hidden); } + + private void onReorder() { + reorder = !reorder; + getActivity().invalidateOptionsMenu(); + + swipeRefresh.setEnabled(!reorder); + adapter.setReorder(reorder); + if (reorder) + touchHelper.attachToRecyclerView(rvFolder); + else + touchHelper.attachToRecyclerView(null); + } + + private void onResetOrder() { + Bundle args = new Bundle(); + + new SimpleTask() { + @Override + protected Void onExecute(Context context, Bundle args) { + DB db = DB.getInstance(context); + db.folder().resetFolderOrder(); + return null; + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex); + } + }.execute(this, args, "folders:reset"); + } + + private ItemTouchHelper touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() { + @Override + public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { + int flags = 0; + int pos = viewHolder.getAdapterPosition(); + if (pos != RecyclerView.NO_POSITION) { + if (pos - 1 >= 0) + flags |= ItemTouchHelper.UP; + if (pos + 1 < rvFolder.getAdapter().getItemCount()) + flags |= ItemTouchHelper.DOWN; + } + + return makeMovementFlags(flags, 0); + } + + @Override + public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder source, @NonNull RecyclerView.ViewHolder target) { + ((AdapterFolder) rvFolder.getAdapter()).onMove(source.getAdapterPosition(), target.getAdapterPosition()); + return true; + } + + @Override + public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { + } + }); } diff --git a/app/src/main/res/menu/menu_folders.xml b/app/src/main/res/menu/menu_folders.xml index b621a2ae0d..6a68c36741 100644 --- a/app/src/main/res/menu/menu_folders.xml +++ b/app/src/main/res/menu/menu_folders.xml @@ -14,4 +14,15 @@ android:checkable="true" android:title="@string/title_show_folders" app:showAsAction="never" /> + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 275a00b1e8..04e4d4e2c1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -612,6 +612,7 @@ Report Fix Enable + Reorder Do not ask this again No message text found Unsupported encoding: %1$s