Auto-select identity for folder

This commit is contained in:
M66B 2024-05-07 16:15:44 +02:00
parent f9cc6bdae1
commit 3b11de947c
6 changed files with 66 additions and 12 deletions

View File

@ -343,6 +343,12 @@ public interface DaoMessage {
" ORDER BY message.received DESC")
List<Long> getMessageIdsByFolder(Long folder);
@Query("SELECT identity, COUNT(*) AS count" +
" FROM message" +
" WHERE folder = :folder" +
" GROUP BY identity")
List<TupleIdentityCount> getIdentitiesByFolder(long folder);
@Transaction
@Query("SELECT message.id FROM message" +
" JOIN folder_view AS folder ON folder.id = message.folder" +

View File

@ -272,7 +272,7 @@ public class FragmentAccounts extends FragmentBase {
getContext(),
getViewLifecycleOwner(),
getParentFragmentManager(),
fabCompose, -1L);
fabCompose, -1L, -1L);
}
});

View File

@ -49,6 +49,8 @@ import java.util.ArrayList;
import java.util.List;
public class FragmentDialogIdentity extends FragmentDialogBase {
private static final int MIN_IDENTITY_MESSAGES = 20;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
@ -144,17 +146,26 @@ public class FragmentDialogIdentity extends FragmentDialogBase {
AdapterIdentitySelect iadapter = new AdapterIdentitySelect(context, identities);
spIdentity.setAdapter(iadapter);
long aid = args.getLong("account");
long iid = args.getLong("identity", -1L);
Integer selected = null;
long account = getArguments().getLong("account");
for (int pos = 0; pos < identities.size(); pos++) {
EntityIdentity identity = identities.get(pos);
if (identity.account.equals(account)) {
if (identity.primary) {
if (iid < 0) {
if (identity.account.equals(aid)) {
if (identity.primary) {
selected = pos;
break;
}
if (selected == null)
selected = pos;
}
} else {
if (identity.id.equals(iid)) {
selected = pos;
break;
}
if (selected == null)
selected = pos;
}
}
@ -180,7 +191,7 @@ public class FragmentDialogIdentity extends FragmentDialogBase {
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, new Bundle(), "identity:select");
}.execute(this, getArguments(), "identity:select");
return new AlertDialog.Builder(context)
.setView(dview)
@ -200,9 +211,10 @@ public class FragmentDialogIdentity extends FragmentDialogBase {
.create();
}
static void onCompose(Context context, LifecycleOwner owner, FragmentManager manager, FloatingActionButton fabCompose, long account) {
static void onCompose(Context context, LifecycleOwner owner, FragmentManager manager, FloatingActionButton fabCompose, long account, long folder) {
Bundle args = new Bundle();
args.putLong("account", account);
args.putLong("folder", folder);
new SimpleTask<Boolean>() {
@Override
@ -217,12 +229,22 @@ public class FragmentDialogIdentity extends FragmentDialogBase {
@Override
protected Boolean onExecute(Context context, Bundle args) {
DB db = DB.getInstance(context);
long folder = args.getLong("folder");
if (folder >= 0) {
List<TupleIdentityCount> counts = db.message().getIdentitiesByFolder(folder);
if (counts != null &&
counts.size() == 1 &&
counts.get(0).count >= MIN_IDENTITY_MESSAGES)
args.putLong("identity", counts.get(0).identity);
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean identities_asked = prefs.getBoolean("identities_asked", false);
if (identities_asked)
return false;
DB db = DB.getInstance(context);
List<TupleIdentityEx> identities = db.identity().getComposableIdentities(null);
return (identities != null && identities.size() > 1);
}
@ -236,7 +258,8 @@ public class FragmentDialogIdentity extends FragmentDialogBase {
} else
context.startActivity(new Intent(context, ActivityCompose.class)
.putExtra("action", "new")
.putExtra("account", account));
.putExtra("account", account)
.putExtra("identity", args.getLong("identity", -1L)));
}
@Override

View File

@ -374,7 +374,7 @@ public class FragmentFolders extends FragmentBase {
getContext(),
getViewLifecycleOwner(),
getParentFragmentManager(),
fabCompose, account);
fabCompose, account, -1L);
}
});

View File

@ -1577,7 +1577,7 @@ public class FragmentMessages extends FragmentBase
getContext(),
getViewLifecycleOwner(),
getParentFragmentManager(),
fabCompose, account);
fabCompose, account, folder);
}
});

View File

@ -0,0 +1,25 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2024 by Marcel Bokhorst (M66B)
*/
public class TupleIdentityCount {
public long identity;
public int count;
}