mirror of https://github.com/M66B/FairEmail.git
parent
46f23faf9a
commit
918bc628c6
@ -0,0 +1,308 @@
|
||||
package eu.faircode.email;
|
||||
|
||||
/*
|
||||
This file is part of FairEmail.
|
||||
|
||||
FairEmail is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
FairEmail is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2018-2020 by Marcel Bokhorst (M66B)
|
||||
*/
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
import androidx.recyclerview.widget.ListUpdateCallback;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.flask.colorpicker.ColorPickerView;
|
||||
import com.flask.colorpicker.builder.ColorPickerClickListener;
|
||||
import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class AdapterKeyword extends RecyclerView.Adapter<AdapterKeyword.ViewHolder> {
|
||||
private Context context;
|
||||
private LifecycleOwner owner;
|
||||
private LayoutInflater inflater;
|
||||
private boolean pro;
|
||||
|
||||
private long id;
|
||||
private List<TupleKeyword> all = new ArrayList<>();
|
||||
|
||||
public class ViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
|
||||
private View view;
|
||||
private CheckBox cbKeyword;
|
||||
private ViewButtonColor btnColor;
|
||||
|
||||
ViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
|
||||
view = itemView.findViewById(R.id.clItem);
|
||||
cbKeyword = itemView.findViewById(R.id.cbKeyword);
|
||||
btnColor = itemView.findViewById(R.id.btnColor);
|
||||
}
|
||||
|
||||
private void wire() {
|
||||
cbKeyword.setOnCheckedChangeListener(this);
|
||||
btnColor.setOnClickListener(this);
|
||||
}
|
||||
|
||||
private void unwire() {
|
||||
cbKeyword.setOnCheckedChangeListener(null);
|
||||
btnColor.setOnClickListener(null);
|
||||
}
|
||||
|
||||
private void bindTo(TupleKeyword keyword) {
|
||||
cbKeyword.setText(keyword.name);
|
||||
cbKeyword.setChecked(keyword.selected);
|
||||
cbKeyword.setEnabled(pro);
|
||||
btnColor.setColor(keyword.color);
|
||||
btnColor.setEnabled(pro);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
int pos = getAdapterPosition();
|
||||
if (pos == RecyclerView.NO_POSITION)
|
||||
return;
|
||||
|
||||
TupleKeyword keyword = all.get(pos);
|
||||
keyword.selected = isChecked;
|
||||
|
||||
Bundle args = new Bundle();
|
||||
args.putLong("id", id);
|
||||
args.putString("keyword", keyword.name);
|
||||
args.putBoolean("selected", keyword.selected);
|
||||
|
||||
new SimpleTask<Void>() {
|
||||
@Override
|
||||
protected Void onExecute(Context context, Bundle args) {
|
||||
long id = args.getLong("id");
|
||||
String keyword = args.getString("keyword");
|
||||
boolean selected = args.getBoolean("selected");
|
||||
|
||||
DB db = DB.getInstance(context);
|
||||
|
||||
EntityMessage message = db.message().getMessage(id);
|
||||
if (message == null)
|
||||
return null;
|
||||
|
||||
List<String> keywords = new ArrayList<>(Arrays.asList(message.keywords));
|
||||
if (selected)
|
||||
keywords.add(keyword);
|
||||
else
|
||||
keywords.remove(keyword);
|
||||
|
||||
db.message().setMessageKeywords(message.id, TextUtils.join(" ", keywords));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onException(Bundle args, Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
}.execute(context, owner, args, "keyword:set");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
int pos = getAdapterPosition();
|
||||
if (pos == RecyclerView.NO_POSITION)
|
||||
return;
|
||||
|
||||
final TupleKeyword keyword = all.get(pos);
|
||||
|
||||
ColorPickerDialogBuilder builder = ColorPickerDialogBuilder
|
||||
.with(context)
|
||||
.setTitle(context.getString(R.string.title_color))
|
||||
.showColorEdit(true)
|
||||
.wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
|
||||
.density(6)
|
||||
.lightnessSliderOnly()
|
||||
.setNegativeButton(R.string.title_reset, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
update(keyword, null);
|
||||
}
|
||||
})
|
||||
.setPositiveButton(android.R.string.ok, new ColorPickerClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) {
|
||||
update(keyword, selectedColor);
|
||||
}
|
||||
});
|
||||
|
||||
if (keyword.color != null)
|
||||
builder.initialColor(keyword.color);
|
||||
|
||||
builder.build().show();
|
||||
}
|
||||
|
||||
private void update(TupleKeyword keyword, Integer color) {
|
||||
btnColor.setColor(color);
|
||||
keyword.color = color;
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
if (color == null)
|
||||
prefs.edit().remove("keyword." + keyword.name).apply();
|
||||
else
|
||||
prefs.edit().putInt("keyword." + keyword.name, keyword.color).apply();
|
||||
|
||||
Bundle args = new Bundle();
|
||||
args.putLong("id", id);
|
||||
|
||||
new SimpleTask<Void>() {
|
||||
@Override
|
||||
protected Void onExecute(Context context, Bundle args) {
|
||||
long id = args.getLong("id");
|
||||
|
||||
DB db = DB.getInstance(context);
|
||||
|
||||
EntityMessage message = db.message().getMessage(id);
|
||||
if (message == null)
|
||||
return null;
|
||||
|
||||
// Update keyword colors
|
||||
try {
|
||||
db.beginTransaction();
|
||||
|
||||
db.message().setMessageKeywords(message.id, "");
|
||||
db.message().setMessageKeywords(message.id, TextUtils.join(" ", message.keywords));
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onException(Bundle args, Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
}.execute(context, owner, args, "keyword:set");
|
||||
}
|
||||
}
|
||||
|
||||
AdapterKeyword(Context context, LifecycleOwner owner) {
|
||||
this.context = context;
|
||||
this.owner = owner;
|
||||
this.inflater = LayoutInflater.from(context);
|
||||
this.pro = ActivityBilling.isPro(context);
|
||||
|
||||
setHasStableIds(false);
|
||||
}
|
||||
|
||||
public void set(long id, @NonNull List<TupleKeyword> keywords) {
|
||||
Log.i("Set id=" + id + " keywords=" + keywords.size());
|
||||
|
||||
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(all, keywords), false);
|
||||
|
||||
this.id = id;
|
||||
this.all = keywords;
|
||||
|
||||
diff.dispatchUpdatesTo(new ListUpdateCallback() {
|
||||
@Override
|
||||
public void onInserted(int position, int count) {
|
||||
Log.d("Inserted @" + position + " #" + count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRemoved(int position, int count) {
|
||||
Log.d("Removed @" + position + " #" + count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMoved(int fromPosition, int toPosition) {
|
||||
Log.d("Moved " + fromPosition + ">" + toPosition);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChanged(int position, int count, Object payload) {
|
||||
Log.d("Changed @" + position + " #" + count);
|
||||
}
|
||||
});
|
||||
diff.dispatchUpdatesTo(this);
|
||||
}
|
||||
|
||||
|
||||
private class DiffCallback extends DiffUtil.Callback {
|
||||
private List<TupleKeyword> prev = new ArrayList<>();
|
||||
private List<TupleKeyword> next = new ArrayList<>();
|
||||
|
||||
DiffCallback(List<TupleKeyword> prev, List<TupleKeyword> next) {
|
||||
this.prev.addAll(prev);
|
||||
this.next.addAll(next);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOldListSize() {
|
||||
return prev.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNewListSize() {
|
||||
return next.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
|
||||
TupleKeyword k1 = prev.get(oldItemPosition);
|
||||
TupleKeyword k2 = next.get(newItemPosition);
|
||||
return k1.name.equals(k2.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
|
||||
TupleKeyword k1 = prev.get(oldItemPosition);
|
||||
TupleKeyword k2 = next.get(newItemPosition);
|
||||
return k1.equals(k2);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return all.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
return new ViewHolder(inflater.inflate(R.layout.item_keyword, parent, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||
holder.unwire();
|
||||
TupleKeyword contact = all.get(position);
|
||||
holder.bindTo(contact);
|
||||
holder.wire();
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
package eu.faircode.email;
|
||||
|
||||
/*
|
||||
This file is part of FairEmail.
|
||||
|
||||
FairEmail is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
FairEmail is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2018-2020 by Marcel Bokhorst (M66B)
|
||||
*/
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class TupleKeyword {
|
||||
public String name;
|
||||
public boolean selected;
|
||||
public Integer color;
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (obj instanceof TupleKeyword) {
|
||||
TupleKeyword other = (TupleKeyword) obj;
|
||||
return (this.name.equals(other.name) &&
|
||||
this.selected == other.selected &&
|
||||
Objects.equals(this.color, other.color));
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
public static class Persisted {
|
||||
public String[] selected;
|
||||
public String[] available;
|
||||
}
|
||||
|
||||
static List<TupleKeyword> from(Context context, Persisted data) {
|
||||
List<TupleKeyword> result = new ArrayList<>();
|
||||
|
||||
List<String> keywords = new ArrayList<>();
|
||||
|
||||
for (String keyword : data.selected)
|
||||
if (!keywords.contains(keyword))
|
||||
keywords.add(keyword);
|
||||
|
||||
for (String keyword : data.available)
|
||||
if (!keywords.contains(keyword))
|
||||
keywords.add(keyword);
|
||||
|
||||
Collections.sort(keywords);
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
|
||||
for (String keyword : keywords) {
|
||||
TupleKeyword k = new TupleKeyword();
|
||||
k.name = keyword;
|
||||
k.selected = Arrays.asList(data.selected).contains(keyword);
|
||||
|
||||
String c = "keyword." + keyword;
|
||||
if (prefs.contains(c))
|
||||
k.color = prefs.getInt(c, -1);
|
||||
|
||||
result.add(k);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="12dp">
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/rvKeyword"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="6dp"
|
||||
android:clipToPadding="false"
|
||||
android:paddingBottom="60dp"
|
||||
android:scrollbarStyle="outsideOverlay"
|
||||
android:scrollbarThumbVertical="@drawable/scroll_thumb_dialog"
|
||||
android:scrollbars="vertical"
|
||||
app:layout_constrainedHeight="true"
|
||||
app:layout_constraintBottom_toTopOf="@+id/tvPro"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvPro"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/title_pro_feature"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||
android:textColor="?android:attr/textColorLink"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/rvKeyword" />
|
||||
|
||||
<com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
android:id="@+id/fabAdd"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="end|bottom"
|
||||
android:contentDescription="@string/title_add"
|
||||
android:tint="?attr/colorFabForeground"
|
||||
app:backgroundTint="?attr/colorFabBackground"
|
||||
app:fabSize="mini"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:srcCompat="@drawable/baseline_add_24" />
|
||||
|
||||
<eu.faircode.email.ContentLoadingProgressBar
|
||||
android:id="@+id/pbWait"
|
||||
style="@style/Base.Widget.AppCompat.ProgressBar"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:indeterminate="true"
|
||||
android:padding="24dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:id="@+id/clItem"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?attr/activatableItemBackground"
|
||||
android:padding="6dp">
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/cbKeyword"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Keyword"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/btnColor"
|
||||
app:layout_constraintEnd_toStartOf="@+id/btnColor"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@+id/btnColor" />
|
||||
|
||||
<eu.faircode.email.ViewButtonColor
|
||||
android:id="@+id/btnColor"
|
||||
style="@style/buttonStyleSmall"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:minWidth="0dp"
|
||||
android:minHeight="0dp"
|
||||
android:padding="9dp"
|
||||
android:text="@string/title_select"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/cbKeyword"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</FrameLayout>
|
Loading…
Reference in new issue