Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email

159 lines
5.8 KiB

  1. package eu.faircode.email;
  2. /*
  3. This file is part of Safe email.
  4. Safe email is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. NetGuard is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
  14. Copyright 2018 by Marcel Bokhorst (M66B)
  15. */
  16. import android.content.Context;
  17. import android.os.Bundle;
  18. import android.text.TextUtils;
  19. import android.view.LayoutInflater;
  20. import android.view.View;
  21. import android.view.ViewGroup;
  22. import android.widget.Button;
  23. import android.widget.CheckBox;
  24. import android.widget.EditText;
  25. import android.widget.ProgressBar;
  26. import android.widget.Toast;
  27. import androidx.annotation.NonNull;
  28. import androidx.annotation.Nullable;
  29. import androidx.lifecycle.Observer;
  30. public class FragmentFolder extends FragmentEx {
  31. private ViewGroup view;
  32. private CheckBox cbSynchronize;
  33. private EditText etAfter;
  34. private Button btnSave;
  35. private ProgressBar pbSave;
  36. private ProgressBar pbWait;
  37. @Override
  38. @Nullable
  39. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  40. setSubtitle(R.string.title_edit_folder);
  41. view = (ViewGroup) inflater.inflate(R.layout.fragment_folder, container, false);
  42. // Get arguments
  43. Bundle args = getArguments();
  44. final long id = (args == null ? -1 : args.getLong("id"));
  45. // Get controls
  46. cbSynchronize = view.findViewById(R.id.cbSynchronize);
  47. etAfter = view.findViewById(R.id.etAfter);
  48. pbSave = view.findViewById(R.id.pbSave);
  49. btnSave = view.findViewById(R.id.btnSave);
  50. pbWait = view.findViewById(R.id.pbWait);
  51. btnSave.setOnClickListener(new View.OnClickListener() {
  52. @Override
  53. public void onClick(View v) {
  54. Helper.setViewsEnabled(view, false);
  55. btnSave.setEnabled(false);
  56. pbSave.setVisibility(View.VISIBLE);
  57. Bundle args = new Bundle();
  58. args.putLong("id", id);
  59. args.putBoolean("synchronize", cbSynchronize.isChecked());
  60. args.putString("after", etAfter.getText().toString());
  61. new SimpleTask<Void>() {
  62. @Override
  63. protected Void onLoad(Context context, Bundle args) throws Throwable {
  64. try {
  65. ServiceSynchronize.stop(getContext(), "folder");
  66. long id = args.getLong("id");
  67. boolean synchronize = args.getBoolean("synchronize");
  68. String after = args.getString("after");
  69. int days = (TextUtils.isEmpty(after) ? 7 : Integer.parseInt(after));
  70. DB db = DB.getInstance(getContext());
  71. try {
  72. db.beginTransaction();
  73. EntityFolder folder = db.folder().getFolder(id);
  74. folder.synchronize = synchronize;
  75. folder.after = days;
  76. db.folder().updateFolder(folder);
  77. if (!folder.synchronize)
  78. db.message().deleteMessages(folder.id);
  79. db.setTransactionSuccessful();
  80. } finally {
  81. db.endTransaction();
  82. }
  83. return null;
  84. } finally {
  85. ServiceSynchronize.restart(getContext(), "folder");
  86. }
  87. }
  88. @Override
  89. protected void onLoaded(Bundle args, Void data) {
  90. getFragmentManager().popBackStack();
  91. }
  92. @Override
  93. protected void onException(Bundle args, Throwable ex) {
  94. Helper.setViewsEnabled(view, true);
  95. btnSave.setEnabled(true);
  96. pbSave.setVisibility(View.GONE);
  97. Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
  98. }
  99. }.load(FragmentFolder.this, args);
  100. }
  101. });
  102. // Initialize
  103. Helper.setViewsEnabled(view, false);
  104. btnSave.setEnabled(false);
  105. pbSave.setVisibility(View.GONE);
  106. pbWait.setVisibility(View.VISIBLE);
  107. return view;
  108. }
  109. @Override
  110. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  111. super.onActivityCreated(savedInstanceState);
  112. // Get arguments
  113. Bundle args = getArguments();
  114. long id = (args == null ? -1 : args.getLong("id"));
  115. // Observe
  116. DB.getInstance(getContext()).folder().liveFolder(id).observe(getViewLifecycleOwner(), new Observer<EntityFolder>() {
  117. @Override
  118. public void onChanged(@Nullable EntityFolder folder) {
  119. if (folder != null) {
  120. cbSynchronize.setChecked(folder.synchronize);
  121. etAfter.setText(Integer.toString(folder.after));
  122. }
  123. pbWait.setVisibility(View.GONE);
  124. Helper.setViewsEnabled(view, true);
  125. btnSave.setEnabled(true);
  126. }
  127. });
  128. }
  129. }