Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
6.0 KiB

  1. package eu.faircode.email;
  2. /*
  3. This file is part of FairEmail.
  4. FairEmail 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. FairEmail 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 FairEmail. 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.Html;
  19. import android.view.LayoutInflater;
  20. import android.view.MenuItem;
  21. import android.view.View;
  22. import android.view.ViewGroup;
  23. import android.widget.EditText;
  24. import android.widget.ProgressBar;
  25. import com.google.android.material.bottomnavigation.BottomNavigationView;
  26. import androidx.annotation.NonNull;
  27. import androidx.annotation.Nullable;
  28. import androidx.constraintlayout.widget.Group;
  29. import androidx.lifecycle.Observer;
  30. public class FragmentAnswer extends FragmentEx {
  31. private ViewGroup view;
  32. private EditText etName;
  33. private EditText etText;
  34. private BottomNavigationView bottom_navigation;
  35. private ProgressBar pbWait;
  36. private Group grpReady;
  37. private long id = -1;
  38. @Override
  39. public void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. // Get arguments
  42. Bundle args = getArguments();
  43. id = (args == null ? -1 : args.getLong("id", -1));
  44. }
  45. @Override
  46. @Nullable
  47. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  48. view = (ViewGroup) inflater.inflate(R.layout.fragment_answer, container, false);
  49. // Get controls
  50. etName = view.findViewById(R.id.etName);
  51. etText = view.findViewById(R.id.etText);
  52. etText = view.findViewById(R.id.etText);
  53. bottom_navigation = view.findViewById(R.id.bottom_navigation);
  54. pbWait = view.findViewById(R.id.pbWait);
  55. grpReady = view.findViewById(R.id.grpReady);
  56. bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
  57. @Override
  58. public boolean onNavigationItemSelected(MenuItem menuItem) {
  59. switch (menuItem.getItemId()) {
  60. case R.id.action_delete:
  61. onActionTrash();
  62. return true;
  63. case R.id.action_save:
  64. onActionSave();
  65. return true;
  66. }
  67. return false;
  68. }
  69. });
  70. // Initialize
  71. grpReady.setVisibility(View.GONE);
  72. pbWait.setVisibility(View.VISIBLE);
  73. return view;
  74. }
  75. @Override
  76. public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
  77. super.onActivityCreated(savedInstanceState);
  78. DB.getInstance(getContext()).answer().liveAnswer(id).observe(getViewLifecycleOwner(), new Observer<EntityAnswer>() {
  79. @Override
  80. public void onChanged(EntityAnswer answer) {
  81. etName.setText(answer == null ? null : answer.name);
  82. etText.setText(answer == null ? null : Html.fromHtml(answer.text));
  83. bottom_navigation.findViewById(R.id.action_delete).setVisibility(answer == null ? View.GONE : View.VISIBLE);
  84. pbWait.setVisibility(View.GONE);
  85. grpReady.setVisibility(View.VISIBLE);
  86. }
  87. });
  88. }
  89. private void onActionTrash() {
  90. Helper.setViewsEnabled(view, false);
  91. Bundle args = new Bundle();
  92. args.putLong("id", id);
  93. new SimpleTask<Void>() {
  94. @Override
  95. protected Void onLoad(Context context, Bundle args) {
  96. long id = args.getLong("id");
  97. DB.getInstance(context).answer().deleteAnswer(id);
  98. return null;
  99. }
  100. @Override
  101. protected void onLoaded(Bundle args, Void data) {
  102. finish();
  103. }
  104. @Override
  105. protected void onException(Bundle args, Throwable ex) {
  106. Helper.setViewsEnabled(view, true);
  107. Helper.unexpectedError(getContext(), ex);
  108. }
  109. }.load(this, args);
  110. }
  111. private void onActionSave() {
  112. Helper.setViewsEnabled(view, false);
  113. Bundle args = new Bundle();
  114. args.putLong("id", id);
  115. args.putString("name", etName.getText().toString());
  116. args.putString("text", Html.toHtml(etText.getText()));
  117. new SimpleTask<Void>() {
  118. @Override
  119. protected Void onLoad(Context context, Bundle args) {
  120. long id = args.getLong("id");
  121. String name = args.getString("name");
  122. String text = args.getString("text");
  123. DB db = DB.getInstance(context);
  124. if (id < 0) {
  125. EntityAnswer answer = new EntityAnswer();
  126. answer.name = name;
  127. answer.text = text;
  128. answer.id = db.answer().insertAnswer(answer);
  129. } else {
  130. EntityAnswer answer = db.answer().getAnswer(id);
  131. answer.name = name;
  132. answer.text = text;
  133. db.answer().updateAnswer(answer);
  134. }
  135. return null;
  136. }
  137. @Override
  138. protected void onLoaded(Bundle args, Void data) {
  139. finish();
  140. }
  141. @Override
  142. protected void onException(Bundle args, Throwable ex) {
  143. Helper.setViewsEnabled(view, true);
  144. Helper.unexpectedError(getContext(), ex);
  145. }
  146. }.load(this, args);
  147. }
  148. }