Skip to content

Commit

Permalink
Minor code simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeso committed Jun 19, 2024
1 parent 94a2f9d commit 6c707be
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

### Notes

- Minor code simplifications
- Add Obtainium badge
- Update Gradle wrapper
- Add toolchain resolver plugin
Expand Down
18 changes: 8 additions & 10 deletions src/main/kotlin/ui/ClockScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,16 @@ fun ClockScreen(
onClockStop = onClockStop,
)

OrientationHandler(onOrientationChanged = { orientation = it })
OrientationHandler { orientation = it }

LeaningSideHandler(
orientationProvider = { orientation },
leaningSideProvider = { leaningSide },
onLeaningSideChanged = {
leaningSide = when (leaningSide) {
LeaningSide.LEFT -> LeaningSide.RIGHT
LeaningSide.RIGHT -> LeaningSide.LEFT
}
},
)
orientationProvider = { orientation }, leaningSideProvider = { leaningSide },
) {
leaningSide = when (leaningSide) {
LeaningSide.LEFT -> LeaningSide.RIGHT
LeaningSide.RIGHT -> LeaningSide.LEFT
}
}

ClockBackHandler(
clockStateProvider = { clockState },
Expand Down
8 changes: 2 additions & 6 deletions src/main/kotlin/ui/components/OrientationHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ fun OrientationHandler(onOrientationChanged: (orientation: Int) -> Unit) {

val orientationEventListener = object : OrientationEventListener(context) {
override fun onOrientationChanged(orientation: Int) {
if (orientation == ORIENTATION_UNKNOWN) {
return
} else {
if (orientation != ORIENTATION_UNKNOWN) {
currentOnOrientationChanged((orientation + rotation) % 360)
}
}
Expand All @@ -45,8 +43,6 @@ fun OrientationHandler(onOrientationChanged: (orientation: Int) -> Unit) {
LifecycleStartEffect(Unit, lifecycleOwner) {
orientationEventListener.enable()

onStopOrDispose {
orientationEventListener.disable()
}
onStopOrDispose { orientationEventListener.disable() }
}
}
24 changes: 9 additions & 15 deletions src/test/kotlin/ui/ClockViewModelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,11 @@ class ClockViewModelTest {
clockViewModel.save()
clockViewModel.restore(addMinutes = addMinutes, addSeconds = addSeconds)

val newMinutes: Float
val newSeconds: Float
initialTime.toComponents { minutes, seconds, nanoseconds ->
newMinutes = minutes.toFloat() + addMinutes
newSeconds = seconds.toFloat() + nanoseconds.toFloat() / 1_000_000_000F + addSeconds
val expectedTime = initialTime.toComponents { minutes, seconds, nanoseconds ->
val newMinutes = minutes.toFloat() + addMinutes
val newSeconds = seconds.toFloat() + nanoseconds.toFloat() / 1_000_000_000F + addSeconds
newMinutes.roundToInt().minutes + newSeconds.roundToInt().seconds
}
val expectedTime = newMinutes.roundToInt().minutes + newSeconds.roundToInt().seconds
assertEquals(expectedTime, clockViewModel.whiteTime.value)
assertEquals(expectedTime, clockViewModel.blackTime.value)
}
Expand Down Expand Up @@ -227,16 +225,12 @@ class ClockViewModelTest {
clockViewModel.save()
clockViewModel.restore(addMinutes = addMinutes, addSeconds = addSeconds)

val newMinutes: Float
val newSeconds: Float
(initialTime - delayTime).toComponents { minutes, seconds, nanoseconds ->
newMinutes = minutes.toFloat() + addMinutes
newSeconds = seconds.toFloat() + nanoseconds.toFloat() / 1_000_000_000F + addSeconds
val expectedTime = (initialTime - delayTime).toComponents { minutes, seconds, nanoseconds ->
val newMinutes = minutes.toFloat() + addMinutes
val newSeconds = seconds.toFloat() + nanoseconds.toFloat() / 1_000_000_000F + addSeconds
newMinutes.roundToInt().minutes + newSeconds.roundToInt().seconds
}
assertEquals(
newMinutes.roundToInt().minutes + newSeconds.roundToInt().seconds,
clockViewModel.whiteTime.value,
)
assertEquals(expectedTime, clockViewModel.whiteTime.value)
}

@Test
Expand Down

0 comments on commit 6c707be

Please sign in to comment.