Use default swipe actions for new account

pull/212/head
M66B 2 years ago
parent 73d6cccda7
commit f209c689ce

@ -179,13 +179,13 @@ public class FragmentDialogAccount extends FragmentDialogBase {
if (swipes != null && swipes.size() == 1) { if (swipes != null && swipes.size() == 1) {
String left; String left;
if (swipes.get(0).swipe_left != null && swipes.get(0).swipe_left < 0) if (swipes.get(0).swipe_left != null && swipes.get(0).swipe_left < 0)
left = getSwipeTitle(context, (long) swipes.get(0).swipe_left); left = FragmentDialogSwipes.getActionTitle(context, swipes.get(0).swipe_left);
else else
left = swipes.get(0).left_name; left = swipes.get(0).left_name;
String right; String right;
if (swipes.get(0).swipe_right != null && swipes.get(0).swipe_right < 0) if (swipes.get(0).swipe_right != null && swipes.get(0).swipe_right < 0)
right = getSwipeTitle(context, (long) swipes.get(0).swipe_right); right = FragmentDialogSwipes.getActionTitle(context, swipes.get(0).swipe_right);
else else
right = swipes.get(0).right_name; right = swipes.get(0).right_name;
@ -196,16 +196,6 @@ public class FragmentDialogAccount extends FragmentDialogBase {
tvRight.setText("?"); tvRight.setText("?");
} }
} }
private String getSwipeTitle(Context context, long type) {
if (type == EntityMessage.SWIPE_ACTION_SEEN)
return context.getString(R.string.title_seen);
if (type == EntityMessage.SWIPE_ACTION_DELETE)
return context.getString(R.string.title_delete_permanently);
return "???";
}
}); });
db.folder().liveSystemFolders(account).observe(this, new Observer<List<EntityFolder>>() { db.folder().liveSystemFolders(account).observe(this, new Observer<List<EntityFolder>>() {

@ -24,6 +24,7 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.util.Pair;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
@ -46,38 +47,27 @@ public class FragmentDialogSwipes extends FragmentDialogBase {
@NonNull @NonNull
@Override @Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_swipes, null); final Context context = getContext();
View dview = LayoutInflater.from(context).inflate(R.layout.dialog_swipes, null);
spLeft = dview.findViewById(R.id.spLeft); spLeft = dview.findViewById(R.id.spLeft);
spRight = dview.findViewById(R.id.spRight); spRight = dview.findViewById(R.id.spRight);
adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, new ArrayList<EntityFolder>()); adapter = new ArrayAdapter<>(context, R.layout.spinner_item1, android.R.id.text1, new ArrayList<EntityFolder>());
adapter.setDropDownViewResource(R.layout.spinner_item1_dropdown); adapter.setDropDownViewResource(R.layout.spinner_item1_dropdown);
spLeft.setAdapter(adapter); spLeft.setAdapter(adapter);
spRight.setAdapter(adapter); spRight.setAdapter(adapter);
List<EntityFolder> folders = FragmentAccount.getFolderActions(getContext()); adapter.addAll(getFolderActions(context));
EntityFolder trash = new EntityFolder(); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
trash.id = 2L;
trash.name = getString(R.string.title_trash);
folders.add(1, trash);
EntityFolder archive = new EntityFolder();
archive.id = 1L;
archive.name = getString(R.string.title_archive);
folders.add(1, archive);
adapter.addAll(folders);
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
int leftPos = prefs.getInt("swipe_left_default", 2); // Trash int leftPos = prefs.getInt("swipe_left_default", 2); // Trash
int rightPos = prefs.getInt("swipe_right_default", 1); // Archive int rightPos = prefs.getInt("swipe_right_default", 1); // Archive
spLeft.setSelection(leftPos); spLeft.setSelection(leftPos);
spRight.setSelection(rightPos); spRight.setSelection(rightPos);
return new AlertDialog.Builder(getContext()) return new AlertDialog.Builder(context)
.setView(dview) .setView(dview)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override @Override
@ -97,16 +87,9 @@ public class FragmentDialogSwipes extends FragmentDialogBase {
.putBoolean("button_hide", true) .putBoolean("button_hide", true)
.apply(); .apply();
Bundle args = new Bundle();
args.putLong("left", left == null ? 0 : left.id);
args.putLong("right", right == null ? 0 : right.id);
new SimpleTask<Void>() { new SimpleTask<Void>() {
@Override @Override
protected Void onExecute(Context context, Bundle args) { protected Void onExecute(Context context, Bundle args) {
long left = args.getLong("left");
long right = args.getLong("right");
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
try { try {
db.beginTransaction(); db.beginTransaction();
@ -114,10 +97,7 @@ public class FragmentDialogSwipes extends FragmentDialogBase {
List<EntityAccount> accounts = db.account().getAccounts(); List<EntityAccount> accounts = db.account().getAccounts();
for (EntityAccount account : accounts) for (EntityAccount account : accounts)
if (account.protocol == EntityAccount.TYPE_IMAP) if (account.protocol == EntityAccount.TYPE_IMAP)
db.account().setAccountSwipes( setDefaultFolderActions(context, account.id);
account.id,
getAction(context, left, account.id),
getAction(context, right, account.id));
db.setTransactionSuccessful(); db.setTransactionSuccessful();
} finally { } finally {
@ -129,30 +109,69 @@ public class FragmentDialogSwipes extends FragmentDialogBase {
@Override @Override
protected void onExecuted(Bundle args, Void data) { protected void onExecuted(Bundle args, Void data) {
ToastEx.makeText(getContext(), R.string.title_completed, Toast.LENGTH_LONG).show(); ToastEx.makeText(context, R.string.title_completed, Toast.LENGTH_LONG).show();
} }
@Override @Override
protected void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex); Log.unexpectedError(getParentFragmentManager(), ex);
} }
}.execute(context, getViewLifecycleOwner(), new Bundle(), "dialog:swipe");
private Long getAction(Context context, long selection, long account) {
if (selection < 0)
return selection;
else if (selection == 0)
return null;
else {
DB db = DB.getInstance(context);
String type = (selection == 2 ? EntityFolder.TRASH : EntityFolder.ARCHIVE);
EntityFolder archive = db.folder().getFolderByType(account, type);
return (archive == null ? null : archive.id);
}
}
}.execute(getContext(), getViewLifecycleOwner(), args, "dialog:swipe");
} }
}) })
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
.create(); .create();
} }
static void setDefaultFolderActions(Context context, long account) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int leftPos = prefs.getInt("swipe_left_default", 2); // Trash
int rightPos = prefs.getInt("swipe_right_default", 1); // Archive
List<EntityFolder> actions = getFolderActions(context);
EntityFolder left = (leftPos < 0 || leftPos >= actions.size() ? null : actions.get(leftPos));
EntityFolder right = (rightPos < 0 || rightPos >= actions.size() ? null : actions.get(rightPos));
DB db = DB.getInstance(context);
db.account().setAccountSwipes(account,
getAction(context, left == null ? 0 : left.id, account),
getAction(context, right == null ? 0 : right.id, account));
}
static List<EntityFolder> getFolderActions(Context context) {
List<EntityFolder> folders = FragmentAccount.getFolderActions(context);
EntityFolder trash = new EntityFolder();
trash.id = 2L;
trash.name = context.getString(R.string.title_trash);
folders.add(1, trash);
EntityFolder archive = new EntityFolder();
archive.id = 1L;
archive.name = context.getString(R.string.title_archive);
folders.add(1, archive);
return folders;
}
static String getActionTitle(Context context, long id) {
for (EntityFolder action : getFolderActions(context))
if (action.id.equals(id))
return action.name;
return "???";
}
private static Long getAction(Context context, long selection, long account) {
if (selection < 0)
return selection;
else if (selection == 0)
return null;
else {
DB db = DB.getInstance(context);
String type = (selection == 2 ? EntityFolder.TRASH : EntityFolder.ARCHIVE);
EntityFolder folder = db.folder().getFolderByType(account, type);
return (folder == null ? null : folder.id);
}
}
} }

@ -557,13 +557,8 @@ public class FragmentGmail extends FragmentBase {
if (pop) { if (pop) {
account.swipe_left = EntityMessage.SWIPE_ACTION_DELETE; account.swipe_left = EntityMessage.SWIPE_ACTION_DELETE;
account.swipe_right = EntityMessage.SWIPE_ACTION_SEEN; account.swipe_right = EntityMessage.SWIPE_ACTION_SEEN;
} else { } else
for (EntityFolder folder : folders) FragmentDialogSwipes.setDefaultFolderActions(context, account.id);
if (EntityFolder.TRASH.equals(folder.type))
account.swipe_left = folder.id;
else if (EntityFolder.ARCHIVE.equals(folder.type))
account.swipe_right = folder.id;
}
db.account().updateAccount(account); db.account().updateAccount(account);

@ -943,13 +943,8 @@ public class FragmentOAuth extends FragmentBase {
if (pop) { if (pop) {
account.swipe_left = EntityMessage.SWIPE_ACTION_DELETE; account.swipe_left = EntityMessage.SWIPE_ACTION_DELETE;
account.swipe_right = EntityMessage.SWIPE_ACTION_SEEN; account.swipe_right = EntityMessage.SWIPE_ACTION_SEEN;
} else { } else
for (EntityFolder folder : folders) FragmentDialogSwipes.setDefaultFolderActions(context, account.id);
if (EntityFolder.TRASH.equals(folder.type))
account.swipe_left = folder.id;
else if (EntityFolder.ARCHIVE.equals(folder.type))
account.swipe_right = folder.id;
}
db.account().updateAccount(account); db.account().updateAccount(account);

@ -33,6 +33,7 @@ import android.text.Editable;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.util.Pair;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -621,13 +622,7 @@ public class FragmentQuickSetup extends FragmentBase {
} }
// Set swipe left/right folder // Set swipe left/right folder
for (EntityFolder folder : folders) FragmentDialogSwipes.setDefaultFolderActions(context, account.id);
if (EntityFolder.TRASH.equals(folder.type))
account.swipe_left = folder.id;
else if (EntityFolder.ARCHIVE.equals(folder.type))
account.swipe_right = folder.id;
db.account().updateAccount(account);
// Create identity // Create identity
EntityIdentity identity = new EntityIdentity(); EntityIdentity identity = new EntityIdentity();

Loading…
Cancel
Save