adding new Search at SearchActivity

This commit is contained in:
tibbi 2023-01-10 18:15:30 +01:00
parent 5256013d8a
commit 793bd46d30
4 changed files with 33 additions and 67 deletions

View file

@ -495,6 +495,10 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
Intent(this, SearchActivity::class.java).apply { Intent(this, SearchActivity::class.java).apply {
startActivity(this) startActivity(this)
} }
main_menu.postDelayed({
main_menu.closeSearch()
}, 500)
} }
private fun showSortingDialog() { private fun showSortingDialog() {

View file

@ -1,19 +1,12 @@
package com.simplemobiletools.gallery.pro.activities package com.simplemobiletools.gallery.pro.activities
import android.app.SearchManager
import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.RelativeLayout import android.widget.RelativeLayout
import androidx.appcompat.widget.SearchView
import androidx.core.view.MenuItemCompat
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.NavigationIcon
import com.simplemobiletools.commons.helpers.VIEW_TYPE_GRID import com.simplemobiletools.commons.helpers.VIEW_TYPE_GRID
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.models.FileDirItem
@ -33,17 +26,17 @@ import kotlinx.android.synthetic.main.activity_search.*
import java.io.File import java.io.File
class SearchActivity : SimpleActivity(), MediaOperationsListener { class SearchActivity : SimpleActivity(), MediaOperationsListener {
private var mIsSearchOpen = false
private var mLastSearchedText = "" private var mLastSearchedText = ""
private var mSearchMenuItem: MenuItem? = null
private var mCurrAsyncTask: GetMediaAsynctask? = null private var mCurrAsyncTask: GetMediaAsynctask? = null
private var mAllMedia = ArrayList<ThumbnailItem>() private var mAllMedia = ArrayList<ThumbnailItem>()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
isMaterialActivity = true
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search) setContentView(R.layout.activity_search)
setupOptionsMenu() setupOptionsMenu()
updateMaterialActivityViews(search_coordinator, search_grid, useTransparentNavigation = true, useTopSearchMenu = true)
search_empty_text_placeholder.setTextColor(getProperTextColor()) search_empty_text_placeholder.setTextColor(getProperTextColor())
getAllMedia() getAllMedia()
search_fastscroller.updateColors(getProperPrimaryColor()) search_fastscroller.updateColors(getProperPrimaryColor())
@ -51,7 +44,7 @@ class SearchActivity : SimpleActivity(), MediaOperationsListener {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
setupToolbar(search_toolbar, NavigationIcon.Arrow, searchMenuItem = mSearchMenuItem) updateMenuColors()
} }
override fun onDestroy() { override fun onDestroy() {
@ -60,8 +53,26 @@ class SearchActivity : SimpleActivity(), MediaOperationsListener {
} }
private fun setupOptionsMenu() { private fun setupOptionsMenu() {
setupSearch(search_toolbar.menu) search_menu.getToolbar().inflateMenu(R.menu.menu_search)
search_toolbar.setOnMenuItemClickListener { menuItem -> search_menu.toggleHideOnScroll(true)
search_menu.setupMenu()
search_menu.toggleForceArrowBackIcon(true)
search_menu.requestFocus()
search_menu.onNavigateBackClickListener = {
if (search_menu.getCurrentQuery().isEmpty()) {
finish()
} else {
search_menu.closeSearch()
}
}
search_menu.onSearchTextChangedListener = { text ->
mLastSearchedText = text
textChanged(text)
}
search_menu.getToolbar().setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) { when (menuItem.itemId) {
R.id.toggle_filename -> toggleFilenameVisibility() R.id.toggle_filename -> toggleFilenameVisibility()
else -> return@setOnMenuItemClickListener false else -> return@setOnMenuItemClickListener false
@ -70,41 +81,9 @@ class SearchActivity : SimpleActivity(), MediaOperationsListener {
} }
} }
private fun setupSearch(menu: Menu) { private fun updateMenuColors() {
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager updateStatusbarColor(getProperBackgroundColor())
mSearchMenuItem = menu.findItem(R.id.search) search_menu.updateColors()
MenuItemCompat.setOnActionExpandListener(mSearchMenuItem, object : MenuItemCompat.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
mIsSearchOpen = true
return true
}
// this triggers on device rotation too, avoid doing anything
override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
if (mIsSearchOpen) {
mIsSearchOpen = false
mLastSearchedText = ""
}
return true
}
})
mSearchMenuItem?.expandActionView()
(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 {
if (mIsSearchOpen) {
mLastSearchedText = newText
textChanged(newText)
}
return true
}
})
}
} }
private fun textChanged(text: String) { private fun textChanged(text: String) {

View file

@ -5,21 +5,10 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout <com.simplemobiletools.commons.views.MySearchMenu
android:id="@+id/search_app_bar_layout" android:id="@+id/search_menu"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content" />
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/search_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/color_primary"
app:menu="@menu/menu_search"
app:title="@string/search"
app:titleTextAppearance="@style/AppTheme.ActionBar.TitleTextStyle" />
</com.google.android.material.appbar.AppBarLayout>
<RelativeLayout <RelativeLayout
android:id="@+id/search_holder" android:id="@+id/search_holder"

View file

@ -1,12 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/ic_search_vector"
android:title="@string/search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="collapseActionView|always" />
<item <item
android:id="@+id/toggle_filename" android:id="@+id/toggle_filename"
android:icon="@drawable/ic_label_vector" android:icon="@drawable/ic_label_vector"