From 5ba67476cc4b52e4f351e81fff36847d9a73e800 Mon Sep 17 00:00:00 2001 From: Joshua Kuestersteffen Date: Fri, 29 Dec 2023 13:17:58 -0600 Subject: [PATCH] Avoid class level state --- .../slimlauncher/ui/main/BinaryClockView.kt | 15 ++++++++++- .../slimlauncher/ui/main/HomeFragment.kt | 26 +++++-------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/sduduzog/slimlauncher/ui/main/BinaryClockView.kt b/app/src/main/java/com/sduduzog/slimlauncher/ui/main/BinaryClockView.kt index 7f3dbef5..970de365 100644 --- a/app/src/main/java/com/sduduzog/slimlauncher/ui/main/BinaryClockView.kt +++ b/app/src/main/java/com/sduduzog/slimlauncher/ui/main/BinaryClockView.kt @@ -4,6 +4,7 @@ import android.content.Context import android.graphics.Canvas import android.graphics.Paint import android.graphics.RectF +import android.text.format.DateFormat import android.util.AttributeSet import com.sduduzog.slimlauncher.R import java.util.Calendar @@ -17,9 +18,10 @@ class BinaryClockView(context: Context, attrs: AttributeSet) private var border: Float private var distance: Float private val bounds = RectF(0F, 0F, 0F, 0F) - var is24Hour: Boolean = false + private var is24Hour: Boolean = false init { + is24Hour = is24HourTimeFormat(context) onPaint.style = Paint.Style.FILL_AND_STROKE offPaint.style = Paint.Style.STROKE context.theme.obtainStyledAttributes( @@ -98,4 +100,15 @@ class BinaryClockView(context: Context, attrs: AttributeSet) setMeasuredDimension(w, h) } } + + private fun is24HourTimeFormat(context: Context): Boolean { + val settingsKey = context.getString(R.string.prefs_settings) + val timeFormatKey = context.getString(R.string.prefs_settings_key_time_format); + val preferences = context.getSharedPreferences(settingsKey, Context.MODE_PRIVATE) + return when (preferences.getInt(timeFormatKey, 0)) { + 1 -> true + 2 -> false + else -> DateFormat.is24HourFormat(context) + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/sduduzog/slimlauncher/ui/main/HomeFragment.kt b/app/src/main/java/com/sduduzog/slimlauncher/ui/main/HomeFragment.kt index 58be6d5b..164ad15b 100644 --- a/app/src/main/java/com/sduduzog/slimlauncher/ui/main/HomeFragment.kt +++ b/app/src/main/java/com/sduduzog/slimlauncher/ui/main/HomeFragment.kt @@ -70,9 +70,6 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener { uninstallAppLauncher = registerForActivityResult(StartActivityForResult()) { refreshApps() } } - private var date = Date() - private var currentLocale = Locale.getDefault() - enum class ClockType { NOCLOCK, DIGITAL, @@ -80,7 +77,6 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener { BINARY } private var clockType = ClockType.DIGITAL - private var timeFormat = 0 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { val coreRepository = unlauncherDataSource.corePreferencesRepo @@ -99,17 +95,9 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener { val alignmentKey: String = getString(R.string.prefs_settings_alignment) val preferences = requireContext().getSharedPreferences(settingsKey, Context.MODE_PRIVATE) val alignment = preferences.getInt(alignmentKey, 3) - timeFormat = preferences.getInt(getString(R.string.prefs_settings_key_time_format), 0) - val is24Hour = when (timeFormat) { - 1 -> true - 2 -> false - else -> DateFormat.is24HourFormat(context) - } - clockType = ClockType.values()[preferences.getInt(getString(R.string.prefs_settings_key_clock_type), ClockType.DIGITAL.ordinal)] home_fragment_analog_time.setHiddenState(clockType != ClockType.ANALOG) home_fragment_bin_time.setHiddenState(clockType != ClockType.BINARY) - home_fragment_bin_time.is24Hour = is24Hour if (clockType != ClockType.DIGITAL) { home_fragment_time.height = 0 @@ -331,19 +319,19 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener { } private fun updateClockDigital () { + val timeFormat = context?.getSharedPreferences(getString(R.string.prefs_settings), Context.MODE_PRIVATE) + ?.getInt(getString(R.string.prefs_settings_key_time_format), 0) val fWatchTime = when (timeFormat) { - 1 -> SimpleDateFormat("H:mm", currentLocale) - 2 -> SimpleDateFormat("h:mm aa", currentLocale) + 1 -> SimpleDateFormat("H:mm", Locale.getDefault()) + 2 -> SimpleDateFormat("h:mm aa", Locale.getDefault()) else -> DateFormat.getTimeFormat(context) } - home_fragment_time.text = fWatchTime.format(date) + home_fragment_time.text = fWatchTime.format(Date()) } private fun updateDate() { - date = Date() - currentLocale = Locale.getDefault() - val fWatchDate = SimpleDateFormat(getString(R.string.main_date_format), currentLocale) - home_fragment_date.text = fWatchDate.format(date) + val fWatchDate = SimpleDateFormat(getString(R.string.main_date_format), Locale.getDefault()) + home_fragment_date.text = fWatchDate.format(Date()) } override fun onLaunch(app: HomeApp, view: View) {