add viewpager tab fragments
This commit is contained in:
parent
7bd4296d64
commit
a64b1903cd
|
@ -41,7 +41,7 @@ android {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'com.simplemobiletools:commons:3.13.17'
|
||||
implementation 'com.simplemobiletools:commons:3.13.20'
|
||||
}
|
||||
|
||||
Properties props = new Properties()
|
||||
|
|
|
@ -1,24 +1,32 @@
|
|||
package com.simplemobiletools.clock.activities
|
||||
|
||||
import android.content.Intent
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.os.Bundle
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.view.WindowManager
|
||||
import com.simplemobiletools.clock.BuildConfig
|
||||
import com.simplemobiletools.clock.R
|
||||
import com.simplemobiletools.clock.adapters.ViewPagerAdapter
|
||||
import com.simplemobiletools.clock.extensions.config
|
||||
import com.simplemobiletools.commons.extensions.restartActivity
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.helpers.LICENSE_KOTLIN
|
||||
import com.simplemobiletools.commons.models.FAQItem
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
|
||||
class MainActivity : SimpleActivity() {
|
||||
private var storedUseEnglish = false
|
||||
private var storedTextColor = 0
|
||||
private var storedBackgroundColor = 0
|
||||
private var storedPrimaryColor = 0
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
appLaunched()
|
||||
storeStateVariables()
|
||||
initFragments()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
|
@ -28,6 +36,24 @@ class MainActivity : SimpleActivity() {
|
|||
return
|
||||
}
|
||||
|
||||
val configTextColor = config.textColor
|
||||
if (storedTextColor != configTextColor) {
|
||||
getInactiveTabIndexes(viewpager.currentItem).forEach {
|
||||
main_tabs_holder.getTabAt(it)?.icon?.applyColorFilter(configTextColor)
|
||||
}
|
||||
}
|
||||
|
||||
val configBackgroundColor = config.backgroundColor
|
||||
if (storedBackgroundColor != configBackgroundColor) {
|
||||
main_tabs_holder.background = ColorDrawable(configBackgroundColor)
|
||||
}
|
||||
|
||||
val configPrimaryColor = config.primaryColor
|
||||
if (storedPrimaryColor != configPrimaryColor) {
|
||||
main_tabs_holder.setSelectedTabIndicatorColor(getAdjustedPrimaryColor())
|
||||
main_tabs_holder.getTabAt(viewpager.currentItem)?.icon?.applyColorFilter(getAdjustedPrimaryColor())
|
||||
}
|
||||
|
||||
if (config.preventPhoneFromSleeping) {
|
||||
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||
}
|
||||
|
@ -41,6 +67,11 @@ class MainActivity : SimpleActivity() {
|
|||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
config.lastUsedViewPagerPage = viewpager.currentItem
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
menuInflater.inflate(R.menu.menu, menu)
|
||||
return true
|
||||
|
@ -56,9 +87,52 @@ class MainActivity : SimpleActivity() {
|
|||
}
|
||||
|
||||
private fun storeStateVariables() {
|
||||
storedUseEnglish = config.useEnglish
|
||||
config.apply {
|
||||
storedTextColor = textColor
|
||||
storedBackgroundColor = backgroundColor
|
||||
storedPrimaryColor = primaryColor
|
||||
storedUseEnglish = useEnglish
|
||||
}
|
||||
}
|
||||
|
||||
private fun initFragments() {
|
||||
viewpager.adapter = ViewPagerAdapter(this)
|
||||
viewpager.onPageChangeListener {
|
||||
main_tabs_holder.getTabAt(it)?.select()
|
||||
}
|
||||
|
||||
viewpager.currentItem = config.lastUsedViewPagerPage
|
||||
main_tabs_holder.onTabSelectionChanged(
|
||||
tabUnselectedAction = {
|
||||
it.icon?.applyColorFilter(config.textColor)
|
||||
},
|
||||
tabSelectedAction = {
|
||||
viewpager.currentItem = it.position
|
||||
it.icon?.applyColorFilter(getAdjustedPrimaryColor())
|
||||
}
|
||||
)
|
||||
|
||||
setupTabColors()
|
||||
}
|
||||
|
||||
private fun setupTabColors() {
|
||||
val lastUsedPage = config.lastUsedViewPagerPage
|
||||
main_tabs_holder.apply {
|
||||
background = ColorDrawable(config.backgroundColor)
|
||||
setSelectedTabIndicatorColor(getAdjustedPrimaryColor())
|
||||
getTabAt(lastUsedPage)?.apply {
|
||||
select()
|
||||
icon?.applyColorFilter(getAdjustedPrimaryColor())
|
||||
}
|
||||
|
||||
getInactiveTabIndexes(lastUsedPage).forEach {
|
||||
getTabAt(it)?.icon?.applyColorFilter(config.textColor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getInactiveTabIndexes(activeIndex: Int) = arrayListOf(0, 1, 2).filter { it != activeIndex }
|
||||
|
||||
private fun launchSettings() {
|
||||
startActivity(Intent(applicationContext, SettingsActivity::class.java))
|
||||
}
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
package com.simplemobiletools.clock.adapters
|
||||
|
||||
import android.support.v4.view.PagerAdapter
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.simplemobiletools.clock.R
|
||||
import com.simplemobiletools.clock.activities.SimpleActivity
|
||||
|
||||
class ViewPagerAdapter(val activity: SimpleActivity) : PagerAdapter() {
|
||||
|
||||
override fun instantiateItem(container: ViewGroup, position: Int): Any {
|
||||
val layout = getFragment(position)
|
||||
val view = activity.layoutInflater.inflate(layout, container, false)
|
||||
container.addView(view)
|
||||
return view
|
||||
}
|
||||
|
||||
override fun destroyItem(container: ViewGroup, position: Int, item: Any) {
|
||||
container.removeView(item as View)
|
||||
}
|
||||
|
||||
override fun getCount() = 3
|
||||
override fun isViewFromObject(view: View, item: Any) = view == item
|
||||
|
||||
private fun getFragment(position: Int) = when (position) {
|
||||
0 -> R.layout.fragment_clock
|
||||
1 -> R.layout.fragment_alarm
|
||||
else -> R.layout.fragment_stopwatch
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.simplemobiletools.clock.fragments
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.widget.RelativeLayout
|
||||
|
||||
class AlarmFragment(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet) {
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.simplemobiletools.clock.fragments
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.widget.RelativeLayout
|
||||
|
||||
class ClockFragment(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet) {
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.simplemobiletools.clock.fragments
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.widget.RelativeLayout
|
||||
|
||||
class StopwatchkFragment(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet) {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<com.simplemobiletools.clock.fragments.AlarmFragment
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/alarm_fragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
</com.simplemobiletools.clock.fragments.AlarmFragment>
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<com.simplemobiletools.clock.fragments.ClockFragment
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/clock_fragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
</com.simplemobiletools.clock.fragments.ClockFragment>
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<com.simplemobiletools.clock.fragments.StopwatchkFragment
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/stopwatch_fragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
</com.simplemobiletools.clock.fragments.StopwatchkFragment>
|
Loading…
Reference in New Issue