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);
drawerList = findViewById(R.id.drawer_list);
final DrawerAdapter drawerArray = new DrawerAdapter(this);
drawerList.setAdapter(drawerArray);
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
DrawerItem item = (DrawerItem) parent.getAdapter().getItem(position);
switch (item.getId()) {
DrawerItem item = drawerArray.getItem(position);
switch (item.getMenuId()) {
case R.string.title_setup_export:
onMenuExport();
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_black_theme:
case R.string.title_setup_system_theme:
onMenuTheme(item.getId());
onMenuTheme(item.getMenuId());
break;
case R.string.title_setup_notifications:
onManageNotifications();
@ -174,8 +178,8 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On
drawerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
DrawerItem item = (DrawerItem) parent.getAdapter().getItem(position);
if (item.getId() == R.string.menu_privacy) {
DrawerItem item = drawerArray.getItem(position);
if (item.getMenuId() == R.string.menu_privacy) {
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
@ -197,39 +201,35 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On
});
PackageManager pm = getPackageManager();
DrawerAdapter drawerArray = new DrawerAdapter(this);
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)
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);
String theme = prefs.getString("theme", "system");
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))
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))
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
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)
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)
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)
drawerArray.add(new DrawerItem(this, R.layout.item_drawer, R.drawable.baseline_account_box_24, R.string.menu_privacy));
drawerArray.add(new DrawerItem(this, R.layout.item_drawer, R.drawable.baseline_info_24, R.string.menu_about));
drawerList.setAdapter(drawerArray);
drawerArray.add(new DrawerItem(-12, R.string.menu_privacy, R.drawable.baseline_account_box_24));
drawerArray.add(new DrawerItem(-13, R.string.menu_about, R.drawable.baseline_info_24));
getSupportFragmentManager().addOnBackStackChangedListener(this);

View File

@ -79,6 +79,7 @@ import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Properties;
import javax.mail.Session;
@ -169,14 +170,15 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
drawerLayout.addDrawerListener(drawerToggle);
drawerList = findViewById(R.id.drawer_list);
final DrawerAdapter drawerArray = new DrawerAdapter(ActivityView.this);
drawerList.setAdapter(drawerArray);
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
DrawerItem item = (DrawerItem) parent.getAdapter().getItem(position);
switch (item.getId()) {
case -1:
onMenuFolders((long) item.getData());
break;
DrawerItem item = drawerArray.getItem(position);
switch (item.getMenuId()) {
case R.string.menu_answers:
onMenuAnswers();
break;
@ -186,7 +188,6 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
case R.string.menu_setup:
onMenuSetup();
break;
case R.string.menu_legend:
onMenuLegend();
break;
@ -202,7 +203,6 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
case R.string.menu_about:
onMenuAbout();
break;
case R.string.menu_pro:
onMenuPro();
break;
@ -215,19 +215,21 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
case R.string.menu_other:
onMenuOtherApps();
break;
default:
long account = item.getId();
if (account > 0)
onMenuFolders(account);
}
drawerLayout.closeDrawer(drawerList);
}
});
drawerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
DrawerItem item = (DrawerItem) parent.getAdapter().getItem(position);
switch (item.getId()) {
case -1:
onMenuInbox((long) item.getData());
break;
DrawerItem item = drawerArray.getItem(position);
switch (item.getMenuId()) {
case R.string.menu_operations:
onShowLog();
break;
@ -246,7 +248,11 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
checkUpdate(true);
break;
default:
return false;
long account = item.getId();
if (account < 0)
return false;
else
onMenuInbox(account);
}
drawerLayout.closeDrawer(drawerList);
@ -264,22 +270,6 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
if (accounts == null)
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());
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)
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,
account.color,
account.unseen > 0 ? getString(R.string.title_unseen_count, account.name, account.unseen) : account.toString(),
account.unseen > 0,
account.id));
account.unseen > 0));
drawerArray.add(new DrawerItem(R.layout.item_drawer_separator));
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_reply_24, R.string.menu_answers));
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_list_24, R.string.menu_operations));
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_settings_applications_24, R.string.menu_setup));
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));
drawerArray.add(new DrawerItem(-1));
drawerArray.add(new DrawerItem(-2, R.string.menu_answers, R.drawable.baseline_reply_24));
drawerArray.add(new DrawerItem(-3, R.string.menu_operations, R.drawable.baseline_list_24));
drawerArray.add(new DrawerItem(-4, R.string.menu_setup, R.drawable.baseline_settings_applications_24));
drawerArray.add(new DrawerItem(-5));
drawerArray.add(new DrawerItem(-6, R.string.menu_legend, R.drawable.baseline_help_24));
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)
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)
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 invite = (getIntentInvite().resolveActivity(getPackageManager()) != null);
@ -327,21 +335,19 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
boolean other = (getIntentOtherApps().resolveActivity(getPackageManager()) != null);
if (pro || invite || rate || other)
drawerArray.add(new DrawerItem(R.layout.item_drawer_separator));
drawerArray.add(new DrawerItem(-11));
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)
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)
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)
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_get_app_24, R.string.menu_other));
drawerList.setAdapter(drawerArray);
drawerArray.add(new DrawerItem(-15, R.string.menu_other, R.drawable.baseline_get_app_24));
}
});

View File

@ -30,7 +30,7 @@ public class DrawerAdapter extends ArrayAdapter<DrawerItem> {
}
if (tv != null) {
tv.setText(item.getTitle());
tv.setText(item.getTitle(getContext()));
tv.setTextColor(Helper.resolveColor(getContext(),
item.getHighlight() ? R.attr.colorUnread : android.R.attr.textColorSecondary));
@ -39,6 +39,17 @@ public class DrawerAdapter extends ArrayAdapter<DrawerItem> {
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
public boolean isEnabled(int position) {
DrawerItem item = getItem(position);

View File

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