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

153 lines
5.5 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
This file is part of Safe email.
Safe email 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.
NetGuard 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 NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
2018-08-04 17:41:48 +00:00
import android.widget.ProgressBar;
2018-08-02 13:33:06 +00:00
import android.widget.Toast;
2018-08-08 06:55:47 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
public class FragmentFolder extends FragmentEx {
private ViewGroup view;
2018-08-02 13:33:06 +00:00
private CheckBox cbSynchronize;
private EditText etAfter;
2018-08-06 10:27:21 +00:00
private Button btnSave;
private ProgressBar pbSave;
2018-08-04 17:41:48 +00:00
private ProgressBar pbWait;
2018-08-02 13:33:06 +00:00
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_edit_folder);
view = (ViewGroup) inflater.inflate(R.layout.fragment_folder, container, false);
2018-08-02 13:33:06 +00:00
// Get arguments
Bundle args = getArguments();
2018-08-05 16:14:43 +00:00
final long id = (args == null ? -1 : args.getLong("id"));
2018-08-02 13:33:06 +00:00
// Get controls
cbSynchronize = view.findViewById(R.id.cbSynchronize);
etAfter = view.findViewById(R.id.etAfter);
pbSave = view.findViewById(R.id.pbSave);
2018-08-06 10:27:21 +00:00
btnSave = view.findViewById(R.id.btnSave);
2018-08-04 17:41:48 +00:00
pbWait = view.findViewById(R.id.pbWait);
2018-08-02 13:33:06 +00:00
2018-08-06 10:27:21 +00:00
btnSave.setOnClickListener(new View.OnClickListener() {
2018-08-02 13:33:06 +00:00
@Override
public void onClick(View v) {
Helper.setViewsEnabled(view, false);
2018-08-06 10:27:21 +00:00
btnSave.setEnabled(false);
pbSave.setVisibility(View.VISIBLE);
2018-08-02 13:33:06 +00:00
Bundle args = new Bundle();
args.putLong("id", id);
args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putString("after", etAfter.getText().toString());
2018-08-02 13:33:06 +00:00
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) throws Throwable {
try {
ServiceSynchronize.stop(getContext(), "folder");
long id = args.getLong("id");
boolean synchronize = args.getBoolean("synchronize");
String after = args.getString("after");
int days = (TextUtils.isEmpty(after) ? 7 : Integer.parseInt(after));
DB db = DB.getInstance(getContext());
DaoFolder dao = db.folder();
EntityFolder folder = dao.getFolder(id);
folder.synchronize = synchronize;
folder.after = days;
dao.updateFolder(folder);
if (!folder.synchronize)
db.message().deleteMessages(folder.id);
return null;
} finally {
ServiceSynchronize.restart(getContext(), "folder");
}
}
@Override
protected void onLoaded(Bundle args, Void data) {
getFragmentManager().popBackStack();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.setViewsEnabled(view, true);
btnSave.setEnabled(true);
pbSave.setVisibility(View.GONE);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(FragmentFolder.this, args);
2018-08-02 13:33:06 +00:00
}
});
2018-08-04 17:41:48 +00:00
// Initialize
Helper.setViewsEnabled(view, false);
btnSave.setEnabled(false);
pbSave.setVisibility(View.GONE);
2018-08-04 17:41:48 +00:00
pbWait.setVisibility(View.VISIBLE);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get arguments
Bundle args = getArguments();
long id = (args == null ? -1 : args.getLong("id"));
2018-08-04 17:41:48 +00:00
// Observe
DB.getInstance(getContext()).folder().liveFolder(id).observe(getViewLifecycleOwner(), new Observer<EntityFolder>() {
2018-08-02 13:33:06 +00:00
@Override
public void onChanged(@Nullable EntityFolder folder) {
if (folder != null) {
cbSynchronize.setChecked(folder.synchronize);
etAfter.setText(Integer.toString(folder.after));
}
2018-08-04 17:41:48 +00:00
pbWait.setVisibility(View.GONE);
Helper.setViewsEnabled(view, true);
btnSave.setEnabled(true);
2018-08-02 13:33:06 +00:00
}
});
}
}