11package com.simplemobiletools.contacts.pro.activities
22
33import android.app.Activity
4- import android.app.SearchManager
54import android.content.ActivityNotFoundException
6- import android.content.Context
75import android.content.Intent
86import android.net.Uri
97import android.os.Bundle
108import android.provider.ContactsContract
119import android.provider.ContactsContract.CommonDataKinds.Email
1210import android.provider.ContactsContract.CommonDataKinds.Phone
13- import android.view.Menu
1411import android.view.MenuItem
1512import android.widget.ImageView
1613import android.widget.TextView
17- import androidx.appcompat.widget.SearchView
18- import androidx.core.view.MenuItemCompat
1914import androidx.viewpager.widget.ViewPager
2015import com.simplemobiletools.commons.extensions.*
2116import com.simplemobiletools.commons.helpers.*
@@ -55,6 +50,7 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
5550 setContentView(R .layout.activity_insert_edit_contact)
5651 setupOptionsMenu()
5752 isSelectContactIntent = intent.action == Intent .ACTION_PICK
53+ updateMaterialActivityViews(insert_edit_coordinator, insert_edit_contact_holder, useTransparentNavigation = false , useTopSearchMenu = true )
5854
5955 if (isSelectContactIntent) {
6056 specialMimeType = when (intent.data) {
@@ -89,8 +85,8 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
8985
9086 override fun onResume () {
9187 super .onResume()
88+ updateMenuColors()
9289 setupTabColors()
93- setupToolbar(insert_edit_toolbar, NavigationIcon .None , searchMenuItem = searchMenuItem)
9490 }
9591
9692 override fun onStop () {
@@ -99,8 +95,21 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
9995 }
10096
10197 private fun setupOptionsMenu () {
102- setupSearch(insert_edit_toolbar.menu)
103- insert_edit_toolbar.setOnMenuItemClickListener { menuItem ->
98+ insert_edit_menu.getToolbar().inflateMenu(R .menu.menu_insert_or_edit)
99+ insert_edit_menu.toggleHideOnScroll(false )
100+ insert_edit_menu.setupMenu()
101+
102+ insert_edit_menu.onSearchClosedListener = {
103+ getAllFragments().forEach {
104+ it?.onSearchClosed()
105+ }
106+ }
107+
108+ insert_edit_menu.onSearchTextChangedListener = { text ->
109+ getCurrentFragment()?.onSearchQueryChanged(text)
110+ }
111+
112+ insert_edit_menu.getToolbar().setOnMenuItemClickListener { menuItem ->
104113 when (menuItem.itemId) {
105114 R .id.sort -> showSortingDialog()
106115 R .id.filter -> showFilterDialog()
@@ -110,6 +119,11 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
110119 }
111120 }
112121
122+ private fun updateMenuColors () {
123+ updateStatusbarColor(getProperBackgroundColor())
124+ insert_edit_menu.updateColors()
125+ }
126+
113127 override fun onActivityResult (requestCode : Int , resultCode : Int , resultData : Intent ? ) {
114128 super .onActivityResult(requestCode, resultCode, resultData)
115129 if (resultCode == Activity .RESULT_OK ) {
@@ -118,14 +132,22 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
118132 }
119133 }
120134
135+ override fun onBackPressed () {
136+ if (insert_edit_menu.isSearchOpen) {
137+ insert_edit_menu.closeSearch()
138+ } else {
139+ super .onBackPressed()
140+ }
141+ }
142+
121143 private fun initFragments () {
122144 view_pager.addOnPageChangeListener(object : ViewPager .OnPageChangeListener {
123145 override fun onPageScrollStateChanged (state : Int ) {}
124146
125147 override fun onPageScrolled (position : Int , positionOffset : Float , positionOffsetPixels : Int ) {}
126148
127149 override fun onPageSelected (position : Int ) {
128- insert_or_edit_tabs_holder .getTabAt(position)?.select()
150+ insert_edit_tabs_holder .getTabAt(position)?.select()
129151 getAllFragments().forEach {
130152 it?.finishActMode()
131153 }
@@ -145,69 +167,29 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
145167 }
146168
147169 private fun setupTabs () {
148- insert_or_edit_tabs_holder .removeAllTabs()
170+ insert_edit_tabs_holder .removeAllTabs()
149171 contactsFavoritesList.forEachIndexed { index, value ->
150172 if (config.showTabs and value != 0 ) {
151- insert_or_edit_tabs_holder .newTab().setCustomView(R .layout.bottom_tablayout_item).apply {
173+ insert_edit_tabs_holder .newTab().setCustomView(R .layout.bottom_tablayout_item).apply {
152174 customView?.findViewById<ImageView >(R .id.tab_item_icon)?.setImageDrawable(getTabIcon(index))
153175 customView?.findViewById<TextView >(R .id.tab_item_label)?.text = getTabLabel(index)
154- insert_or_edit_tabs_holder .addTab(this )
176+ insert_edit_tabs_holder .addTab(this )
155177 }
156178 }
157179 }
158180
159- insert_or_edit_tabs_holder .onTabSelectionChanged(
181+ insert_edit_tabs_holder .onTabSelectionChanged(
160182 tabUnselectedAction = {
161- updateBottomTabItemColors(it.customView, false )
183+ updateBottomTabItemColors(it.customView, false , getDeselectedTabDrawableIds()[it.position] )
162184 },
163185 tabSelectedAction = {
164- closeSearch()
186+ insert_edit_menu. closeSearch()
165187 view_pager.currentItem = it.position
166- updateBottomTabItemColors(it.customView, true )
188+ updateBottomTabItemColors(it.customView, true , getSelectedTabDrawableIds()[it.position] )
167189 }
168190 )
169191
170- insert_or_edit_tabs_holder.beGoneIf(insert_or_edit_tabs_holder.tabCount == 1 )
171- }
172-
173- private fun setupSearch (menu : Menu ) {
174- val searchManager = getSystemService(Context .SEARCH_SERVICE ) as SearchManager
175- searchMenuItem = menu.findItem(R .id.search)
176- (searchMenuItem!! .actionView as SearchView ).apply {
177- setSearchableInfo(searchManager.getSearchableInfo(componentName))
178- isSubmitButtonEnabled = false
179- queryHint = getString(getSearchString())
180- setOnQueryTextListener(object : SearchView .OnQueryTextListener {
181- override fun onQueryTextSubmit (query : String ) = false
182-
183- override fun onQueryTextChange (newText : String ): Boolean {
184- if (isSearchOpen) {
185- getCurrentFragment()?.onSearchQueryChanged(newText)
186- }
187- return true
188- }
189- })
190- }
191-
192- MenuItemCompat .setOnActionExpandListener(searchMenuItem, object : MenuItemCompat .OnActionExpandListener {
193- override fun onMenuItemActionExpand (item : MenuItem ? ): Boolean {
194- isSearchOpen = true
195- return true
196- }
197-
198- override fun onMenuItemActionCollapse (item : MenuItem ? ): Boolean {
199- getCurrentFragment()?.onSearchClosed()
200- isSearchOpen = false
201- return true
202- }
203- })
204- }
205-
206- private fun getSearchString (): Int {
207- return when (getCurrentFragment()) {
208- favorites_fragment -> R .string.search_favorites
209- else -> R .string.search_contacts
210- }
192+ insert_edit_tabs_holder.beGoneIf(insert_edit_tabs_holder.tabCount == 1 )
211193 }
212194
213195 private fun getCurrentFragment (): MyViewPagerFragment ? {
@@ -221,20 +203,30 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
221203 private fun getAllFragments () = arrayListOf (contacts_fragment, favorites_fragment)
222204
223205 private fun setupTabColors () {
224- val activeView = insert_or_edit_tabs_holder .getTabAt(view_pager.currentItem)?.customView
225- updateBottomTabItemColors(activeView, true )
206+ val activeView = insert_edit_tabs_holder .getTabAt(view_pager.currentItem)?.customView
207+ updateBottomTabItemColors(activeView, true , getSelectedTabDrawableIds()[view_pager.currentItem] )
226208
227209 getInactiveTabIndexes(view_pager.currentItem).forEach { index ->
228- val inactiveView = insert_or_edit_tabs_holder .getTabAt(index)?.customView
229- updateBottomTabItemColors(inactiveView, false )
210+ val inactiveView = insert_edit_tabs_holder .getTabAt(index)?.customView
211+ updateBottomTabItemColors(inactiveView, false , getDeselectedTabDrawableIds()[index] )
230212 }
231213
232214 val bottomBarColor = getBottomNavigationBackgroundColor()
233- insert_or_edit_tabs_holder .setBackgroundColor(bottomBarColor)
215+ insert_edit_tabs_holder .setBackgroundColor(bottomBarColor)
234216 updateNavigationBarColor(bottomBarColor)
235217 }
236218
237- private fun getInactiveTabIndexes (activeIndex : Int ) = (0 until contactsFavoritesList.size).filter { it != activeIndex }
219+ private fun getInactiveTabIndexes (activeIndex : Int ) = (0 until insert_edit_tabs_holder.tabCount).filter { it != activeIndex }
220+
221+ private fun getSelectedTabDrawableIds () = arrayOf(
222+ R .drawable.ic_person_vector,
223+ R .drawable.ic_star_vector
224+ )
225+
226+ private fun getDeselectedTabDrawableIds () = arrayOf(
227+ R .drawable.ic_person_outline_vector,
228+ R .drawable.ic_star_outline_vector
229+ )
238230
239231 override fun refreshContacts (refreshTabsMask : Int ) {
240232 if (isDestroyed || isFinishing) {
@@ -270,10 +262,12 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
270262 }
271263
272264 if (refreshTabsMask and TAB_CONTACTS != 0 ) {
265+ contacts_fragment?.skipHashComparing = true
273266 contacts_fragment?.refreshContacts(contacts, placeholderText)
274267 }
275268
276269 if (refreshTabsMask and TAB_FAVORITES != 0 ) {
270+ favorites_fragment?.skipHashComparing = true
277271 favorites_fragment?.refreshContacts(contacts, placeholderText)
278272 }
279273 }
@@ -350,10 +344,6 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
350344 }
351345 }
352346
353- fun fabClicked () {
354- createNewContact()
355- }
356-
357347 private fun closeSearch () {
358348 if (isSearchOpen) {
359349 getAllFragments().forEach {
0 commit comments