mirror of
				https://github.com/SimpleMobileTools/Simple-Contacts.git
				synced 2025-06-05 21:59:27 +02:00 
			
		
		
		
	implementing the search functionality
This commit is contained in:
		| @@ -5,6 +5,8 @@ import android.content.Context | |||||||
| import android.content.Intent | import android.content.Intent | ||||||
| import android.graphics.drawable.ColorDrawable | import android.graphics.drawable.ColorDrawable | ||||||
| import android.os.Bundle | import android.os.Bundle | ||||||
|  | import android.support.v4.view.MenuItemCompat | ||||||
|  | import android.support.v4.view.ViewPager | ||||||
| import android.support.v7.widget.SearchView | import android.support.v7.widget.SearchView | ||||||
| import android.view.Menu | import android.view.Menu | ||||||
| import android.view.MenuItem | 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.ChangeSortingDialog | ||||||
| import com.simplemobiletools.contacts.dialogs.FilterContactSourcesDialog | import com.simplemobiletools.contacts.dialogs.FilterContactSourcesDialog | ||||||
| import com.simplemobiletools.contacts.extensions.config | import com.simplemobiletools.contacts.extensions.config | ||||||
| import com.simplemobiletools.contacts.extensions.onPageChanged |  | ||||||
| import com.simplemobiletools.contacts.extensions.onTabSelectionChanged | import com.simplemobiletools.contacts.extensions.onTabSelectionChanged | ||||||
| import com.simplemobiletools.contacts.interfaces.RefreshContactsListener | import com.simplemobiletools.contacts.interfaces.RefreshContactsListener | ||||||
| import kotlinx.android.synthetic.main.activity_main.* | import kotlinx.android.synthetic.main.activity_main.* | ||||||
| @@ -25,6 +26,8 @@ import kotlinx.android.synthetic.main.fragment_favorites.* | |||||||
|  |  | ||||||
| class MainActivity : SimpleActivity(), RefreshContactsListener { | class MainActivity : SimpleActivity(), RefreshContactsListener { | ||||||
|     private var isFirstResume = true |     private var isFirstResume = true | ||||||
|  |     private var isSearchOpen = false | ||||||
|  |     private var searchMenuItem: MenuItem? = null | ||||||
|  |  | ||||||
|     private var storedUseEnglish = false |     private var storedUseEnglish = false | ||||||
|     private var storedTextColor = 0 |     private var storedTextColor = 0 | ||||||
| @@ -144,22 +147,41 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { | |||||||
|  |  | ||||||
|     private fun setupSearch(menu: Menu) { |     private fun setupSearch(menu: Menu) { | ||||||
|         val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager |         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)) |             setSearchableInfo(searchManager.getSearchableInfo(componentName)) | ||||||
|             isSubmitButtonEnabled = false |             isSubmitButtonEnabled = false | ||||||
|             queryHint = getString(if (viewpager.currentItem == 0) R.string.search_contacts else R.string.search_favorites) |             queryHint = getString(if (viewpager.currentItem == 0) R.string.search_contacts else R.string.search_favorites) | ||||||
|             setOnQueryTextListener(object : SearchView.OnQueryTextListener { |             setOnQueryTextListener(object : SearchView.OnQueryTextListener { | ||||||
|                 override fun onQueryTextSubmit(query: String?): Boolean { |                 override fun onQueryTextSubmit(query: String): Boolean { | ||||||
|                     return false |                     return false | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 override fun onQueryTextChange(newText: String?): Boolean { |                 override fun onQueryTextChange(newText: String): Boolean { | ||||||
|  |                     if (isSearchOpen) { | ||||||
|  |                         getCurrentFragment().onSearchQueryChanged(newText) | ||||||
|  |                     } | ||||||
|                     return true |                     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() { |     private fun setupTabColors() { | ||||||
|         val lastUsedPage = config.lastUsedViewPagerPage |         val lastUsedPage = config.lastUsedViewPagerPage | ||||||
|         main_tabs_holder.apply { |         main_tabs_holder.apply { | ||||||
| @@ -175,12 +197,24 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { | |||||||
|  |  | ||||||
|     private fun initFragments() { |     private fun initFragments() { | ||||||
|         viewpager.adapter = ViewPagerAdapter(this) |         viewpager.adapter = ViewPagerAdapter(this) | ||||||
|         viewpager.onPageChanged { |         viewpager.setOnPageChangeListener(object : ViewPager.OnPageChangeListener { | ||||||
|             main_tabs_holder.getTabAt(it)?.select() |             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() |                 contacts_fragment?.finishActMode() | ||||||
|                 favorites_fragment?.finishActMode() |                 favorites_fragment?.finishActMode() | ||||||
|                 invalidateOptionsMenu() |                 invalidateOptionsMenu() | ||||||
|             } |             } | ||||||
|  |         }) | ||||||
|         viewpager.currentItem = config.lastUsedViewPagerPage |         viewpager.currentItem = config.lastUsedViewPagerPage | ||||||
|  |  | ||||||
|         main_tabs_holder.onTabSelectionChanged( |         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.* | import kotlinx.android.synthetic.main.fragment_layout.view.* | ||||||
|  |  | ||||||
| abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet) { | abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet) { | ||||||
|     var activity: MainActivity? = null |     protected var activity: MainActivity? = null | ||||||
|     var lastHashCode = 0 |     private var lastHashCode = 0 | ||||||
|     lateinit var config: Config |     private var contactsIgnoringSearch = ArrayList<Contact>() | ||||||
|  |     lateinit private var config: Config | ||||||
|  |  | ||||||
|     fun setupFragment(activity: MainActivity) { |     fun setupFragment(activity: MainActivity) { | ||||||
|         config = activity.config |         config = activity.config | ||||||
| @@ -155,6 +156,17 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) | |||||||
|         (fragment_list.adapter as? ContactsAdapter)?.finishActMode() |         (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() { |     private fun updateViewStuff() { | ||||||
|         context.updateTextColors(fragment_wrapper) |         context.updateTextColors(fragment_wrapper) | ||||||
|         fragment_fastscroller.updateBubbleColors() |         fragment_fastscroller.updateBubbleColors() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user