Skip to content

Commit

Permalink
wip use reloading on photo view
Browse files Browse the repository at this point in the history
Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
  • Loading branch information
tobiasKaminsky committed Feb 28, 2018
1 parent c9f6c5c commit 89b8829
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ private void selectNavigationItem(final MenuItem menuItem) {
SearchEvent.UnsetType.NO_UNSET), menuItem);
break;
case R.id.nav_photos:
switchToSearchFragment(new SearchEvent("image/%", SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
switchToSearchFragment(new SearchEvent("image/%", SearchOperation.SearchType.PHOTO_SEARCH,
SearchEvent.UnsetType.NO_UNSET), menuItem);
break;
case R.id.nav_on_device:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public class OCFileListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
private ArrayList<OCFile> mFilesAll = new ArrayList<>();
private boolean mJustFolders;
private boolean mHideItemOptions;
private long lastTimestamp;

private boolean gridView = false;
private boolean multiSelect = false;
private HashSet<OCFile> checkedFiles;
Expand Down Expand Up @@ -512,18 +514,20 @@ private void searchForLocalFileInDefaultPath(OCFile file) {
}

public void setData(ArrayList<Object> objects, ExtendedListFragment.SearchType searchType,
FileDataStorageManager storageManager, OCFile folder) {
FileDataStorageManager storageManager, OCFile folder, boolean clear) {
if (storageManager != null && mStorageManager == null) {
mStorageManager = storageManager;
}
mFiles.clear();
if (clear) {
mFiles.clear();
}

// early exit
if (objects.size() > 0 && mStorageManager != null) {
if (searchType.equals(ExtendedListFragment.SearchType.SHARED_FILTER)) {
parseShares(objects);
} else {
parseVirtuals(objects, searchType);
parseVirtuals(objects, searchType, clear);
}
}

Expand Down Expand Up @@ -586,7 +590,7 @@ private void parseShares(ArrayList<Object> objects) {
mStorageManager.saveShares(shares);
}

private void parseVirtuals(ArrayList<Object> objects, ExtendedListFragment.SearchType searchType) {
private void parseVirtuals(ArrayList<Object> objects, ExtendedListFragment.SearchType searchType, boolean clear) {
VirtualFolderType type;
boolean onlyImages = false;
switch (searchType) {
Expand All @@ -596,13 +600,16 @@ private void parseVirtuals(ArrayList<Object> objects, ExtendedListFragment.Searc
case PHOTO_SEARCH:
type = VirtualFolderType.PHOTOS;
onlyImages = true;
lastTimestamp = ((RemoteFile) objects.get(objects.size() - 1)).getModifiedTimestamp() / 1000;
break;
default:
type = VirtualFolderType.NONE;
break;
}

mStorageManager.deleteVirtuals(type);
if (clear) {
mStorageManager.deleteVirtuals(type);
}

ArrayList<ContentValues> contentValues = new ArrayList<>();

Expand All @@ -613,15 +620,15 @@ private void parseVirtuals(ArrayList<Object> objects, ExtendedListFragment.Searc
try {
ocFile = mStorageManager.saveFileWithParent(ocFile, mContext);

if (!onlyImages || MimeTypeUtil.isImage(ocFile)) {
if ((!onlyImages || MimeTypeUtil.isImage(ocFile)) && !mFiles.contains(ocFile)) {
mFiles.add(ocFile);
}

ContentValues cv = new ContentValues();
cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_TYPE, type.toString());
cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_OCFILE_ID, ocFile.getFileId());
ContentValues cv = new ContentValues();
cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_TYPE, type.toString());
cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_OCFILE_ID, ocFile.getFileId());

contentValues.add(cv);
contentValues.add(cv);
}
} catch (RemoteOperationFailedException e) {
Log_OC.e(TAG, "Error saving file with parent" + e.getMessage(), e);
}
Expand Down Expand Up @@ -679,6 +686,14 @@ public Filter getFilter() {
return mFilesFilter;
}

public void resetLastTimestamp() {
lastTimestamp = -1;
}

public long getLastTimestamp() {
return lastTimestamp;
}

private class FilesFilter extends Filter {

@Override
Expand Down
137 changes: 95 additions & 42 deletions src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
private boolean searchFragment = false;
private SearchEvent searchEvent;
private AsyncTask remoteOperationAsyncTask;
private RecyclerView.OnScrollListener onScrollChangeListener;
private boolean photoSearchQueryRunning;
private boolean photoSearchNoNew = false;
private RemoteOperation remoteOperation;

private enum MenuItemAddRemove {
DO_NOTHING, REMOVE_SORT, REMOVE_GRID_AND_SORT, ADD_SORT, ADD_GRID_AND_SORT, ADD_GRID_AND_SORT_WITH_SEARCH,
Expand Down Expand Up @@ -204,6 +208,25 @@ public void onCreate(Bundle savedInstanceState) {
if (isGridViewPreferred(getCurrentFile())) {
switchToGridView();
}

onScrollChangeListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

LinearLayoutManager linearLayoutManager = (LinearLayoutManager) getRecyclerView().getLayoutManager();

int totalItemCount = linearLayoutManager.getItemCount();
int lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
if (!photoSearchQueryRunning && totalItemCount <= (lastVisibleItem + 5)) {
Log_OC.d("SCROLL", "load next");

search();

photoSearchQueryRunning = true;
}
}
};
}

/**
Expand Down Expand Up @@ -349,7 +372,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
searchEvent = Parcels.unwrap(getArguments().getParcelable(OCFileListFragment.SEARCH_EVENT));
prepareCurrentSearch(searchEvent);
if (searchEvent != null && savedInstanceState == null) {
onMessageEvent(searchEvent);
onMessageEvent(searchEvent, true);
}

setTitle();
Expand All @@ -375,6 +398,8 @@ private void prepareCurrentSearch(SearchEvent event) {
currentSearchType = SearchType.RECENTLY_MODIFIED_SEARCH;
} else if (event.getSearchType().equals(SearchOperation.SearchType.SHARED_SEARCH)) {
currentSearchType = SearchType.SHARED_FILTER;
} else if (event.getSearchType().equals(SearchOperation.SearchType.PHOTO_SEARCH)) {
currentSearchType = SearchType.PHOTO_SEARCH;
}

prepareActionBarItems(event);
Expand Down Expand Up @@ -715,7 +740,7 @@ public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
}

if (searchEvent != null) {
onMessageEvent(searchEvent);
onMessageEvent(searchEvent, true);
}
}

Expand Down Expand Up @@ -1452,10 +1477,10 @@ public void onMessageEvent(FavoriteEvent event) {
}

@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onMessageEvent(final SearchEvent event) {
public void onMessageEvent(final SearchEvent event, boolean clear) {
searchFragment = true;
setEmptyListLoadingMessage();
mAdapter.setData(new ArrayList<>(), SearchType.NO_SEARCH, mContainerActivity.getStorageManager(), mFile);
mAdapter.setData(new ArrayList<>(), SearchType.NO_SEARCH, mContainerActivity.getStorageManager(), mFile, true);

setFabEnabled(false);

Expand Down Expand Up @@ -1506,7 +1531,6 @@ public void run() {
new Handler(Looper.getMainLooper()).post(switchViewsRunnable);
}

final RemoteOperation remoteOperation;
if (!currentSearchType.equals(SearchType.SHARED_FILTER)) {
boolean searchOnlyFolders = false;
if (getArguments() != null && getArguments().getBoolean(ARG_SEARCH_ONLY_FOLDER, false)) {
Expand All @@ -1518,55 +1542,83 @@ public void run() {
remoteOperation = new GetRemoteSharesOperation();
}

final Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
search();

remoteOperationAsyncTask = new AsyncTask() {
@Override
protected Object doInBackground(Object[] params) {
if (getContext() != null && !isCancelled()) {
RemoteOperationResult remoteOperationResult = remoteOperation.execute(currentAccount, getContext());
getRecyclerView().setOnScrollListener(onScrollChangeListener);
}

FileDataStorageManager storageManager = null;
if (mContainerActivity != null && mContainerActivity.getStorageManager() != null) {
storageManager = mContainerActivity.getStorageManager();
}
// TODO refactor, can only used for photo search atm
private void search() {
if (!photoSearchQueryRunning && !photoSearchNoNew) {
remoteOperationAsyncTask = new AsyncTask() {
@Override
protected Object doInBackground(Object[] params) {
if (getContext() != null && !isCancelled()) {
Log_OC.d("SCROLL", "re-run search");

if (remoteOperationResult.isSuccess() && remoteOperationResult.getData() != null
&& !isCancelled() && searchFragment) {
if (remoteOperationResult.getData() == null || remoteOperationResult.getData().size() == 0) {
setEmptyView(event);
} else {
mAdapter.setData(remoteOperationResult.getData(), currentSearchType, storageManager, mFile);
final Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());

int limit = -1;
if (currentSearchType.equals(SearchType.PHOTO_SEARCH)) {
limit = 15 * getColumnSize();
}

long timestamp = -1;
if (mAdapter.getLastTimestamp() > 0) {
timestamp = mAdapter.getLastTimestamp();
}

if (remoteOperation instanceof SearchOperation) {
SearchOperation searchOperation = (SearchOperation) remoteOperation;
searchOperation.setLimit(limit);
searchOperation.setTimestamp(timestamp);
}

final ToolbarActivity fileDisplayActivity = (ToolbarActivity) getActivity();
if (fileDisplayActivity != null) {
fileDisplayActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (fileDisplayActivity != null) {
RemoteOperationResult remoteOperationResult = remoteOperation.execute(currentAccount, getContext());

FileDataStorageManager storageManager = null;
if (mContainerActivity != null && mContainerActivity.getStorageManager() != null) {
storageManager = mContainerActivity.getStorageManager();
}

if (remoteOperationResult.isSuccess() && remoteOperationResult.getData() != null
&& !isCancelled() && searchFragment) {
if (remoteOperationResult.getData() == null || remoteOperationResult.getData().size() == 0) {
photoSearchNoNew = true;
// todo how distinguish to show
// setEmptyView(event);
} else {
mAdapter.setData(remoteOperationResult.getData(), currentSearchType, storageManager, mFile, false);
}

final ToolbarActivity fileDisplayActivity = (ToolbarActivity) getActivity();
if (fileDisplayActivity != null) {
fileDisplayActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
fileDisplayActivity.setIndeterminate(false);
}
}
});
});
}
}
}

return remoteOperationResult.isSuccess();
} else {
return false;
return remoteOperationResult.isSuccess();
} else {
return false;
}
}
}

@Override
protected void onPostExecute(Object o) {
if (!isCancelled()) {
mAdapter.notifyDataSetChanged();
@Override
protected void onPostExecute(Object o) {
photoSearchQueryRunning = false;
if (!isCancelled()) {
mAdapter.notifyDataSetChanged();
}
}
}
};
};

remoteOperationAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, true);
remoteOperationAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, true);
}
}

@Subscribe(threadMode = ThreadMode.BACKGROUND)
Expand Down Expand Up @@ -1650,7 +1702,8 @@ public void onStop() {
@Override
public void onRefresh() {
if (searchEvent != null && searchFragment) {
onMessageEvent(searchEvent);
mAdapter.resetLastTimestamp();
onMessageEvent(searchEvent, true);

mRefreshListLayout.setRefreshing(false);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/owncloud/android/utils/DisplayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
break;
case R.id.nav_bar_photos:
SearchEvent photosEvent = new SearchEvent("image/%",
SearchOperation.SearchType.CONTENT_TYPE_SEARCH,
SearchOperation.SearchType.PHOTO_SEARCH,
SearchEvent.UnsetType.UNSET_DRAWER);

switchToSearchFragment(activity, photosEvent);
Expand Down

0 comments on commit 89b8829

Please sign in to comment.