Stable navigation menu, refactoring

This commit is contained in:
M66B 2019-02-26 17:44:15 +00:00
parent 3573d75ce9
commit 274c9dd471
4 changed files with 119 additions and 99 deletions

View File

@ -130,11 +130,15 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On
drawerLayout.addDrawerListener(drawerToggle); drawerLayout.addDrawerListener(drawerToggle);
drawerList = findViewById(R.id.drawer_list); drawerList = findViewById(R.id.drawer_list);
final DrawerAdapter drawerArray = new DrawerAdapter(this);
drawerList.setAdapter(drawerArray);
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
DrawerItem item = (DrawerItem) parent.getAdapter().getItem(position); DrawerItem item = drawerArray.getItem(position);
switch (item.getId()) { switch (item.getMenuId()) {
case R.string.title_setup_export: case R.string.title_setup_export:
onMenuExport(); onMenuExport();
break; break;
@ -145,7 +149,7 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On
case R.string.title_setup_dark_theme: case R.string.title_setup_dark_theme:
case R.string.title_setup_black_theme: case R.string.title_setup_black_theme:
case R.string.title_setup_system_theme: case R.string.title_setup_system_theme:
onMenuTheme(item.getId()); onMenuTheme(item.getMenuId());
break; break;
case R.string.title_setup_notifications: case R.string.title_setup_notifications:
onManageNotifications(); onManageNotifications();
@ -174,8 +178,8 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On
drawerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { drawerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override @Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
DrawerItem item = (DrawerItem) parent.getAdapter().getItem(position); DrawerItem item = drawerArray.getItem(position);
if (item.getId() == R.string.menu_privacy) { if (item.getMenuId() == R.string.menu_privacy) {
new SimpleTask<Void>() { new SimpleTask<Void>() {
@Override @Override
protected Void onExecute(Context context, Bundle args) { protected Void onExecute(Context context, Bundle args) {
@ -197,39 +201,35 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On
}); });
PackageManager pm = getPackageManager(); PackageManager pm = getPackageManager();
DrawerAdapter drawerArray = new DrawerAdapter(this);
if (getIntentExport().resolveActivity(pm) != null) if (getIntentExport().resolveActivity(pm) != null)
drawerArray.add(new DrawerItem(this, R.layout.item_drawer, R.drawable.baseline_archive_24, R.string.title_setup_export)); drawerArray.add(new DrawerItem(-1, R.string.title_setup_export, R.drawable.baseline_archive_24));
if (getIntentImport().resolveActivity(pm) != null) if (getIntentImport().resolveActivity(pm) != null)
drawerArray.add(new DrawerItem(this, R.layout.item_drawer, R.drawable.baseline_unarchive_24, R.string.title_setup_import)); drawerArray.add(new DrawerItem(-2, R.string.title_setup_import, R.drawable.baseline_unarchive_24));
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String theme = prefs.getString("theme", "system"); String theme = prefs.getString("theme", "system");
if ("light".equals(theme)) if ("light".equals(theme))
drawerArray.add(new DrawerItem(this, R.layout.item_drawer, R.drawable.baseline_palette_24, R.string.title_setup_dark_theme)); drawerArray.add(new DrawerItem(-3, R.string.title_setup_dark_theme, R.drawable.baseline_palette_24));
else if ("dark".equals(theme)) else if ("dark".equals(theme))
drawerArray.add(new DrawerItem(this, R.layout.item_drawer, R.drawable.baseline_palette_24, R.string.title_setup_black_theme)); drawerArray.add(new DrawerItem(-4, R.string.title_setup_black_theme, R.drawable.baseline_palette_24));
else if ("black".equals(theme)) else if ("black".equals(theme))
drawerArray.add(new DrawerItem(this, R.layout.item_drawer, R.drawable.baseline_palette_24, R.string.title_setup_system_theme)); drawerArray.add(new DrawerItem(-5, R.string.title_setup_system_theme, R.drawable.baseline_palette_24));
else else
drawerArray.add(new DrawerItem(this, R.layout.item_drawer, R.drawable.baseline_palette_24, R.string.title_setup_light_theme)); drawerArray.add(new DrawerItem(-6, R.string.title_setup_light_theme, R.drawable.baseline_palette_24));
if (getIntentNotifications(this).resolveActivity(pm) != null) if (getIntentNotifications(this).resolveActivity(pm) != null)
drawerArray.add(new DrawerItem(this, R.layout.item_drawer, R.drawable.baseline_notifications_24, R.string.title_setup_notifications)); drawerArray.add(new DrawerItem(-7, R.string.title_setup_notifications, R.drawable.baseline_notifications_24));
drawerArray.add(new DrawerItem(this, R.layout.item_drawer, R.drawable.baseline_settings_applications_24, R.string.title_setup_advanced)); drawerArray.add(new DrawerItem(-8, R.string.title_setup_advanced, R.drawable.baseline_settings_applications_24));
drawerArray.add(new DrawerItem(R.layout.item_drawer_separator)); drawerArray.add(new DrawerItem(-9));
drawerArray.add(new DrawerItem(this, R.layout.item_drawer, R.drawable.baseline_help_24, R.string.menu_legend)); drawerArray.add(new DrawerItem(-10, R.string.menu_legend, R.drawable.baseline_help_24));
if (Helper.getIntentFAQ().resolveActivity(getPackageManager()) != null) if (Helper.getIntentFAQ().resolveActivity(getPackageManager()) != null)
drawerArray.add(new DrawerItem(this, R.layout.item_drawer, R.drawable.baseline_question_answer_24, R.string.menu_faq)); drawerArray.add(new DrawerItem(-11, R.string.menu_faq, R.drawable.baseline_question_answer_24));
if (Helper.getIntentPrivacy().resolveActivity(getPackageManager()) != null) if (Helper.getIntentPrivacy().resolveActivity(getPackageManager()) != null)
drawerArray.add(new DrawerItem(this, R.layout.item_drawer, R.drawable.baseline_account_box_24, R.string.menu_privacy)); drawerArray.add(new DrawerItem(-12, R.string.menu_privacy, R.drawable.baseline_account_box_24));
drawerArray.add(new DrawerItem(this, R.layout.item_drawer, R.drawable.baseline_info_24, R.string.menu_about)); drawerArray.add(new DrawerItem(-13, R.string.menu_about, R.drawable.baseline_info_24));
drawerList.setAdapter(drawerArray);
getSupportFragmentManager().addOnBackStackChangedListener(this); getSupportFragmentManager().addOnBackStackChangedListener(this);

View File

@ -79,6 +79,7 @@ import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Objects;
import java.util.Properties; import java.util.Properties;
import javax.mail.Session; import javax.mail.Session;
@ -169,14 +170,15 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
drawerLayout.addDrawerListener(drawerToggle); drawerLayout.addDrawerListener(drawerToggle);
drawerList = findViewById(R.id.drawer_list); drawerList = findViewById(R.id.drawer_list);
final DrawerAdapter drawerArray = new DrawerAdapter(ActivityView.this);
drawerList.setAdapter(drawerArray);
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
DrawerItem item = (DrawerItem) parent.getAdapter().getItem(position); DrawerItem item = drawerArray.getItem(position);
switch (item.getId()) { switch (item.getMenuId()) {
case -1:
onMenuFolders((long) item.getData());
break;
case R.string.menu_answers: case R.string.menu_answers:
onMenuAnswers(); onMenuAnswers();
break; break;
@ -186,7 +188,6 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
case R.string.menu_setup: case R.string.menu_setup:
onMenuSetup(); onMenuSetup();
break; break;
case R.string.menu_legend: case R.string.menu_legend:
onMenuLegend(); onMenuLegend();
break; break;
@ -202,7 +203,6 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
case R.string.menu_about: case R.string.menu_about:
onMenuAbout(); onMenuAbout();
break; break;
case R.string.menu_pro: case R.string.menu_pro:
onMenuPro(); onMenuPro();
break; break;
@ -215,19 +215,21 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
case R.string.menu_other: case R.string.menu_other:
onMenuOtherApps(); onMenuOtherApps();
break; break;
default:
long account = item.getId();
if (account > 0)
onMenuFolders(account);
} }
drawerLayout.closeDrawer(drawerList); drawerLayout.closeDrawer(drawerList);
} }
}); });
drawerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { drawerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override @Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
DrawerItem item = (DrawerItem) parent.getAdapter().getItem(position); DrawerItem item = drawerArray.getItem(position);
switch (item.getId()) { switch (item.getMenuId()) {
case -1:
onMenuInbox((long) item.getData());
break;
case R.string.menu_operations: case R.string.menu_operations:
onShowLog(); onShowLog();
break; break;
@ -246,7 +248,11 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
checkUpdate(true); checkUpdate(true);
break; break;
default: default:
return false; long account = item.getId();
if (account < 0)
return false;
else
onMenuInbox(account);
} }
drawerLayout.closeDrawer(drawerList); drawerLayout.closeDrawer(drawerList);
@ -264,22 +270,6 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
if (accounts == null) if (accounts == null)
accounts = new ArrayList<>(); accounts = new ArrayList<>();
boolean changed = false;
if (last.size() == accounts.size()) {
for (int i = 0; i < accounts.size(); i++)
if (!last.get(i).equals(accounts.get(i))) {
changed = true;
break;
}
} else
changed = true;
if (!changed)
return;
last = accounts;
DrawerAdapter drawerArray = new DrawerAdapter(ActivityView.this);
final Collator collator = Collator.getInstance(Locale.getDefault()); final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
@ -290,36 +280,54 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
} }
}); });
boolean changed = false;
if (last.size() == accounts.size()) {
for (int i = 0; i < accounts.size(); i++) {
TupleAccountEx other = last.get(i);
TupleAccountEx account = accounts.get(i);
if (!account.id.equals(other.id) ||
!Objects.equals(account.name, other.name) ||
!Objects.equals(account.color, other.color) ||
!Objects.equals(account.state, other.state) ||
account.unseen != other.unseen) {
changed = true;
break;
}
}
} else
changed = true;
if (!changed)
return;
last = accounts;
drawerArray.clear();
for (TupleAccountEx account : accounts) for (TupleAccountEx account : accounts)
drawerArray.add(new DrawerItem( drawerArray.add(new DrawerItem(
R.layout.item_drawer, -1, account.id,
"connected".equals(account.state) ? R.drawable.baseline_folder_24 : R.drawable.baseline_folder_open_24, "connected".equals(account.state) ? R.drawable.baseline_folder_24 : R.drawable.baseline_folder_open_24,
account.color, account.color,
account.unseen > 0 ? getString(R.string.title_unseen_count, account.name, account.unseen) : account.toString(), account.unseen > 0 ? getString(R.string.title_unseen_count, account.name, account.unseen) : account.toString(),
account.unseen > 0, account.unseen > 0));
account.id));
drawerArray.add(new DrawerItem(R.layout.item_drawer_separator)); drawerArray.add(new DrawerItem(-1));
drawerArray.add(new DrawerItem(-2, R.string.menu_answers, R.drawable.baseline_reply_24));
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_reply_24, R.string.menu_answers)); drawerArray.add(new DrawerItem(-3, R.string.menu_operations, R.drawable.baseline_list_24));
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_list_24, R.string.menu_operations)); drawerArray.add(new DrawerItem(-4, R.string.menu_setup, R.drawable.baseline_settings_applications_24));
drawerArray.add(new DrawerItem(-5));
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_settings_applications_24, R.string.menu_setup)); drawerArray.add(new DrawerItem(-6, R.string.menu_legend, R.drawable.baseline_help_24));
drawerArray.add(new DrawerItem(R.layout.item_drawer_separator));
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_help_24, R.string.menu_legend));
if (Helper.getIntentFAQ().resolveActivity(getPackageManager()) != null) if (Helper.getIntentFAQ().resolveActivity(getPackageManager()) != null)
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_question_answer_24, R.string.menu_faq)); drawerArray.add(new DrawerItem(-7, R.string.menu_faq, R.drawable.baseline_question_answer_24));
if (BuildConfig.BETA_RELEASE) if (BuildConfig.BETA_RELEASE)
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_report_problem_24, R.string.menu_issue)); drawerArray.add(new DrawerItem(-8, R.string.menu_issue, R.drawable.baseline_report_problem_24));
if (Helper.getIntentPrivacy().resolveActivity(getPackageManager()) != null) if (Helper.getIntentPrivacy().resolveActivity(getPackageManager()) != null)
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_account_box_24, R.string.menu_privacy)); drawerArray.add(new DrawerItem(-9, R.string.menu_privacy, R.drawable.baseline_account_box_24));
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_info_24, R.string.menu_about)); drawerArray.add(new DrawerItem(-10, R.string.menu_about, R.drawable.baseline_info_24));
boolean pro = (getIntentPro() == null || getIntentPro().resolveActivity(getPackageManager()) != null); boolean pro = (getIntentPro() == null || getIntentPro().resolveActivity(getPackageManager()) != null);
boolean invite = (getIntentInvite().resolveActivity(getPackageManager()) != null); boolean invite = (getIntentInvite().resolveActivity(getPackageManager()) != null);
@ -327,21 +335,19 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
boolean other = (getIntentOtherApps().resolveActivity(getPackageManager()) != null); boolean other = (getIntentOtherApps().resolveActivity(getPackageManager()) != null);
if (pro || invite || rate || other) if (pro || invite || rate || other)
drawerArray.add(new DrawerItem(R.layout.item_drawer_separator)); drawerArray.add(new DrawerItem(-11));
if (pro) if (pro)
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_monetization_on_24, R.string.menu_pro)); drawerArray.add(new DrawerItem(-12, R.string.menu_pro, R.drawable.baseline_monetization_on_24));
if (invite) if (invite)
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_people_24, R.string.menu_invite)); drawerArray.add(new DrawerItem(-13, R.string.menu_invite, R.drawable.baseline_people_24));
if (rate) if (rate)
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_star_24, R.string.menu_rate)); drawerArray.add(new DrawerItem(-14, R.string.menu_rate, R.drawable.baseline_star_24));
if (other) if (other)
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_get_app_24, R.string.menu_other)); drawerArray.add(new DrawerItem(-15, R.string.menu_other, R.drawable.baseline_get_app_24));
drawerList.setAdapter(drawerArray);
} }
}); });

View File

@ -30,7 +30,7 @@ public class DrawerAdapter extends ArrayAdapter<DrawerItem> {
} }
if (tv != null) { if (tv != null) {
tv.setText(item.getTitle()); tv.setText(item.getTitle(getContext()));
tv.setTextColor(Helper.resolveColor(getContext(), tv.setTextColor(Helper.resolveColor(getContext(),
item.getHighlight() ? R.attr.colorUnread : android.R.attr.textColorSecondary)); item.getHighlight() ? R.attr.colorUnread : android.R.attr.textColorSecondary));
@ -39,6 +39,17 @@ public class DrawerAdapter extends ArrayAdapter<DrawerItem> {
return row; return row;
} }
@Override
public boolean hasStableIds() {
return true;
}
@Override
public long getItemId(int position) {
DrawerItem item = getItem(position);
return (item == null ? 0 : item.getId());
}
@Override @Override
public boolean isEnabled(int position) { public boolean isEnabled(int position) {
DrawerItem item = getItem(position); DrawerItem item = getItem(position);

View File

@ -3,44 +3,49 @@ package eu.faircode.email;
import android.content.Context; import android.content.Context;
public class DrawerItem { public class DrawerItem {
private long id;
private int menu;
private int layout; private int layout;
private int id;
private int icon; private int icon;
private Integer color; private Integer color;
private int resid;
private String title; private String title;
private boolean highlight; private boolean highlight;
private Object data;
DrawerItem(int layout) { DrawerItem(long id) {
this.id = 0;
this.layout = layout;
}
DrawerItem(Context context, int layout, int icon, int title) {
this.layout = layout;
this.id = title;
this.icon = icon;
this.title = context.getString(title);
}
DrawerItem(int layout, int id, int icon, Integer color, String title, boolean highlight, Object data) {
this.layout = layout;
this.id = id; this.id = id;
this.layout = R.layout.item_drawer_separator;
}
DrawerItem(long id, int resid, int icon) {
this.id = id;
this.menu = resid;
this.layout = R.layout.item_drawer;
this.icon = icon;
this.resid = resid;
}
DrawerItem(long id, int icon, Integer color, String title, boolean highlight) {
this.id = id;
this.layout = R.layout.item_drawer;
this.icon = icon; this.icon = icon;
this.color = color; this.color = color;
this.title = title; this.title = title;
this.highlight = highlight; this.highlight = highlight;
this.data = data;
} }
int getLayout() { int getLayout() {
return this.layout; return this.layout;
} }
int getId() { long getId() {
return this.id; return this.id;
} }
int getMenuId() {
return this.menu;
}
int getIcon() { int getIcon() {
return this.icon; return this.icon;
} }
@ -49,15 +54,13 @@ public class DrawerItem {
return this.color; return this.color;
} }
String getTitle() { String getTitle(Context context) {
if (this.title == null && resid > 0)
this.title = context.getString(resid);
return this.title; return this.title;
} }
boolean getHighlight() { boolean getHighlight() {
return this.highlight; return this.highlight;
} }
Object getData() {
return this.data;
}
} }