GalleryFragment.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.OCFile;
  28. import com.owncloud.android.lib.common.utils.Log_OC;
  29. import com.owncloud.android.ui.asynctasks.GallerySearchTask;
  30. import com.owncloud.android.ui.events.ChangeMenuEvent;
  31. import androidx.annotation.NonNull;
  32. import androidx.recyclerview.widget.GridLayoutManager;
  33. import androidx.recyclerview.widget.RecyclerView;
  34. /**
  35. * A Fragment that lists all files and folders in a given path
  36. */
  37. public class GalleryFragment extends OCFileListFragment {
  38. private static final int MAX_ITEMS_PER_ROW = 10;
  39. private boolean photoSearchQueryRunning = false;
  40. private AsyncTask<Void, Void, GallerySearchTask.Result> photoSearchTask;
  41. private long startDate;
  42. private long endDate;
  43. private long daySpan = 30;
  44. private int limit = 300;
  45. @Override
  46. public void onCreate(Bundle savedInstanceState) {
  47. super.onCreate(savedInstanceState);
  48. searchFragment = true;
  49. }
  50. @Override
  51. public void onPause() {
  52. super.onPause();
  53. if (photoSearchTask != null) {
  54. photoSearchTask.cancel(true);
  55. }
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. @Override
  61. public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  62. View v = super.onCreateView(inflater, container, savedInstanceState);
  63. getRecyclerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
  64. @Override
  65. public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
  66. loadMoreWhenEndReached(recyclerView, dy);
  67. }
  68. });
  69. Log_OC.i(this, "onCreateView() in GalleryFragment end");
  70. return v;
  71. }
  72. @Override
  73. public void onActivityCreated(Bundle savedInstanceState) {
  74. super.onActivityCreated(savedInstanceState);
  75. mAdapter.setShowMetadata(false);
  76. currentSearchType = SearchType.GALLERY_SEARCH;
  77. switchToGridView();
  78. menuItemAddRemoveValue = MenuItemAddRemove.REMOVE_GRID_AND_SORT;
  79. requireActivity().invalidateOptionsMenu();
  80. handleSearchEvent();
  81. }
  82. @Override
  83. public void onRefresh() {
  84. super.onRefresh();
  85. handleSearchEvent();
  86. }
  87. @Override
  88. public void onResume() {
  89. super.onResume();
  90. setLoading(photoSearchQueryRunning);
  91. }
  92. @Override
  93. public void onMessageEvent(ChangeMenuEvent changeMenuEvent) {
  94. super.onMessageEvent(changeMenuEvent);
  95. }
  96. private void handleSearchEvent() {
  97. prepareCurrentSearch(searchEvent);
  98. setEmptyListLoadingMessage();
  99. // always show first stored items
  100. mAdapter.showAllGalleryItems(mContainerActivity.getStorageManager());
  101. setFabVisible(false);
  102. searchAndDisplay();
  103. }
  104. private void searchAndDisplay() {
  105. // first: always search from now to -30 days
  106. if (!photoSearchQueryRunning) {
  107. photoSearchQueryRunning = true;
  108. startDate = (System.currentTimeMillis() / 1000) - 30 * 24 * 60 * 60;
  109. endDate = System.currentTimeMillis() / 1000;
  110. photoSearchTask = new GallerySearchTask(this,
  111. accountManager.getUser(),
  112. mContainerActivity.getStorageManager(),
  113. startDate,
  114. endDate,
  115. limit)
  116. .execute();
  117. }
  118. }
  119. public void searchCompleted(boolean emptySearch, long lastTimeStamp) {
  120. photoSearchQueryRunning = false;
  121. mAdapter.notifyDataSetChanged();
  122. if (mAdapter.isEmpty()) {
  123. setEmptyListMessage(SearchType.GALLERY_SEARCH);
  124. }
  125. if (emptySearch && getAdapter().getItemCount() > 0) {
  126. Log_OC.d(this, "End gallery search");
  127. return;
  128. }
  129. if (daySpan == 30) {
  130. daySpan = 90;
  131. } else if (daySpan == 90) {
  132. daySpan = 180;
  133. } else if (daySpan == 180) {
  134. daySpan = 999;
  135. } else if (daySpan == 999 && limit > 0) {
  136. limit = -1; // no limit
  137. } else {
  138. Log_OC.d(this, "End gallery search");
  139. return;
  140. }
  141. if (lastTimeStamp > -1) {
  142. endDate = lastTimeStamp;
  143. }
  144. startDate = endDate - (daySpan * 24 * 60 * 60);
  145. photoSearchTask = new GallerySearchTask(this,
  146. accountManager.getUser(),
  147. mContainerActivity.getStorageManager(),
  148. startDate,
  149. endDate,
  150. limit)
  151. .execute();
  152. }
  153. @Override
  154. public boolean isLoading() {
  155. return photoSearchQueryRunning;
  156. }
  157. private void loadMoreWhenEndReached(@NonNull RecyclerView recyclerView, int dy) {
  158. if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
  159. GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
  160. // scroll down
  161. if (dy > 0 && !photoSearchQueryRunning) {
  162. int visibleItemCount = gridLayoutManager.getChildCount();
  163. int totalItemCount = gridLayoutManager.getItemCount();
  164. int lastVisibleItem = gridLayoutManager.findLastCompletelyVisibleItemPosition();
  165. if ((totalItemCount - visibleItemCount) <= (lastVisibleItem + MAX_ITEMS_PER_ROW)
  166. && (totalItemCount - visibleItemCount) > 0) {
  167. // Almost reached the end, continue to load new photos
  168. OCFile lastFile = mAdapter.getItem(lastVisibleItem - 1);
  169. daySpan = 30;
  170. endDate = lastFile.getModificationTimestamp() / 1000;
  171. startDate = endDate - (daySpan * 24 * 60 * 60);
  172. photoSearchQueryRunning = true;
  173. photoSearchTask = new GallerySearchTask(this,
  174. accountManager.getUser(),
  175. mContainerActivity.getStorageManager(),
  176. startDate,
  177. endDate,
  178. limit)
  179. .execute();
  180. }
  181. }
  182. }
  183. }
  184. }