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.

152 lines
5.2 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.arch.lifecycle.LiveData;
  17. import android.arch.lifecycle.Observer;
  18. import android.content.Intent;
  19. import android.os.Bundle;
  20. import android.support.annotation.NonNull;
  21. import android.support.annotation.Nullable;
  22. import android.support.constraint.Group;
  23. import android.support.design.widget.FloatingActionButton;
  24. import android.support.v4.app.Fragment;
  25. import android.support.v7.app.AppCompatActivity;
  26. import android.support.v7.widget.LinearLayoutManager;
  27. import android.support.v7.widget.RecyclerView;
  28. import android.view.LayoutInflater;
  29. import android.view.View;
  30. import android.view.ViewGroup;
  31. import android.widget.ProgressBar;
  32. import android.widget.TextView;
  33. import java.util.List;
  34. public class FragmentMessages extends Fragment {
  35. private RecyclerView rvMessage;
  36. private TextView tvNoEmail;
  37. private ProgressBar pbWait;
  38. private Group grpReady;
  39. private FloatingActionButton fab;
  40. private AdapterMessage adapter;
  41. private LiveData<TupleFolderEx> liveFolder;
  42. @Override
  43. @Nullable
  44. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  45. View view = inflater.inflate(R.layout.fragment_messages, container, false);
  46. // Get arguments
  47. long folder = getArguments().getLong("folder", -1);
  48. long thread = getArguments().getLong("thread", -1); // message ID
  49. // Get controls
  50. rvMessage = view.findViewById(R.id.rvFolder);
  51. tvNoEmail = view.findViewById(R.id.tvNoEmail);
  52. pbWait = view.findViewById(R.id.pbWait);
  53. grpReady = view.findViewById(R.id.grpReady);
  54. fab = view.findViewById(R.id.fab);
  55. // Wire controls
  56. rvMessage.setHasFixedSize(false);
  57. LinearLayoutManager llm = new LinearLayoutManager(getContext());
  58. rvMessage.setLayoutManager(llm);
  59. adapter = new AdapterMessage(getContext(),
  60. thread < 0
  61. ? AdapterMessage.ViewType.FOLDER
  62. : AdapterMessage.ViewType.THREAD);
  63. rvMessage.setAdapter(adapter);
  64. fab.setOnClickListener(new View.OnClickListener() {
  65. @Override
  66. public void onClick(View view) {
  67. startActivity(new Intent(getContext(), ActivityCompose.class));
  68. }
  69. });
  70. // Initialize
  71. tvNoEmail.setVisibility(View.GONE);
  72. grpReady.setVisibility(View.GONE);
  73. pbWait.setVisibility(View.VISIBLE);
  74. DB db = DB.getInstance(getContext());
  75. // Observe folder
  76. liveFolder = (thread < 0 ? DB.getInstance(getContext()).folder().liveFolderEx(folder) : null);
  77. // Observe messages
  78. if (thread < 0)
  79. if (folder < 0)
  80. db.message().liveUnifiedInbox().observe(this, messagesObserver);
  81. else
  82. db.message().liveMessages(folder).observe(this, messagesObserver);
  83. else {
  84. db.message().liveThread(thread).observe(this, messagesObserver);
  85. }
  86. return view;
  87. }
  88. @Override
  89. public void onResume() {
  90. super.onResume();
  91. if (liveFolder == null)
  92. ((AppCompatActivity) getActivity()).getSupportActionBar().setSubtitle(R.string.title_folder_thread);
  93. else
  94. liveFolder.observe(this, folderObserver);
  95. }
  96. @Override
  97. public void onPause() {
  98. super.onPause();
  99. if (liveFolder != null)
  100. liveFolder.removeObservers(this);
  101. }
  102. Observer<TupleFolderEx> folderObserver = new Observer<TupleFolderEx>() {
  103. @Override
  104. public void onChanged(@Nullable TupleFolderEx folder) {
  105. ((AppCompatActivity) getActivity()).getSupportActionBar().setSubtitle(folder.name == null
  106. ? getString(R.string.title_folder_unified)
  107. : Helper.localizeFolderName(getContext(), folder));
  108. }
  109. };
  110. Observer<List<TupleMessageEx>> messagesObserver = new Observer<List<TupleMessageEx>>() {
  111. @Override
  112. public void onChanged(@Nullable List<TupleMessageEx> messages) {
  113. adapter.set(messages);
  114. pbWait.setVisibility(View.GONE);
  115. grpReady.setVisibility(View.VISIBLE);
  116. if (messages.size() == 0) {
  117. tvNoEmail.setVisibility(View.VISIBLE);
  118. rvMessage.setVisibility(View.GONE);
  119. } else {
  120. tvNoEmail.setVisibility(View.GONE);
  121. rvMessage.setVisibility(View.VISIBLE);
  122. }
  123. }
  124. };
  125. }