@@ -12,9 +12,10 @@ import android.graphics.drawable.Icon
1212import android.graphics.drawable.LayerDrawable
1313import android.net.Uri
1414import android.os.Bundle
15- import android.os.Handler
1615import android.view.Menu
1716import android.view.MenuItem
17+ import android.widget.ImageView
18+ import android.widget.TextView
1819import android.widget.Toast
1920import androidx.appcompat.widget.SearchView
2021import androidx.core.view.MenuItemCompat
@@ -75,7 +76,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
7576 appLaunched(BuildConfig .APPLICATION_ID )
7677
7778 storeStateVariables()
78- setupTabColors ()
79+ setupTabs ()
7980 checkContactPermissions()
8081 checkWhatsNewDialog()
8182 }
@@ -122,7 +123,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
122123 it?.setupColors(getProperTextColor(), properPrimaryColor)
123124 }
124125
125- updateTabColors ()
126+ setupTabColors ()
126127
127128 val configStartNameWithSurname = config.startNameWithSurname
128129 if (storedStartNameWithSurname != configStartNameWithSurname) {
@@ -138,7 +139,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
138139 }
139140
140141 if (werePermissionsHandled && ! isFirstResume) {
141- if (viewpager .adapter == null ) {
142+ if (view_pager .adapter == null ) {
142143 initFragments()
143144 } else {
144145 refreshContacts(ALL_TABS_MASK )
@@ -159,7 +160,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
159160 override fun onPause () {
160161 super .onPause()
161162 storeStateVariables()
162- config.lastUsedViewPagerPage = viewpager .currentItem
163+ config.lastUsedViewPagerPage = view_pager .currentItem
163164 }
164165
165166 override fun onDestroy () {
@@ -261,13 +262,6 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
261262 })
262263 }
263264
264- private fun updateTabColors () {
265- getInactiveTabIndexes(viewpager.currentItem).forEach {
266- main_tabs_holder.getTabAt(it)?.icon?.applyColorFilter(getProperTextColor())
267- }
268- main_tabs_holder.getTabAt(viewpager.currentItem)?.icon?.applyColorFilter(getProperPrimaryColor())
269- }
270-
271265 private fun getSearchString (): Int {
272266 return when (getCurrentFragment()) {
273267 favorites_fragment -> R .string.search_favorites
@@ -322,28 +316,28 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
322316 fragments.add(groups_fragment)
323317 }
324318
325- return fragments.getOrNull(viewpager .currentItem)
319+ return fragments.getOrNull(view_pager .currentItem)
326320 }
327321
328322 private fun setupTabColors () {
329- val lastUsedPage = getDefaultTab()
330- main_tabs_holder.apply {
331- background = ColorDrawable (getProperBackgroundColor())
332- setSelectedTabIndicatorColor(getProperPrimaryColor())
333- getTabAt(lastUsedPage)?.select()
334- getTabAt(lastUsedPage)?.icon?.applyColorFilter(getProperPrimaryColor())
335-
336- getInactiveTabIndexes(lastUsedPage).forEach {
337- getTabAt(it)?.icon?.applyColorFilter(getProperTextColor())
338- }
323+ val activeView = main_tabs_holder.getTabAt(view_pager.currentItem)?.customView
324+ updateBottomTabItemColors(activeView, true )
325+
326+ getInactiveTabIndexes(view_pager.currentItem).forEach { index ->
327+ val inactiveView = main_tabs_holder.getTabAt(index)?.customView
328+ updateBottomTabItemColors(inactiveView, false )
339329 }
330+
331+ val bottomBarColor = getBottomTabsBackgroundColor()
332+ main_tabs_holder.setBackgroundColor(bottomBarColor)
333+ updateNavigationBarColor(bottomBarColor)
340334 }
341335
342336 private fun getInactiveTabIndexes (activeIndex : Int ) = (0 until tabsList.size).filter { it != activeIndex }
343337
344338 private fun initFragments () {
345- viewpager .offscreenPageLimit = tabsList.size - 1
346- viewpager .addOnPageChangeListener(object : ViewPager .OnPageChangeListener {
339+ view_pager .offscreenPageLimit = tabsList.size - 1
340+ view_pager .addOnPageChangeListener(object : ViewPager .OnPageChangeListener {
347341 override fun onPageScrollStateChanged (state : Int ) {
348342 if (isSearchOpen) {
349343 getCurrentFragment()?.onSearchQueryChanged(" " )
@@ -362,54 +356,51 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
362356 }
363357 })
364358
365- viewpager .onGlobalLayout {
359+ view_pager .onGlobalLayout {
366360 refreshContacts(ALL_TABS_MASK )
367361 }
368362
369- main_tabs_holder.onTabSelectionChanged(
370- tabUnselectedAction = {
371- it.icon?.applyColorFilter(getProperTextColor())
372- },
373- tabSelectedAction = {
374- if (isSearchOpen) {
375- getCurrentFragment()?.onSearchQueryChanged(" " )
376- searchMenuItem?.collapseActionView()
377- }
378- viewpager.currentItem = it.position
379- it.icon?.applyColorFilter(getProperPrimaryColor())
380- }
381- )
382-
383363 if (intent?.action == Intent .ACTION_VIEW && intent.data != null ) {
384364 tryImportContactsFromFile(intent.data!! )
385365 intent.data = null
386366 }
387367
368+ main_dialpad_button.setOnClickListener {
369+ launchDialpad()
370+ }
371+ }
372+
373+ private fun setupTabs () {
388374 main_tabs_holder.removeAllTabs()
389- var skippedTabs = 0
390375 tabsList.forEachIndexed { index, value ->
391- if (config.showTabs and value = = 0 ) {
392- skippedTabs ++
393- } else {
394- val tab = main_tabs_holder.newTab().setIcon(getTabIcon( index) )
395- tab.contentDescription = getTabContentDescription(index )
396- main_tabs_holder.addTab(tab, index - skippedTabs, getDefaultTab() == index - skippedTabs)
376+ if (config.showTabs and value ! = 0 ) {
377+ main_tabs_holder.newTab().setCustomView( R .layout.bottom_tablayout_item). apply {
378+ customView?.findViewById< ImageView >( R .id.tab_item_icon)?.setImageDrawable(getTabIcon(index))
379+ customView?.findViewById< TextView >( R .id.tab_item_label)?.text = getTabLabel( index)
380+ main_tabs_holder.addTab( this )
381+ }
397382 }
398383 }
399384
385+ main_tabs_holder.onTabSelectionChanged(
386+ tabUnselectedAction = {
387+ updateBottomTabItemColors(it.customView, false )
388+ },
389+ tabSelectedAction = {
390+ view_pager.currentItem = it.position
391+ updateBottomTabItemColors(it.customView, true )
392+ }
393+ )
394+
400395 // selecting the proper tab sometimes glitches, add an extra selector to make sure we have it right
401- main_tabs_holder.onGlobalLayout {
396+ /* main_tabs_holder.onGlobalLayout {
402397 Handler().postDelayed({
403398 main_tabs_holder.getTabAt(getDefaultTab())?.select()
404399 invalidateOptionsMenu()
405400 }, 100L)
406- }
401+ }*/
407402
408- main_tabs_holder.beVisibleIf(skippedTabs < tabsList.size - 1 )
409-
410- main_dialpad_button.setOnClickListener {
411- launchDialpad()
412- }
403+ main_tabs_holder.beGoneIf(main_tabs_holder.tabCount == 1 )
413404 }
414405
415406 private fun showSortingDialog (showCustomSorting : Boolean ) {
@@ -579,10 +570,9 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
579570
580571 isGettingContacts = true
581572
582- if (viewpager.adapter == null ) {
583- viewpager.adapter = ViewPagerAdapter (this , tabsList, config.showTabs)
584- viewpager.currentItem = getDefaultTab()
585- updateTabColors()
573+ if (view_pager.adapter == null ) {
574+ view_pager.adapter = ViewPagerAdapter (this , tabsList, config.showTabs)
575+ view_pager.currentItem = getDefaultTab()
586576 }
587577
588578 ContactsHelper (this ).getContacts { contacts ->
0 commit comments