Refactoring

This commit is contained in:
M66B 2021-02-13 08:49:57 +01:00
parent c7c20002d5
commit f33e776501
3 changed files with 188 additions and 140 deletions

View File

@ -19,12 +19,10 @@ package eu.faircode.email;
Copyright 2018-2021 by Marcel Bokhorst (M66B)
*/
import android.app.Dialog;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
@ -48,16 +46,13 @@ import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
import androidx.constraintlayout.widget.Group;
import androidx.core.content.pm.ShortcutInfoCompat;
@ -74,6 +69,8 @@ import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.snackbar.Snackbar;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
@ -81,8 +78,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static android.app.Activity.RESULT_OK;
public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder> {
private Fragment parentFragment;
private long account;
@ -662,12 +657,79 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
private void onActionSync(boolean childs) {
Bundle args = new Bundle();
args.putLong("folder", folder.id);
args.putInt("months", -1);
args.putBoolean("childs", childs);
Intent data = new Intent();
data.putExtra("args", args);
parentFragment.onActivityResult(FragmentFolders.REQUEST_SYNC, RESULT_OK, data);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long fid = args.getLong("folder");
boolean childs = args.getBoolean("childs");
if (!ConnectionHelper.getNetworkState(context).isSuitable())
throw new IllegalStateException(context.getString(R.string.title_no_internet));
boolean now = true;
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityFolder folder = db.folder().getFolder(fid);
if (folder == null)
return null;
if (folder.selectable)
EntityOperation.sync(context, folder.id, true);
if (childs) {
List<EntityFolder> folders = db.folder().getChildFolders(folder.id);
if (folders != null)
for (EntityFolder child : folders)
if (child.selectable)
EntityOperation.sync(context, child.id, true);
}
if (folder.account != null) {
EntityAccount account = db.account().getAccount(folder.account);
if (account != null && !"connected".equals(account.state))
now = false;
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
ServiceSynchronize.eval(context, "refresh/folder");
if (!now)
throw new IllegalArgumentException(context.getString(R.string.title_no_connection));
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalStateException) {
Snackbar snackbar = Snackbar.make(parentFragment.getView(), ex.getMessage(), Snackbar.LENGTH_LONG)
.setGestureInsetBottomIgnored(true);
snackbar.setAction(R.string.title_fix, new View.OnClickListener() {
@Override
public void onClick(View view) {
context.startActivity(
new Intent(context, ActivitySetup.class)
.putExtra("tab", "connection"));
}
});
snackbar.show();
} else if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG)
.setGestureInsetBottomIgnored(true).show();
else
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "folder:sync");
}
private void onActionEnable(boolean enabled) {
@ -717,8 +779,7 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
FragmentDialogSync sync = new FragmentDialogSync();
sync.setArguments(args);
sync.setTargetFragment(parentFragment, FragmentFolders.REQUEST_SYNC);
sync.show(parentFragment.getParentFragmentManager(), "folder:sync");
sync.show(parentFragment.getParentFragmentManager(), "folder:months");
}
private void onActionProperty(int property, boolean enabled) {
@ -1272,39 +1333,4 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
interface IFolderSelectedListener {
void onFolderSelected(TupleFolderEx folder);
}
public static class FragmentDialogSync extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
String name = getArguments().getString("name");
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_sync, null);
final TextView tvFolder = view.findViewById(R.id.tvFolder);
final EditText etMonths = view.findViewById(R.id.etMonths);
tvFolder.setText(name);
etMonths.setText(null);
return new AlertDialog.Builder(getContext())
.setView(view)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String months = etMonths.getText().toString();
if (TextUtils.isEmpty(months))
getArguments().putInt("months", 0);
else
try {
getArguments().putInt("months", Integer.parseInt(months));
} catch (NumberFormatException ex) {
Log.e(ex);
}
sendResult(RESULT_OK);
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
}
}
}

View File

@ -0,0 +1,111 @@
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-2021 by Marcel Bokhorst (M66B)
*/
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
public class FragmentDialogSync extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
String name = getArguments().getString("name");
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_sync, null);
final TextView tvFolder = view.findViewById(R.id.tvFolder);
final EditText etMonths = view.findViewById(R.id.etMonths);
tvFolder.setText(name);
etMonths.setText(null);
return new AlertDialog.Builder(getContext())
.setView(view)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String months = etMonths.getText().toString();
Bundle args = getArguments();
if (TextUtils.isEmpty(months))
args.putInt("months", 0);
else
try {
args.putInt("months", Integer.parseInt(months));
} catch (NumberFormatException ex) {
Log.e(ex);
return;
}
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
int months = args.getInt("months", -1);
long fid = args.getLong("folder");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityFolder folder = db.folder().getFolder(fid);
if (folder == null || !folder.selectable)
return null;
if (months == 0) {
db.folder().setFolderInitialize(folder.id, Integer.MAX_VALUE);
db.folder().setFolderKeep(folder.id, Integer.MAX_VALUE);
} else if (months > 0) {
db.folder().setFolderInitialize(folder.id, months * 30);
db.folder().setFolderKeep(folder.id, Math.max(folder.keep_days, months * 30));
}
EntityOperation.sync(context, folder.id, true);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
ServiceSynchronize.eval(context, "folder:months");
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(getContext(), getViewLifecycleOwner(), args, "folder:months");
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
}
}

View File

@ -91,10 +91,9 @@ public class FragmentFolders extends FragmentBase {
private NumberFormat NF = NumberFormat.getNumberInstance();
static final int REQUEST_SYNC = 1;
static final int REQUEST_DELETE_LOCAL = 2;
static final int REQUEST_EMPTY_FOLDER = 3;
static final int REQUEST_DELETE_FOLDER = 4;
static final int REQUEST_DELETE_LOCAL = 1;
static final int REQUEST_EMPTY_FOLDER = 2;
static final int REQUEST_DELETE_FOLDER = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -596,10 +595,6 @@ public class FragmentFolders extends FragmentBase {
try {
switch (requestCode) {
case REQUEST_SYNC:
if (resultCode == RESULT_OK && data != null)
onSync(data.getBundleExtra("args"));
break;
case REQUEST_DELETE_LOCAL:
if (resultCode == RESULT_OK && data != null)
onDeleteLocal(data.getBundleExtra("args"));
@ -618,90 +613,6 @@ public class FragmentFolders extends FragmentBase {
}
}
private void onSync(Bundle args) {
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
int months = args.getInt("months", -1);
long fid = args.getLong("folder");
boolean childs = args.getBoolean("childs");
if (months < 0 && !ConnectionHelper.getNetworkState(context).isSuitable())
throw new IllegalStateException(context.getString(R.string.title_no_internet));
boolean now = true;
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityFolder folder = db.folder().getFolder(fid);
if (folder == null)
return null;
if (folder.selectable) {
if (months == 0) {
db.folder().setFolderInitialize(folder.id, Integer.MAX_VALUE);
db.folder().setFolderKeep(folder.id, Integer.MAX_VALUE);
} else if (months > 0) {
db.folder().setFolderInitialize(folder.id, months * 30);
db.folder().setFolderKeep(folder.id, Math.max(folder.keep_days, months * 30));
}
EntityOperation.sync(context, folder.id, true);
}
if (childs) {
List<EntityFolder> folders = db.folder().getChildFolders(folder.id);
if (folders != null)
for (EntityFolder child : folders)
if (child.selectable)
EntityOperation.sync(context, child.id, true);
}
if (folder.account != null) {
EntityAccount account = db.account().getAccount(folder.account);
if (account != null && !"connected".equals(account.state))
now = false;
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
ServiceSynchronize.eval(context, "refresh/folder");
if (!now)
throw new IllegalArgumentException(context.getString(R.string.title_no_connection));
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalStateException) {
Snackbar snackbar = Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG)
.setGestureInsetBottomIgnored(true);
snackbar.setAction(R.string.title_fix, new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(
new Intent(getContext(), ActivitySetup.class)
.putExtra("tab", "connection"));
}
});
snackbar.show();
} else if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG)
.setGestureInsetBottomIgnored(true).show();
else
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "folder:sync");
}
private void onDeleteLocal(Bundle args) {
new SimpleTask<Void>() {
@Override