PhotoFragment.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.Bundle;
  23. import android.view.LayoutInflater;
  24. import android.view.View;
  25. import android.view.ViewGroup;
  26. import com.owncloud.android.datamodel.VirtualFolderType;
  27. import com.owncloud.android.lib.common.utils.Log_OC;
  28. import com.owncloud.android.lib.resources.files.SearchRemoteOperation;
  29. import com.owncloud.android.ui.asynctasks.PhotoSearchTask;
  30. import com.owncloud.android.ui.events.ChangeMenuEvent;
  31. import com.owncloud.android.ui.events.SearchEvent;
  32. import org.jetbrains.annotations.NotNull;
  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. @Override
  47. public void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. @Override
  54. public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  55. View v = super.onCreateView(inflater, container, savedInstanceState);
  56. getRecyclerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
  57. @Override
  58. public void onScrolled(@NotNull RecyclerView recyclerView, int dx, int dy) {
  59. loadMoreWhenEndReached(recyclerView, dy);
  60. }
  61. });
  62. Log_OC.i(this, "onCreateView() in PhotoFragment end");
  63. return v;
  64. }
  65. @Override
  66. public void onActivityCreated(Bundle savedInstanceState) {
  67. super.onActivityCreated(savedInstanceState);
  68. currentSearchType = SearchType.PHOTO_SEARCH;
  69. switchToGridView();
  70. menuItemAddRemoveValue = MenuItemAddRemove.REMOVE_GRID_AND_SORT;
  71. requireActivity().invalidateOptionsMenu();
  72. handleSearchEvent(searchEvent, false);
  73. }
  74. @Override
  75. public void onRefresh() {
  76. super.onRefresh();
  77. handleSearchEvent(searchEvent, true);
  78. }
  79. @Override
  80. public void onMessageEvent(ChangeMenuEvent changeMenuEvent) {
  81. super.onMessageEvent(changeMenuEvent);
  82. }
  83. private void handleSearchEvent(final SearchEvent event, boolean refresh) {
  84. prepareCurrentSearch(event);
  85. searchFragment = true;
  86. setEmptyListLoadingMessage();
  87. if (refresh || preferences.getPhotoSearchTimestamp() == 0 ||
  88. System.currentTimeMillis() - preferences.getPhotoSearchTimestamp() >= 30 * 1000) {
  89. mAdapter.setData(
  90. new ArrayList<>(),
  91. SearchType.PHOTO_SEARCH,
  92. mContainerActivity.getStorageManager(),
  93. mFile,
  94. true);
  95. } else {
  96. mAdapter.showVirtuals(VirtualFolderType.PHOTOS, true, mContainerActivity.getStorageManager());
  97. preferences.setPhotoSearchTimestamp(System.currentTimeMillis());
  98. return;
  99. }
  100. setFabVisible(false);
  101. if (currentSearchType != SearchType.SHARED_FILTER) {
  102. boolean searchOnlyFolders = false;
  103. if (getArguments() != null && getArguments().getBoolean(ARG_SEARCH_ONLY_FOLDER, false)) {
  104. searchOnlyFolders = true;
  105. }
  106. searchRemoteOperation = new SearchRemoteOperation(event.getSearchQuery(),
  107. event.getSearchType(),
  108. searchOnlyFolders);
  109. }
  110. searchAndDisplay();
  111. }
  112. private void searchAndDisplay() {
  113. if (!photoSearchQueryRunning && !photoSearchNoNew) {
  114. new PhotoSearchTask(getColumnsCount(),
  115. this,
  116. accountManager.getCurrentAccount(),
  117. searchRemoteOperation,
  118. mContainerActivity.getStorageManager())
  119. .execute();
  120. }
  121. }
  122. public void setPhotoSearchQueryRunning(boolean bool) {
  123. photoSearchQueryRunning = bool;
  124. }
  125. public void setSearchDidNotFindNewPhotos(boolean noNewPhotos) {
  126. photoSearchNoNew = noNewPhotos;
  127. }
  128. @Override
  129. public boolean isLoading() {
  130. return !photoSearchNoNew;
  131. }
  132. private void loadMoreWhenEndReached(@NonNull RecyclerView recyclerView, int dy) {
  133. if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
  134. GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
  135. // scroll down
  136. if (dy > 0 && !photoSearchQueryRunning) {
  137. int visibleItemCount = gridLayoutManager.getChildCount();
  138. int totalItemCount = gridLayoutManager.getItemCount();
  139. int firstVisibleItem = gridLayoutManager.findFirstCompletelyVisibleItemPosition();
  140. if ((totalItemCount - visibleItemCount) <= (firstVisibleItem + MAX_ITEMS_PER_ROW)
  141. && (totalItemCount - visibleItemCount) > 0) {
  142. // Almost reached the end, continue to load new photos
  143. searchAndDisplay();
  144. }
  145. }
  146. }
  147. }
  148. }