adding initial search, related to #34

This commit is contained in:
tibbi 2018-01-07 23:51:33 +01:00
parent bef2f89b79
commit e41d5de4aa
5 changed files with 95 additions and 4 deletions

View file

@ -25,6 +25,15 @@
</activity>
<activity android:name=".activities.MainActivity">
<meta-data
android:name="android.app.default_searchable"
android:resource="@xml/searchable"/>
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT"/>
<data android:mimeType="*/*"/>

View file

@ -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<Release>().apply {
add(Release(26, R.string.release_26))

View file

@ -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<FileDirItem>()
private var scrollStates = HashMap<String, Parcelable>()
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<FileDirItem>) {
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<FileDirItem>) -> 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) {

View file

@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/ic_search"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"/>
<item
android:id="@+id/go_home"
android:icon="@drawable/ic_home"

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_folder"
android:label="@string/app_name"/>