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

181 lines
6.7 KiB
Java
Raw Normal View History

2018-08-13 06:59:05 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-13 06:59:05 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-13 06:59:05 +00:00
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.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-13 06:59:05 +00:00
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
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-13 06:59:05 +00:00
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 by Marcel Bokhorst (M66B)
2018-08-13 06:59:05 +00:00
*/
import android.app.Dialog;
import android.content.Context;
2018-12-11 09:01:31 +00:00
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
2018-08-13 06:59:05 +00:00
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
2018-08-13 06:59:05 +00:00
import android.view.View;
import android.view.ViewGroup;
2019-01-13 09:16:42 +00:00
import android.widget.TextView;
2018-08-13 06:59:05 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
2018-08-13 06:59:05 +00:00
import androidx.constraintlayout.widget.Group;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
2019-01-15 17:41:55 +00:00
public class FragmentOperations extends FragmentBase {
2019-01-13 09:16:42 +00:00
private TextView tvNoOperation;
2018-08-13 06:59:05 +00:00
private RecyclerView rvOperation;
2018-12-27 12:31:02 +00:00
private ContentLoadingProgressBar pbWait;
2018-08-13 06:59:05 +00:00
private Group grpReady;
private AdapterOperation adapter;
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2018-09-12 08:19:24 +00:00
setSubtitle(R.string.menu_operations);
setHasOptionsMenu(true);
2018-09-12 08:19:24 +00:00
2018-08-13 06:59:05 +00:00
View view = inflater.inflate(R.layout.fragment_operations, container, false);
// Get controls
2019-01-13 09:16:42 +00:00
tvNoOperation = view.findViewById(R.id.tvNoOperation);
2018-08-13 06:59:05 +00:00
rvOperation = view.findViewById(R.id.rvOperation);
pbWait = view.findViewById(R.id.pbWait);
grpReady = view.findViewById(R.id.grpReady);
// Wire controls
rvOperation.setHasFixedSize(false);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
rvOperation.setLayoutManager(llm);
adapter = new AdapterOperation(this);
2018-08-13 06:59:05 +00:00
rvOperation.setAdapter(adapter);
// Initialize
2019-01-13 09:16:42 +00:00
tvNoOperation.setVisibility(View.GONE);
2018-08-13 06:59:05 +00:00
grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
2018-08-13 06:59:05 +00:00
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Observe folders
DB.getInstance(getContext()).operation().liveOperations().observe(getViewLifecycleOwner(), new Observer<List<TupleOperationEx>>() {
2018-08-13 06:59:05 +00:00
@Override
public void onChanged(@Nullable List<TupleOperationEx> operations) {
2018-08-13 18:31:42 +00:00
if (operations == null)
operations = new ArrayList<>();
2019-01-13 09:16:42 +00:00
tvNoOperation.setVisibility(operations.size() == 0 ? View.VISIBLE : View.GONE);
2018-08-13 18:31:42 +00:00
adapter.set(operations);
2018-08-13 06:59:05 +00:00
pbWait.setVisibility(View.GONE);
2018-08-13 06:59:05 +00:00
grpReady.setVisibility(View.VISIBLE);
}
});
}
@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:
new DialogDelete().show(getFragmentManager(), "operations:delete");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void onMenuHelp() {
startActivity(getFAQIntent());
}
private Intent getFAQIntent() {
Intent intent = new Intent(Intent.ACTION_VIEW);
2019-02-07 19:51:50 +00:00
intent.setData(Uri.parse(Helper.FAQ_URI + "#user-content-faq3"));
return intent;
}
public static class DialogDelete extends DialogFragmentEx {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
return new AlertDialog.Builder(getContext())
.setMessage(R.string.title_delete_operation)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
DB db = DB.getInstance(context);
List<EntityOperation> ops = db.operation().getOperationsError();
Log.i("Operations with error count=" + ops.size());
for (EntityOperation op : ops) {
Log.w("Deleting operation=" + op.id + " error=" + op.error);
if (op.message != null)
db.message().setMessageUiHide(op.message, 0L);
db.operation().deleteOperation(op.id);
}
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getFragmentManager(), ex);
}
}.execute(getContext(), getActivity(), new Bundle(), "operations:delete");
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
}
}
2018-08-13 06:59:05 +00:00
}