Skip to content

Commit

Permalink
Avoid class level state
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuester committed Dec 29, 2023
1 parent 03fdd2f commit 5ba6747
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,13 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
uninstallAppLauncher = registerForActivityResult(StartActivityForResult()) { refreshApps() }
}

private var date = Date()
private var currentLocale = Locale.getDefault()

enum class ClockType {
NOCLOCK,
DIGITAL,
ANALOG,
BINARY
}
private var clockType = ClockType.DIGITAL
private var timeFormat = 0

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val coreRepository = unlauncherDataSource.corePreferencesRepo
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 5ba6747

Please sign in to comment.