moving tabs at the bottom at Insert Or Edit activity too

This commit is contained in:
tibbi 2022-06-26 09:23:38 +02:00
parent 8dc1fda8ba
commit e82481be8e
3 changed files with 87 additions and 60 deletions

View File

@ -5,7 +5,6 @@ import android.app.SearchManager
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.os.Bundle
import android.provider.ContactsContract
@ -13,6 +12,8 @@ import android.provider.ContactsContract.CommonDataKinds.Email
import android.provider.ContactsContract.CommonDataKinds.Phone
import android.view.Menu
import android.view.MenuItem
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.core.view.MenuItemCompat
import androidx.viewpager.widget.ViewPager
@ -69,7 +70,7 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
return
}
setupTabColors()
setupTabs()
// we do not really care about the permission request result. Even if it was denied, load private contacts
handlePermission(PERMISSION_READ_CONTACTS) {
@ -85,6 +86,11 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
}
}
override fun onResume() {
super.onResume()
setupTabColors()
}
override fun onStop() {
super.onStop()
searchMenuItem?.collapseActionView()
@ -115,7 +121,7 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
}
private fun initFragments() {
viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {
if (isSearchOpen) {
getCurrentFragment()?.onSearchQueryChanged("")
@ -131,37 +137,10 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
}
})
viewpager.onGlobalLayout {
view_pager.onGlobalLayout {
refreshContacts(getTabsMask())
}
insert_or_edit_tabs_holder.onTabSelectionChanged(
tabUnselectedAction = {
it.icon?.applyColorFilter(getProperTextColor())
},
tabSelectedAction = {
if (isSearchOpen) {
getCurrentFragment()?.onSearchQueryChanged("")
searchMenuItem?.collapseActionView()
}
viewpager.currentItem = it.position
it.icon?.applyColorFilter(getProperPrimaryColor())
}
)
insert_or_edit_tabs_holder.removeAllTabs()
var skippedTabs = 0
contactsFavoritesList.forEachIndexed { index, value ->
if (config.showTabs and value == 0 && value == TAB_FAVORITES) {
skippedTabs++
} else {
val tab = insert_or_edit_tabs_holder.newTab().setIcon(getTabIcon(index))
insert_or_edit_tabs_holder.addTab(tab, index - skippedTabs, index == 0)
}
}
insert_or_edit_tabs_holder.beVisibleIf(skippedTabs == 0)
select_contact_label?.setTextColor(getProperPrimaryColor())
new_contact_tmb?.setImageDrawable(resources.getColoredDrawableWithColor(R.drawable.ic_add_person_vector, getProperTextColor()))
new_contact_name.setTextColor(getProperTextColor())
@ -170,6 +149,32 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
}
}
private fun setupTabs() {
insert_or_edit_tabs_holder.removeAllTabs()
contactsFavoritesList.forEachIndexed { index, value ->
if (config.showTabs and value != 0) {
insert_or_edit_tabs_holder.newTab().setCustomView(R.layout.bottom_tablayout_item).apply {
customView?.findViewById<ImageView>(R.id.tab_item_icon)?.setImageDrawable(getTabIcon(index))
customView?.findViewById<TextView>(R.id.tab_item_label)?.text = getTabLabel(index)
insert_or_edit_tabs_holder.addTab(this)
}
}
}
insert_or_edit_tabs_holder.onTabSelectionChanged(
tabUnselectedAction = {
updateBottomTabItemColors(it.customView, false)
},
tabSelectedAction = {
closeSearch()
view_pager.currentItem = it.position
updateBottomTabItemColors(it.customView, true)
}
)
insert_or_edit_tabs_holder.beGoneIf(insert_or_edit_tabs_holder.tabCount == 1)
}
private fun setupSearch(menu: Menu) {
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
searchMenuItem = menu.findItem(R.id.search)
@ -212,27 +217,38 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
}
private fun getCurrentFragment(): MyViewPagerFragment? {
return if (viewpager.currentItem == 0) {
return if (view_pager.currentItem == 0) {
contacts_fragment
} else {
favorites_fragment
}
}
private fun getAllFragments() = arrayListOf(contacts_fragment, favorites_fragment)
private fun setupTabColors() {
insert_or_edit_tabs_holder.apply {
background = ColorDrawable(getProperBackgroundColor())
setSelectedTabIndicatorColor(getProperPrimaryColor())
val activeView = insert_or_edit_tabs_holder.getTabAt(view_pager.currentItem)?.customView
updateBottomTabItemColors(activeView, true)
getInactiveTabIndexes(view_pager.currentItem).forEach { index ->
val inactiveView = insert_or_edit_tabs_holder.getTabAt(index)?.customView
updateBottomTabItemColors(inactiveView, false)
}
val bottomBarColor = getBottomTabsBackgroundColor()
insert_or_edit_tabs_holder.setBackgroundColor(bottomBarColor)
updateNavigationBarColor(bottomBarColor)
}
private fun getInactiveTabIndexes(activeIndex: Int) = (0 until contactsFavoritesList.size).filter { it != activeIndex }
override fun refreshContacts(refreshTabsMask: Int) {
if (isDestroyed || isFinishing) {
return
}
if (viewpager.adapter == null) {
viewpager.adapter = ViewPagerAdapter(this, contactsFavoritesList, getTabsMask())
if (view_pager.adapter == null) {
view_pager.adapter = ViewPagerAdapter(this, contactsFavoritesList, getTabsMask())
}
ContactsHelper(this).getContacts {
@ -344,6 +360,15 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
createNewContact()
}
private fun closeSearch() {
if (isSearchOpen) {
getAllFragments().forEach {
it?.onSearchQueryChanged("")
}
searchMenuItem?.collapseActionView()
}
}
private fun showSortingDialog() {
ChangeSortingDialog(this) {
refreshContacts(getTabsMask())

View File

@ -339,9 +339,8 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
view_pager.offscreenPageLimit = tabsList.size - 1
view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {
if (isSearchOpen) {
getCurrentFragment()?.onSearchQueryChanged("")
searchMenuItem?.collapseActionView()
if (state == ViewPager.SCROLL_STATE_SETTLING) {
closeSearch()
}
}
@ -387,19 +386,12 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
updateBottomTabItemColors(it.customView, false)
},
tabSelectedAction = {
closeSearch()
view_pager.currentItem = it.position
updateBottomTabItemColors(it.customView, true)
}
)
// selecting the proper tab sometimes glitches, add an extra selector to make sure we have it right
/*main_tabs_holder.onGlobalLayout {
Handler().postDelayed({
main_tabs_holder.getTabAt(getDefaultTab())?.select()
invalidateOptionsMenu()
}, 100L)
}*/
main_tabs_holder.beGoneIf(main_tabs_holder.tabCount == 1)
}
@ -542,11 +534,13 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
}
private fun launchSettings() {
closeSearch()
hideKeyboard()
startActivity(Intent(applicationContext, SettingsActivity::class.java))
}
private fun launchAbout() {
closeSearch()
val licenses = LICENSE_JODA or LICENSE_GLIDE or LICENSE_GSON or LICENSE_INDICATOR_FAST_SCROLL
val faqItems = arrayListOf(
@ -638,6 +632,15 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
}
}
private fun closeSearch() {
if (isSearchOpen) {
getAllFragments().forEach {
it?.onSearchQueryChanged("")
}
searchMenuItem?.collapseActionView()
}
}
private fun checkWhatsNewDialog() {
arrayListOf<Release>().apply {
add(Release(10, R.string.release_10))

View File

@ -5,21 +5,10 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabLayout
android:id="@+id/insert_or_edit_tabs_holder"
android:layout_width="match_parent"
android:layout_height="48dp"
app:elevation="6dp"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="2dp"
app:tabMinWidth="150dp"
app:tabSelectedTextColor="@android:color/white" />
<RelativeLayout
android:id="@+id/new_contact_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/insert_or_edit_tabs_holder"
android:background="?attr/selectableItemBackground"
android:paddingTop="@dimen/medium_margin"
android:paddingEnd="@dimen/activity_margin"
@ -63,9 +52,19 @@
android:visibility="gone" />
<com.simplemobiletools.commons.views.MyViewPager
android:id="@+id/viewpager"
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/insert_or_edit_tabs_holder"
android:layout_below="@+id/select_contact_label" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/insert_or_edit_tabs_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:tabIndicator="@null"
app:tabMinWidth="150dp"
app:tabRippleColor="@null" />
</RelativeLayout>