Added unified folder reordering

This commit is contained in:
M66B 2019-04-29 21:32:35 +02:00
parent 2414916b89
commit 6ee85a0284
5 changed files with 141 additions and 0 deletions

View File

@ -67,6 +67,7 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
private LayoutInflater inflater;
private LifecycleOwner owner;
private boolean show_hidden;
private boolean reorder = false;
private long account;
private int level;
@ -367,6 +368,9 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
@Override
public boolean onLongClick(View v) {
if (reorder)
return false;
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return false;
@ -631,6 +635,12 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
@Override
public int compare(TupleFolderEx f1, TupleFolderEx f2) {
if (account < 0) {
int o = Integer.compare(
f1.order == null ? -1 : f1.order,
f2.order == null ? -1 : f2.order);
if (o != 0)
return o;
String name1 = f1.getDisplayName(context);
String name2 = f2.getDisplayName(context);
int n = collator.compare(name1, name2);
@ -694,6 +704,10 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
}
}
void setReorder(boolean reorder) {
this.reorder = reorder;
}
private class DiffCallback extends DiffUtil.Callback {
private List<TupleFolderEx> prev = new ArrayList<>();
private List<TupleFolderEx> next = new ArrayList<>();
@ -738,6 +752,51 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
return items.size();
}
void onMove(int from, int to) {
if (from < 0 || from >= 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<Long> 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<Void>() {
@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) {

View File

@ -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);

View File

@ -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<Void>() {
@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) {
}
});
}

View File

@ -14,4 +14,15 @@
android:checkable="true"
android:title="@string/title_show_folders"
app:showAsAction="never" />
<item
android:id="@+id/menu_reorder"
android:checkable="true"
android:title="@string/title_reorder"
app:showAsAction="never" />
<item
android:id="@+id/menu_reset_order"
android:title="@string/title_reset_order"
app:showAsAction="never" />
</menu>

View File

@ -612,6 +612,7 @@
<string name="title_report">Report</string>
<string name="title_fix">Fix</string>
<string name="title_enable">Enable</string>
<string name="title_reorder">Reorder</string>
<string name="title_no_ask_again">Do not ask this again</string>
<string name="title_no_body">No message text found</string>
<string name="title_no_charset">Unsupported encoding: %1$s</string>