Improved log type selection

This commit is contained in:
M66B 2022-01-29 10:24:16 +01:00
parent e30c5b14c4
commit 08e7f29a26
3 changed files with 63 additions and 53 deletions

View File

@ -29,7 +29,6 @@ import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
@ -50,16 +49,6 @@ public class AdapterLog extends RecyclerView.Adapter<AdapterLog.ViewHolder> {
private LifecycleOwner owner;
private LayoutInflater inflater;
private int textColorSecondary;
private int solarizedYellow;
private int solarizedOrange;
private int solarizedRed;
private int solarizedMagenta;
private int solarizedViolet;
private int solarizedBlue;
private int solarizedCyan;
private int solarizedGreen;
private Long account = null;
private Long folder = null;
private Long message = null;
@ -86,34 +75,11 @@ public class AdapterLog extends RecyclerView.Adapter<AdapterLog.ViewHolder> {
private void bindTo(EntityLog log) {
tvTime.setText(TF.format(log.time));
SpannableStringBuilder ssb = new SpannableStringBuilderEx(log.data);
switch (log.type) {
case General:
break;
case Statistics:
ssb.setSpan(new ForegroundColorSpan(solarizedGreen), 0, ssb.length(), 0);
break;
case Scheduling:
ssb.setSpan(new ForegroundColorSpan(solarizedYellow), 0, ssb.length(), 0);
break;
case Network:
ssb.setSpan(new ForegroundColorSpan(solarizedOrange), 0, ssb.length(), 0);
break;
case Account:
ssb.setSpan(new ForegroundColorSpan(solarizedMagenta), 0, ssb.length(), 0);
break;
case Protocol:
ssb.setSpan(new ForegroundColorSpan(textColorSecondary), 0, ssb.length(), 0);
break;
case Classification:
ssb.setSpan(new ForegroundColorSpan(solarizedViolet), 0, ssb.length(), 0);
break;
case Notification:
ssb.setSpan(new ForegroundColorSpan(solarizedBlue), 0, ssb.length(), 0);
break;
case Rules:
ssb.setSpan(new ForegroundColorSpan(solarizedCyan), 0, ssb.length(), 0);
break;
}
Integer color = log.getColor(context);
if (color != null)
ssb.setSpan(new ForegroundColorSpan(color), 0, ssb.length(), 0);
tvData.setText(ssb);
}
}
@ -124,16 +90,6 @@ public class AdapterLog extends RecyclerView.Adapter<AdapterLog.ViewHolder> {
this.owner = parentFragment.getViewLifecycleOwner();
this.inflater = LayoutInflater.from(parentFragment.getContext());
this.textColorSecondary = Helper.resolveColor(context, android.R.attr.textColorSecondary);
this.solarizedYellow = ContextCompat.getColor(context, R.color.solarizedYellow);
this.solarizedOrange = ContextCompat.getColor(context, R.color.solarizedOrange);
this.solarizedRed = ContextCompat.getColor(context, R.color.solarizedRed);
this.solarizedMagenta = ContextCompat.getColor(context, R.color.solarizedMagenta);
this.solarizedViolet = ContextCompat.getColor(context, R.color.solarizedViolet);
this.solarizedBlue = ContextCompat.getColor(context, R.color.solarizedBlue);
this.solarizedCyan = ContextCompat.getColor(context, R.color.solarizedCyan);
this.solarizedGreen = ContextCompat.getColor(context, R.color.solarizedGreen);
this.TF = Helper.getTimeInstance(context);
setHasStableIds(true);

View File

@ -23,12 +23,16 @@ import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
@ -63,6 +67,9 @@ public class EntityLog {
@NonNull
public String data;
@Ignore
private static Map<Type, Integer> mapColor = new HashMap<>();
enum Type {General, Statistics, Scheduling, Network, Account, Protocol, Classification, Notification, Rules}
private static final ExecutorService executor =
@ -194,6 +201,42 @@ public class EntityLog {
}
}
Integer getColor(Context context) {
return getColor(context, this.type);
}
static Integer getColor(Context context, Type type) {
if (!mapColor.containsKey(type))
mapColor.put(type, _getColor(context, type));
return mapColor.get(type);
}
private static Integer _getColor(Context context, Type type) {
// R.color.solarizedRed
switch (type) {
case General:
return Helper.resolveColor(context, android.R.attr.textColorPrimary);
case Statistics:
return ContextCompat.getColor(context, R.color.solarizedGreen);
case Scheduling:
return ContextCompat.getColor(context, R.color.solarizedYellow);
case Network:
return ContextCompat.getColor(context, R.color.solarizedOrange);
case Account:
return ContextCompat.getColor(context, R.color.solarizedMagenta);
case Protocol:
return Helper.resolveColor(context, android.R.attr.textColorSecondary);
case Classification:
return ContextCompat.getColor(context, R.color.solarizedViolet);
case Notification:
return ContextCompat.getColor(context, R.color.solarizedBlue);
case Rules:
return ContextCompat.getColor(context, R.color.solarizedCyan);
default:
return null;
}
}
@Override
public boolean equals(Object obj) {
if (obj instanceof EntityLog) {

View File

@ -19,10 +19,15 @@ package eu.faircode.email;
Copyright 2018-2022 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@ -189,17 +194,23 @@ public class FragmentLogs extends FragmentBase {
}
private void onMenuShow() {
String[] titles = new String[EntityLog.Type.values().length];
final Context context = getContext();
SpannableStringBuilder[] titles = new SpannableStringBuilder[EntityLog.Type.values().length];
boolean[] states = new boolean[EntityLog.Type.values().length];
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
for (int i = 0; i < EntityLog.Type.values().length; i++) {
EntityLog.Type type = EntityLog.Type.values()[i];
titles[i] = type.toString();
titles[i] = new SpannableStringBuilderEx(type.toString());
Integer color = EntityLog.getColor(context, type);
if (color != null)
titles[i].setSpan(new ForegroundColorSpan(color), 0, titles[i].length(), 0);
titles[i].setSpan(new StyleSpan(Typeface.BOLD), 0, titles[i].length(), 0);
String name = type.toString().toLowerCase(Locale.ROOT);
states[i] = prefs.getBoolean("show_log_" + name, true);
}
new AlertDialog.Builder(getContext())
new AlertDialog.Builder(context)
.setIcon(R.drawable.twotone_visibility_24)
.setTitle(R.string.title_unhide)
.setMultiChoiceItems(titles, states, new DialogInterface.OnMultiChoiceClickListener() {