PhotoFragment.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.PhotoSearchTask;
  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 PhotoFragment 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 PhotoFragment() {
  50. this.refresh = false;
  51. }
  52. public PhotoFragment(boolean refresh) {
  53. this.refresh = refresh;
  54. }
  55. @Override
  56. public void onCreate(Bundle savedInstanceState) {
  57. super.onCreate(savedInstanceState);
  58. searchEvent = new SearchEvent("image/%",
  59. SearchRemoteOperation.SearchType.PHOTO_SEARCH,
  60. SearchEvent.UnsetType.NO_UNSET);
  61. searchRemoteOperation = new SearchRemoteOperation(searchEvent.getSearchQuery(),
  62. searchEvent.getSearchType(),
  63. false);
  64. }
  65. @Override
  66. public void onPause() {
  67. super.onPause();
  68. if (photoSearchTask != null) {
  69. photoSearchTask.cancel(true);
  70. }
  71. }
  72. /**
  73. * {@inheritDoc}
  74. */
  75. @Override
  76. public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  77. View v = super.onCreateView(inflater, container, savedInstanceState);
  78. getRecyclerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
  79. @Override
  80. public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
  81. loadMoreWhenEndReached(recyclerView, dy);
  82. }
  83. });
  84. Log_OC.i(this, "onCreateView() in PhotoFragment end");
  85. return v;
  86. }
  87. @Override
  88. public void onActivityCreated(Bundle savedInstanceState) {
  89. super.onActivityCreated(savedInstanceState);
  90. currentSearchType = SearchType.PHOTO_SEARCH;
  91. switchToGridView();
  92. menuItemAddRemoveValue = MenuItemAddRemove.REMOVE_GRID_AND_SORT;
  93. requireActivity().invalidateOptionsMenu();
  94. handleSearchEvent();
  95. }
  96. @Override
  97. public void onRefresh() {
  98. super.onRefresh();
  99. refresh = true;
  100. handleSearchEvent();
  101. }
  102. @Override
  103. public void onMessageEvent(ChangeMenuEvent changeMenuEvent) {
  104. super.onMessageEvent(changeMenuEvent);
  105. }
  106. private void handleSearchEvent() {
  107. prepareCurrentSearch(searchEvent);
  108. searchFragment = true;
  109. setEmptyListLoadingMessage();
  110. if (refresh || preferences.getPhotoSearchTimestamp() == 0 ||
  111. System.currentTimeMillis() - preferences.getPhotoSearchTimestamp() >= 30 * 1000) {
  112. mAdapter.setData(
  113. new ArrayList<>(),
  114. SearchType.PHOTO_SEARCH,
  115. mContainerActivity.getStorageManager(),
  116. mFile,
  117. true);
  118. refresh = false;
  119. } else {
  120. mAdapter.showVirtuals(VirtualFolderType.PHOTOS, true, mContainerActivity.getStorageManager());
  121. preferences.setPhotoSearchTimestamp(System.currentTimeMillis());
  122. return;
  123. }
  124. setFabVisible(false);
  125. searchAndDisplay();
  126. }
  127. private void searchAndDisplay() {
  128. if (!photoSearchQueryRunning && !photoSearchNoNew) {
  129. photoSearchTask = new PhotoSearchTask(getColumnsCount(),
  130. this,
  131. accountManager.getUser(),
  132. searchRemoteOperation,
  133. mContainerActivity.getStorageManager())
  134. .execute();
  135. }
  136. }
  137. public void setPhotoSearchQueryRunning(boolean bool) {
  138. photoSearchQueryRunning = bool;
  139. }
  140. public void setSearchDidNotFindNewPhotos(boolean noNewPhotos) {
  141. photoSearchNoNew = noNewPhotos;
  142. }
  143. @Override
  144. public boolean isLoading() {
  145. return !photoSearchNoNew;
  146. }
  147. private void loadMoreWhenEndReached(@NonNull RecyclerView recyclerView, int dy) {
  148. if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
  149. GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
  150. // scroll down
  151. if (dy > 0 && !photoSearchQueryRunning) {
  152. int visibleItemCount = gridLayoutManager.getChildCount();
  153. int totalItemCount = gridLayoutManager.getItemCount();
  154. int firstVisibleItem = gridLayoutManager.findFirstCompletelyVisibleItemPosition();
  155. if ((totalItemCount - visibleItemCount) <= (firstVisibleItem + MAX_ITEMS_PER_ROW)
  156. && (totalItemCount - visibleItemCount) > 0) {
  157. // Almost reached the end, continue to load new photos
  158. searchAndDisplay();
  159. }
  160. }
  161. }
  162. }
  163. }