From 38ca5b84d794e32eadf45ed86a405d34d710149f Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 7 Jan 2018 17:48:25 +0100 Subject: [PATCH] add basic search to media view, related to #48 --- app/src/main/AndroidManifest.xml | 12 +++++- .../gallery/activities/MediaActivity.kt | 38 +++++++++++++++++++ app/src/main/res/menu/menu_media.xml | 14 +++++-- app/src/main/res/xml/searchable.xml | 5 +++ 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 app/src/main/res/xml/searchable.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4c50f0162..c49d7cf4d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -55,7 +55,17 @@ + android:parentActivityName=".activities.MainActivity"> + + + + + + + + () @@ -130,6 +135,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } } + override fun onStop() { + super.onStop() + MenuItemCompat.collapseActionView(mSearchMenuItem) + } + override fun onDestroy() { super.onDestroy() if (config.showAll) @@ -150,6 +160,33 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } } + private fun setupSearch(menu: Menu) { + val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager + mSearchMenuItem = menu.findItem(R.id.search) + (mSearchMenuItem!!.actionView as SearchView).apply { + setSearchableInfo(searchManager.getSearchableInfo(componentName)) + isSubmitButtonEnabled = false + setOnQueryTextListener(object : SearchView.OnQueryTextListener { + override fun onQueryTextSubmit(query: String) = false + + override fun onQueryTextChange(newText: String): Boolean { + searchQueryChanged(newText) + return true + } + }) + } + } + + private fun searchQueryChanged(text: String) { + Thread { + val filtered = mMedia.filter { it.name.contains(text, true) } as ArrayList + filtered.sortBy { !it.name.startsWith(text, true) } + runOnUiThread { + (media_grid.adapter as? MediaAdapter)?.updateMedia(filtered) + } + }.start() + } + private fun tryloadGallery() { handlePermission(PERMISSION_WRITE_STORAGE) { if (it) { @@ -260,6 +297,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { findItem(R.id.toggle_filename).isVisible = config.viewTypeFiles == VIEW_TYPE_GRID } + setupSearch(menu) return true } diff --git a/app/src/main/res/menu/menu_media.xml b/app/src/main/res/menu/menu_media.xml index 978c684c0..de3b01776 100644 --- a/app/src/main/res/menu/menu_media.xml +++ b/app/src/main/res/menu/menu_media.xml @@ -2,15 +2,21 @@ + android:id="@+id/search" + android:icon="@drawable/ic_search" + android:title="@string/search" + app:actionViewClass="android.support.v7.widget.SearchView" + app:showAsAction="collapseActionView|ifRoom"/> + +