Skip to content

Commit

Permalink
Rename "ChessClock" to "Clock"
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeso committed Mar 28, 2024
1 parent f952042 commit 7a6066c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 52 deletions.
4 changes: 2 additions & 2 deletions app/src/main/kotlin/net/leodesouza/blitz/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import net.leodesouza.blitz.ui.ChessClockScreen
import net.leodesouza.blitz.ui.ClockScreen

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -41,7 +41,7 @@ class MainActivity : ComponentActivity() {
}

setContent {
ChessClockScreen(
ClockScreen(
onClockStart = { window.addFlags(FLAG_KEEP_SCREEN_ON) },
onClockPause = { window.clearFlags(FLAG_KEEP_SCREEN_ON) },
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import net.leodesouza.blitz.ui.components.BasicTime
* @param[backEventSwipeEdgeProvider] Lambda for the swipe edge where the back gesture starts.
*/
@Composable
fun ChessClockContent(
fun ClockContent(
whiteTimeProvider: () -> Long,
blackTimeProvider: () -> Long,
isWhiteTurnProvider: () -> Boolean,
Expand Down Expand Up @@ -142,8 +142,8 @@ fun ChessClockContent(
/** Preview the chess clock screen content in Android Studio. */
@Preview
@Composable
private fun ChessClockContentPreview() {
ChessClockContent(
private fun ClockContentPreview() {
ClockContent(
whiteTimeProvider = { 5L * 60_000L },
blackTimeProvider = { 3L * 1_000L },
isWhiteTurnProvider = { true },
Expand All @@ -152,7 +152,7 @@ private fun ChessClockContentPreview() {
isTickingProvider = { false },
isPausedProvider = { true },
backEventProgressProvider = { 0F },
backEventSwipeEdgeProvider = { BackEventCompat.EDGE_RIGHT },
backEventSwipeEdgeProvider = { BackEventCompat.EDGE_LEFT },
)
}

Expand Down Expand Up @@ -191,7 +191,7 @@ private fun GraphicsLayerScope.setBasicTimeGraphics(
}

translationX = if (isTicking && !isPlayerTurn) {
0F // on pause, no translation on the time of the other player
0F // do not translate the time of the other player if the back gesture pauses the clock
} else {
val sign = if (backEventSwipeEdge == BackEventCompat.EDGE_RIGHT) -1F else 1F
sign * backEventProgress * screenWidth.toPx()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import androidx.compose.ui.input.pointer.pointerInput
* @param[restoreSavedTime] Callback called to restore the saved time.
* @param[restoreSavedConf] Callback called to restore the saved configuration.
*/
fun Modifier.chessClockInput(
fun Modifier.clockInput(
dragSensitivity: Float,
interactionSource: MutableInteractionSource,
isStartedProvider: () -> Boolean,
Expand All @@ -65,15 +65,15 @@ fun Modifier.chessClockInput(
restoreSavedConf: (Float, Float) -> Unit,
): Modifier = then(
clickable(interactionSource = interactionSource, indication = null) {
onChessClockClickEvent(
onClockClickEvent(
isTicking = isTickingProvider(),
isPaused = isPausedProvider(),
start = start,
nextPlayer = nextPlayer,
)
}
.onKeyEvent {
onChessClockKeyEvent(
onClockKeyEvent(
keyEvent = it,
isStarted = isStartedProvider(),
isPaused = isPausedProvider(),
Expand All @@ -87,18 +87,18 @@ fun Modifier.chessClockInput(
.pointerInput(Unit) {
detectHorizontalDragGestures(
onDragStart = {
onChessClockDragStart(
onClockDragStart(
isStarted = isStartedProvider(),
isPaused = isPausedProvider(),
saveTime = saveTime,
saveConf = saveConf,
)
},
onDragEnd = {
onChessClockDragEnd(isTicking = isTickingProvider(), nextPlayer = nextPlayer)
onClockDragEnd(isTicking = isTickingProvider(), nextPlayer = nextPlayer)
},
onHorizontalDrag = { _: PointerInputChange, dragAmount: Float ->
onChessClockHorizontalDrag(
onClockHorizontalDrag(
dragAmount = dragAmount,
dragSensitivity = dragSensitivity,
isStarted = isStartedProvider(),
Expand All @@ -115,18 +115,18 @@ fun Modifier.chessClockInput(
.pointerInput(Unit) {
detectVerticalDragGestures(
onDragStart = {
onChessClockDragStart(
onClockDragStart(
isStarted = isStartedProvider(),
isPaused = isPausedProvider(),
saveTime = saveTime,
saveConf = saveConf,
)
},
onDragEnd = {
onChessClockDragEnd(isTicking = isTickingProvider(), nextPlayer = nextPlayer)
onClockDragEnd(isTicking = isTickingProvider(), nextPlayer = nextPlayer)
},
onVerticalDrag = { _: PointerInputChange, dragAmount: Float ->
onChessClockVerticalDrag(
onClockVerticalDrag(
dragAmount = dragAmount,
dragSensitivity = dragSensitivity,
isStarted = isStartedProvider(),
Expand All @@ -150,7 +150,7 @@ fun Modifier.chessClockInput(
* @param[start] Callback called to start the clock.
* @param[nextPlayer] Callback called to switch to the next player.
*/
private fun onChessClockClickEvent(
private fun onClockClickEvent(
isTicking: Boolean, isPaused: Boolean, start: () -> Unit, nextPlayer: () -> Unit,
) {
if (isPaused) {
Expand All @@ -172,7 +172,7 @@ private fun onChessClockClickEvent(
* @param[restoreSavedTime] Callback called to restore the saved time.
* @param[restoreSavedConf] Callback called to restore the saved configuration.
*/
private fun onChessClockKeyEvent(
private fun onClockKeyEvent(
keyEvent: KeyEvent,
isStarted: Boolean,
isPaused: Boolean,
Expand Down Expand Up @@ -214,7 +214,7 @@ private fun onChessClockKeyEvent(
* @param[saveTime] Callback called to save the time.
* @param[saveConf] Callback called to save the configuration.
*/
private fun onChessClockDragStart(
private fun onClockDragStart(
isStarted: Boolean, isPaused: Boolean, saveTime: () -> Unit, saveConf: () -> Unit,
) {
if (isPaused) {
Expand All @@ -232,7 +232,7 @@ private fun onChessClockDragStart(
* @param[isTicking] Whether the clock is currently ticking.
* @param[nextPlayer] Callback called to switch to the next player.
*/
private fun onChessClockDragEnd(isTicking: Boolean, nextPlayer: () -> Unit) {
private fun onClockDragEnd(isTicking: Boolean, nextPlayer: () -> Unit) {
if (isTicking) {
nextPlayer()
}
Expand All @@ -252,7 +252,7 @@ private fun onChessClockDragEnd(isTicking: Boolean, nextPlayer: () -> Unit) {
* @param[restoreSavedTime] Callback called to restore the saved time.
* @param[restoreSavedConf] Callback called to restore the saved configuration.
*/
private fun onChessClockHorizontalDrag(
private fun onClockHorizontalDrag(
dragAmount: Float,
dragSensitivity: Float,
isStarted: Boolean,
Expand Down Expand Up @@ -300,7 +300,7 @@ private fun onChessClockHorizontalDrag(
* @param[restoreSavedTime] Callback called to restore the saved time.
* @param[restoreSavedConf] Callback called to restore the saved configuration.
*/
private fun onChessClockVerticalDrag(
private fun onClockVerticalDrag(
dragAmount: Float,
dragSensitivity: Float,
isStarted: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ import net.leodesouza.blitz.ui.components.OrientationHandler
* @param[dragSensitivity] How many minutes or seconds to add per dragged pixel.
* @param[onClockStart] Callback called before the clock starts ticking.
* @param[onClockPause] Callback called after the clock stops ticking.
* @param[chessClockViewModel] ViewModel holding the state and logic for this screen.
* @param[clockViewModel] ViewModel holding the state and logic for this screen.
*/
@Composable
fun ChessClockScreen(
fun ClockScreen(
durationMinutes: Long = 5L,
incrementSeconds: Long = 3L,
tickPeriod: Long = 100L,
dragSensitivity: Float = 0.01F,
onClockStart: () -> Unit = {},
onClockPause: () -> Unit = {},
chessClockViewModel: ChessClockViewModel = viewModel {
ChessClockViewModel(durationMinutes, incrementSeconds, tickPeriod)
clockViewModel: ClockViewModel = viewModel {
ClockViewModel(durationMinutes, incrementSeconds, tickPeriod)
},
) {
val uiState by chessClockViewModel.uiState.collectAsStateWithLifecycle()
val uiState by clockViewModel.uiState.collectAsStateWithLifecycle()
val isLandscape = LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE
val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl
var orientation by remember { mutableIntStateOf(0) }
Expand All @@ -88,35 +88,35 @@ fun ChessClockScreen(
onLeaningSideChanged = { isLeaningRight = !isLeaningRight },
)

ChessClockTickingEffect(
ClockTickingEffect(
currentTimeProvider = { uiState.currentTime },
isTickingProvider = { uiState.isTicking },
isFinishedProvider = { uiState.isFinished },
pause = {
chessClockViewModel.pause()
clockViewModel.pause()
onClockPause()
},
tick = chessClockViewModel::tick,
tick = clockViewModel::tick,
)

ChessClockBackHandler(
ClockBackHandler(
isStartedProvider = { uiState.isStarted },
isTickingProvider = { uiState.isTicking },
isDefaultConfProvider = { uiState.isDefaultConf },
pause = {
chessClockViewModel.pause()
chessClockViewModel.restoreSavedTime(isDecimalRestored = true)
clockViewModel.pause()
clockViewModel.restoreSavedTime(isDecimalRestored = true)
onClockPause()
},
resetTime = chessClockViewModel::resetTime,
resetConf = chessClockViewModel::resetConf,
saveTime = chessClockViewModel::saveTime,
resetTime = clockViewModel::resetTime,
resetConf = clockViewModel::resetConf,
saveTime = clockViewModel::saveTime,
updateProgress = { backEventProgress = it },
updateSwipeEdge = { backEventSwipeEdge = it },
)

Box(
modifier = Modifier.chessClockInput(
modifier = Modifier.clockInput(
dragSensitivity = dragSensitivity,
interactionSource = remember { MutableInteractionSource() },
isStartedProvider = { uiState.isStarted },
Expand All @@ -127,16 +127,16 @@ fun ChessClockScreen(
isRtl = isRtl,
start = {
onClockStart()
chessClockViewModel.start()
clockViewModel.start()
},
nextPlayer = chessClockViewModel::nextPlayer,
saveTime = chessClockViewModel::saveTime,
saveConf = chessClockViewModel::saveConf,
restoreSavedTime = chessClockViewModel::restoreSavedTime,
restoreSavedConf = chessClockViewModel::restoreSavedConf,
nextPlayer = clockViewModel::nextPlayer,
saveTime = clockViewModel::saveTime,
saveConf = clockViewModel::saveConf,
restoreSavedTime = clockViewModel::restoreSavedTime,
restoreSavedConf = clockViewModel::restoreSavedConf,
),
) {
ChessClockContent(
ClockContent(
whiteTimeProvider = { uiState.whiteTime },
blackTimeProvider = { uiState.blackTime },
isWhiteTurnProvider = { uiState.isWhiteTurn },
Expand Down Expand Up @@ -184,7 +184,7 @@ private fun IsLeaningRightHandler(
* @param[tick] Callback called to wait until next tick.
*/
@Composable
private fun ChessClockTickingEffect(
private fun ClockTickingEffect(
currentTimeProvider: () -> Long,
isTickingProvider: () -> Boolean,
isFinishedProvider: () -> Boolean,
Expand Down Expand Up @@ -220,7 +220,7 @@ private fun ChessClockTickingEffect(
* @param[updateSwipeEdge] Callback called to update the swipe edge where the back gesture starts.
*/
@Composable
private fun ChessClockBackHandler(
private fun ClockBackHandler(
isStartedProvider: () -> Boolean,
isTickingProvider: () -> Boolean,
isDefaultConfProvider: () -> Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package net.leodesouza.blitz.ui

/**
* UiState for the chess clock screen.
* UI state for the chess clock screen.
*
* @param[whiteTime] Remaining time for the first player in milliseconds.
* @param[blackTime] Remaining time for the second player in milliseconds.
Expand All @@ -26,7 +26,7 @@ package net.leodesouza.blitz.ui
* @param[isTicking] Whether the clock is currently ticking.
* @param[isDefaultConf] Whether the clock is set to its default configuration.
*/
data class ChessClockUiState(
data class ClockUiState(
val whiteTime: Long,
val blackTime: Long,
val isWhiteTurn: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import kotlin.math.sign
* @param[incrementSeconds] Time increment in seconds.
* @param[tickPeriod] Period between ticks in milliseconds.
*/
class ChessClockViewModel(
class ClockViewModel(
durationMinutes: Long,
incrementSeconds: Long,
private val tickPeriod: Long,
Expand All @@ -44,14 +44,14 @@ class ChessClockViewModel(
private var currentIncrement: Long = defaultIncrement
private var targetRealtime: Long = 0L

private val _uiState: MutableStateFlow<ChessClockUiState> = MutableStateFlow(
ChessClockUiState(
private val _uiState: MutableStateFlow<ClockUiState> = MutableStateFlow(
ClockUiState(
whiteTime = defaultDuration + defaultIncrement,
blackTime = defaultDuration + defaultIncrement,
)
)

val uiState: StateFlow<ChessClockUiState> = _uiState.asStateFlow()
val uiState: StateFlow<ClockUiState> = _uiState.asStateFlow()

private var savedDurationMinutes: Float = 0F
set(minutes) {
Expand Down

0 comments on commit 7a6066c

Please sign in to comment.