From f5e9ee86d153d78b41bb20f9c0a4ce9d377f7d2e Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 30 Mar 2020 13:53:55 +0200 Subject: [PATCH] Added setting to sort suggested addresses on usage frequency --- .../java/eu/faircode/email/DaoContact.java | 3 +- .../java/eu/faircode/email/EntityContact.java | 2 -- .../eu/faircode/email/FragmentCompose.java | 34 +++++++++++++++---- .../faircode/email/FragmentOptionsSend.java | 13 ++++++- .../main/res/layout/fragment_options_send.xml | 14 +++++++- app/src/main/res/values/strings.xml | 1 + 6 files changed, 54 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DaoContact.java b/app/src/main/java/eu/faircode/email/DaoContact.java index ea00380e33..9500264c18 100644 --- a/app/src/main/java/eu/faircode/email/DaoContact.java +++ b/app/src/main/java/eu/faircode/email/DaoContact.java @@ -63,8 +63,7 @@ public interface DaoContact { " AND (:type IS NULL OR type = :type)" + " AND (email LIKE :query COLLATE NOCASE OR name LIKE :query COLLATE NOCASE)" + " AND state <> " + EntityContact.STATE_IGNORE + - " GROUP BY name, email" + - " LIMIT " + EntityContact.MAX_SUGGEST) + " GROUP BY name, email") List searchContacts(Long account, Integer type, String query); @Insert diff --git a/app/src/main/java/eu/faircode/email/EntityContact.java b/app/src/main/java/eu/faircode/email/EntityContact.java index 48520c91f1..c440351f3c 100644 --- a/app/src/main/java/eu/faircode/email/EntityContact.java +++ b/app/src/main/java/eu/faircode/email/EntityContact.java @@ -61,8 +61,6 @@ public class EntityContact implements Serializable { static final int STATE_FAVORITE = 1; static final int STATE_IGNORE = 2; - static final int MAX_SUGGEST = 50; // per category: Android, local to/from - @PrimaryKey(autoGenerate = true) public Long id; @NonNull diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index c0ecb40ac9..f4a96f4ffb 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -680,6 +680,7 @@ public class FragmentCompose extends FragmentBase { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); final boolean suggest_sent = prefs.getBoolean("suggest_sent", true); final boolean suggest_received = prefs.getBoolean("suggest_received", false); + final boolean suggest_frequently = prefs.getBoolean("suggest_frequently", false); final boolean cc_bcc = prefs.getBoolean("cc_bcc", false); final boolean circular = prefs.getBoolean("circular", true); final float dp3 = Helper.dp2pixels(getContext(), 3); @@ -764,13 +765,14 @@ public class FragmentCompose extends FragmentBase { new String[]{wildcard, wildcard}, null); - while (map.size() < EntityContact.MAX_SUGGEST && - cursor != null && cursor.moveToNext()) { + while (cursor != null && cursor.moveToNext()) { EntityContact item = new EntityContact(); item.id = 0L; item.name = cursor.getString(0); item.email = cursor.getString(1); item.avatar = cursor.getString(2); + item.times_contacted = 0; + item.last_contacted = 0L; EntityContact existing = map.get(item.email); if (existing == null || (existing.avatar == null && item.avatar != null)) @@ -783,9 +785,15 @@ public class FragmentCompose extends FragmentBase { items.addAll(db.contact().searchContacts(null, EntityContact.TYPE_TO, wildcard)); if (suggest_received) items.addAll(db.contact().searchContacts(null, EntityContact.TYPE_FROM, wildcard)); - for (EntityContact item : items) - if (!map.containsKey(item.email)) + for (EntityContact item : items) { + EntityContact existing = map.get(item.email); + if (existing == null) map.put(item.email, item); + else { + existing.times_contacted = Math.max(existing.times_contacted, item.times_contacted); + existing.last_contacted = Math.max(existing.last_contacted, item.last_contacted); + } + } items = new ArrayList<>(map.values()); @@ -795,10 +803,22 @@ public class FragmentCompose extends FragmentBase { Collections.sort(items, new Comparator() { @Override public int compare(EntityContact i1, EntityContact i2) { - int l = i1.id.compareTo(i2.id); - if (l != 0) - return l; + if (suggest_frequently) { + int t = -i1.times_contacted.compareTo(i2.times_contacted); + if (t != 0) + return t; + int l = -i1.last_contacted.compareTo(i2.last_contacted); + if (l != 0) + return l; + } else { + int a = -Boolean.compare(i1.id == 0, i2.id == 0); + if (a != 0) + return a; + } + + if (TextUtils.isEmpty(i1.name) && TextUtils.isEmpty(i2.name)) + return 0; if (TextUtils.isEmpty(i1.name) && !TextUtils.isEmpty(i2.name)) return 1; if (!TextUtils.isEmpty(i1.name) && TextUtils.isEmpty(i2.name)) diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java b/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java index 2c05269133..1d7fda7c64 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsSend.java @@ -45,6 +45,7 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc private SwitchCompat swKeyboard; private SwitchCompat swSuggestSent; private SwitchCompat swSuggestReceived; + private SwitchCompat swSuggestFrequently; private Button btnLocalContacts; private SwitchCompat swSendReminders; private Spinner spSendDelayed; @@ -64,7 +65,8 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc private SwitchCompat swLookupMx; private final static String[] RESET_OPTIONS = new String[]{ - "keyboard", "suggest_sent", "suggested_received", "send_reminders", "send_delayed", + "keyboard", "suggest_sent", "suggested_received", "suggest_frequently", + "send_reminders", "send_delayed", "prefix_once", "extended_reply", "quote_reply", "resize_reply", "signature_location", "plain_only", "format_flowed", "usenet_signature", "remove_signatures", "receipt_default", "receipt_type", "lookup_mx" @@ -83,6 +85,7 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc swKeyboard = view.findViewById(R.id.swKeyboard); swSuggestSent = view.findViewById(R.id.swSuggestSent); swSuggestReceived = view.findViewById(R.id.swSuggestReceived); + swSuggestFrequently = view.findViewById(R.id.swSuggestFrequently); btnLocalContacts = view.findViewById(R.id.btnLocalContacts); swSendReminders = view.findViewById(R.id.swSendReminders); spSendDelayed = view.findViewById(R.id.spSendDelayed); @@ -128,6 +131,13 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc } }); + swSuggestFrequently.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { + prefs.edit().putBoolean("suggest_frequently", checked).apply(); + } + }); + btnLocalContacts.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -299,6 +309,7 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc swKeyboard.setChecked(prefs.getBoolean("keyboard", true)); swSuggestSent.setChecked(prefs.getBoolean("suggest_sent", true)); swSuggestReceived.setChecked(prefs.getBoolean("suggest_received", false)); + swSuggestFrequently.setChecked(prefs.getBoolean("suggest_frequently", false)); swSendReminders.setChecked(prefs.getBoolean("send_reminders", true)); int send_delayed = prefs.getInt("send_delayed", 0); diff --git a/app/src/main/res/layout/fragment_options_send.xml b/app/src/main/res/layout/fragment_options_send.xml index 390b0a84ba..d79a3ad88c 100644 --- a/app/src/main/res/layout/fragment_options_send.xml +++ b/app/src/main/res/layout/fragment_options_send.xml @@ -87,6 +87,18 @@ app:layout_constraintTop_toBottomOf="@id/swSuggestSent" app:switchPadding="12dp" /> + +