From e41d5de4aa4417a4e133c7cf27b62dacf0087594 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 7 Jan 2018 23:51:33 +0100 Subject: [PATCH] adding initial search, related to #34 --- app/src/main/AndroidManifest.xml | 9 ++++ .../filemanager/activities/MainActivity.kt | 52 +++++++++++++++++-- .../filemanager/fragments/ItemsFragment.kt | 27 +++++++++- app/src/main/res/menu/menu.xml | 6 +++ app/src/main/res/xml/searchable.xml | 5 ++ 5 files changed, 95 insertions(+), 4 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 7eaf67be..485c0473 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -25,6 +25,15 @@ + + + + + + + diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt index 7a57b663..7e8211d5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt @@ -1,11 +1,15 @@ package com.simplemobiletools.filemanager.activities import android.app.Activity +import android.app.SearchManager import android.content.ClipData +import android.content.Context import android.content.Intent import android.media.RingtoneManager import android.os.Bundle import android.os.Handler +import android.support.v4.view.MenuItemCompat +import android.support.v7.widget.SearchView import android.view.Menu import android.view.MenuItem import com.simplemobiletools.commons.dialogs.RadioGroupDialog @@ -26,9 +30,13 @@ import java.io.File import java.util.* class MainActivity : SimpleActivity() { + var isSearchOpen = false + private val BACK_PRESS_TIMEOUT = 5000 private var wasBackJustPressed = false - private var mStoredUseEnglish = false + private var searchMenuItem: MenuItem? = null + + private var storedUseEnglish = false private lateinit var fragment: ItemsFragment @@ -54,7 +62,7 @@ class MainActivity : SimpleActivity() { override fun onResume() { super.onResume() - if (mStoredUseEnglish != config.useEnglish) { + if (storedUseEnglish != config.useEnglish) { restartActivity() return } @@ -85,11 +93,45 @@ class MainActivity : SimpleActivity() { findItem(R.id.stop_showing_hidden).isVisible = config.temporarilyShowHidden } + setupSearch(menu) return true } private fun storeStateVariables() { - mStoredUseEnglish = config.useEnglish + storedUseEnglish = config.useEnglish + } + + private fun setupSearch(menu: Menu) { + val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager + searchMenuItem = menu.findItem(R.id.search) + (searchMenuItem!!.actionView as SearchView).apply { + setSearchableInfo(searchManager.getSearchableInfo(componentName)) + isSubmitButtonEnabled = false + queryHint = getString(R.string.search_folder) + setOnQueryTextListener(object : SearchView.OnQueryTextListener { + override fun onQueryTextSubmit(query: String) = false + + override fun onQueryTextChange(newText: String): Boolean { + if (isSearchOpen) { + fragment.searchQueryChanged(newText) + } + return true + } + }) + } + + MenuItemCompat.setOnActionExpandListener(searchMenuItem, object : MenuItemCompat.OnActionExpandListener { + override fun onMenuItemActionExpand(item: MenuItem?): Boolean { + isSearchOpen = true + return true + } + + override fun onMenuItemActionCollapse(item: MenuItem?): Boolean { + isSearchOpen = false + fragment.searchClosed() + return true + } + }) } private fun tryInitFileManager() { @@ -275,6 +317,10 @@ class MainActivity : SimpleActivity() { finish() } + fun openedDirectory() { + MenuItemCompat.collapseActionView(searchMenuItem) + } + private fun checkWhatsNewDialog() { arrayListOf().apply { add(Release(26, R.string.release_26)) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt index 9378407c..a483751b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt @@ -36,11 +36,13 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener, Breadcrum var isGetRingtonePicker = false var isPickMultipleIntent = false - private var storedTextColor = 0 private var showHidden = false + private var skipItemUpdating = false private var storedItems = ArrayList() private var scrollStates = HashMap() + private var storedTextColor = 0 + private lateinit var mView: View override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { @@ -127,6 +129,7 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener, Breadcrum } private fun addItems(items: ArrayList) { + skipItemUpdating = false mView.apply { activity?.runOnUiThread { items_swipe_refresh?.isRefreshing = false @@ -168,6 +171,7 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener, Breadcrum private fun getRecyclerLayoutManager() = (mView.items_list.layoutManager as LinearLayoutManager) private fun getItems(path: String, callback: (items: ArrayList) -> Unit) { + skipItemUpdating = false Thread { if (activity?.isActivityDestroyed() == false) { if (!context!!.config.enableRootAccess || !context!!.isPathOnRoot(path)) { @@ -227,6 +231,10 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener, Breadcrum private fun itemClicked(item: FileDirItem) { if (item.isDirectory) { + (activity as? MainActivity)?.apply { + skipItemUpdating = isSearchOpen + openedDirectory() + } openPath(item.path) } else { val path = item.path @@ -245,6 +253,23 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener, Breadcrum } } + fun searchQueryChanged(text: String) { + Thread { + val filtered = storedItems.filter { it.name.contains(text, true) } as ArrayList + filtered.sortBy { !it.name.startsWith(text, true) } + activity?.runOnUiThread { + (items_list.adapter as ItemsAdapter).updateItems(filtered) + } + }.start() + } + + fun searchClosed() { + if (!skipItemUpdating) { + (items_list.adapter as ItemsAdapter).updateItems(storedItems) + } + skipItemUpdating = false + } + private fun createNewItem() { CreateNewItemDialog(activity as SimpleActivity, currentPath) { if (it) { diff --git a/app/src/main/res/menu/menu.xml b/app/src/main/res/menu/menu.xml index 76e618f0..f6fee237 100644 --- a/app/src/main/res/menu/menu.xml +++ b/app/src/main/res/menu/menu.xml @@ -1,6 +1,12 @@ + +