From af84c04accaf5a89339fa9b9daf9a24fd122aebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Wed, 16 Aug 2023 12:51:06 +0200 Subject: [PATCH] Fix colors updating on resume on main activity tabs --- .../clock/fragments/AlarmFragment.kt | 25 ++++---------- .../clock/fragments/ClockFragment.kt | 23 +++++-------- .../clock/fragments/StopwatchFragment.kt | 20 ++++------- .../clock/fragments/TimerFragment.kt | 34 +++++++------------ 4 files changed, 34 insertions(+), 68 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt index ea70e656..75b68280 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt @@ -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() 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) + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/ClockFragment.kt b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/ClockFragment.kt index 9d3b15b1..5a20153e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/ClockFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/ClockFragment.kt @@ -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) + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/StopwatchFragment.kt b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/StopwatchFragment.kt index 57f1ff65..29de2314 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/StopwatchFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/StopwatchFragment.kt @@ -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 { diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt index fe12e5de..f41f6250 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt @@ -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()