Refactoring

master
M66B 1 week ago
parent 1d1f087b7a
commit 49b8b90aac

@ -55,7 +55,7 @@ public class AdapterKeyword extends RecyclerView.Adapter<AdapterKeyword.ViewHold
private LayoutInflater inflater;
private SharedPreferences prefs;
private long id;
private long[] ids;
private List<TupleKeyword> all = new ArrayList<>();
public class ViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
@ -113,24 +113,33 @@ public class AdapterKeyword extends RecyclerView.Adapter<AdapterKeyword.ViewHold
keyword.selected = isChecked;
Bundle args = new Bundle();
args.putLong("id", id);
args.putLongArray("ids", ids);
args.putString("name", keyword.name);
args.putBoolean("set", keyword.selected);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
long[] ids = args.getLongArray("ids");
String name = args.getString("name");
boolean set = args.getBoolean("set");
DB db = DB.getInstance(context);
EntityMessage message = db.message().getMessage(id);
if (message == null)
return null;
try {
db.beginTransaction();
if (ids != null)
for (long id : ids) {
EntityMessage message = db.message().getMessage(id);
if (message != null)
EntityOperation.queue(context, message, EntityOperation.KEYWORD, name, set);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}
@ -245,12 +254,12 @@ public class AdapterKeyword extends RecyclerView.Adapter<AdapterKeyword.ViewHold
setHasStableIds(false);
}
public void set(long id, @NonNull List<TupleKeyword> keywords) {
Log.i("Set id=" + id + " keywords=" + keywords.size());
public void set(long[] ids, @NonNull List<TupleKeyword> keywords) {
Log.i("Set ids=" + ids.length + " keywords=" + keywords.size());
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(all, keywords), false);
this.id = id;
this.ids = ids;
this.all = keywords;
diff.dispatchUpdatesTo(new ListUpdateCallback() {

@ -7516,7 +7516,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
private void onMenuManageKeywords(TupleMessageEx message) {
Bundle args = new Bundle();
args.putLong("id", message.id);
args.putLongArray("ids", new long[]{message.id});
FragmentDialogKeywordManage fragment = new FragmentDialogKeywordManage();
fragment.setArguments(args);

@ -21,7 +21,7 @@ package eu.faircode.email;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
@ -31,7 +31,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Observer;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@ -39,65 +38,62 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class FragmentDialogKeywordManage extends FragmentDialogBase {
private View dview;
private RecyclerView rvKeyword;
private FloatingActionButton fabAdd;
private ContentLoadingProgressBar pbWait;
private AdapterKeyword adapter;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final long id = getArguments().getLong("id");
final Bundle args = getArguments();
final Context context = getContext();
final View dview = LayoutInflater.from(context).inflate(R.layout.dialog_keyword_manage, null);
final RecyclerView rvKeyword = dview.findViewById(R.id.rvKeyword);
final FloatingActionButton fabAdd = dview.findViewById(R.id.fabAdd);
final ContentLoadingProgressBar pbWait = dview.findViewById(R.id.pbWait);
dview = LayoutInflater.from(context).inflate(R.layout.dialog_keyword_manage, null);
rvKeyword = dview.findViewById(R.id.rvKeyword);
fabAdd = dview.findViewById(R.id.fabAdd);
pbWait = dview.findViewById(R.id.pbWait);
rvKeyword.setHasFixedSize(false);
final LinearLayoutManager llm = new LinearLayoutManager(context);
rvKeyword.setLayoutManager(llm);
final AdapterKeyword adapter = new AdapterKeyword(context, getViewLifecycleOwner());
adapter = new AdapterKeyword(context, getViewLifecycleOwner());
rvKeyword.setAdapter(adapter);
fabAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle args = new Bundle();
args.putLongArray("ids", new long[]{id});
FragmentDialogKeywordAdd fragment = new FragmentDialogKeywordAdd();
fragment.setArguments(args);
fragment.setTargetFragment(FragmentDialogKeywordManage.this, 1);
fragment.show(getParentFragmentManager(), "keyword:add");
}
});
pbWait.setVisibility(View.VISIBLE);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
long[] ids = args.getLongArray("ids");
if (ids.length == 1) {
DB db = DB.getInstance(context);
db.message().liveMessageKeywords(id).observe(getViewLifecycleOwner(), new Observer<TupleKeyword.Persisted>() {
db.message().liveMessageKeywords(ids[0]).observe(getViewLifecycleOwner(), new Observer<TupleKeyword.Persisted>() {
@Override
public void onChanged(TupleKeyword.Persisted data) {
if (data == null)
data = new TupleKeyword.Persisted();
String global = prefs.getString("global_keywords", null);
if (global != null) {
List<String> available = new ArrayList<>();
if (data.available != null)
available.addAll(Arrays.asList(data.available));
for (String kw : global.split(" "))
if (!available.contains(kw))
available.add(kw);
data.available = available.toArray(new String[0]);
}
pbWait.setVisibility(View.GONE);
adapter.set(id, TupleKeyword.from(context, data));
adapter.set(ids, TupleKeyword.from(context, data));
}
});
} else {
task.execute(context, getViewLifecycleOwner(), args, "keywords:get");
}
return new AlertDialog.Builder(context)
.setIcon(R.drawable.twotone_label_important_24)
@ -114,4 +110,51 @@ public class FragmentDialogKeywordManage extends FragmentDialogBase {
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
//dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
task.execute(getContext(), getViewLifecycleOwner(), getArguments(), "keywords:get");
}
final SimpleTask<TupleKeyword.Persisted> task = new SimpleTask<TupleKeyword.Persisted>() {
@Override
protected TupleKeyword.Persisted onExecute(Context context, Bundle args) {
long[] ids = args.getLongArray("ids");
List<String> selected = new ArrayList<>();
List<String> available = new ArrayList<>();
DB db = DB.getInstance(context);
if (ids != null)
for (long id : ids) {
TupleKeyword.Persisted kws = db.message().getMessageKeywords(id);
List<String> list = (kws == null || kws.selected == null
? Collections.emptyList() : Arrays.asList(kws.selected));
if (id == ids[0]) // First
selected.addAll(list);
else // Check if all message have all keywords
for (String kw : new ArrayList<>(selected))
if (!list.contains(kw))
selected.remove(kw);
if (kws != null && kws.available != null)
for (String kw : kws.available)
if (!available.contains(kw))
available.add(kw);
}
return new TupleKeyword.Persisted(selected, available);
}
@Override
protected void onExecuted(Bundle args, TupleKeyword.Persisted data) {
pbWait.setVisibility(View.GONE);
adapter.set(args.getLongArray("ids"), TupleKeyword.from(getContext(), data));
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
};
}

@ -449,6 +449,7 @@ public class FragmentMessages extends FragmentBase
static final int REQUEST_CALENDAR = 29;
static final int REQUEST_EDIT_SUBJECT = 30;
private static final int REQUEST_ANSWER_SETTINGS = 31;
private static final int REQUEST_DESELECT = 32;
static final String ACTION_STORE_RAW = BuildConfig.APPLICATION_ID + ".STORE_RAW";
static final String ACTION_VERIFYDECRYPT = BuildConfig.APPLICATION_ID + ".VERIFYDECRYPT";
@ -5202,122 +5203,11 @@ public class FragmentMessages extends FragmentBase
Bundle args = new Bundle();
args.putLongArray("ids", getSelection());
new SimpleTask<Pair<List<String>, List<String>>>() {
@Override
protected Pair<List<String>, List<String>> onExecute(Context context, Bundle args) {
long[] ids = args.getLongArray("ids");
List<String> selected = new ArrayList<>();
List<String> available = new ArrayList<>();
DB db = DB.getInstance(context);
if (ids != null)
for (long id : ids) {
TupleKeyword.Persisted kws = db.message().getMessageKeywords(id);
List<String> list = (kws == null || kws.selected == null
? Collections.emptyList() : Arrays.asList(kws.selected));
if (id == ids[0]) // First
selected.addAll(list);
else // Check if all message have all keywords
for (String kw : new ArrayList<>(selected))
if (!list.contains(kw))
selected.remove(kw);
if (kws != null && kws.available != null)
for (String kw : kws.available)
if (!available.contains(kw))
available.add(kw);
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String global = prefs.getString("global_keywords", null);
if (global != null)
for (String kw : global.split(" "))
if (!available.contains(kw))
available.add(kw);
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(available, collator);
return new Pair<>(selected, available);
}
@Override
protected void onExecuted(Bundle args, Pair<List<String>, List<String>> data) {
boolean[] checked = new boolean[data.second.size()];
for (int i = 0; i < checked.length; i++)
if (data.first.contains(data.second.get(i)))
checked[i] = true;
new AlertDialog.Builder(getContext())
.setIcon(R.drawable.twotone_label_important_24)
.setTitle(R.string.title_manage_keywords)
.setMultiChoiceItems(data.second.toArray(new String[0]), checked, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
args.putString("keyword", data.second.get(which));
args.putBoolean("set", isChecked);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long[] ids = args.getLongArray("ids");
String keyword = args.getString("keyword");
boolean set = args.getBoolean("set");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
if (ids != null)
for (long id : ids) {
EntityMessage message = db.message().getMessage(id);
if (message != null)
EntityOperation.queue(context, message, EntityOperation.KEYWORD, keyword, set);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(FragmentMessages.this, args, "keywords:set");
}
})
.setNegativeButton(R.string.title_setup_done, null)
.setNeutralButton(R.string.title_add, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
FragmentDialogKeywordAdd fragment = new FragmentDialogKeywordAdd();
FragmentDialogKeywordManage fragment = new FragmentDialogKeywordManage();
fragment.setArguments(args);
fragment.show(getParentFragmentManager(), "keywords:add");
}
})
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (clear && selectionTracker != null)
selectionTracker.clearSelection();
}
})
.show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(FragmentMessages.this, args, "keywords:get");
if (clear)
fragment.setTargetFragment(FragmentMessages.this, REQUEST_DESELECT);
fragment.show(getParentFragmentManager(), "keyword:manage");
}
private void onActionMoveSelection(Bundle args) {
@ -9558,6 +9448,10 @@ public class FragmentMessages extends FragmentBase
if (resultCode == RESULT_OK)
updateAnswerIcon();
break;
case REQUEST_DESELECT:
if (selectionTracker != null)
selectionTracker.clearSelection();
break;
}
} catch (Throwable ex) {
Log.e(ex);

@ -55,6 +55,16 @@ public class TupleKeyword {
public static class Persisted {
public String[] selected;
public String[] available;
public Persisted() {
selected = new String[0];
available = new String[0];
}
public Persisted(List<String> selected, List<String> available) {
this.selected = selected.toArray(new String[0]);
this.available = available.toArray(new String[0]);
}
}
static List<TupleKeyword> from(Context context, Persisted data) {
@ -78,6 +88,12 @@ public class TupleKeyword {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String global = prefs.getString("global_keywords", null);
if (global != null)
for (String kw : global.split(" "))
if (!all.contains(kw))
all.add(kw);
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc

Loading…
Cancel
Save