Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9a5f47b8 authored by tibbi's avatar tibbi
Browse files

some code shortening at the SecurityDialog

parent 184057c7
Loading
Loading
Loading
Loading
+9 −28
Original line number Diff line number Diff line
@@ -3,8 +3,6 @@ package com.simplemobiletools.commons.dialogs
import android.app.Activity
import android.view.LayoutInflater
import androidx.appcompat.app.AlertDialog
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.adapters.PasswordTypesAdapter
import com.simplemobiletools.commons.extensions.*
@@ -29,18 +27,10 @@ class SecurityDialog(val activity: Activity, val requiredHash: String, val showT
            viewPager.offscreenPageLimit = 2
            tabsAdapter = PasswordTypesAdapter(context, requiredHash, this@SecurityDialog, dialog_scrollview)
            viewPager.adapter = tabsAdapter
            viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                override fun onPageScrollStateChanged(state: Int) {
            viewPager.onPageChangeListener {
                dialog_tab_layout.getTabAt(it)!!.select()
            }

                override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                }

                override fun onPageSelected(position: Int) {
                    dialog_tab_layout.getTabAt(position)!!.select()
                }
            })

            viewPager.onGlobalLayout {
                updateTabVisibility()
            }
@@ -53,22 +43,13 @@ class SecurityDialog(val activity: Activity, val requiredHash: String, val showT

                dialog_tab_layout.setTabTextColors(textColor, textColor)
                dialog_tab_layout.setSelectedTabIndicatorColor(context.baseConfig.primaryColor)
                dialog_tab_layout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                    override fun onTabReselected(tab: TabLayout.Tab?) {
                    }

                    override fun onTabUnselected(tab: TabLayout.Tab) {
                    }

                    override fun onTabSelected(tab: TabLayout.Tab) {
                dialog_tab_layout.onTabSelectionChanged(tabSelectedAction = {
                    viewPager.currentItem = when {
                            tab.text.toString().equals(resources.getString(R.string.pattern), true) -> PROTECTION_PATTERN
                            tab.text.toString().equals(resources.getString(R.string.pin), true) -> PROTECTION_PIN
                        it.text.toString().equals(resources.getString(R.string.pattern), true) -> PROTECTION_PATTERN
                        it.text.toString().equals(resources.getString(R.string.pin), true) -> PROTECTION_PIN
                        else -> PROTECTION_FINGERPRINT
                    }

                    updateTabVisibility()
                    }
                })
            } else {
                dialog_tab_layout.beGone()
+3 −3
Original line number Diff line number Diff line
@@ -2,14 +2,14 @@ package com.simplemobiletools.commons.extensions

import com.google.android.material.tabs.TabLayout

fun TabLayout.onTabSelectionChanged(tabUnselectedAction: (inactiveTab: TabLayout.Tab) -> Unit, tabSelectedAction: (activeTab: TabLayout.Tab) -> Unit) =
fun TabLayout.onTabSelectionChanged(tabUnselectedAction: ((inactiveTab: TabLayout.Tab) -> Unit)? = null, tabSelectedAction: ((activeTab: TabLayout.Tab) -> Unit)? = null) =
        setOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                tabSelectedAction(tab)
                tabSelectedAction?.invoke(tab)
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {
                tabUnselectedAction(tab)
                tabUnselectedAction?.invoke(tab)
            }

            override fun onTabReselected(tab: TabLayout.Tab) {
+2 −4
Original line number Diff line number Diff line
@@ -4,11 +4,9 @@ import androidx.viewpager.widget.ViewPager

fun ViewPager.onPageChangeListener(pageChangedAction: (newPosition: Int) -> Unit) =
        addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }
            override fun onPageScrollStateChanged(state: Int) {}

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

            override fun onPageSelected(position: Int) {
                pageChangedAction(position)