Skip to content

Commit

Permalink
Refactor: do some refactoring and add a string resource
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit9625 committed Aug 14, 2024
1 parent 8380851 commit 13b1801
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ data class OnlineGameState(
val boardValues: String = "---------",
val playerAtTurn: String = "X",
val winner: String? = null,
val draws: Int = 0,
val showDialog: Boolean = false,
val winningLine: List<Int> = emptyList(),
val draws: Int = 0
)

fun OnlineGameState.toGameState() = GameState(
Expand All @@ -22,8 +22,8 @@ fun OnlineGameState.toGameState() = GameState(
gameCode = gameCode,
boardValues = boardValues.toBoardMap(),
winner = winner,
draws = draws,
showDialog = showDialog
winningLine = winningLine,
draws = draws
)

fun GameState.toOnlineGameState() = OnlineGameState(
Expand All @@ -33,8 +33,8 @@ fun GameState.toOnlineGameState() = OnlineGameState(
gameCode = gameCode,
boardValues = boardValues.toBoardString(),
winner = winner,
draws = draws,
showDialog = showDialog
winningLine = winningLine,
draws = draws
)

private fun Map<Int, String?>.toBoardString(): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.devx.tictacpro.presentation.components

import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.tween
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Box
Expand All @@ -11,6 +13,7 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedCard
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -25,6 +28,7 @@ import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.devx.tictacpro.ui.theme.TicTacProTheme
import kotlinx.coroutines.launch

@Composable
fun SelectableIcon(
Expand Down Expand Up @@ -94,6 +98,66 @@ fun CircleIcon(
}
}

@Composable
fun AnimatedCross(
color: Color,
modifier: Modifier = Modifier,
animationDuration: Int = 500,
strokeWidth: Dp = 4.dp
) {
val lineLength = remember { Animatable(0f) }
val line2Length = remember { Animatable(1f) }

LaunchedEffect(key1 = Unit) {
launch { lineLength.animateTo(1f, tween(animationDuration)) }
launch { line2Length.animateTo(0f, tween(animationDuration)) }
}
Canvas(modifier = modifier) {
drawLine(
color = color,
start = Offset(0f, 0f),
end = Offset(size.width, size.height) * lineLength.value,
strokeWidth = strokeWidth.toPx(),
cap = StrokeCap.Round
)
drawLine(
color = color,
start = Offset(size.width, 0f),
end = Offset(size.width * line2Length.value, size.height * lineLength.value),
strokeWidth = strokeWidth.toPx(),
cap = StrokeCap.Round
)
}
}

@Composable
fun AnimatedCircle(
color: Color,
modifier: Modifier = Modifier,
animationDuration: Int = 500,
strokeWidth: Dp = 4.dp,
) {
val sweepAngle = remember { Animatable(0f) }
LaunchedEffect(key1 = Unit) {
sweepAngle.animateTo(360f, tween(animationDuration))
}
Canvas(modifier = modifier) {
drawArc(
color = color,
startAngle = 0f,
sweepAngle = sweepAngle.value,
topLeft = Offset(
0f,0f
),
useCenter = false,
size = this.size,
style = Stroke(
width = strokeWidth.toPx()
)
)
}
}

@PreviewLightDark
@Composable
private fun SelectableIconPreview() {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@
<string name="game_exit_confirmation">Do you want to exit the game?</string>
<string name="exit">Exit</string>
<string name="other_player_left">Other player had left the game</string>
<string name="share_game_code_invitation">Let\'s Play Together!\nJoin the game by this code: %s</string>
</resources>

0 comments on commit 13b1801

Please sign in to comment.