Added log type selection dialog

pull/204/head
M66B 3 years ago
parent c2e48ea1f1
commit 21c8c0ee5c

@ -19,6 +19,7 @@ package eu.faircode.email;
Copyright 2018-2021 by Marcel Bokhorst (M66B) Copyright 2018-2021 by Marcel Bokhorst (M66B)
*/ */
import android.content.DialogInterface;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -26,12 +27,12 @@ import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
@ -153,13 +154,6 @@ public class FragmentLogs extends FragmentBase {
menu.findItem(R.id.menu_show).setVisible(all); menu.findItem(R.id.menu_show).setVisible(all);
menu.findItem(R.id.menu_clear).setVisible(all); menu.findItem(R.id.menu_clear).setVisible(all);
List<EntityLog.Type> types = getTypes();
SubMenu smenu = menu.findItem(R.id.menu_show).getSubMenu();
smenu.clear();
for (EntityLog.Type type : EntityLog.Type.values())
smenu.add(1, type.ordinal(), type.ordinal(), type.toString())
.setCheckable(true).setChecked(types.contains(type));
super.onPrepareOptionsMenu(menu); super.onPrepareOptionsMenu(menu);
} }
@ -176,10 +170,8 @@ public class FragmentLogs extends FragmentBase {
item.setChecked(enabled); item.setChecked(enabled);
onMenuAutoScoll(enabled); onMenuAutoScoll(enabled);
return true; return true;
} else if (item.getGroupId() == 1 && item.isCheckable()) { } else if (itemId == R.id.menu_show) {
boolean enabled = !item.isChecked(); onMenuShow();
item.setChecked(enabled);
onMenuShowTypes(item.getOrder(), enabled);
} else if (itemId == R.id.menu_clear) { } else if (itemId == R.id.menu_clear) {
onMenuClear(); onMenuClear();
return true; return true;
@ -196,26 +188,46 @@ public class FragmentLogs extends FragmentBase {
autoScroll = enabled; autoScroll = enabled;
} }
private void onMenuShowTypes(int ordinal, boolean enabled) { private void onMenuShow() {
String[] titles = new String[EntityLog.Type.values().length];
boolean[] states = new boolean[EntityLog.Type.values().length];
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
String name = EntityLog.Type.values()[ordinal].toString().toLowerCase(Locale.ROOT); for (int i = 0; i < EntityLog.Type.values().length; i++) {
prefs.edit().putBoolean("show_log_" + name, enabled).apply(); EntityLog.Type type = EntityLog.Type.values()[i];
adapter.setTypes(getTypes()); titles[i] = type.toString();
String name = type.toString().toLowerCase(Locale.ROOT);
states[i] = prefs.getBoolean("show_log_" + name, true);
}
new AlertDialog.Builder(getContext())
.setTitle(R.string.title_unhide)
.setMultiChoiceItems(titles, states, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int pos, boolean checked) {
EntityLog.Type type = EntityLog.Type.values()[pos];
prefs.edit().putBoolean(getKey(type), checked).apply();
adapter.setTypes(getTypes());
}
})
.setPositiveButton(R.string.title_setup_done, null)
.show();
} }
private List<EntityLog.Type> getTypes() { private List<EntityLog.Type> getTypes() {
List<EntityLog.Type> types = new ArrayList<>(); List<EntityLog.Type> types = new ArrayList<>();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
for (EntityLog.Type type : EntityLog.Type.values()) { for (EntityLog.Type type : EntityLog.Type.values())
String name = type.toString().toLowerCase(Locale.ROOT); if (prefs.getBoolean(getKey(type), true))
if (prefs.getBoolean("show_log_" + name, true))
types.add(type); types.add(type);
}
return types; return types;
} }
private String getKey(EntityLog.Type type) {
return "show_log_" + type.toString().toLowerCase(Locale.ROOT);
}
private void onMenuClear() { private void onMenuClear() {
EntityLog.clear(getContext()); EntityLog.clear(getContext());
} }

Loading…
Cancel
Save