GalleryFragment.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2019 Tobias Kaminsky
  6. * Copyright (C) 2019 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.fragment;
  22. import android.os.AsyncTask;
  23. import android.os.Bundle;
  24. import android.view.LayoutInflater;
  25. import android.view.View;
  26. import android.view.ViewGroup;
  27. import com.owncloud.android.datamodel.VirtualFolderType;
  28. import com.owncloud.android.lib.common.utils.Log_OC;
  29. import com.owncloud.android.lib.resources.files.SearchRemoteOperation;
  30. import com.owncloud.android.ui.asynctasks.GallerySearchTask;
  31. import com.owncloud.android.ui.events.ChangeMenuEvent;
  32. import com.owncloud.android.ui.events.SearchEvent;
  33. import java.util.ArrayList;
  34. import androidx.annotation.NonNull;
  35. import androidx.recyclerview.widget.GridLayoutManager;
  36. import androidx.recyclerview.widget.RecyclerView;
  37. /**
  38. * A Fragment that lists all files and folders in a given path. TODO refactor to get rid of direct dependency on
  39. * FileDisplayActivity
  40. */
  41. public class GalleryFragment extends OCFileListFragment {
  42. private static final int MAX_ITEMS_PER_ROW = 10;
  43. private boolean photoSearchQueryRunning = false;
  44. private boolean photoSearchNoNew = false;
  45. private SearchRemoteOperation searchRemoteOperation;
  46. private AsyncTask photoSearchTask;
  47. private SearchEvent searchEvent;
  48. private boolean refresh;
  49. public GalleryFragment() {
  50. this.refresh = false;
  51. }
  52. public GalleryFragment(boolean refresh) {
  53. this.refresh = refresh;
  54. }
  55. @Override
  56. public void onCreate(Bundle savedInstanceState) {
  57. super.onCreate(savedInstanceState);
  58. searchEvent = new SearchEvent("", SearchRemoteOperation.SearchType.GALLERY_SEARCH);
  59. searchRemoteOperation = new SearchRemoteOperation(searchEvent.getSearchQuery(),
  60. searchEvent.getSearchType(),
  61. false);
  62. }
  63. @Override
  64. public void onPause() {
  65. super.onPause();
  66. if (photoSearchTask != null) {
  67. photoSearchTask.cancel(true);
  68. }
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. @Override
  74. public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  75. View v = super.onCreateView(inflater, container, savedInstanceState);
  76. getRecyclerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
  77. @Override
  78. public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
  79. loadMoreWhenEndReached(recyclerView, dy);
  80. }
  81. });
  82. Log_OC.i(this, "onCreateView() in GalleryFragment end");
  83. return v;
  84. }
  85. @Override
  86. public void onActivityCreated(Bundle savedInstanceState) {
  87. super.onActivityCreated(savedInstanceState);
  88. currentSearchType = SearchType.GALLERY_SEARCH;
  89. switchToGridView();
  90. menuItemAddRemoveValue = MenuItemAddRemove.REMOVE_GRID_AND_SORT;
  91. requireActivity().invalidateOptionsMenu();
  92. handleSearchEvent();
  93. }
  94. @Override
  95. public void onRefresh() {
  96. super.onRefresh();
  97. refresh = true;
  98. handleSearchEvent();
  99. }
  100. @Override
  101. public void onMessageEvent(ChangeMenuEvent changeMenuEvent) {
  102. super.onMessageEvent(changeMenuEvent);
  103. }
  104. private void handleSearchEvent() {
  105. prepareCurrentSearch(searchEvent);
  106. searchFragment = true;
  107. setEmptyListLoadingMessage(false);
  108. if (refresh || preferences.getPhotoSearchTimestamp() == 0 ||
  109. System.currentTimeMillis() - preferences.getPhotoSearchTimestamp() >= 30 * 1000) {
  110. mAdapter.setData(
  111. new ArrayList<>(),
  112. SearchType.GALLERY_SEARCH,
  113. mContainerActivity.getStorageManager(),
  114. mFile,
  115. true);
  116. refresh = false;
  117. } else {
  118. mAdapter.showVirtuals(VirtualFolderType.GALLERY, true, mContainerActivity.getStorageManager());
  119. preferences.setPhotoSearchTimestamp(System.currentTimeMillis());
  120. return;
  121. }
  122. setFabVisible(false);
  123. searchAndDisplay();
  124. }
  125. private void searchAndDisplay() {
  126. if (!photoSearchQueryRunning && !photoSearchNoNew) {
  127. photoSearchTask = new GallerySearchTask(getColumnsCount(),
  128. this,
  129. accountManager.getUser(),
  130. searchRemoteOperation,
  131. mContainerActivity.getStorageManager())
  132. .execute();
  133. }
  134. }
  135. public void setPhotoSearchQueryRunning(boolean bool) {
  136. photoSearchQueryRunning = bool;
  137. }
  138. public void setSearchDidNotFindNewPhotos(boolean noNewPhotos) {
  139. photoSearchNoNew = noNewPhotos;
  140. }
  141. @Override
  142. public boolean isLoading() {
  143. return !photoSearchNoNew;
  144. }
  145. private void loadMoreWhenEndReached(@NonNull RecyclerView recyclerView, int dy) {
  146. if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
  147. GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
  148. // scroll down
  149. if (dy > 0 && !photoSearchQueryRunning) {
  150. int visibleItemCount = gridLayoutManager.getChildCount();
  151. int totalItemCount = gridLayoutManager.getItemCount();
  152. int firstVisibleItem = gridLayoutManager.findFirstCompletelyVisibleItemPosition();
  153. if ((totalItemCount - visibleItemCount) <= (firstVisibleItem + MAX_ITEMS_PER_ROW)
  154. && (totalItemCount - visibleItemCount) > 0) {
  155. // Almost reached the end, continue to load new photos
  156. searchAndDisplay();
  157. }
  158. }
  159. }
  160. }
  161. }