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

104 lines
3.5 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.os.Bundle;
  17. import android.view.LayoutInflater;
  18. import android.view.View;
  19. import android.view.ViewGroup;
  20. import android.widget.ProgressBar;
  21. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  22. import java.util.List;
  23. import androidx.annotation.NonNull;
  24. import androidx.annotation.Nullable;
  25. import androidx.constraintlayout.widget.Group;
  26. import androidx.fragment.app.FragmentTransaction;
  27. import androidx.lifecycle.Observer;
  28. import androidx.recyclerview.widget.LinearLayoutManager;
  29. import androidx.recyclerview.widget.RecyclerView;
  30. public class FragmentAccounts extends FragmentEx {
  31. private RecyclerView rvAccount;
  32. private ProgressBar pbWait;
  33. private Group grpReady;
  34. private FloatingActionButton fab;
  35. private AdapterAccount adapter;
  36. @Override
  37. @Nullable
  38. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  39. setSubtitle(R.string.title_list_accounts);
  40. View view = inflater.inflate(R.layout.fragment_accounts, container, false);
  41. // Get controls
  42. rvAccount = view.findViewById(R.id.rvAccount);
  43. pbWait = view.findViewById(R.id.pbWait);
  44. grpReady = view.findViewById(R.id.grpReady);
  45. fab = view.findViewById(R.id.fab);
  46. // Wire controls
  47. rvAccount.setHasFixedSize(false);
  48. LinearLayoutManager llm = new LinearLayoutManager(getContext());
  49. rvAccount.setLayoutManager(llm);
  50. adapter = new AdapterAccount(getContext());
  51. rvAccount.setAdapter(adapter);
  52. fab.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View view) {
  55. FragmentAccount fragment = new FragmentAccount();
  56. fragment.setArguments(new Bundle());
  57. FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
  58. fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("account");
  59. fragmentTransaction.commit();
  60. }
  61. });
  62. // Initialize
  63. grpReady.setVisibility(View.GONE);
  64. pbWait.setVisibility(View.VISIBLE);
  65. return view;
  66. }
  67. @Override
  68. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  69. super.onActivityCreated(savedInstanceState);
  70. // Observe accounts
  71. DB.getInstance(getContext()).account().liveAccounts().observe(getViewLifecycleOwner(), new Observer<List<EntityAccount>>() {
  72. @Override
  73. public void onChanged(@Nullable List<EntityAccount> accounts) {
  74. if (accounts != null)
  75. adapter.set(accounts);
  76. pbWait.setVisibility(View.GONE);
  77. grpReady.setVisibility(View.VISIBLE);
  78. }
  79. });
  80. }
  81. }