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) {
String left;
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
left = swipes.get(0).left_name;
String right;
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
right = swipes.get(0).right_name;
@ -196,16 +196,6 @@ public class FragmentDialogAccount extends FragmentDialogBase {
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>>() {

@ -24,6 +24,7 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
@ -46,38 +47,27 @@ public class FragmentDialogSwipes extends FragmentDialogBase {
@NonNull
@Override
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);
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);
spLeft.setAdapter(adapter);
spRight.setAdapter(adapter);
List<EntityFolder> folders = FragmentAccount.getFolderActions(getContext());
adapter.addAll(getFolderActions(context));
EntityFolder trash = new EntityFolder();
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());
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int leftPos = prefs.getInt("swipe_left_default", 2); // Trash
int rightPos = prefs.getInt("swipe_right_default", 1); // Archive
spLeft.setSelection(leftPos);
spRight.setSelection(rightPos);
return new AlertDialog.Builder(getContext())
return new AlertDialog.Builder(context)
.setView(dview)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
@ -97,16 +87,9 @@ public class FragmentDialogSwipes extends FragmentDialogBase {
.putBoolean("button_hide", true)
.apply();
Bundle args = new Bundle();
args.putLong("left", left == null ? 0 : left.id);
args.putLong("right", right == null ? 0 : right.id);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long left = args.getLong("left");
long right = args.getLong("right");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
@ -114,10 +97,7 @@ public class FragmentDialogSwipes extends FragmentDialogBase {
List<EntityAccount> accounts = db.account().getAccounts();
for (EntityAccount account : accounts)
if (account.protocol == EntityAccount.TYPE_IMAP)
db.account().setAccountSwipes(
account.id,
getAction(context, left, account.id),
getAction(context, right, account.id));
setDefaultFolderActions(context, account.id);
db.setTransactionSuccessful();
} finally {
@ -129,15 +109,60 @@ public class FragmentDialogSwipes extends FragmentDialogBase {
@Override
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
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(context, getViewLifecycleOwner(), new Bundle(), "dialog:swipe");
}
})
.setNegativeButton(android.R.string.cancel, null)
.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);
private Long getAction(Context context, long selection, long account) {
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)
@ -145,14 +170,8 @@ public class FragmentDialogSwipes extends FragmentDialogBase {
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);
EntityFolder folder = db.folder().getFolderByType(account, type);
return (folder == null ? null : folder.id);
}
}
}.execute(getContext(), getViewLifecycleOwner(), args, "dialog:swipe");
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
}
}

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

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

@ -33,6 +33,7 @@ import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.util.Pair;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
@ -621,13 +622,7 @@ public class FragmentQuickSetup extends FragmentBase {
}
// Set swipe left/right folder
for (EntityFolder folder : folders)
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);
FragmentDialogSwipes.setDefaultFolderActions(context, account.id);
// Create identity
EntityIdentity identity = new EntityIdentity();

Loading…
Cancel
Save