FairEmail/app/src/main/java/eu/faircode/email/FragmentDialogSync.java

152 lines
6.3 KiB
Java
Raw Normal View History

2021-02-13 07:49:57 +00:00
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/>.
2024-01-01 07:50:49 +00:00
Copyright 2018-2024 by Marcel Bokhorst (M66B)
2021-02-13 07:49:57 +00:00
*/
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;
2022-03-21 15:33:08 +00:00
import java.util.ArrayList;
2021-02-13 08:34:38 +00:00
import java.util.List;
2021-02-13 07:49:57 +00:00
public class FragmentDialogSync extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
2021-02-13 08:34:38 +00:00
Bundle args = getArguments();
long fid = args.getLong("folder");
String name = args.getString("name");
String type = args.getString("type");
2021-02-13 07:49:57 +00:00
2024-01-31 15:07:25 +00:00
final Context context = getContext();
View view = LayoutInflater.from(context).inflate(R.layout.dialog_sync, null);
2021-02-13 07:49:57 +00:00
final TextView tvFolder = view.findViewById(R.id.tvFolder);
final EditText etMonths = view.findViewById(R.id.etMonths);
2021-08-12 11:29:46 +00:00
final TextView tvRemark = view.findViewById(R.id.tvRemark);
2021-02-13 07:49:57 +00:00
2021-02-13 08:34:38 +00:00
if (fid < 0) {
if (TextUtils.isEmpty(type))
tvFolder.setText(R.string.title_folder_unified);
else
2024-01-31 15:07:25 +00:00
tvFolder.setText(EntityFolder.localizeType(context, type));
2021-02-13 08:34:38 +00:00
} else
tvFolder.setText(name);
2024-01-31 14:36:44 +00:00
etMonths.setText("3");
2021-02-13 07:49:57 +00:00
2021-08-12 11:29:46 +00:00
tvRemark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Helper.viewFAQ(view.getContext(), 39);
}
});
2024-01-31 15:07:25 +00:00
return new AlertDialog.Builder(context)
2021-02-13 07:49:57 +00:00
.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) {
long fid = args.getLong("folder");
2021-02-13 08:34:38 +00:00
String type = args.getString("type");
int months = args.getInt("months", -1);
2022-03-21 15:33:08 +00:00
boolean children = args.getBoolean("children");
2021-02-13 07:49:57 +00:00
DB db = DB.getInstance(context);
try {
db.beginTransaction();
2021-02-13 08:34:38 +00:00
List<EntityFolder> folders;
if (fid < 0)
folders = db.folder().getFoldersUnified(type, false);
else {
EntityFolder folder = db.folder().getFolder(fid);
2021-02-13 13:52:09 +00:00
if (folder == null)
2021-02-13 08:34:38 +00:00
return null;
2022-03-21 15:33:08 +00:00
folders = new ArrayList<>();
folders.add(folder);
if (children) {
2024-04-24 06:00:52 +00:00
List<EntityFolder> sub = EntityFolder.getChildFolders(context, folder.id);
folders.addAll(sub);
2022-03-21 15:33:08 +00:00
}
2021-02-13 07:49:57 +00:00
}
2021-02-13 13:52:09 +00:00
for (EntityFolder folder : folders)
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);
2024-01-30 13:07:13 +00:00
db.folder().setFolderKeep(folder.id, months * 30);
2021-02-13 13:52:09 +00:00
}
EntityOperation.sync(context, folder.id, true);
2021-02-13 08:34:38 +00:00
}
2021-02-13 07:49:57 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
ServiceSynchronize.eval(context, "folder:months");
2021-02-13 07:49:57 +00:00
return null;
2021-03-25 08:36:45 +00:00
}
2021-02-13 07:49:57 +00:00
@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();
}
}