Show number of unseen messages in unified system folders

This commit is contained in:
M66B 2019-07-22 14:34:06 +02:00
parent bd3cc5018d
commit ed92a999f0
4 changed files with 71 additions and 30 deletions

View File

@ -387,15 +387,15 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
} }
}); });
db.folder().liveUnifiedTypes().observe(this, new Observer<List<String>>() { db.folder().liveUnified().observe(this, new Observer<List<EntityFolderUnified>>() {
@Override @Override
public void onChanged(List<String> types) { public void onChanged(List<EntityFolderUnified> folders) {
if (types == null) if (folders == null)
types = new ArrayList<>(); folders = new ArrayList<>();
ivExpanderUnified.setVisibility(types.size() > 0 ? View.VISIBLE : View.GONE); ivExpanderUnified.setVisibility(folders.size() > 0 ? View.VISIBLE : View.GONE);
boolean unified_system = prefs.getBoolean("unified_system", false); boolean unified_system = prefs.getBoolean("unified_system", false);
grpUnified.setVisibility(unified_system && types.size() > 0 ? View.VISIBLE : View.GONE); grpUnified.setVisibility(unified_system && folders.size() > 0 ? View.VISIBLE : View.GONE);
uadapter.set(types); uadapter.set(folders);
} }
}); });

View File

@ -34,6 +34,7 @@ import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback; import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
@ -44,7 +45,9 @@ public class AdapterNavUnified extends RecyclerView.Adapter<AdapterNavUnified.Vi
private LifecycleOwner owner; private LifecycleOwner owner;
private LayoutInflater inflater; private LayoutInflater inflater;
private List<String> items = new ArrayList<>(); private List<EntityFolderUnified> items = new ArrayList<>();
private NumberFormat NF = NumberFormat.getNumberInstance();
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private View view; private View view;
@ -71,9 +74,18 @@ public class AdapterNavUnified extends RecyclerView.Adapter<AdapterNavUnified.Vi
view.setOnClickListener(null); view.setOnClickListener(null);
} }
private void bindTo(String type) { private void bindTo(EntityFolderUnified folder) {
ivItem.setImageResource(EntityFolder.getIcon(type)); ivItem.setImageResource(EntityFolder.getIcon(folder.type));
tvItem.setText(Helper.localizeFolderType(context, type));
if (folder.unseen == 0)
tvItem.setText(Helper.localizeFolderType(context, folder.type));
else
tvItem.setText(context.getString(R.string.title_name_count,
Helper.localizeFolderType(context, folder.type), NF.format(folder.unseen)));
tvItem.setTextColor(Helper.resolveColor(context,
folder.unseen == 0 ? android.R.attr.textColorSecondary : R.attr.colorUnread));
tvItemExtra.setVisibility(View.GONE); tvItemExtra.setVisibility(View.GONE);
ivWarning.setVisibility(View.GONE); ivWarning.setVisibility(View.GONE);
} }
@ -84,14 +96,14 @@ public class AdapterNavUnified extends RecyclerView.Adapter<AdapterNavUnified.Vi
if (pos == RecyclerView.NO_POSITION) if (pos == RecyclerView.NO_POSITION)
return; return;
String type = items.get(pos); EntityFolderUnified folder = items.get(pos);
if (type == null) if (folder == null || folder.type == null)
return; return;
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast( lbm.sendBroadcast(
new Intent(ActivityView.ACTION_VIEW_MESSAGES) new Intent(ActivityView.ACTION_VIEW_MESSAGES)
.putExtra("type", type)); .putExtra("type", folder.type));
} }
} }
@ -101,14 +113,14 @@ public class AdapterNavUnified extends RecyclerView.Adapter<AdapterNavUnified.Vi
this.inflater = LayoutInflater.from(context); this.inflater = LayoutInflater.from(context);
} }
public void set(@NonNull List<String> types) { public void set(@NonNull List<EntityFolderUnified> types) {
Log.i("Set nav unified=" + types.size()); Log.i("Set nav unified=" + types.size());
Collections.sort(types, new Comparator<String>() { Collections.sort(types, new Comparator<EntityFolderUnified>() {
@Override @Override
public int compare(String t1, String t2) { public int compare(EntityFolderUnified f1, EntityFolderUnified f2) {
int i1 = EntityFolder.FOLDER_SORT_ORDER.indexOf(t1); int i1 = EntityFolder.FOLDER_SORT_ORDER.indexOf(f1.type);
int i2 = EntityFolder.FOLDER_SORT_ORDER.indexOf(t2); int i2 = EntityFolder.FOLDER_SORT_ORDER.indexOf(f2.type);
return Integer.compare(i1, i2); return Integer.compare(i1, i2);
} }
}); });
@ -142,10 +154,10 @@ public class AdapterNavUnified extends RecyclerView.Adapter<AdapterNavUnified.Vi
} }
private class DiffCallback extends DiffUtil.Callback { private class DiffCallback extends DiffUtil.Callback {
private List<String> prev = new ArrayList<>(); private List<EntityFolderUnified> prev = new ArrayList<>();
private List<String> next = new ArrayList<>(); private List<EntityFolderUnified> next = new ArrayList<>();
DiffCallback(List<String> prev, List<String> next) { DiffCallback(List<EntityFolderUnified> prev, List<EntityFolderUnified> next) {
this.prev.addAll(prev); this.prev.addAll(prev);
this.next.addAll(next); this.next.addAll(next);
} }
@ -162,14 +174,16 @@ public class AdapterNavUnified extends RecyclerView.Adapter<AdapterNavUnified.Vi
@Override @Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) { public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
String t1 = prev.get(oldItemPosition); EntityFolderUnified f1 = prev.get(oldItemPosition);
String t2 = next.get(newItemPosition); EntityFolderUnified f2 = next.get(newItemPosition);
return t1.equals(t2); return f1.type.equals(f2.type);
} }
@Override @Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) { public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return true; EntityFolderUnified f1 = prev.get(oldItemPosition);
EntityFolderUnified f2 = next.get(newItemPosition);
return (f1.unseen == f2.unseen);
} }
} }
@ -187,8 +201,8 @@ public class AdapterNavUnified extends RecyclerView.Adapter<AdapterNavUnified.Vi
@Override @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.unwire(); holder.unwire();
String type = items.get(position); EntityFolderUnified folder = items.get(position);
holder.bindTo(type); holder.bindTo(folder);
holder.wire(); holder.wire();
} }
} }

View File

@ -154,15 +154,17 @@ public interface DaoFolder {
" AND type <> '" + EntityFolder.USER + "'") " AND type <> '" + EntityFolder.USER + "'")
List<EntityFolder> getSystemFolders(long account); List<EntityFolder> getSystemFolders(long account);
@Query("SELECT folder.type" + @Query("SELECT folder.type," +
" SUM(CASE WHEN NOT message.ui_seen AND message.ui_hide = 0 THEN 1 ELSE 0 END) AS unseen" +
" FROM folder" + " FROM folder" +
" JOIN account ON account.id = folder.account" + " JOIN account ON account.id = folder.account" +
" LEFT JOIN message ON message.folder = folder.id" +
" WHERE account.synchronize" + " WHERE account.synchronize" +
" AND folder.type <> '" + EntityFolder.SYSTEM + "'" + " AND folder.type <> '" + EntityFolder.SYSTEM + "'" +
" AND folder.type <> '" + EntityFolder.USER + "'" + " AND folder.type <> '" + EntityFolder.USER + "'" +
" GROUP BY folder.type" + " GROUP BY folder.type" +
" HAVING COUNT(folder.id) > 1") " HAVING COUNT(folder.id) > 1")
LiveData<List<String>> liveUnifiedTypes(); LiveData<List<EntityFolderUnified>> liveUnified();
@Query("SELECT * FROM folder WHERE id = :id") @Query("SELECT * FROM folder WHERE id = :id")
EntityFolder getFolder(Long id); EntityFolder getFolder(Long id);

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-2019 by Marcel Bokhorst (M66B)
*/
public class EntityFolderUnified {
public String type;
public long unseen;
}