Merge pull request #239 from Aga-C/add-manage-tabs

Added hiding tabs (#183)
This commit is contained in:
Tibor Kaputa 2021-11-28 19:50:56 +01:00 committed by GitHub
commit c37e988d01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 234 additions and 38 deletions

View File

@ -39,6 +39,7 @@ class MainActivity : SimpleActivity() {
private var isSearchOpen = false
private var launchedDialer = false
private var searchMenuItem: MenuItem? = null
private var storedShowTabs = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -84,7 +85,10 @@ class MainActivity : SimpleActivity() {
}
if (!isSearchOpen) {
refreshItems()
if (storedShowTabs != config.showTabs) {
hideTabs()
}
refreshItems(true)
}
checkShortcuts()
@ -95,6 +99,7 @@ class MainActivity : SimpleActivity() {
override fun onPause() {
super.onPause()
storedShowTabs = config.showTabs
config.lastUsedViewPagerPage = viewpager.currentItem
}
@ -226,6 +231,16 @@ class MainActivity : SimpleActivity() {
getTabAt(it)?.icon?.applyColorFilter(config.textColor)
}
}
main_tabs_holder.onTabSelectionChanged(
tabUnselectedAction = {
it.icon?.applyColorFilter(config.textColor)
},
tabSelectedAction = {
viewpager.currentItem = it.position
it.icon?.applyColorFilter(getAdjustedPrimaryColor())
}
)
}
private fun getInactiveTabIndexes(activeIndex: Int) = (0 until tabsList.size).filter { it != activeIndex }
@ -248,29 +263,13 @@ class MainActivity : SimpleActivity() {
}
})
main_tabs_holder.onTabSelectionChanged(
tabUnselectedAction = {
it.icon?.applyColorFilter(config.textColor)
},
tabSelectedAction = {
viewpager.currentItem = it.position
it.icon?.applyColorFilter(getAdjustedPrimaryColor())
}
)
main_tabs_holder.removeAllTabs()
tabsList.forEachIndexed { index, value ->
val tab = main_tabs_holder.newTab().setIcon(getTabIcon(index))
main_tabs_holder.addTab(tab, index, getDefaultTab() == index)
}
// selecting the proper tab sometimes glitches, add an extra selector to make sure we have it right
main_tabs_holder.onGlobalLayout {
Handler().postDelayed({
var wantedTab = getDefaultTab()
// open the Recents tab if we got here by clicking a missed call notification
if (intent.action == Intent.ACTION_VIEW) {
if (intent.action == Intent.ACTION_VIEW && config.showTabs and TAB_CALL_HISTORY > 0) {
wantedTab = main_tabs_holder.tabCount - 1
}
@ -289,6 +288,32 @@ class MainActivity : SimpleActivity() {
}
}
private fun hideTabs() {
val selectedTabIndex = main_tabs_holder.selectedTabPosition
viewpager.adapter = null
main_tabs_holder.removeAllTabs()
var skippedTabs = 0
var isAnySelected = false
tabsList.forEachIndexed { index, value ->
if (config.showTabs and value == 0) {
skippedTabs++
} else {
val tab = main_tabs_holder.newTab().setIcon(getTabIcon(index))
val wasAlreadySelected = selectedTabIndex > -1 && selectedTabIndex == index - skippedTabs
val shouldSelect = !isAnySelected && wasAlreadySelected
if (shouldSelect) {
isAnySelected = true
}
main_tabs_holder.addTab(tab, index - skippedTabs, shouldSelect)
}
}
if (!isAnySelected) {
main_tabs_holder.selectTab(main_tabs_holder.getTabAt(getDefaultTab()))
}
main_tabs_holder.beGoneIf(main_tabs_holder.tabCount == 1)
storedShowTabs = config.showTabs
}
private fun getTabIcon(position: Int): Drawable {
val drawableId = when (position) {
0 -> R.drawable.ic_person_vector
@ -299,14 +324,14 @@ class MainActivity : SimpleActivity() {
return resources.getColoredDrawableWithColor(drawableId, config.textColor)
}
private fun refreshItems() {
private fun refreshItems(openLastTab: Boolean = false) {
if (isDestroyed || isFinishing) {
return
}
if (viewpager.adapter == null) {
viewpager.adapter = ViewPagerAdapter(this)
viewpager.currentItem = getDefaultTab()
viewpager.currentItem = if (openLastTab) main_tabs_holder.selectedTabPosition else getDefaultTab()
viewpager.onGlobalLayout {
refreshFragments()
}
@ -327,20 +352,52 @@ class MainActivity : SimpleActivity() {
recents_fragment?.refreshItems()
}
private fun getAllFragments() = arrayListOf(contacts_fragment, favorites_fragment, recents_fragment).toMutableList() as ArrayList<MyViewPagerFragment?>
private fun getAllFragments(): ArrayList<MyViewPagerFragment?> {
val showTabs = config.showTabs
val fragments = arrayListOf<MyViewPagerFragment?>()
private fun getCurrentFragment(): MyViewPagerFragment? = when (viewpager.currentItem) {
0 -> contacts_fragment
1 -> favorites_fragment
else -> recents_fragment
if (showTabs and TAB_CONTACTS > 0) {
fragments.add(contacts_fragment)
}
if (showTabs and TAB_FAVORITES > 0) {
fragments.add(favorites_fragment)
}
if (showTabs and TAB_CALL_HISTORY > 0) {
fragments.add(recents_fragment)
}
return fragments
}
private fun getCurrentFragment(): MyViewPagerFragment? = getAllFragments().getOrNull(viewpager.currentItem)
private fun getDefaultTab(): Int {
val showTabsMask = config.showTabs
return when (config.defaultTab) {
TAB_LAST_USED -> config.lastUsedViewPagerPage
TAB_LAST_USED -> if (config.lastUsedViewPagerPage < main_tabs_holder.tabCount) config.lastUsedViewPagerPage else 0
TAB_CONTACTS -> 0
TAB_FAVORITES -> 1
else -> 2
TAB_FAVORITES -> if (showTabsMask and TAB_CONTACTS > 0) 1 else 0
else -> {
if (showTabsMask and TAB_CALL_HISTORY > 0) {
if (showTabsMask and TAB_CONTACTS > 0) {
if (showTabsMask and TAB_FAVORITES > 0) {
2
} else {
1
}
} else {
if (showTabsMask and TAB_FAVORITES > 0) {
1
} else {
0
}
}
} else {
0
}
}
}
}

View File

@ -12,6 +12,7 @@ import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.dialogs.ManageVisibleTabsDialog
import com.simplemobiletools.dialer.extensions.config
import kotlinx.android.synthetic.main.activity_settings.*
import java.util.*
@ -32,6 +33,7 @@ class SettingsActivity : SimpleActivity() {
setupManageSpeedDial()
setupChangeDateTimeFormat()
setupFontSize()
setupManageShownTabs()
setupDefaultTab()
setupDialPadOpen()
setupGroupSubsequentCalls()
@ -147,6 +149,12 @@ class SettingsActivity : SimpleActivity() {
}
}
private fun setupManageShownTabs() {
settings_manage_tabs_holder.setOnClickListener {
ManageVisibleTabsDialog(this)
}
}
private fun setupDefaultTab() {
settings_default_tab.text = getDefaultTabText()
settings_default_tab_holder.setOnClickListener {

View File

@ -3,8 +3,12 @@ package com.simplemobiletools.dialer.adapters
import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.PagerAdapter
import com.simplemobiletools.commons.helpers.TAB_CALL_HISTORY
import com.simplemobiletools.commons.helpers.TAB_CONTACTS
import com.simplemobiletools.commons.helpers.TAB_FAVORITES
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.activities.SimpleActivity
import com.simplemobiletools.dialer.extensions.config
import com.simplemobiletools.dialer.fragments.MyViewPagerFragment
class ViewPagerAdapter(val activity: SimpleActivity) : PagerAdapter() {
@ -30,10 +34,20 @@ class ViewPagerAdapter(val activity: SimpleActivity) : PagerAdapter() {
override fun isViewFromObject(view: View, item: Any) = view == item
private fun getFragment(position: Int): Int {
return when (position) {
0 -> R.layout.fragment_contacts
1 -> R.layout.fragment_favorites
else -> R.layout.fragment_recents
val showTabs = activity.config.showTabs
val fragments = arrayListOf<Int>()
if (showTabs and TAB_CONTACTS > 0) {
fragments.add(R.layout.fragment_contacts)
}
if (showTabs and TAB_FAVORITES > 0) {
fragments.add(R.layout.fragment_favorites)
}
if (showTabs and TAB_CALL_HISTORY > 0) {
fragments.add(R.layout.fragment_recents)
}
return if (position < fragments.size) fragments[position] else fragments.last()
}
}

View File

@ -0,0 +1,52 @@
package com.simplemobiletools.dialer.dialogs
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.helpers.TAB_CONTACTS
import com.simplemobiletools.commons.helpers.TAB_FAVORITES
import com.simplemobiletools.commons.helpers.TAB_CALL_HISTORY
import com.simplemobiletools.commons.views.MyAppCompatCheckbox
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.extensions.config
import com.simplemobiletools.dialer.helpers.ALL_TABS_MASK
class ManageVisibleTabsDialog(val activity: BaseSimpleActivity) {
private var view = activity.layoutInflater.inflate(R.layout.dialog_manage_visible_tabs, null)
private val tabs = LinkedHashMap<Int, Int>()
init {
tabs.apply {
put(TAB_CONTACTS, R.id.manage_visible_tabs_contacts)
put(TAB_FAVORITES, R.id.manage_visible_tabs_favorites)
put(TAB_CALL_HISTORY, R.id.manage_visible_tabs_call_history)
}
val showTabs = activity.config.showTabs
for ((key, value) in tabs) {
view.findViewById<MyAppCompatCheckbox>(value).isChecked = showTabs and key != 0
}
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok) { dialog, which -> dialogConfirmed() }
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this)
}
}
private fun dialogConfirmed() {
var result = 0
for ((key, value) in tabs) {
if (view.findViewById<MyAppCompatCheckbox>(value).isChecked) {
result += key
}
}
if (result == 0) {
result = ALL_TABS_MASK
}
activity.config.showTabs = result
}
}

View File

@ -51,4 +51,8 @@ class Config(context: Context) : BaseConfig(context) {
var disableProximitySensor: Boolean
get() = prefs.getBoolean(DISABLE_PROXIMITY_SENSOR, false)
set(disableProximitySensor) = prefs.edit().putBoolean(DISABLE_PROXIMITY_SENSOR, disableProximitySensor).apply()
var showTabs: Int
get() = prefs.getInt(SHOW_TABS, ALL_TABS_MASK)
set(showTabs) = prefs.edit().putInt(SHOW_TABS, showTabs).apply()
}

View File

@ -1,17 +1,20 @@
package com.simplemobiletools.dialer.helpers
import com.simplemobiletools.commons.helpers.TAB_CONTACTS
import com.simplemobiletools.commons.helpers.TAB_FAVORITES
import com.simplemobiletools.commons.helpers.TAB_CALL_HISTORY
// shared prefs
const val SPEED_DIAL = "speed_dial"
const val REMEMBER_SIM_PREFIX = "remember_sim_"
const val GROUP_SUBSEQUENT_CALLS = "group_subsequent_calls"
const val OPEN_DIAL_PAD_AT_LAUNCH = "open_dial_pad_at_launch"
const val DISABLE_PROXIMITY_SENSOR = "disable_proximity_sensor"
const val SHOW_TABS = "show_tabs"
const val CONTACTS_TAB_MASK = 1
const val FAVORITES_TAB_MASK = 2
const val RECENTS_TAB_MASK = 4
const val ALL_TABS_MASK = TAB_CONTACTS or TAB_FAVORITES or TAB_CALL_HISTORY
val tabsList = arrayListOf(CONTACTS_TAB_MASK, FAVORITES_TAB_MASK, RECENTS_TAB_MASK)
val tabsList = arrayListOf(TAB_CONTACTS, TAB_FAVORITES, TAB_CALL_HISTORY)
private const val PATH = "com.simplemobiletools.dialer.action."
const val ACCEPT_CALL = PATH + "accept_call"

View File

@ -194,12 +194,28 @@
android:background="@drawable/section_holder_stroke"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/settings_manage_tabs_holder"
style="@style/SettingsHolderTextViewOneLinerStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple_top_corners">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/settings_manage_tabs"
style="@style/SettingsTextLabelStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/manage_shown_tabs" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/settings_default_tab_holder"
style="@style/SettingsHolderTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple_top_corners">
android:background="@drawable/ripple_background">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/settings_default_tab_label"

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/manage_visible_tabs_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/manage_visible_tabs_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingStart="@dimen/activity_margin"
android:paddingEnd="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/manage_visible_tabs_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin"
android:text="@string/contacts_tab"/>
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/manage_visible_tabs_favorites"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin"
android:text="@string/favorites"/>
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/manage_visible_tabs_call_history"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin"
android:text="@string/call_history_tab"/>
</LinearLayout>
</ScrollView>

View File

@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.6.0'
ext.kotlin_version = '1.5.31'
repositories {
google()
jcenter()