implementing the search functionality
This commit is contained in:
parent
5ff8eade7c
commit
a36f3743b8
3 changed files with 59 additions and 29 deletions
|
@ -5,6 +5,8 @@ import android.content.Context
|
|||
import android.content.Intent
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.os.Bundle
|
||||
import android.support.v4.view.MenuItemCompat
|
||||
import android.support.v4.view.ViewPager
|
||||
import android.support.v7.widget.SearchView
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
|
@ -16,7 +18,6 @@ import com.simplemobiletools.contacts.adapters.ViewPagerAdapter
|
|||
import com.simplemobiletools.contacts.dialogs.ChangeSortingDialog
|
||||
import com.simplemobiletools.contacts.dialogs.FilterContactSourcesDialog
|
||||
import com.simplemobiletools.contacts.extensions.config
|
||||
import com.simplemobiletools.contacts.extensions.onPageChanged
|
||||
import com.simplemobiletools.contacts.extensions.onTabSelectionChanged
|
||||
import com.simplemobiletools.contacts.interfaces.RefreshContactsListener
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
|
@ -25,6 +26,8 @@ import kotlinx.android.synthetic.main.fragment_favorites.*
|
|||
|
||||
class MainActivity : SimpleActivity(), RefreshContactsListener {
|
||||
private var isFirstResume = true
|
||||
private var isSearchOpen = false
|
||||
private var searchMenuItem: MenuItem? = null
|
||||
|
||||
private var storedUseEnglish = false
|
||||
private var storedTextColor = 0
|
||||
|
@ -144,22 +147,41 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
|
|||
|
||||
private fun setupSearch(menu: Menu) {
|
||||
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
|
||||
(menu.findItem(R.id.search).actionView as SearchView).apply {
|
||||
searchMenuItem = menu.findItem(R.id.search)
|
||||
(searchMenuItem!!.actionView as SearchView).apply {
|
||||
setSearchableInfo(searchManager.getSearchableInfo(componentName))
|
||||
isSubmitButtonEnabled = false
|
||||
queryHint = getString(if (viewpager.currentItem == 0) R.string.search_contacts else R.string.search_favorites)
|
||||
setOnQueryTextListener(object : SearchView.OnQueryTextListener {
|
||||
override fun onQueryTextSubmit(query: String?): Boolean {
|
||||
override fun onQueryTextSubmit(query: String): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
override fun onQueryTextChange(newText: String?): Boolean {
|
||||
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 {
|
||||
isSearchOpen = false
|
||||
return true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun getCurrentFragment() = if (viewpager.currentItem == 0) contacts_fragment else favorites_fragment
|
||||
|
||||
private fun setupTabColors() {
|
||||
val lastUsedPage = config.lastUsedViewPagerPage
|
||||
main_tabs_holder.apply {
|
||||
|
@ -175,12 +197,24 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
|
|||
|
||||
private fun initFragments() {
|
||||
viewpager.adapter = ViewPagerAdapter(this)
|
||||
viewpager.onPageChanged {
|
||||
main_tabs_holder.getTabAt(it)?.select()
|
||||
contacts_fragment?.finishActMode()
|
||||
favorites_fragment?.finishActMode()
|
||||
invalidateOptionsMenu()
|
||||
}
|
||||
viewpager.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
|
||||
override fun onPageScrollStateChanged(state: Int) {
|
||||
if (isSearchOpen) {
|
||||
getCurrentFragment().onSearchQueryChanged("")
|
||||
MenuItemCompat.collapseActionView(searchMenuItem)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
|
||||
}
|
||||
|
||||
override fun onPageSelected(position: Int) {
|
||||
main_tabs_holder.getTabAt(position)?.select()
|
||||
contacts_fragment?.finishActMode()
|
||||
favorites_fragment?.finishActMode()
|
||||
invalidateOptionsMenu()
|
||||
}
|
||||
})
|
||||
viewpager.currentItem = config.lastUsedViewPagerPage
|
||||
|
||||
main_tabs_holder.onTabSelectionChanged(
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
package com.simplemobiletools.contacts.extensions
|
||||
|
||||
import android.support.v4.view.ViewPager
|
||||
|
||||
fun ViewPager.onPageChanged(pageChangedAction: (activePage: Int) -> Unit) =
|
||||
addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
|
||||
override fun onPageSelected(position: Int) {
|
||||
pageChangedAction(position)
|
||||
}
|
||||
|
||||
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
|
||||
}
|
||||
|
||||
override fun onPageScrollStateChanged(state: Int) {
|
||||
}
|
||||
})
|
|
@ -23,9 +23,10 @@ import com.simplemobiletools.contacts.models.Contact
|
|||
import kotlinx.android.synthetic.main.fragment_layout.view.*
|
||||
|
||||
abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet) {
|
||||
var activity: MainActivity? = null
|
||||
var lastHashCode = 0
|
||||
lateinit var config: Config
|
||||
protected var activity: MainActivity? = null
|
||||
private var lastHashCode = 0
|
||||
private var contactsIgnoringSearch = ArrayList<Contact>()
|
||||
lateinit private var config: Config
|
||||
|
||||
fun setupFragment(activity: MainActivity) {
|
||||
config = activity.config
|
||||
|
@ -155,6 +156,17 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
|
|||
(fragment_list.adapter as? ContactsAdapter)?.finishActMode()
|
||||
}
|
||||
|
||||
fun onSearchQueryChanged(text: String) {
|
||||
(fragment_list.adapter as ContactsAdapter).apply {
|
||||
val filtered = contactsIgnoringSearch.filter { it.getFullName(startNameWithSurname).contains(text, true) } as ArrayList
|
||||
updateItems(filtered)
|
||||
}
|
||||
}
|
||||
|
||||
fun onSearchOpened() {
|
||||
contactsIgnoringSearch = (fragment_list.adapter as ContactsAdapter).contactItems as ArrayList
|
||||
}
|
||||
|
||||
private fun updateViewStuff() {
|
||||
context.updateTextColors(fragment_wrapper)
|
||||
fragment_fastscroller.updateBubbleColors()
|
||||
|
|
Loading…
Reference in a new issue