From b85dd7d7d2d0397a6570ed90bd31c462c5ca0dd9 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 4 Jan 2023 20:32:46 +0100 Subject: [PATCH] use the new redesigned search menu --- .../activities/InsertOrEditContactActivity.kt | 1 - .../contacts/pro/activities/MainActivity.kt | 157 +++++++----------- .../pro/fragments/MyViewPagerFragment.kt | 5 - app/src/main/res/layout/activity_main.xml | 17 +- app/src/main/res/menu/menu.xml | 6 - 5 files changed, 61 insertions(+), 125 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/InsertOrEditContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/InsertOrEditContactActivity.kt index 62745d15..f87123ab 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/InsertOrEditContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/InsertOrEditContactActivity.kt @@ -191,7 +191,6 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener { MenuItemCompat.setOnActionExpandListener(searchMenuItem, object : MenuItemCompat.OnActionExpandListener { override fun onMenuItemActionExpand(item: MenuItem?): Boolean { - getCurrentFragment()?.onSearchOpened() isSearchOpen = true return true } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt index ad124878..9004fae2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt @@ -2,9 +2,7 @@ package com.simplemobiletools.contacts.pro.activities import android.annotation.SuppressLint import android.app.Activity -import android.app.SearchManager import android.content.ActivityNotFoundException -import android.content.Context import android.content.Intent import android.content.pm.ShortcutInfo import android.graphics.drawable.ColorDrawable @@ -12,13 +10,9 @@ import android.graphics.drawable.Icon import android.graphics.drawable.LayerDrawable import android.net.Uri import android.os.Bundle -import android.view.Menu -import android.view.MenuItem import android.widget.ImageView import android.widget.TextView import android.widget.Toast -import androidx.appcompat.widget.SearchView -import androidx.core.view.MenuItemCompat import androidx.viewpager.widget.ViewPager import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.extensions.* @@ -57,9 +51,6 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { private val PICK_IMPORT_SOURCE_INTENT = 1 private val PICK_EXPORT_FILE_INTENT = 2 - private var isSearchOpen = false - private var mSearchMenuItem: MenuItem? = null - private var searchQuery = "" private var werePermissionsHandled = false private var isFirstResume = true private var isGettingContacts = false @@ -77,6 +68,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { appLaunched(BuildConfig.APPLICATION_ID) setupOptionsMenu() refreshMenuItems() + updateMaterialActivityViews(main_coordinator, main_holder, useTransparentNavigation = false, useTopSearchMenu = true) storeStateVariables() setupTabs() checkContactPermissions() @@ -126,8 +118,8 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { it?.setupColors(getProperTextColor(), properPrimaryColor) } + updateMenuColors() setupTabColors() - setupToolbar(main_toolbar, searchMenuItem = mSearchMenuItem) val configStartNameWithSurname = config.startNameWithSurname if (storedStartNameWithSurname != configStartNameWithSurname) { @@ -174,34 +166,6 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { } } - private fun refreshMenuItems() { - val currentFragment = getCurrentFragment() - main_toolbar.menu.apply { - findItem(R.id.sort).isVisible = currentFragment != groups_fragment - findItem(R.id.filter).isVisible = currentFragment != groups_fragment - findItem(R.id.dialpad).isVisible = !config.showDialpadButton - findItem(R.id.more_apps_from_us).isVisible = !resources.getBoolean(R.bool.hide_google_relations) - } - } - - private fun setupOptionsMenu() { - setupSearch(main_toolbar.menu) - main_toolbar.setOnMenuItemClickListener { menuItem -> - when (menuItem.itemId) { - R.id.sort -> showSortingDialog(showCustomSorting = getCurrentFragment() is FavoritesFragment) - R.id.filter -> showFilterDialog() - R.id.dialpad -> launchDialpad() - R.id.import_contacts -> tryImportContacts() - R.id.export_contacts -> tryExportContacts() - R.id.more_apps_from_us -> launchMoreAppsFromUsIntent() - R.id.settings -> launchSettings() - R.id.about -> launchAbout() - else -> return@setOnMenuItemClickListener false - } - return@setOnMenuItemClickListener true - } - } - override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { super.onActivityResult(requestCode, resultCode, resultData) if (requestCode == PICK_IMPORT_SOURCE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) { @@ -217,13 +181,64 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { } override fun onBackPressed() { - if (isSearchOpen && mSearchMenuItem != null) { - mSearchMenuItem!!.collapseActionView() + if (main_menu.isSearchOpen) { + main_menu.closeSearch() } else { super.onBackPressed() } } + private fun refreshMenuItems() { + val currentFragment = getCurrentFragment() + main_menu.getToolbar().menu.apply { + findItem(R.id.sort).isVisible = currentFragment != groups_fragment + findItem(R.id.filter).isVisible = currentFragment != groups_fragment + findItem(R.id.dialpad).isVisible = !config.showDialpadButton + findItem(R.id.more_apps_from_us).isVisible = !resources.getBoolean(R.bool.hide_google_relations) + } + } + + private fun setupOptionsMenu() { + main_menu.getToolbar().inflateMenu(R.menu.menu) + main_menu.toggleHideOnScroll(false) + main_menu.setupMenu() + + main_menu.onSearchOpenListener = { + main_dialpad_button.beGone() + } + + main_menu.onSearchClosedListener = { + getAllFragments().forEach { + it?.onSearchClosed() + } + main_dialpad_button.beVisibleIf(config.showDialpadButton) + } + + main_menu.onSearchTextChangedListener = { text -> + getCurrentFragment()?.onSearchQueryChanged(text) + } + + main_menu.getToolbar().setOnMenuItemClickListener { menuItem -> + when (menuItem.itemId) { + R.id.sort -> showSortingDialog(showCustomSorting = getCurrentFragment() is FavoritesFragment) + R.id.filter -> showFilterDialog() + R.id.dialpad -> launchDialpad() + R.id.import_contacts -> tryImportContacts() + R.id.export_contacts -> tryExportContacts() + R.id.more_apps_from_us -> launchMoreAppsFromUsIntent() + R.id.settings -> launchSettings() + R.id.about -> launchAbout() + else -> return@setOnMenuItemClickListener false + } + return@setOnMenuItemClickListener true + } + } + + private fun updateMenuColors() { + updateStatusbarColor(getProperBackgroundColor()) + main_menu.updateColors() + } + private fun storeStateVariables() { config.apply { storedShowContactThumbnails = showContactThumbnails @@ -234,51 +249,6 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { } } - 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(getSearchString()) - 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 { - getCurrentFragment()?.onSearchOpened() - isSearchOpen = true - main_dialpad_button.beGone() - return true - } - - override fun onMenuItemActionCollapse(item: MenuItem?): Boolean { - getCurrentFragment()?.onSearchClosed() - isSearchOpen = false - main_dialpad_button.beVisibleIf(config.showDialpadButton) - return true - } - }) - } - - private fun getSearchString(): Int { - return when (getCurrentFragment()) { - favorites_fragment -> R.string.search_favorites - groups_fragment -> R.string.search_groups - else -> R.string.search_contacts - } - } - @SuppressLint("NewApi") private fun checkShortcuts() { val appIconColor = config.appIconColor @@ -393,7 +363,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { updateBottomTabItemColors(it.customView, false) }, tabSelectedAction = { - closeSearch() + main_menu.closeSearch() view_pager.currentItem = it.position updateBottomTabItemColors(it.customView, true) } @@ -541,13 +511,11 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { } private fun launchSettings() { - closeSearch() hideKeyboard() startActivity(Intent(applicationContext, SettingsActivity::class.java)) } private fun launchAbout() { - closeSearch() val licenses = LICENSE_JODA or LICENSE_GLIDE or LICENSE_GSON or LICENSE_INDICATOR_FAST_SCROLL or LICENSE_AUTOFITTEXTVIEW val faqItems = arrayListOf( @@ -599,8 +567,8 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { groups_fragment?.refreshContacts(contacts) } - if (isSearchOpen) { - getCurrentFragment()?.onSearchQueryChanged(searchQuery) + if (main_menu.isSearchOpen) { + getCurrentFragment()?.onSearchQueryChanged(main_menu.getCurrentQuery()) } } } @@ -639,15 +607,6 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { } } - private fun closeSearch() { - if (isSearchOpen) { - getAllFragments().forEach { - it?.onSearchQueryChanged("") - } - mSearchMenuItem?.collapseActionView() - } - } - private fun checkWhatsNewDialog() { arrayListOf().apply { add(Release(10, R.string.release_10)) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt index 6bfb80a1..0fcd0019 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt @@ -391,11 +391,6 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) } } - fun onSearchOpened() { - contactsIgnoringSearch = (fragment_list?.adapter as? ContactsAdapter)?.contactItems ?: ArrayList() - groupsIgnoringSearch = (fragment_list?.adapter as? GroupsAdapter)?.groups ?: ArrayList() - } - fun onSearchClosed() { if (fragment_list.adapter is ContactsAdapter) { (fragment_list.adapter as? ContactsAdapter)?.updateItems(contactsIgnoringSearch) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index a4073ef8..58d6a76c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -5,21 +5,10 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - - - - - + android:layout_height="wrap_content" /> -