Check account dirty, refactoring

pull/156/head
M66B 6 years ago
parent c885aa4927
commit f9b8afa988

@ -130,7 +130,7 @@ public class FragmentAccount extends FragmentBase {
private ArrayAdapter<EntityFolder> adapter; private ArrayAdapter<EntityFolder> adapter;
private Spinner spDrafts; private Spinner spDrafts;
private Spinner spSent; private Spinner spSent;
private Spinner spAll; private Spinner spArchive;
private Spinner spTrash; private Spinner spTrash;
private Spinner spJunk; private Spinner spJunk;
private Spinner spLeft; private Spinner spLeft;
@ -207,7 +207,7 @@ public class FragmentAccount extends FragmentBase {
spDrafts = view.findViewById(R.id.spDrafts); spDrafts = view.findViewById(R.id.spDrafts);
spSent = view.findViewById(R.id.spSent); spSent = view.findViewById(R.id.spSent);
spAll = view.findViewById(R.id.spAll); spArchive = view.findViewById(R.id.spArchive);
spTrash = view.findViewById(R.id.spTrash); spTrash = view.findViewById(R.id.spTrash);
spJunk = view.findViewById(R.id.spJunk); spJunk = view.findViewById(R.id.spJunk);
spLeft = view.findViewById(R.id.spLeft); spLeft = view.findViewById(R.id.spLeft);
@ -421,7 +421,7 @@ public class FragmentAccount extends FragmentBase {
btnSave.setOnClickListener(new View.OnClickListener() { btnSave.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
onSave(); onSave(false);
} }
}); });
@ -430,12 +430,20 @@ public class FragmentAccount extends FragmentBase {
spDrafts.setAdapter(adapter); spDrafts.setAdapter(adapter);
spSent.setAdapter(adapter); spSent.setAdapter(adapter);
spAll.setAdapter(adapter); spArchive.setAdapter(adapter);
spTrash.setAdapter(adapter); spTrash.setAdapter(adapter);
spJunk.setAdapter(adapter); spJunk.setAdapter(adapter);
spLeft.setAdapter(adapter); spLeft.setAdapter(adapter);
spRight.setAdapter(adapter); spRight.setAdapter(adapter);
addBackPressedListener(new ActivityBase.IBackPressedListener() {
@Override
public boolean onBackPressed() {
onSave(true);
return true;
}
});
// Initialize // Initialize
Helper.setViewsEnabled(view, false); Helper.setViewsEnabled(view, false);
@ -716,10 +724,10 @@ public class FragmentAccount extends FragmentBase {
}.execute(FragmentAccount.this, args, "account:check"); }.execute(FragmentAccount.this, args, "account:check");
} }
private void onSave() { private void onSave(boolean should) {
EntityFolder drafts = (EntityFolder) spDrafts.getSelectedItem(); EntityFolder drafts = (EntityFolder) spDrafts.getSelectedItem();
EntityFolder sent = (EntityFolder) spSent.getSelectedItem(); EntityFolder sent = (EntityFolder) spSent.getSelectedItem();
EntityFolder all = (EntityFolder) spAll.getSelectedItem(); EntityFolder archive = (EntityFolder) spArchive.getSelectedItem();
EntityFolder trash = (EntityFolder) spTrash.getSelectedItem(); EntityFolder trash = (EntityFolder) spTrash.getSelectedItem();
EntityFolder junk = (EntityFolder) spJunk.getSelectedItem(); EntityFolder junk = (EntityFolder) spJunk.getSelectedItem();
EntityFolder left = (EntityFolder) spLeft.getSelectedItem(); EntityFolder left = (EntityFolder) spLeft.getSelectedItem();
@ -729,8 +737,8 @@ public class FragmentAccount extends FragmentBase {
drafts = null; drafts = null;
if (sent != null && sent.type == null) if (sent != null && sent.type == null)
sent = null; sent = null;
if (all != null && all.type == null) if (archive != null && archive.type == null)
all = null; archive = null;
if (trash != null && trash.type == null) if (trash != null && trash.type == null)
trash = null; trash = null;
if (junk != null && junk.type == null) if (junk != null && junk.type == null)
@ -764,13 +772,15 @@ public class FragmentAccount extends FragmentBase {
args.putSerializable("drafts", drafts); args.putSerializable("drafts", drafts);
args.putSerializable("sent", sent); args.putSerializable("sent", sent);
args.putSerializable("all", all); args.putSerializable("archive", archive);
args.putSerializable("trash", trash); args.putSerializable("trash", trash);
args.putSerializable("junk", junk); args.putSerializable("junk", junk);
args.putSerializable("left", left); args.putSerializable("left", left);
args.putSerializable("right", right); args.putSerializable("right", right);
new SimpleTask<Void>() { args.putBoolean("should", should);
new SimpleTask<Boolean>() {
@Override @Override
protected void onPreExecute(Bundle args) { protected void onPreExecute(Bundle args) {
saving = true; saving = true;
@ -789,7 +799,7 @@ public class FragmentAccount extends FragmentBase {
} }
@Override @Override
protected Void onExecute(Context context, Bundle args) throws Throwable { protected Boolean onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id"); long id = args.getLong("id");
int auth_type = args.getInt("auth_type"); int auth_type = args.getInt("auth_type");
@ -813,26 +823,29 @@ public class FragmentAccount extends FragmentBase {
EntityFolder drafts = (EntityFolder) args.getSerializable("drafts"); EntityFolder drafts = (EntityFolder) args.getSerializable("drafts");
EntityFolder sent = (EntityFolder) args.getSerializable("sent"); EntityFolder sent = (EntityFolder) args.getSerializable("sent");
EntityFolder all = (EntityFolder) args.getSerializable("all"); EntityFolder archive = (EntityFolder) args.getSerializable("archive");
EntityFolder trash = (EntityFolder) args.getSerializable("trash"); EntityFolder trash = (EntityFolder) args.getSerializable("trash");
EntityFolder junk = (EntityFolder) args.getSerializable("junk"); EntityFolder junk = (EntityFolder) args.getSerializable("junk");
EntityFolder left = (EntityFolder) args.getSerializable("left"); EntityFolder left = (EntityFolder) args.getSerializable("left");
EntityFolder right = (EntityFolder) args.getSerializable("right"); EntityFolder right = (EntityFolder) args.getSerializable("right");
if (TextUtils.isEmpty(host)) boolean should = args.getBoolean("should");
if (!should && TextUtils.isEmpty(host))
throw new IllegalArgumentException(context.getString(R.string.title_no_host)); throw new IllegalArgumentException(context.getString(R.string.title_no_host));
if (TextUtils.isEmpty(port)) if (TextUtils.isEmpty(port))
port = (starttls ? "143" : "993"); port = (starttls ? "143" : "993");
if (TextUtils.isEmpty(user)) if (!should && TextUtils.isEmpty(user))
throw new IllegalArgumentException(context.getString(R.string.title_no_user)); throw new IllegalArgumentException(context.getString(R.string.title_no_user));
if (synchronize && TextUtils.isEmpty(password) && !insecure) if (!should && synchronize && TextUtils.isEmpty(password) && !insecure)
throw new IllegalArgumentException(context.getString(R.string.title_no_password)); throw new IllegalArgumentException(context.getString(R.string.title_no_password));
if (TextUtils.isEmpty(interval)) if (TextUtils.isEmpty(interval))
interval = Integer.toString(EntityAccount.DEFAULT_KEEP_ALIVE_INTERVAL); interval = Integer.toString(EntityAccount.DEFAULT_KEEP_ALIVE_INTERVAL);
if (TextUtils.isEmpty(realm)) if (TextUtils.isEmpty(realm))
realm = null; realm = null;
if (TextUtils.isEmpty(name))
name = user;
if (Color.TRANSPARENT == color) if (Color.TRANSPARENT == color)
color = null; color = null;
@ -841,6 +854,71 @@ public class FragmentAccount extends FragmentBase {
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
EntityAccount account = db.account().getAccount(id); EntityAccount account = db.account().getAccount(id);
if (should) {
if (account == null)
return !TextUtils.isEmpty(host) && !TextUtils.isEmpty(user);
if (!Objects.equals(account.auth_type, auth_type))
return true;
if (!Objects.equals(account.host, host))
return true;
if (!Objects.equals(account.starttls, starttls))
return true;
if (!Objects.equals(account.insecure, insecure))
return true;
if (!Objects.equals(account.port, Integer.parseInt(port)))
return true;
if (!Objects.equals(account.user, user))
return true;
if (!Objects.equals(account.password, password))
return true;
if (!Objects.equals(account.realm, realm))
return true;
if (!Objects.equals(account.name, name))
return true;
if (!Objects.equals(account.color, color))
return true;
if (!Objects.equals(account.synchronize, synchronize))
return true;
if (!Objects.equals(account.primary, account.synchronize && primary))
return true;
if (!Objects.equals(account.notify, notify))
return true;
if (!Objects.equals(account.browse, browse))
return true;
if (!Objects.equals(account.poll_interval, Integer.parseInt(interval)))
return true;
if (!Objects.equals(account.partial_fetch, partial_fetch))
return true;
EntityFolder edrafts = db.folder().getFolderByType(account.id, EntityFolder.DRAFTS);
if (!Objects.equals(edrafts == null ? null : edrafts.id, drafts == null ? null : drafts.id))
return true;
EntityFolder esent = db.folder().getFolderByType(account.id, EntityFolder.SENT);
if (!Objects.equals(esent == null ? null : esent.id, sent == null ? null : sent.id))
return true;
EntityFolder earchive = db.folder().getFolderByType(account.id, EntityFolder.ARCHIVE);
if (!Objects.equals(earchive == null ? null : earchive.id, archive == null ? null : archive.id))
return true;
EntityFolder etrash = db.folder().getFolderByType(account.id, EntityFolder.TRASH);
if (!Objects.equals(etrash == null ? null : etrash.id, trash == null ? null : trash.id))
return true;
EntityFolder ejunk = db.folder().getFolderByType(account.id, EntityFolder.JUNK);
if (!Objects.equals(ejunk == null ? null : ejunk.id, junk == null ? null : junk.id))
return true;
if (!Objects.equals(account.swipe_left, left == null ? null : left.id == null ? -1L : left.id))
return true;
if (!Objects.equals(account.swipe_right, right == null ? null : right.id == null ? -1L : right.id))
return true;
return false;
}
String accountRealm = (account == null ? null : account.realm); String accountRealm = (account == null ? null : account.realm);
boolean check = (synchronize && (account == null || boolean check = (synchronize && (account == null ||
@ -894,13 +972,9 @@ public class FragmentAccount extends FragmentBase {
inbox.keep_days = EntityFolder.DEFAULT_KEEP; inbox.keep_days = EntityFolder.DEFAULT_KEEP;
} }
} }
} }
} }
if (TextUtils.isEmpty(name))
name = user;
try { try {
db.beginTransaction(); db.beginTransaction();
@ -972,9 +1046,9 @@ public class FragmentAccount extends FragmentBase {
sent.type = EntityFolder.SENT; sent.type = EntityFolder.SENT;
folders.add(sent); folders.add(sent);
} }
if (all != null) { if (archive != null) {
all.type = EntityFolder.ARCHIVE; archive.type = EntityFolder.ARCHIVE;
folders.add(all); folders.add(archive);
} }
if (trash != null) { if (trash != null) {
trash.type = EntityFolder.TRASH; trash.type = EntityFolder.TRASH;
@ -1042,12 +1116,29 @@ public class FragmentAccount extends FragmentBase {
nm.cancel("receive", account.id.intValue()); nm.cancel("receive", account.id.intValue());
} }
return null; return false;
} }
@Override @Override
protected void onExecuted(Bundle args, Void data) { protected void onExecuted(Bundle args, Boolean dirty) {
getFragmentManager().popBackStack(); if (dirty)
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(R.string.title_ask_save)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
onSave(false);
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
getFragmentManager().popBackStack();
}
})
.show();
else
getFragmentManager().popBackStack();
} }
@Override @Override
@ -1428,7 +1519,7 @@ public class FragmentAccount extends FragmentBase {
else if (EntityFolder.SENT.equals(folder.type)) else if (EntityFolder.SENT.equals(folder.type))
spSent.setSelection(pos); spSent.setSelection(pos);
else if (EntityFolder.ARCHIVE.equals(folder.type)) else if (EntityFolder.ARCHIVE.equals(folder.type))
spAll.setSelection(pos); spArchive.setSelection(pos);
else if (EntityFolder.TRASH.equals(folder.type)) else if (EntityFolder.TRASH.equals(folder.type))
spTrash.setSelection(pos); spTrash.setSelection(pos);
else if (EntityFolder.JUNK.equals(folder.type)) else if (EntityFolder.JUNK.equals(folder.type))

@ -343,10 +343,10 @@
android:layout_marginStart="12dp" android:layout_marginStart="12dp"
android:background="@null" android:background="@null"
android:contentDescription="@string/title_legend_default_color" android:contentDescription="@string/title_legend_default_color"
app:srcCompat="@drawable/baseline_delete_24"
app:layout_constraintBottom_toBottomOf="@id/btnColor" app:layout_constraintBottom_toBottomOf="@id/btnColor"
app:layout_constraintStart_toEndOf="@id/vwColor" app:layout_constraintStart_toEndOf="@id/vwColor"
app:layout_constraintTop_toTopOf="@id/btnColor" /> app:layout_constraintTop_toTopOf="@id/btnColor"
app:srcCompat="@drawable/baseline_delete_24" />
<TextView <TextView
android:id="@+id/tvColorPro" android:id="@+id/tvColorPro"
@ -543,14 +543,14 @@
app:layout_constraintTop_toTopOf="@+id/spSent" /> app:layout_constraintTop_toTopOf="@+id/spSent" />
<TextView <TextView
android:id="@+id/tvAll" android:id="@+id/tvArchive"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/title_folder_all" android:text="@string/title_folder_all"
android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintBottom_toBottomOf="@+id/spAll" app:layout_constraintBottom_toBottomOf="@+id/spArchive"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/spAll" /> app:layout_constraintTop_toTopOf="@+id/spArchive" />
<TextView <TextView
android:id="@+id/tvTrash" android:id="@+id/tvTrash"
@ -598,7 +598,8 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="6dp" android:layout_marginStart="6dp"
app:barrierDirection="end" app:barrierDirection="end"
app:constraint_referenced_ids="tvDrafts,tvSent,tvAll,tvTrash,tvJunk,tvLeft,tvRight" /> app:constraint_referenced_ids="
tvDrafts,tvSent,tvArchive,tvTrash,tvJunk,tvLeft,tvRight" />
<Spinner <Spinner
android:id="@+id/spDrafts" android:id="@+id/spDrafts"
@ -631,7 +632,7 @@
app:layout_constraintTop_toBottomOf="@id/tvDraftsRemark" /> app:layout_constraintTop_toBottomOf="@id/tvDraftsRemark" />
<Spinner <Spinner
android:id="@+id/spAll" android:id="@+id/spArchive"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="6dp" android:layout_marginStart="6dp"
@ -648,7 +649,7 @@
android:layout_marginTop="12dp" android:layout_marginTop="12dp"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/barrier_folders" app:layout_constraintStart_toEndOf="@id/barrier_folders"
app:layout_constraintTop_toBottomOf="@id/spAll" /> app:layout_constraintTop_toBottomOf="@id/spArchive" />
<Spinner <Spinner
android:id="@+id/spJunk" android:id="@+id/spJunk"
@ -741,39 +742,39 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
app:constraint_referenced_ids=" app:constraint_referenced_ids="
tvDomain,tvDomainHint,etDomain,btnAutoConfig, tvDomain,tvDomainHint,etDomain,btnAutoConfig,
tvImap,tvPopSupport,tvHost,etHost,rgEncryption,cbInsecure,tvPort,etPort" /> tvImap,tvPopSupport,tvHost,etHost,rgEncryption,cbInsecure,tvPort,etPort" />
<androidx.constraintlayout.widget.Group <androidx.constraintlayout.widget.Group
android:id="@+id/grpAuthorize" android:id="@+id/grpAuthorize"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
app:constraint_referenced_ids=" app:constraint_referenced_ids="
tvUser,etUser,tvPassword,tilPassword,tvRealm,etRealm, tvUser,etUser,tvPassword,tilPassword,tvRealm,etRealm,
tvName,tvNameRemark,etName,btnColor,vwColor,ibColorDefault,tvColorPro" /> tvName,tvNameRemark,etName,btnColor,vwColor,ibColorDefault,tvColorPro" />
<androidx.constraintlayout.widget.Group <androidx.constraintlayout.widget.Group
android:id="@+id/grpAdvanced" android:id="@+id/grpAdvanced"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
app:constraint_referenced_ids=" app:constraint_referenced_ids="
cbNotify,tvNotifyPro, cbNotify,tvNotifyPro,
cbSynchronize,cbPrimary, cbSynchronize,cbPrimary,
cbBrowse,tvBrowseHint, cbBrowse,tvBrowseHint,
tvInterval,etInterval,tvIntervalRemark, tvInterval,etInterval,tvIntervalRemark,
cbPartialFetch,tvPartialFetchRemark" /> cbPartialFetch,tvPartialFetchRemark" />
<androidx.constraintlayout.widget.Group <androidx.constraintlayout.widget.Group
android:id="@+id/grpFolders" android:id="@+id/grpFolders"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
app:constraint_referenced_ids=" app:constraint_referenced_ids="
tvDrafts,spDrafts,tvDraftsRemark, tvDrafts,spDrafts,tvDraftsRemark,
tvSent,spSent, tvSent,spSent,
tvAll,spAll, tvArchive,spArchive,
tvTrash,spTrash, tvTrash,spTrash,
tvJunk,spJunk, tvJunk,spJunk,
vSeparatorSwipe,tvLeft,spLeft,tvRight,spRight" /> vSeparatorSwipe,tvLeft,spLeft,tvRight,spRight" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView> </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

@ -449,6 +449,7 @@
<string name="title_attachment_saved">Attachment saved</string> <string name="title_attachment_saved">Attachment saved</string>
<string name="title_attachments_saved">Attachments saved</string> <string name="title_attachments_saved">Attachments saved</string>
<string name="title_attachments_incomplete">Some attachments or images were not downloaded and could not be added</string> <string name="title_attachments_incomplete">Some attachments or images were not downloaded and could not be added</string>
<string name="title_ask_save">Save changes?</string>
<string name="title_ask_delete">Delete message permanently?</string> <string name="title_ask_delete">Delete message permanently?</string>
<string name="title_ask_delete_answer">Delete reply template permanently?</string> <string name="title_ask_delete_answer">Delete reply template permanently?</string>
<string name="title_ask_delete_rule">Delete rule permanently?</string> <string name="title_ask_delete_rule">Delete rule permanently?</string>

Loading…
Cancel
Save