adding Search at the Select Contact activity

This commit is contained in:
tibbi 2019-12-06 23:01:14 +01:00
parent 3da4c170d8
commit a88d37aa41
3 changed files with 105 additions and 2 deletions

View file

@ -147,6 +147,14 @@
android:label="@string/select_contact"
android:parentActivityName=".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.INSERT_OR_EDIT"/>
<category android:name="android.intent.category.DEFAULT"/>

View file

@ -1,10 +1,15 @@
package com.simplemobiletools.contacts.pro.activities
import android.app.SearchManager
import android.content.Context
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.provider.ContactsContract
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.widget.SearchView
import androidx.core.view.MenuItemCompat
import androidx.viewpager.widget.ViewPager
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_GET_ACCOUNTS
@ -14,6 +19,7 @@ import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.adapters.ViewPagerAdapter
import com.simplemobiletools.contacts.pro.extensions.config
import com.simplemobiletools.contacts.pro.extensions.getContactPublicUri
import com.simplemobiletools.contacts.pro.fragments.MyViewPagerFragment
import com.simplemobiletools.contacts.pro.helpers.*
import com.simplemobiletools.contacts.pro.interfaces.RefreshContactsListener
import com.simplemobiletools.contacts.pro.models.Contact
@ -25,7 +31,11 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
private val START_INSERT_ACTIVITY = 1
private val START_EDIT_ACTIVITY = 2
private val contactsFavoritesList = arrayListOf(CONTACTS_TAB_MASK,
private var isSearchOpen = false
private var searchMenuItem: MenuItem? = null
private val contactsFavoritesList = arrayListOf(
CONTACTS_TAB_MASK,
FAVORITES_TAB_MASK
)
@ -53,14 +63,26 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
}
}
override fun onStop() {
super.onStop()
searchMenuItem?.collapseActionView()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_insert_or_edit, menu)
setupSearch(menu)
updateMenuItemColors(menu)
return super.onCreateOptionsMenu(menu)
}
private fun initFragments() {
viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {}
override fun onPageScrollStateChanged(state: Int) {
if (isSearchOpen) {
getCurrentFragment()?.onSearchQueryChanged("")
searchMenuItem?.collapseActionView()
}
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
@ -79,6 +101,10 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
it.icon?.applyColorFilter(config.textColor)
},
tabSelectedAction = {
if (isSearchOpen) {
getCurrentFragment()?.onSearchQueryChanged("")
searchMenuItem?.collapseActionView()
}
viewpager.currentItem = it.position
it.icon?.applyColorFilter(getAdjustedPrimaryColor())
}
@ -104,6 +130,55 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
}
}
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(getSearchString())
setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String) = false
override fun onQueryTextChange(newText: String): Boolean {
if (isSearchOpen) {
getCurrentFragment()?.onSearchQueryChanged(newText)
}
return true
}
})
}
MenuItemCompat.setOnActionExpandListener(searchMenuItem, object : MenuItemCompat.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
getCurrentFragment()?.onSearchOpened()
isSearchOpen = true
return true
}
override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
getCurrentFragment()?.onSearchClosed()
isSearchOpen = false
return true
}
})
}
private fun getSearchString(): Int {
return when (getCurrentFragment()) {
favorites_fragment -> R.string.search_favorites
else -> R.string.search_contacts
}
}
private fun getCurrentFragment(): MyViewPagerFragment? {
return if (viewpager.currentItem == 0) {
contacts_fragment
} else {
favorites_fragment
}
}
private fun setupTabColors() {
insert_or_edit_tabs_holder.apply {
background = ColorDrawable(config.backgroundColor)

View file

@ -0,0 +1,20 @@
<?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_vector"
android:title="@string/search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom" />
<item
android:id="@+id/sort"
android:icon="@drawable/ic_sort_vector"
android:title="@string/sort_by"
app:showAsAction="ifRoom" />
<item
android:id="@+id/filter"
android:icon="@drawable/ic_filter_vector"
android:title="@string/filter"
app:showAsAction="ifRoom" />
</menu>