From 5e0386ee0e08b5a853be50411dea140682732147 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 14 Aug 2019 17:57:13 +0200 Subject: [PATCH] fix #421, allow searching groups --- .../contacts/pro/activities/MainActivity.kt | 11 ++++- .../pro/fragments/MyViewPagerFragment.kt | 45 ++++++++++++++----- 2 files changed, 42 insertions(+), 14 deletions(-) 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 b07d0dbc..4e4392d8 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 @@ -183,7 +183,6 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { val currentFragment = getCurrentFragment() menu.apply { - findItem(R.id.search).isVisible = currentFragment != groups_fragment findItem(R.id.sort).isVisible = currentFragment != groups_fragment findItem(R.id.filter).isVisible = currentFragment != groups_fragment setupSearch(this) @@ -225,7 +224,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { (searchMenuItem!!.actionView as SearchView).apply { setSearchableInfo(searchManager.getSearchableInfo(componentName)) isSubmitButtonEnabled = false - queryHint = getString(if (getCurrentFragment() == contacts_fragment) R.string.search_contacts else R.string.search_favorites) + queryHint = getString(getSearchString()) setOnQueryTextListener(object : SearchView.OnQueryTextListener { override fun onQueryTextSubmit(query: String) = false @@ -253,6 +252,14 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { }) } + 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 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 27446be4..a9df7e47 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 @@ -29,6 +29,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) private var lastHashCode = 0 private var contactsIgnoringSearch = ArrayList() + private var groupsIgnoringSearch = ArrayList() private lateinit var config: Config var skipHashComparing = false @@ -130,6 +131,8 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) if (this is ContactsFragment || this is FavoritesFragment) { contactsIgnoringSearch = (fragment_list?.adapter as? ContactsAdapter)?.contactItems ?: ArrayList() + } else if (this is GroupsFragment) { + groupsIgnoringSearch = (fragment_list?.adapter as? GroupsAdapter)?.groups ?: ArrayList() } } @@ -177,7 +180,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) } private fun setupContactsFavoritesAdapter(contacts: ArrayList) { - setupViewVisibility(contacts) + setupViewVisibility(contacts.isNotEmpty()) val currAdapter = fragment_list.adapter if (currAdapter == null || forceListRedraw) { forceListRedraw = false @@ -227,9 +230,10 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) } fun onSearchQueryChanged(text: String) { - val shouldNormalize = text.normalizeString() == text - val convertLetters = config.showDialpadLetters - (fragment_list.adapter as? ContactsAdapter)?.apply { + val adapter = fragment_list.adapter + if (adapter is ContactsAdapter) { + val shouldNormalize = text.normalizeString() == text + val convertLetters = config.showDialpadLetters val filtered = contactsIgnoringSearch.filter { getProperText(it.getNameToDisplay(), shouldNormalize).contains(text, true) || getProperText(it.nickname, shouldNormalize).contains(text, true) || @@ -249,11 +253,22 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) } if (filtered.isEmpty() && this@MyViewPagerFragment is FavoritesFragment) { - fragment_placeholder.text = activity.getString(R.string.no_items_found) + fragment_placeholder.text = activity?.getString(R.string.no_items_found) } fragment_placeholder.beVisibleIf(filtered.isEmpty()) - updateItems(filtered, text.normalizeString()) + (adapter as? ContactsAdapter)?.updateItems(filtered, text.normalizeString()) + } else if (adapter is GroupsAdapter) { + val filtered = groupsIgnoringSearch.filter { + it.title.contains(text, true) + } as ArrayList + + if (filtered.isEmpty()) { + fragment_placeholder.text = activity?.getString(R.string.no_items_found) + } + + fragment_placeholder.beVisibleIf(filtered.isEmpty()) + (adapter as? GroupsAdapter)?.updateItems(filtered) } } @@ -261,11 +276,17 @@ 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() { - (fragment_list.adapter as? ContactsAdapter)?.updateItems(contactsIgnoringSearch) - setupViewVisibility(contactsIgnoringSearch) + if (fragment_list.adapter is ContactsAdapter) { + (fragment_list.adapter as? ContactsAdapter)?.updateItems(contactsIgnoringSearch) + setupViewVisibility(contactsIgnoringSearch.isNotEmpty()) + } else if (fragment_list.adapter is GroupsAdapter) { + (fragment_list.adapter as? GroupsAdapter)?.updateItems(groupsIgnoringSearch) + setupViewVisibility(groupsIgnoringSearch.isNotEmpty()) + } if (this is FavoritesFragment) { fragment_placeholder.text = activity?.getString(R.string.no_favorites) @@ -279,10 +300,10 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) fragment_placeholder_2.setTextColor(context.getAdjustedPrimaryColor()) } - private fun setupViewVisibility(contacts: ArrayList) { - fragment_placeholder_2.beVisibleIf(contacts.isEmpty()) - fragment_placeholder.beVisibleIf(contacts.isEmpty()) - fragment_list.beVisibleIf(contacts.isNotEmpty()) + private fun setupViewVisibility(hasItemsToShow: Boolean) { + fragment_placeholder_2.beVisibleIf(!hasItemsToShow) + fragment_placeholder.beVisibleIf(!hasItemsToShow) + fragment_list.beVisibleIf(hasItemsToShow) } abstract fun fabClicked()