TrashbinActivity.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2018 Tobias Kaminsky
  6. * Copyright (C) 2018 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui.trashbin;
  22. import android.content.Intent;
  23. import android.os.Bundle;
  24. import android.view.Menu;
  25. import android.view.MenuItem;
  26. import android.view.View;
  27. import android.widget.ImageView;
  28. import android.widget.PopupMenu;
  29. import android.widget.TextView;
  30. import com.google.android.material.snackbar.Snackbar;
  31. import com.owncloud.android.R;
  32. import com.owncloud.android.db.PreferenceManager;
  33. import com.owncloud.android.lib.resources.trashbin.model.TrashbinFile;
  34. import com.owncloud.android.ui.EmptyRecyclerView;
  35. import com.owncloud.android.ui.activity.FileActivity;
  36. import com.owncloud.android.ui.activity.FileDisplayActivity;
  37. import com.owncloud.android.ui.adapter.TrashbinListAdapter;
  38. import com.owncloud.android.ui.dialog.SortingOrderDialogFragment;
  39. import com.owncloud.android.ui.interfaces.TrashbinActivityInterface;
  40. import com.owncloud.android.utils.FileSortOrder;
  41. import com.owncloud.android.utils.ThemeUtils;
  42. import java.util.List;
  43. import androidx.appcompat.widget.Toolbar;
  44. import androidx.fragment.app.FragmentManager;
  45. import androidx.fragment.app.FragmentTransaction;
  46. import androidx.recyclerview.widget.LinearLayoutManager;
  47. import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
  48. import butterknife.BindString;
  49. import butterknife.BindView;
  50. import butterknife.ButterKnife;
  51. import butterknife.Unbinder;
  52. /**
  53. * Presenting trashbin data, received from presenter
  54. */
  55. public class TrashbinActivity extends FileActivity implements TrashbinActivityInterface,
  56. SortingOrderDialogFragment.OnSortingOrderListener, TrashbinContract.View {
  57. @BindView(R.id.empty_list_view_text)
  58. public TextView emptyContentMessage;
  59. @BindView(R.id.empty_list_view_headline)
  60. public TextView emptyContentHeadline;
  61. @BindView(R.id.empty_list_icon)
  62. public ImageView emptyContentIcon;
  63. @BindView(android.R.id.list)
  64. public EmptyRecyclerView recyclerView;
  65. @BindView(R.id.swipe_containing_list)
  66. public SwipeRefreshLayout swipeListRefreshLayout;
  67. @BindString(R.string.trashbin_empty_headline)
  68. public String noResultsHeadline;
  69. @BindString(R.string.trashbin_empty_message)
  70. public String noResultsMessage;
  71. private Unbinder unbinder;
  72. private TrashbinListAdapter trashbinListAdapter;
  73. private TrashbinPresenter trashbinPresenter;
  74. private boolean active;
  75. @Override
  76. protected void onCreate(Bundle savedInstanceState) {
  77. super.onCreate(savedInstanceState);
  78. trashbinPresenter = new TrashbinPresenter(new RemoteTrashbinRepository(this), this);
  79. setContentView(R.layout.trashbin_activity);
  80. unbinder = ButterKnife.bind(this);
  81. // setup toolbar
  82. setupToolbar();
  83. // setup drawer
  84. setupDrawer(R.id.nav_trashbin);
  85. ThemeUtils.setColoredTitle(getSupportActionBar(), R.string.trashbin_activity_title, this);
  86. }
  87. @Override
  88. protected void onStart() {
  89. super.onStart();
  90. active = true;
  91. setupContent();
  92. }
  93. private void setupContent() {
  94. recyclerView = findViewById(android.R.id.list);
  95. recyclerView.setEmptyView(findViewById(R.id.empty_list_view));
  96. findViewById(R.id.empty_list_progress).setVisibility(View.GONE);
  97. emptyContentIcon.setImageResource(R.drawable.ic_delete);
  98. emptyContentIcon.setVisibility(View.VISIBLE);
  99. emptyContentHeadline.setText(noResultsHeadline);
  100. emptyContentMessage.setText(noResultsMessage);
  101. emptyContentMessage.setVisibility(View.VISIBLE);
  102. trashbinListAdapter = new TrashbinListAdapter(this, getStorageManager(), this);
  103. recyclerView.setAdapter(trashbinListAdapter);
  104. recyclerView.setHasFixedSize(true);
  105. recyclerView.setHasFooter(true);
  106. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  107. swipeListRefreshLayout.setOnRefreshListener(this::loadFolder);
  108. loadFolder();
  109. }
  110. private void loadFolder() {
  111. swipeListRefreshLayout.setRefreshing(true);
  112. trashbinPresenter.loadFolder();
  113. }
  114. @Override
  115. public void showFiles(boolean onDeviceOnly) {
  116. super.showFiles(onDeviceOnly);
  117. Intent i = new Intent(getApplicationContext(), FileDisplayActivity.class);
  118. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  119. startActivity(i);
  120. }
  121. @Override
  122. public boolean onOptionsItemSelected(MenuItem item) {
  123. boolean retval = true;
  124. switch (item.getItemId()) {
  125. case android.R.id.home:
  126. if (isDrawerOpen()) {
  127. closeDrawer();
  128. } else if (trashbinPresenter.isRoot()) {
  129. onBackPressed();
  130. } else {
  131. openDrawer();
  132. }
  133. break;
  134. case R.id.action_sort: {
  135. FragmentManager fm = getSupportFragmentManager();
  136. FragmentTransaction ft = fm.beginTransaction();
  137. ft.addToBackStack(null);
  138. SortingOrderDialogFragment mSortingOrderDialogFragment = SortingOrderDialogFragment.newInstance(
  139. PreferenceManager.getSortOrderByType(this, FileSortOrder.Type.trashBinView,
  140. FileSortOrder.sort_new_to_old));
  141. mSortingOrderDialogFragment.show(ft, SortingOrderDialogFragment.SORTING_ORDER_FRAGMENT);
  142. break;
  143. }
  144. case R.id.action_empty_trashbin:
  145. trashbinPresenter.emptyTrashbin();
  146. break;
  147. default:
  148. retval = super.onOptionsItemSelected(item);
  149. break;
  150. }
  151. return retval;
  152. }
  153. @Override
  154. public void onDestroy() {
  155. super.onDestroy();
  156. unbinder.unbind();
  157. }
  158. @Override
  159. public void onOverflowIconClicked(TrashbinFile file, View view) {
  160. PopupMenu popup = new PopupMenu(this, view);
  161. popup.inflate(R.menu.trashbin_actions_menu);
  162. popup.setOnMenuItemClickListener(item -> {
  163. trashbinPresenter.removeTrashbinFile(file);
  164. return true;
  165. });
  166. popup.show();
  167. }
  168. @Override
  169. public void onItemClicked(TrashbinFile file) {
  170. if (file.isFolder()) {
  171. trashbinPresenter.enterFolder(file.getRemotePath());
  172. mDrawerToggle.setDrawerIndicatorEnabled(false);
  173. Toolbar toolbar = findViewById(R.id.toolbar);
  174. if (toolbar != null && toolbar.getNavigationIcon() != null) {
  175. ThemeUtils.tintDrawable(toolbar.getNavigationIcon(), ThemeUtils.fontColor(this));
  176. }
  177. }
  178. }
  179. @Override
  180. public void onRestoreIconClicked(TrashbinFile file, View view) {
  181. trashbinPresenter.restoreTrashbinFile(file);
  182. }
  183. @Override
  184. public boolean onCreateOptionsMenu(Menu menu) {
  185. getMenuInflater().inflate(R.menu.trashbin_options_menu, menu);
  186. return true;
  187. }
  188. @Override
  189. protected void onPause() {
  190. super.onPause();
  191. active = false;
  192. trashbinListAdapter.cancelAllPendingTasks();
  193. }
  194. @Override
  195. public void onBackPressed() {
  196. trashbinPresenter.navigateUp();
  197. }
  198. public void close() {
  199. super.onBackPressed();
  200. }
  201. public void setDrawerIndicatorEnabled(boolean bool) {
  202. mDrawerToggle.setDrawerIndicatorEnabled(bool);
  203. }
  204. @Override
  205. public void onSortingOrderChosen(FileSortOrder sortOrder) {
  206. trashbinListAdapter.setSortOrder(sortOrder);
  207. }
  208. @Override
  209. public void showTrashbinFolder(List<Object> trashbinFiles) {
  210. if (active) {
  211. trashbinListAdapter.setTrashbinFiles(trashbinFiles, true);
  212. swipeListRefreshLayout.setRefreshing(false);
  213. }
  214. }
  215. @Override
  216. public void removeFile(TrashbinFile file) {
  217. if (active) {
  218. trashbinListAdapter.removeFile(file);
  219. }
  220. }
  221. @Override
  222. public void removeAllFiles() {
  223. trashbinListAdapter.removeAllFiles();
  224. }
  225. @Override
  226. public void showSnackbarError(int message, TrashbinFile file) {
  227. if (active) {
  228. swipeListRefreshLayout.setRefreshing(false);
  229. Snackbar.make(recyclerView, String.format(getString(message), file.getFileName()), Snackbar.LENGTH_LONG)
  230. .show();
  231. }
  232. }
  233. @Override
  234. public void showError(int message) {
  235. if (active) {
  236. swipeListRefreshLayout.setRefreshing(false);
  237. if (emptyContentMessage != null) {
  238. emptyContentHeadline.setText(R.string.common_error);
  239. emptyContentIcon.setImageDrawable(getResources().getDrawable(R.drawable.ic_list_empty_error));
  240. emptyContentMessage.setText(message);
  241. emptyContentMessage.setVisibility(View.VISIBLE);
  242. emptyContentIcon.setVisibility(View.VISIBLE);
  243. }
  244. }
  245. }
  246. }