Made operations available without debug mode

This commit is contained in:
M66B 2018-12-11 09:47:37 +01:00
parent fb397b3e82
commit 93d7dd19a1
7 changed files with 175 additions and 73 deletions

View File

@ -281,9 +281,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
drawerArray.add(new DrawerItem(R.layout.item_drawer_separator));
if (PreferenceManager.getDefaultSharedPreferences(ActivityView.this).getBoolean("debug", false))
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_list_24, R.string.menu_operations));
drawerArray.add(new DrawerItem(ActivityView.this, R.layout.item_drawer, R.drawable.baseline_help_24, R.string.menu_legend));
if (getIntentFAQ().resolveActivity(getPackageManager()) != null)

View File

@ -22,16 +22,17 @@ package eu.faircode.email;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import androidx.annotation.NonNull;
@ -45,16 +46,14 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
private Context context;
private LifecycleOwner owner;
private List<EntityOperation> all = new ArrayList<>();
private List<EntityOperation> filtered = new ArrayList<>();
private List<TupleOperationEx> all = new ArrayList<>();
private List<TupleOperationEx> filtered = new ArrayList<>();
private DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.LONG);
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private View itemView;
private TextView tvFolder;
private TextView tvMessage;
private TextView tvName;
private TextView tvArgs;
private TextView tvOperation;
private TextView tvTime;
private TextView tvError;
@ -62,16 +61,15 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
super(itemView);
this.itemView = itemView;
tvFolder = itemView.findViewById(R.id.tvFolder);
tvMessage = itemView.findViewById(R.id.tvMessage);
tvName = itemView.findViewById(R.id.tvName);
tvArgs = itemView.findViewById(R.id.tvArgs);
tvOperation = itemView.findViewById(R.id.tvOperation);
tvTime = itemView.findViewById(R.id.tvTime);
tvError = itemView.findViewById(R.id.tvError);
}
private void wire() {
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
private void unwire() {
@ -79,11 +77,21 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
itemView.setOnLongClickListener(null);
}
private void bindTo(EntityOperation operation) {
private void bindTo(TupleOperationEx operation) {
StringBuilder sb = new StringBuilder();
sb.append(operation.name);
try {
JSONArray jarray = new JSONArray(operation.args);
if (jarray.length() > 0)
sb.append(' ').append(operation.args);
} catch (JSONException ex) {
sb.append(' ').append(ex.toString());
}
tvFolder.setText(operation.accountName + "/" + operation.folderName);
tvMessage.setText(operation.message == null ? null : Long.toString(operation.message));
tvName.setText(operation.name);
tvArgs.setText(operation.args);
tvTime.setText(df.format(new Date(operation.created)));
tvOperation.setText(sb.toString());
tvTime.setText(DateUtils.getRelativeTimeSpanString(context, operation.created));
tvError.setText(operation.error);
tvError.setVisibility(operation.error == null ? View.GONE : View.VISIBLE);
}
@ -94,7 +102,7 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
if (pos == RecyclerView.NO_POSITION)
return;
EntityOperation operation = filtered.get(pos);
TupleOperationEx operation = filtered.get(pos);
if (operation.message == null) {
Bundle args = new Bundle();
args.putLong("id", operation.folder);
@ -139,33 +147,6 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
}.load(context, owner, args);
}
}
@Override
public boolean onLongClick(View view) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return false;
EntityOperation operation = filtered.get(pos);
Bundle args = new Bundle();
args.putLong("id", operation.id);
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) {
DB.getInstance(context).operation().deleteOperation(args.getLong("id"));
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(context, owner, ex);
}
}.load(context, owner, args);
return true;
}
}
AdapterOperation(Context context, LifecycleOwner owner) {
@ -174,7 +155,7 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
setHasStableIds(true);
}
public void set(@NonNull List<EntityOperation> operations) {
public void set(@NonNull List<TupleOperationEx> operations) {
Log.i(Helper.TAG, "Set operations=" + operations.size());
all = operations;
@ -209,10 +190,10 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
}
private class MessageDiffCallback extends DiffUtil.Callback {
private List<EntityOperation> prev;
private List<EntityOperation> next;
private List<TupleOperationEx> prev;
private List<TupleOperationEx> next;
MessageDiffCallback(List<EntityOperation> prev, List<EntityOperation> next) {
MessageDiffCallback(List<TupleOperationEx> prev, List<TupleOperationEx> next) {
this.prev = prev;
this.next = next;
}
@ -229,15 +210,15 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
EntityOperation a1 = prev.get(oldItemPosition);
EntityOperation a2 = next.get(newItemPosition);
TupleOperationEx a1 = prev.get(oldItemPosition);
TupleOperationEx a2 = next.get(newItemPosition);
return a1.id.equals(a2.id);
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
EntityOperation a1 = prev.get(oldItemPosition);
EntityOperation a2 = next.get(newItemPosition);
TupleOperationEx a1 = prev.get(oldItemPosition);
TupleOperationEx a2 = next.get(newItemPosition);
return a1.equals(a2);
}
}
@ -262,7 +243,7 @@ public class AdapterOperation extends RecyclerView.Adapter<AdapterOperation.View
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.unwire();
EntityOperation operation = filtered.get(position);
TupleOperationEx operation = filtered.get(position);
holder.bindTo(operation);
holder.wire();

View File

@ -38,8 +38,12 @@ public interface DaoOperation {
", id")
List<EntityOperation> getOperationsByFolder(long folder, boolean outbox);
@Query("SELECT * FROM operation ORDER BY id")
LiveData<List<EntityOperation>> liveOperations();
@Query("SELECT operation.*, account.name AS accountName, folder.name AS folderName" +
" FROM operation" +
" JOIN folder ON folder.id = operation.folder" +
" JOIN account ON account.id = folder.account" +
" ORDER BY operation.id")
LiveData<List<TupleOperationEx>> liveOperations();
@Query("SELECT * FROM operation WHERE folder = :folder ORDER BY id")
LiveData<List<EntityOperation>> liveOperations(long folder);
@ -47,6 +51,9 @@ public interface DaoOperation {
@Query("SELECT * FROM operation ORDER BY id")
List<EntityOperation> getOperations();
@Query("SELECT * FROM operation ORDER BY id LIMIT 1")
EntityOperation getOperationFirst();
@Query("SELECT COUNT(id) FROM operation" +
" WHERE folder = :folder" +
" AND (:name IS NULL OR operation.name = :name)")

View File

@ -19,8 +19,15 @@ package eu.faircode.email;
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
@ -46,6 +53,7 @@ public class FragmentOperations extends FragmentEx {
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.menu_operations);
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_operations, container, false);
@ -75,9 +83,9 @@ public class FragmentOperations extends FragmentEx {
super.onActivityCreated(savedInstanceState);
// Observe folders
DB.getInstance(getContext()).operation().liveOperations().observe(getViewLifecycleOwner(), new Observer<List<EntityOperation>>() {
DB.getInstance(getContext()).operation().liveOperations().observe(getViewLifecycleOwner(), new Observer<List<TupleOperationEx>>() {
@Override
public void onChanged(@Nullable List<EntityOperation> operations) {
public void onChanged(@Nullable List<TupleOperationEx> operations) {
if (operations == null)
operations = new ArrayList<>();
@ -88,4 +96,58 @@ public class FragmentOperations extends FragmentEx {
}
});
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_operations, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
PackageManager pm = getContext().getPackageManager();
menu.findItem(R.id.menu_help).setVisible(getFAQIntent().resolveActivity(pm) != null);
super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_help:
onMenuHelp();
return true;
case R.id.menu_delete:
onMenuDelete();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void onMenuHelp() {
startActivity(getFAQIntent());
}
private void onMenuDelete() {
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) {
DB db = DB.getInstance(context);
EntityOperation operation = db.operation().getOperationFirst();
if (operation != null) {
if (operation.message != null)
db.message().setMessageUiHide(operation.message, false);
db.operation().deleteOperation(operation.id);
}
return null;
}
}.load(this, new Bundle());
}
private Intent getFAQIntent() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://github.com/M66B/open-source-email/blob/master/FAQ.md#user-content-faq3"));
return intent;
}
}

View File

@ -0,0 +1,36 @@
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 by Marcel Bokhorst (M66B)
*/
public class TupleOperationEx extends EntityOperation {
public String accountName;
public String folderName;
@Override
public boolean equals(Object obj) {
if (obj instanceof TupleOperationEx) {
TupleOperationEx other = (TupleOperationEx) obj;
return (super.equals(obj) &&
(this.accountName == null ? other.accountName == null : accountName.equals(other.accountName)) &&
(this.folderName == null ? other.folderName == null : this.folderName.equals(other.folderName)));
} else
return false;
}
}

View File

@ -7,35 +7,39 @@
android:layout_marginBottom="3dp">
<TextView
android:id="@+id/tvMessage"
android:layout_width="50dp"
android:id="@+id/tvFolder"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:text="Msg#"
android:ellipsize="start"
android:singleLine="true"
android:text="Folder"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:id="@+id/tvMessage"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:text="Name"
android:gravity="end"
android:text="1234567"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toEndOf="@id/tvMessage"
app:layout_constraintStart_toEndOf="@id/tvFolder"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvArgs"
android:id="@+id/tvOperation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginEnd="6dp"
android:text="Arguments"
android:ellipsize="end"
android:maxLines="1"
android:text="Name"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toStartOf="@+id/tvTime"
app:layout_constraintStart_toEndOf="@id/tvName"
app:layout_constraintStart_toEndOf="@id/tvMessage"
app:layout_constraintTop_toTopOf="parent" />
<TextView
@ -65,6 +69,6 @@
android:textColor="?attr/colorWarning"
android:textIsSelectable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/tvName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/barrier" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_help"
android:icon="@drawable/baseline_info_24"
android:title=""
app:showAsAction="always" />
<item
android:id="@+id/menu_delete"
android:title="@string/title_delete"
app:showAsAction="never" />
</menu>