rewriting search, use the new top menu

This commit is contained in:
tibbi 2023-01-04 16:25:56 +01:00
parent 1d6ce0eff2
commit 696920fdf0
4 changed files with 46 additions and 104 deletions

View File

@ -62,7 +62,7 @@ android {
} }
dependencies { dependencies {
implementation 'com.github.SimpleMobileTools:Simple-Commons:48be8434c3' implementation 'com.github.SimpleMobileTools:Simple-Commons:d46d16ced5'
implementation 'com.github.tibbi:IndicatorFastScroll:4524cd0b61' implementation 'com.github.tibbi:IndicatorFastScroll:4524cd0b61'
implementation 'me.grantland:autofittextview:0.2.1' implementation 'me.grantland:autofittextview:0.2.1'
} }

View File

@ -2,8 +2,6 @@ package com.simplemobiletools.dialer.activities
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.app.SearchManager
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.ShortcutInfo import android.content.pm.ShortcutInfo
import android.content.res.Configuration import android.content.res.Configuration
@ -13,13 +11,9 @@ import android.graphics.drawable.LayerDrawable
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.provider.Settings import android.provider.Settings
import android.view.Menu
import android.view.MenuItem
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.widget.SearchView
import androidx.core.view.MenuItemCompat
import androidx.viewpager.widget.ViewPager import androidx.viewpager.widget.ViewPager
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.ConfirmationDialog
@ -45,11 +39,8 @@ import kotlinx.android.synthetic.main.fragment_recents.*
import me.grantland.widget.AutofitHelper import me.grantland.widget.AutofitHelper
class MainActivity : SimpleActivity() { class MainActivity : SimpleActivity() {
private var isSearchOpen = false
private var launchedDialer = false private var launchedDialer = false
private var mSearchMenuItem: MenuItem? = null
private var storedShowTabs = 0 private var storedShowTabs = 0
private var searchQuery = ""
var cachedContacts = ArrayList<SimpleContact>() var cachedContacts = ArrayList<SimpleContact>()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -59,7 +50,7 @@ class MainActivity : SimpleActivity() {
appLaunched(BuildConfig.APPLICATION_ID) appLaunched(BuildConfig.APPLICATION_ID)
setupOptionsMenu() setupOptionsMenu()
refreshMenuItems() refreshMenuItems()
updateMaterialActivityViews(main_coordinator, main_holder, useTransparentNavigation = false, useTopSearchMenu = false) updateMaterialActivityViews(main_coordinator, main_holder, useTransparentNavigation = false, useTopSearchMenu = true)
launchedDialer = savedInstanceState?.getBoolean(OPEN_DIAL_PAD_AT_LAUNCH) ?: false launchedDialer = savedInstanceState?.getBoolean(OPEN_DIAL_PAD_AT_LAUNCH) ?: false
@ -103,17 +94,11 @@ class MainActivity : SimpleActivity() {
return return
} }
val statusBarColor = if (getCurrentFragment()?.getScrollingView() == null) { updateMenuColors()
getProperBackgroundColor()
} else {
window.statusBarColor
}
val properPrimaryColor = getProperPrimaryColor() val properPrimaryColor = getProperPrimaryColor()
val dialpadIcon = resources.getColoredDrawableWithColor(R.drawable.ic_dialpad_vector, properPrimaryColor.getContrastColor()) val dialpadIcon = resources.getColoredDrawableWithColor(R.drawable.ic_dialpad_vector, properPrimaryColor.getContrastColor())
main_dialpad_button.setImageDrawable(dialpadIcon) main_dialpad_button.setImageDrawable(dialpadIcon)
setupToolbar(main_toolbar, statusBarColor = statusBarColor, searchMenuItem = mSearchMenuItem)
updateTextColors(main_holder) updateTextColors(main_holder)
setupTabColors() setupTabColors()
@ -121,7 +106,7 @@ class MainActivity : SimpleActivity() {
it?.setupColors(getProperTextColor(), getProperPrimaryColor(), getProperPrimaryColor()) it?.setupColors(getProperTextColor(), getProperPrimaryColor(), getProperPrimaryColor())
} }
if (!isSearchOpen) { if (!main_menu.isSearchOpen) {
refreshItems(true) refreshItems(true)
} }
@ -159,8 +144,8 @@ class MainActivity : SimpleActivity() {
} }
override fun onBackPressed() { override fun onBackPressed() {
if (isSearchOpen && mSearchMenuItem != null) { if (main_menu.isSearchOpen) {
mSearchMenuItem!!.collapseActionView() main_menu.closeSearch()
} else { } else {
super.onBackPressed() super.onBackPressed()
} }
@ -168,7 +153,7 @@ class MainActivity : SimpleActivity() {
private fun refreshMenuItems() { private fun refreshMenuItems() {
val currentFragment = getCurrentFragment() val currentFragment = getCurrentFragment()
main_toolbar.menu.apply { main_menu.getToolbar().menu.apply {
findItem(R.id.clear_call_history).isVisible = currentFragment == recents_fragment findItem(R.id.clear_call_history).isVisible = currentFragment == recents_fragment
findItem(R.id.sort).isVisible = currentFragment != recents_fragment findItem(R.id.sort).isVisible = currentFragment != recents_fragment
findItem(R.id.create_new_contact).isVisible = currentFragment == contacts_fragment findItem(R.id.create_new_contact).isVisible = currentFragment == contacts_fragment
@ -177,8 +162,25 @@ class MainActivity : SimpleActivity() {
} }
private fun setupOptionsMenu() { private fun setupOptionsMenu() {
setupSearch(main_toolbar.menu) main_menu.getToolbar().inflateMenu(R.menu.menu)
main_toolbar.setOnMenuItemClickListener { menuItem -> main_menu.toggleHideOnScroll(false)
main_menu.setupMenu()
main_menu.onSearchOpenListener = {
main_dialpad_button.beGone()
}
main_menu.onSearchClosedListener = {
main_dialpad_button.beVisible()
getAllFragments().forEach {
it?.onSearchQueryChanged("")
}
}
main_menu.onSearchTextChangedListener = { text ->
getCurrentFragment()?.onSearchQueryChanged(text)
}
main_menu.getToolbar().setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) { when (menuItem.itemId) {
R.id.clear_call_history -> clearCallHistory() R.id.clear_call_history -> clearCallHistory()
R.id.create_new_contact -> launchCreateNewContactIntent() R.id.create_new_contact -> launchCreateNewContactIntent()
@ -192,51 +194,17 @@ class MainActivity : SimpleActivity() {
} }
} }
private fun updateMenuColors() {
updateStatusbarColor(getProperBackgroundColor())
main_menu.updateColors()
}
private fun checkContactPermissions() { private fun checkContactPermissions() {
handlePermission(PERMISSION_READ_CONTACTS) { handlePermission(PERMISSION_READ_CONTACTS) {
initFragments() initFragments()
} }
} }
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
queryHint = getString(R.string.search)
setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String) = false
override fun onQueryTextChange(newText: String): Boolean {
if (isSearchOpen) {
searchQuery = newText
getCurrentFragment()?.onSearchQueryChanged(newText)
}
return true
}
})
}
MenuItemCompat.setOnActionExpandListener(mSearchMenuItem, object : MenuItemCompat.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
isSearchOpen = true
main_dialpad_button.beGone()
return true
}
override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
if (isSearchOpen) {
getCurrentFragment()?.onSearchClosed()
}
isSearchOpen = false
main_dialpad_button.beVisible()
return true
}
})
}
private fun clearCallHistory() { private fun clearCallHistory() {
val confirmationText = "${getString(R.string.remove_confirmation)}\n\n${getString(R.string.cannot_be_undone)}" val confirmationText = "${getString(R.string.remove_confirmation)}\n\n${getString(R.string.cannot_be_undone)}"
ConfirmationDialog(this, confirmationText) { ConfirmationDialog(this, confirmationText) {
@ -279,11 +247,6 @@ class MainActivity : SimpleActivity() {
.build() .build()
} }
private fun updateStatusBarChanger() {
setupMaterialScrollListener(getCurrentFragment()?.getScrollingView(), main_toolbar)
updateStatusBarOnPageChange()
}
private fun setupTabColors() { private fun setupTabColors() {
val activeView = main_tabs_holder.getTabAt(view_pager.currentItem)?.customView val activeView = main_tabs_holder.getTabAt(view_pager.currentItem)?.customView
updateBottomTabItemColors(activeView, true, getSelectedTabDrawableIds()[view_pager.currentItem]) updateBottomTabItemColors(activeView, true, getSelectedTabDrawableIds()[view_pager.currentItem])
@ -351,7 +314,6 @@ class MainActivity : SimpleActivity() {
it?.finishActMode() it?.finishActMode()
} }
refreshMenuItems() refreshMenuItems()
updateStatusBarChanger()
} }
}) })
@ -407,16 +369,13 @@ class MainActivity : SimpleActivity() {
updateBottomTabItemColors(it.customView, false, getDeselectedTabDrawableIds()[it.position]) updateBottomTabItemColors(it.customView, false, getDeselectedTabDrawableIds()[it.position])
}, },
tabSelectedAction = { tabSelectedAction = {
closeSearch() main_menu.closeSearch()
view_pager.currentItem = it.position view_pager.currentItem = it.position
updateBottomTabItemColors(it.customView, true, getSelectedTabDrawableIds()[it.position]) updateBottomTabItemColors(it.customView, true, getSelectedTabDrawableIds()[it.position])
} }
) )
main_tabs_holder.beGoneIf(main_tabs_holder.tabCount == 1) main_tabs_holder.beGoneIf(main_tabs_holder.tabCount == 1)
main_tabs_holder.onGlobalLayout {
updateStatusBarChanger()
}
storedShowTabs = config.showTabs storedShowTabs = config.showTabs
} }
@ -529,13 +488,11 @@ class MainActivity : SimpleActivity() {
} }
private fun launchSettings() { private fun launchSettings() {
closeSearch()
hideKeyboard() hideKeyboard()
startActivity(Intent(applicationContext, SettingsActivity::class.java)) startActivity(Intent(applicationContext, SettingsActivity::class.java))
} }
private fun launchAbout() { private fun launchAbout() {
closeSearch()
val licenses = LICENSE_GLIDE or LICENSE_INDICATOR_FAST_SCROLL or LICENSE_AUTOFITTEXTVIEW val licenses = LICENSE_GLIDE or LICENSE_INDICATOR_FAST_SCROLL or LICENSE_AUTOFITTEXTVIEW
val faqItems = arrayListOf( val faqItems = arrayListOf(
@ -554,28 +511,19 @@ class MainActivity : SimpleActivity() {
private fun showSortingDialog(showCustomSorting: Boolean) { private fun showSortingDialog(showCustomSorting: Boolean) {
ChangeSortingDialog(this, showCustomSorting) { ChangeSortingDialog(this, showCustomSorting) {
favorites_fragment?.refreshItems { favorites_fragment?.refreshItems {
if (isSearchOpen) { if (main_menu.isSearchOpen) {
getCurrentFragment()?.onSearchQueryChanged(searchQuery) getCurrentFragment()?.onSearchQueryChanged(main_menu.getCurrentQuery())
} }
} }
contacts_fragment?.refreshItems { contacts_fragment?.refreshItems {
if (isSearchOpen) { if (main_menu.isSearchOpen) {
getCurrentFragment()?.onSearchQueryChanged(searchQuery) getCurrentFragment()?.onSearchQueryChanged(main_menu.getCurrentQuery())
} }
} }
} }
} }
private fun closeSearch() {
if (isSearchOpen) {
getAllFragments().forEach {
it?.onSearchQueryChanged("")
}
mSearchMenuItem?.collapseActionView()
}
}
fun cacheContacts(contacts: List<SimpleContact>) { fun cacheContacts(contacts: List<SimpleContact>) {
try { try {
cachedContacts.clear() cachedContacts.clear()

View File

@ -5,13 +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.MaterialToolbar <com.simplemobiletools.commons.views.MySearchMenu
android:id="@+id/main_toolbar" android:id="@+id/main_menu"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" android:layout_height="wrap_content" />
android:background="@color/color_primary"
app:menu="@menu/menu"
app:titleTextAppearance="@style/AppTheme.ActionBar.TitleTextStyle" />
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/main_holder" android:id="@+id/main_holder"

View File

@ -1,32 +1,29 @@
<?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 xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/search" tools:ignore="AlwaysShowAction,AppCompatResource">
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/sort" android:id="@+id/sort"
android:icon="@drawable/ic_sort_vector" android:icon="@drawable/ic_sort_vector"
android:showAsAction="always"
android:title="@string/sort_by" android:title="@string/sort_by"
app:showAsAction="always" /> app:showAsAction="always" />
<item <item
android:id="@+id/create_new_contact" android:id="@+id/create_new_contact"
android:icon="@drawable/ic_plus_vector" android:icon="@drawable/ic_plus_vector"
android:title="@string/create_new_contact" android:title="@string/create_new_contact"
app:showAsAction="ifRoom" /> app:showAsAction="always" />
<item <item
android:id="@+id/clear_call_history" android:id="@+id/clear_call_history"
android:icon="@drawable/ic_delete_vector" android:icon="@drawable/ic_delete_vector"
android:title="@string/clear_call_history" android:title="@string/clear_call_history"
app:showAsAction="ifRoom" /> app:showAsAction="always" />
<item <item
android:id="@+id/more_apps_from_us" android:id="@+id/more_apps_from_us"
android:icon="@drawable/ic_google_play_vector" android:icon="@drawable/ic_google_play_vector"
android:title="@string/more_apps_from_us" android:title="@string/more_apps_from_us"
app:showAsAction="always" /> app:showAsAction="ifRoom" />
<item <item
android:id="@+id/settings" android:id="@+id/settings"
android:icon="@drawable/ic_settings_cog_vector" android:icon="@drawable/ic_settings_cog_vector"