Fix colors updating on resume on main activity tabs

This commit is contained in:
Ensar Sarajčić 2023-08-16 12:51:06 +02:00
parent 69330a24f7
commit af84c04acc
4 changed files with 34 additions and 68 deletions

View File

@ -15,6 +15,7 @@ import com.simplemobiletools.clock.extensions.*
import com.simplemobiletools.clock.helpers.*
import com.simplemobiletools.clock.interfaces.ToggleAlarmInterface
import com.simplemobiletools.clock.models.Alarm
import com.simplemobiletools.commons.extensions.getProperBackgroundColor
import com.simplemobiletools.commons.extensions.getProperTextColor
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.updateTextColors
@ -26,12 +27,9 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
private var alarms = ArrayList<Alarm>()
private var currentEditAlarmDialog: EditAlarmDialog? = null
private var storedTextColor = 0
private lateinit var binding: FragmentAlarmBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
storeStateVariables()
binding = FragmentAlarmBinding.inflate(inflater, container, false)
return binding.root
}
@ -39,16 +37,6 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
override fun onResume() {
super.onResume()
setupViews()
val configTextColor = requireContext().getProperTextColor()
if (storedTextColor != configTextColor) {
(binding.alarmsList.adapter as AlarmsAdapter).updateTextColor(configTextColor)
}
}
override fun onPause() {
super.onPause()
storeStateVariables()
}
fun showSortingDialog() {
@ -57,10 +45,6 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
}
}
private fun storeStateVariables() {
storedTextColor = requireContext().getProperTextColor()
}
private fun setupViews() {
binding.apply {
requireContext().updateTextColors(alarmFragment)
@ -108,7 +92,12 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
binding.alarmsList.adapter = this
}
} else {
(currAdapter as AlarmsAdapter).updateItems(alarms)
(currAdapter as AlarmsAdapter).apply {
updatePrimaryColor()
updateBackgroundColor(requireContext().getProperBackgroundColor())
updateTextColor(requireContext().getProperTextColor())
updateItems(alarms)
}
}
}

View File

@ -17,6 +17,7 @@ import com.simplemobiletools.clock.extensions.*
import com.simplemobiletools.clock.helpers.getPassedSeconds
import com.simplemobiletools.clock.models.MyTimeZone
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.getProperBackgroundColor
import com.simplemobiletools.commons.extensions.getProperTextColor
import com.simplemobiletools.commons.extensions.updateTextColors
import java.util.Calendar
@ -28,12 +29,9 @@ class ClockFragment : Fragment() {
private var calendar = Calendar.getInstance()
private val updateHandler = Handler()
private var storedTextColor = 0
private lateinit var binding: FragmentClockBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
storeStateVariables()
binding = FragmentClockBinding.inflate(inflater, container, false)
return binding.root
}
@ -42,22 +40,12 @@ class ClockFragment : Fragment() {
super.onResume()
setupDateTime()
val configTextColor = requireContext().getProperTextColor()
if (storedTextColor != configTextColor) {
(binding.timeZonesList.adapter as? TimeZonesAdapter)?.updateTextColor(configTextColor)
}
binding.clockDate.setTextColor(configTextColor)
binding.clockDate.setTextColor(requireContext().getProperTextColor())
}
override fun onPause() {
super.onPause()
updateHandler.removeCallbacksAndMessages(null)
storeStateVariables()
}
private fun storeStateVariables() {
storedTextColor = requireContext().getProperTextColor()
}
private fun setupDateTime() {
@ -139,7 +127,12 @@ class ClockFragment : Fragment() {
this@ClockFragment.binding.timeZonesList.adapter = this
}
} else {
(currAdapter as TimeZonesAdapter).updateItems(timeZones)
(currAdapter as TimeZonesAdapter).apply {
updatePrimaryColor()
updateBackgroundColor(requireContext().getProperBackgroundColor())
updateTextColor(requireContext().getProperTextColor())
updateItems(timeZones)
}
}
}

View File

@ -25,13 +25,10 @@ import com.simplemobiletools.commons.helpers.SORT_DESCENDING
class StopwatchFragment : Fragment() {
private var storedTextColor = 0
lateinit var stopwatchAdapter: StopwatchAdapter
private lateinit var binding: FragmentStopwatchBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
storeStateVariables()
val sorting = requireContext().config.stopwatchLapsSort
Lap.sorting = sorting
binding = FragmentStopwatchBinding.inflate(inflater, container, false).apply {
@ -81,11 +78,6 @@ class StopwatchFragment : Fragment() {
super.onResume()
setupViews()
val configTextColor = requireContext().getProperTextColor()
if (storedTextColor != configTextColor) {
stopwatchAdapter.updateTextColor(configTextColor)
}
Stopwatch.addUpdateListener(updateListener)
updateLaps()
binding.stopwatchSortingIndicatorsHolder.beVisibleIf(Stopwatch.laps.isNotEmpty())
@ -101,14 +93,9 @@ class StopwatchFragment : Fragment() {
override fun onPause() {
super.onPause()
storeStateVariables()
Stopwatch.removeUpdateListener(updateListener)
}
private fun storeStateVariables() {
storedTextColor = requireContext().getProperTextColor()
}
private fun setupViews() {
val properPrimaryColor = requireContext().getProperPrimaryColor()
binding.apply {
@ -202,7 +189,12 @@ class StopwatchFragment : Fragment() {
}
private fun updateLaps() {
stopwatchAdapter.updateItems(Stopwatch.laps)
stopwatchAdapter.apply {
updatePrimaryColor()
updateBackgroundColor(requireContext().getProperBackgroundColor())
updateTextColor(requireContext().getProperTextColor())
updateItems(Stopwatch.laps)
}
}
private val updateListener = object : Stopwatch.UpdateListener {

View File

@ -17,6 +17,7 @@ import com.simplemobiletools.clock.extensions.timerHelper
import com.simplemobiletools.clock.helpers.DisabledItemChangeAnimator
import com.simplemobiletools.clock.models.Timer
import com.simplemobiletools.clock.models.TimerEvent
import com.simplemobiletools.commons.extensions.getProperBackgroundColor
import com.simplemobiletools.commons.extensions.getProperTextColor
import com.simplemobiletools.commons.extensions.hideKeyboard
import com.simplemobiletools.commons.extensions.updateTextColors
@ -30,7 +31,6 @@ class TimerFragment : Fragment() {
private lateinit var binding: FragmentTimerBinding
private lateinit var timerAdapter: TimerAdapter
private var timerPositionToScrollTo = INVALID_POSITION
private var storedTextColor = 0
private var currentEditAlarmDialog: EditTimerDialog? = null
override fun onCreate(savedInstanceState: Bundle?) {
@ -45,7 +45,6 @@ class TimerFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentTimerBinding.inflate(inflater, container, false).apply {
storeStateVariables()
timersList.itemAnimator = DisabledItemChangeAnimator()
timerAdd.setOnClickListener {
activity?.run {
@ -55,7 +54,7 @@ class TimerFragment : Fragment() {
}
}
initAdapter()
initOrUpdateAdapter()
refreshTimers()
// the initial timer is created asynchronously at first launch, make sure we show it once created
@ -68,25 +67,22 @@ class TimerFragment : Fragment() {
return binding.root
}
private fun initAdapter() {
timerAdapter = TimerAdapter(requireActivity() as SimpleActivity, binding.timersList, ::refreshTimers, ::openEditTimer)
binding.timersList.adapter = timerAdapter
private fun initOrUpdateAdapter() {
if (this::timerAdapter.isInitialized) {
timerAdapter.updatePrimaryColor()
timerAdapter.updateBackgroundColor(requireContext().getProperBackgroundColor())
timerAdapter.updateTextColor(requireContext().getProperTextColor())
} else {
timerAdapter = TimerAdapter(requireActivity() as SimpleActivity, binding.timersList, ::refreshTimers, ::openEditTimer)
binding.timersList.adapter = timerAdapter
}
}
override fun onResume() {
super.onResume()
requireContext().updateTextColors(binding.root)
val configTextColor = requireContext().getProperTextColor()
if (storedTextColor != configTextColor) {
initAdapter()
timerAdapter.updateTextColor(configTextColor)
refreshTimers()
}
}
override fun onPause() {
super.onPause()
storeStateVariables()
initOrUpdateAdapter()
refreshTimers()
}
private fun refreshTimers(scrollToLatest: Boolean = false) {
@ -106,10 +102,6 @@ class TimerFragment : Fragment() {
}
}
private fun storeStateVariables() {
storedTextColor = requireContext().getProperTextColor()
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(event: TimerEvent.Refresh) {
refreshTimers()