Merge pull request #5296 from nextcloud/fix/defective-search

Fixed defective search
This commit is contained in:
Tobias Kaminsky 2020-02-05 10:56:19 +01:00 committed by GitHub
commit 7c84a9f929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 127 additions and 121 deletions

View File

@ -5,4 +5,3 @@ NC_TEST_SERVER_PASSWORD=test
android.enableJetifier=true android.enableJetifier=true
android.useAndroidX=true android.useAndroidX=true
android.debug.obsoleteApi=true android.debug.obsoleteApi=true

View File

@ -790,42 +790,31 @@ public class FileDisplayActivity extends FileActivity
//focus the SearchView //focus the SearchView
if (!TextUtils.isEmpty(searchQuery)) { if (!TextUtils.isEmpty(searchQuery)) {
searchView.post(new Runnable() { searchView.post(() -> {
@Override searchView.setIconified(false);
public void run() { searchView.setQuery(searchQuery, true);
searchView.setIconified(false); searchView.clearFocus();
searchView.setQuery(searchQuery, true);
searchView.clearFocus();
}
}); });
} }
final View mSearchEditFrame = searchView final View mSearchEditFrame = searchView
.findViewById(androidx.appcompat.R.id.search_edit_frame); .findViewById(androidx.appcompat.R.id.search_edit_frame);
searchView.setOnCloseListener(new SearchView.OnCloseListener() { searchView.setOnCloseListener(() -> {
@Override if (TextUtils.isEmpty(searchView.getQuery().toString())) {
public boolean onClose() { searchView.onActionViewCollapsed();
if (TextUtils.isEmpty(searchView.getQuery().toString())) { setDrawerIndicatorEnabled(isDrawerIndicatorAvailable()); // order matters
searchView.onActionViewCollapsed(); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setDrawerIndicatorEnabled(isDrawerIndicatorAvailable()); // order matters mDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle.syncState();
if (getListOfFilesFragment() != null) { if (getListOfFilesFragment() != null) {
getListOfFilesFragment().setSearchFragment(false); getListOfFilesFragment().setSearchFragment(false);
getListOfFilesFragment().refreshDirectory(); getListOfFilesFragment().refreshDirectory();
}
} else {
searchView.post(new Runnable() {
@Override
public void run() {
searchView.setQuery("", true);
}
});
} }
return true; } else {
searchView.post(() -> searchView.setQuery("", true));
} }
return true;
}); });
ViewTreeObserver vto = mSearchEditFrame.getViewTreeObserver(); ViewTreeObserver vto = mSearchEditFrame.getViewTreeObserver();
@ -1141,22 +1130,28 @@ public class FileDisplayActivity extends FileActivity
} }
} }
/*
* BackPressed priority/hierarchy:
* 1. close search view if opened
* 2. close drawer if opened
* 3. close FAB if open (only if drawer isn't open)
* 4. navigate up (only if drawer and FAB aren't open)
*/
@Override @Override
public void onBackPressed() { public void onBackPressed() {
boolean isDrawerOpen = isDrawerOpen(); boolean isDrawerOpen = isDrawerOpen();
boolean isSearchOpen = isSearchOpen(); boolean isSearchOpen = isSearchOpen();
/* OCFileListFragment listOfFiles = getListOfFilesFragment();
* BackPressed priority/hierarchy:
* 1. close search view if opened
* 2. close drawer if opened
* 3. close FAB if open (only if drawer isn't open)
* 4. navigate up (only if drawer and FAB aren't open)
*/
if (isSearchOpen && searchView != null) { if (isSearchOpen && searchView != null) {
searchView.setQuery("", true); searchView.setQuery("", true);
searchView.onActionViewCollapsed(); searchView.onActionViewCollapsed();
searchView.clearFocus();
// Remove the list to the original state
listOfFiles.performSearch("", true);
setDrawerIndicatorEnabled(isDrawerIndicatorAvailable()); setDrawerIndicatorEnabled(isDrawerIndicatorAvailable());
} else if (isDrawerOpen) { } else if (isDrawerOpen) {
// close drawer first // close drawer first
@ -1164,7 +1159,7 @@ public class FileDisplayActivity extends FileActivity
} else { } else {
// all closed // all closed
OCFileListFragment listOfFiles = getListOfFilesFragment(); listOfFiles = getListOfFilesFragment();
if (mDualPane || getSecondFragment() == null) { if (mDualPane || getSecondFragment() == null) {
OCFile currentDir = getCurrentDir(); OCFile currentDir = getCurrentDir();
if (currentDir == null || currentDir.getParentId() == FileDataStorageManager.ROOT_PARENT_ID) { if (currentDir == null || currentDir.getParentId() == FileDataStorageManager.ROOT_PARENT_ID) {

View File

@ -45,6 +45,7 @@ import android.view.ScaleGestureDetector;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewTreeObserver; import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView; import android.widget.GridView;
@ -91,11 +92,11 @@ import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
public class ExtendedListFragment extends Fragment implements public class ExtendedListFragment extends Fragment implements
OnItemClickListener, OnItemClickListener,
OnEnforceableRefreshListener, OnEnforceableRefreshListener,
SearchView.OnQueryTextListener, SearchView.OnQueryTextListener,
SearchView.OnCloseListener, SearchView.OnCloseListener,
Injectable { Injectable {
protected static final String TAG = ExtendedListFragment.class.getSimpleName(); protected static final String TAG = ExtendedListFragment.class.getSimpleName();
@ -136,6 +137,7 @@ public class ExtendedListFragment extends Fragment implements
private EmptyRecyclerView mRecyclerView; private EmptyRecyclerView mRecyclerView;
protected SearchView searchView; protected SearchView searchView;
private ImageView closeButton;
private Handler handler = new Handler(Looper.getMainLooper()); private Handler handler = new Handler(Looper.getMainLooper());
private float mScale = AppPreferencesImpl.DEFAULT_GRID_COLUMN; private float mScale = AppPreferencesImpl.DEFAULT_GRID_COLUMN;
@ -191,6 +193,7 @@ public class ExtendedListFragment extends Fragment implements
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
final MenuItem item = menu.findItem(R.id.action_search); final MenuItem item = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(item); searchView = (SearchView) MenuItemCompat.getActionView(item);
closeButton = searchView.findViewById(androidx.appcompat.R.id.search_close_btn);
searchView.setOnQueryTextListener(this); searchView.setOnQueryTextListener(this);
searchView.setOnCloseListener(this); searchView.setOnCloseListener(this);
ThemeUtils.themeSearchView(searchView, true, requireContext()); ThemeUtils.themeSearchView(searchView, true, requireContext());
@ -216,20 +219,27 @@ public class ExtendedListFragment extends Fragment implements
} }
} }
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() { searchView.setOnQueryTextFocusChangeListener((v, hasFocus) -> handler.post(() -> {
@Override if (getActivity() != null && !(getActivity() instanceof FolderPickerActivity)
public void onFocusChange(View v, final boolean hasFocus) { && !(getActivity() instanceof UploadFilesActivity)) {
setFabVisible(!hasFocus);
if (TextUtils.isEmpty(searchView.getQuery())) {
closeButton.setVisibility(View.INVISIBLE);
}
}
}));
handler.postDelayed(new Runnable() { // On close -> empty field, show keyboard and
@Override closeButton.setOnClickListener(view -> {
public void run() { searchView.setQuery("", true);
if (getActivity() != null && !(getActivity() instanceof FolderPickerActivity) searchView.requestFocus();
&& !(getActivity() instanceof UploadFilesActivity)) { searchView.onActionViewExpanded();
setFabVisible(!hasFocus);
} InputMethodManager inputMethodManager =
} (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
}, 100);
if (inputMethodManager != null) {
inputMethodManager.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT);
} }
}); });
@ -274,8 +284,15 @@ public class ExtendedListFragment extends Fragment implements
} }
public boolean onQueryTextChange(final String query) { public boolean onQueryTextChange(final String query) {
// After 300 ms, set the query
closeButton.setVisibility(View.VISIBLE);
if (query.isEmpty()) {
closeButton.setVisibility(View.INVISIBLE);
}
if (getFragmentManager() != null && getFragmentManager(). if (getFragmentManager() != null && getFragmentManager().
findFragmentByTag(FileDisplayActivity.TAG_SECOND_FRAGMENT) instanceof ExtendedListFragment) { findFragmentByTag(FileDisplayActivity.TAG_SECOND_FRAGMENT) instanceof ExtendedListFragment) {
performSearch(query, false); performSearch(query, false);
return true; return true;
} else { } else {
@ -285,58 +302,41 @@ public class ExtendedListFragment extends Fragment implements
@Override @Override
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
performSearch(query, true); performSearch(query, false);
return true; return true;
} }
private void performSearch(final String query, boolean isSubmit) { public void performSearch(final String query, boolean isBackPressed) {
handler.removeCallbacksAndMessages(null); handler.removeCallbacksAndMessages(null);
RecyclerView.Adapter adapter = getRecyclerView().getAdapter(); RecyclerView.Adapter adapter = getRecyclerView().getAdapter();
Activity activity = getActivity();
if (!TextUtils.isEmpty(query)) { if (activity != null) {
int delay = 500; if (activity instanceof FileDisplayActivity) {
if (isBackPressed && TextUtils.isEmpty(query)) {
if (isSubmit) {
delay = 0;
}
if (adapter instanceof OCFileListAdapter) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
EventBus.getDefault().post(new SearchEvent(query,
SearchRemoteOperation.SearchType.FILE_SEARCH,
SearchEvent.UnsetType.NO_UNSET));
}
}, delay);
} else if (adapter instanceof LocalFileListAdapter) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
LocalFileListAdapter localFileListAdapter = (LocalFileListAdapter) adapter;
localFileListAdapter.filter(query);
}
}, delay);
}
if (searchView != null && delay == 0) {
searchView.clearFocus();
}
} else {
Activity activity;
if ((activity = getActivity()) != null) {
if (activity instanceof FileDisplayActivity) {
FileDisplayActivity fileDisplayActivity = (FileDisplayActivity) activity; FileDisplayActivity fileDisplayActivity = (FileDisplayActivity) activity;
fileDisplayActivity.resetSearchView(); fileDisplayActivity.resetSearchView();
fileDisplayActivity.updateListOfFilesFragment(true); fileDisplayActivity.updateListOfFilesFragment(true);
} else if (activity instanceof UploadFilesActivity) { } else {
LocalFileListAdapter localFileListAdapter = (LocalFileListAdapter) adapter; handler.post(() -> {
localFileListAdapter.filter(query); if (adapter instanceof OCFileListAdapter) {
} else if (activity instanceof FolderPickerActivity) { EventBus.getDefault().post(new SearchEvent(query,
((FolderPickerActivity) activity).refreshListOfFilesFragment(true); SearchRemoteOperation.SearchType.FILE_SEARCH,
SearchEvent.UnsetType.NO_UNSET));
} else if (adapter instanceof LocalFileListAdapter) {
LocalFileListAdapter localFileListAdapter = (LocalFileListAdapter) adapter;
localFileListAdapter.filter(query);
}
});
if (searchView != null) {
searchView.clearFocus();
}
} }
} else if (activity instanceof UploadFilesActivity) {
LocalFileListAdapter localFileListAdapter = (LocalFileListAdapter) adapter;
localFileListAdapter.filter(query);
} else if (activity instanceof FolderPickerActivity) {
((FolderPickerActivity) activity).refreshListOfFilesFragment(true);
} }
} }
} }
@ -369,7 +369,7 @@ public class ExtendedListFragment extends Fragment implements
mScale = preferences.getGridColumns(); mScale = preferences.getGridColumns();
setGridViewColumns(1f); setGridViewColumns(1f);
mScaleGestureDetector = new ScaleGestureDetector(MainApp.getAppContext(),new ScaleListener()); mScaleGestureDetector = new ScaleGestureDetector(MainApp.getAppContext(), new ScaleListener());
getRecyclerView().setOnTouchListener((view, motionEvent) -> { getRecyclerView().setOnTouchListener((view, motionEvent) -> {
mScaleGestureDetector.onTouchEvent(motionEvent); mScaleGestureDetector.onTouchEvent(motionEvent);
@ -492,7 +492,7 @@ public class ExtendedListFragment extends Fragment implements
int top = mTops.remove(mTops.size() - 1); int top = mTops.remove(mTops.size() - 1);
Log_OC.v(TAG, "Setting selection to position: " + firstPosition + "; top: " Log_OC.v(TAG, "Setting selection to position: " + firstPosition + "; top: "
+ top + "; index: " + index); + top + "; index: " + index);
scrollToPosition(firstPosition); scrollToPosition(firstPosition);
} }
@ -503,7 +503,7 @@ public class ExtendedListFragment extends Fragment implements
if (mRecyclerView != null) { if (mRecyclerView != null) {
int visibleItemCount = linearLayoutManager.findLastCompletelyVisibleItemPosition() - int visibleItemCount = linearLayoutManager.findLastCompletelyVisibleItemPosition() -
linearLayoutManager.findFirstCompletelyVisibleItemPosition(); linearLayoutManager.findFirstCompletelyVisibleItemPosition();
linearLayoutManager.scrollToPositionWithOffset(position, (visibleItemCount / 2) * mHeightCell); linearLayoutManager.scrollToPositionWithOffset(position, (visibleItemCount / 2) * mHeightCell);
} }
} }
@ -579,7 +579,7 @@ public class ExtendedListFragment extends Fragment implements
/** /**
* Sets the 'visibility' state of the FAB contained in the fragment. * Sets the 'visibility' state of the FAB contained in the fragment.
* * <p>
* When 'false' is set, FAB visibility is set to View.GONE programmatically. * When 'false' is set, FAB visibility is set to View.GONE programmatically.
* *
* @param visible Desired visibility for the FAB. * @param visible Desired visibility for the FAB.
@ -599,7 +599,7 @@ public class ExtendedListFragment extends Fragment implements
/** /**
* Sets the 'visibility' state of the FAB contained in the fragment. * Sets the 'visibility' state of the FAB contained in the fragment.
* * <p>
* When 'false' is set, FAB is greyed out * When 'false' is set, FAB is greyed out
* *
* @param enabled Desired visibility for the FAB. * @param enabled Desired visibility for the FAB.
@ -622,8 +622,7 @@ public class ExtendedListFragment extends Fragment implements
} }
/** /**
/** * /** Set message for empty list view.
* Set message for empty list view.
*/ */
public void setMessageForEmptyList(String message) { public void setMessageForEmptyList(String message) {
if (mEmptyListContainer != null && mEmptyListMessage != null) { if (mEmptyListContainer != null && mEmptyListMessage != null) {
@ -685,50 +684,63 @@ public class ExtendedListFragment extends Fragment implements
if (searchType == SearchType.NO_SEARCH) { if (searchType == SearchType.NO_SEARCH) {
setMessageForEmptyList( setMessageForEmptyList(
R.string.file_list_empty_headline, R.string.file_list_empty_headline,
R.string.file_list_empty, R.string.file_list_empty,
R.drawable.ic_list_empty_folder, R.drawable.ic_list_empty_folder,
true true
); );
} else if (searchType == SearchType.FILE_SEARCH) { } else if (searchType == SearchType.FILE_SEARCH) {
setMessageForEmptyList(R.string.file_list_empty_headline_server_search, setMessageForEmptyList(R.string.file_list_empty_headline_server_search,
R.string.file_list_empty, R.drawable.ic_search_light_grey); R.string.file_list_empty,
R.drawable.ic_search_light_grey);
} else if (searchType == SearchType.FAVORITE_SEARCH) { } else if (searchType == SearchType.FAVORITE_SEARCH) {
setMessageForEmptyList(R.string.file_list_empty_favorite_headline, setMessageForEmptyList(R.string.file_list_empty_favorite_headline,
R.string.file_list_empty_favorites_filter_list, R.drawable.ic_star_light_yellow); R.string.file_list_empty_favorites_filter_list,
R.drawable.ic_star_light_yellow);
} else if (searchType == SearchType.VIDEO_SEARCH) { } else if (searchType == SearchType.VIDEO_SEARCH) {
setMessageForEmptyList(R.string.file_list_empty_headline_server_search_videos, setMessageForEmptyList(R.string.file_list_empty_headline_server_search_videos,
R.string.file_list_empty_text_videos, R.drawable.ic_list_empty_video); R.string.file_list_empty_text_videos,
R.drawable.ic_list_empty_video);
} else if (searchType == SearchType.PHOTO_SEARCH) { } else if (searchType == SearchType.PHOTO_SEARCH) {
setMessageForEmptyList(R.string.file_list_empty_headline_server_search_photos, setMessageForEmptyList(R.string.file_list_empty_headline_server_search_photos,
R.string.file_list_empty_text_photos, R.drawable.ic_list_empty_image); R.string.file_list_empty_text_photos,
R.drawable.ic_list_empty_image);
} else if (searchType == SearchType.RECENTLY_MODIFIED_SEARCH) { } else if (searchType == SearchType.RECENTLY_MODIFIED_SEARCH) {
setMessageForEmptyList(R.string.file_list_empty_headline_server_search, setMessageForEmptyList(R.string.file_list_empty_headline_server_search,
R.string.file_list_empty_recently_modified, R.drawable.ic_list_empty_recent); R.string.file_list_empty_recently_modified,
R.drawable.ic_list_empty_recent);
} else if (searchType == SearchType.RECENTLY_ADDED_SEARCH) { } else if (searchType == SearchType.RECENTLY_ADDED_SEARCH) {
setMessageForEmptyList(R.string.file_list_empty_headline_server_search, setMessageForEmptyList(R.string.file_list_empty_headline_server_search,
R.string.file_list_empty_recently_added, R.drawable.ic_list_empty_recent); R.string.file_list_empty_recently_added,
R.drawable.ic_list_empty_recent);
} else if (searchType == SearchType.REGULAR_FILTER) { } else if (searchType == SearchType.REGULAR_FILTER) {
setMessageForEmptyList(R.string.file_list_empty_headline_search, setMessageForEmptyList(R.string.file_list_empty_headline_search,
R.string.file_list_empty_search, R.drawable.ic_search_light_grey); R.string.file_list_empty_search,
R.drawable.ic_search_light_grey);
} else if (searchType == SearchType.FAVORITE_SEARCH_FILTER) { } else if (searchType == SearchType.FAVORITE_SEARCH_FILTER) {
setMessageForEmptyList(R.string.file_list_empty_headline_server_search, setMessageForEmptyList(R.string.file_list_empty_headline_server_search,
R.string.file_list_empty_favorites_filter, R.drawable.ic_star_light_yellow); R.string.file_list_empty_favorites_filter,
R.drawable.ic_star_light_yellow);
} else if (searchType == SearchType.VIDEO_SEARCH_FILTER) { } else if (searchType == SearchType.VIDEO_SEARCH_FILTER) {
setMessageForEmptyList(R.string.file_list_empty_headline_server_search_videos, setMessageForEmptyList(R.string.file_list_empty_headline_server_search_videos,
R.string.file_list_empty_text_videos_filter, R.drawable.ic_list_empty_video); R.string.file_list_empty_text_videos_filter,
R.drawable.ic_list_empty_video);
} else if (searchType == SearchType.PHOTOS_SEARCH_FILTER) { } else if (searchType == SearchType.PHOTOS_SEARCH_FILTER) {
setMessageForEmptyList(R.string.file_list_empty_headline_server_search_photos, setMessageForEmptyList(R.string.file_list_empty_headline_server_search_photos,
R.string.file_list_empty_text_photos_filter, R.drawable.ic_list_empty_image); R.string.file_list_empty_text_photos_filter,
R.drawable.ic_list_empty_image);
} else if (searchType == SearchType.RECENTLY_MODIFIED_SEARCH_FILTER) { } else if (searchType == SearchType.RECENTLY_MODIFIED_SEARCH_FILTER) {
setMessageForEmptyList(R.string.file_list_empty_headline_server_search, setMessageForEmptyList(R.string.file_list_empty_headline_server_search,
R.string.file_list_empty_recently_modified_filter, R.drawable.ic_list_empty_recent); R.string.file_list_empty_recently_modified_filter,
R.drawable.ic_list_empty_recent);
} else if (searchType == SearchType.RECENTLY_ADDED_SEARCH_FILTER) { } else if (searchType == SearchType.RECENTLY_ADDED_SEARCH_FILTER) {
setMessageForEmptyList(R.string.file_list_empty_headline_server_search, setMessageForEmptyList(R.string.file_list_empty_headline_server_search,
R.string.file_list_empty_recently_added_filter, R.drawable.ic_list_empty_recent); R.string.file_list_empty_recently_added_filter,
R.drawable.ic_list_empty_recent);
} else if (searchType == SearchType.SHARED_FILTER) { } else if (searchType == SearchType.SHARED_FILTER) {
setMessageForEmptyList(R.string.file_list_empty_shared_headline, setMessageForEmptyList(R.string.file_list_empty_shared_headline,
R.string.file_list_empty_shared, R.drawable.ic_list_empty_shared); R.string.file_list_empty_shared,
R.drawable.ic_list_empty_shared);
} }
} }
}); });