Skip to content

Commit 186cf64

Browse files
committed
add sliding transitions when navigating between screens
1 parent 1268df9 commit 186cf64

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

app/src/main/java/com/devx/tictacpro/MainActivity.kt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import androidx.activity.compose.setContent
66
import androidx.activity.enableEdgeToEdge
77
import androidx.compose.animation.ExperimentalSharedTransitionApi
88
import androidx.compose.animation.SharedTransitionLayout
9+
import androidx.compose.animation.fadeIn
10+
import androidx.compose.animation.fadeOut
11+
import androidx.compose.animation.slideInHorizontally
12+
import androidx.compose.animation.slideOutHorizontally
913
import androidx.compose.runtime.Composable
1014
import androidx.compose.runtime.getValue
1115
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
@@ -56,7 +60,14 @@ fun TicTacProNav(
5660

5761
SharedTransitionLayout {
5862
NavHost(navController = navController, startDestination = startDest) {
59-
composable<Route.AuthScreen> {
63+
composable<Route.AuthScreen>(
64+
enterTransition = {
65+
slideInHorizontally(initialOffsetX = { it })
66+
},
67+
exitTransition = {
68+
slideOutHorizontally(targetOffsetX = { -it })
69+
}
70+
) {
6071
val viewModel = viewModel<AuthViewModel> {
6172
AuthViewModel(
6273
TicTacProApp.appModule.userPrefs,
@@ -74,7 +85,7 @@ fun TicTacProNav(
7485
)
7586
}
7687

77-
composable<Route.HomeScreen> {
88+
composable<Route.HomeScreen>{
7889
val viewModel = viewModel<HomeViewModel>{
7990
HomeViewModel(
8091
TicTacProApp.appModule.userPrefs,
@@ -126,7 +137,15 @@ fun TicTacProNav(
126137
)
127138
}
128139

129-
composable<Route.Game>(typeMap = mapOf(typeOf<Player?>() to PlayerNavType)) {
140+
composable<Route.Game>(
141+
typeMap = mapOf(typeOf<Player?>() to PlayerNavType),
142+
enterTransition = {
143+
slideInHorizontally(initialOffsetX = { it })
144+
},
145+
exitTransition = {
146+
slideOutHorizontally(targetOffsetX = { it })
147+
}
148+
) {
130149
val args = it.toRoute<Route.Game>()
131150
val viewModel = viewModel<GameViewModel> {
132151
GameViewModel(
@@ -141,6 +160,7 @@ fun TicTacProNav(
141160
gameState = gameState,
142161
onEvent = viewModel::onEvent,
143162
onNavigateBack = { navController.navigateUp() },
163+
isOnlineGame = args.isOnlineGame,
144164
myTurn = args.myTurn
145165
)
146166
}

app/src/main/java/com/devx/tictacpro/presentation/game/GameScreen.kt

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import kotlinx.coroutines.delay
6969
@Composable
7070
fun GameScreen(
7171
gameState: GameState,
72+
isOnlineGame: Boolean,
7273
onEvent: (GameEvent)-> Unit,
7374
onNavigateBack: ()-> Unit = {},
7475
myTurn: String? = null
@@ -195,18 +196,20 @@ fun GameScreen(
195196
}
196197
}
197198

198-
TicTacToeField(
199-
state = gameState,
200-
onTap = { pos->
201-
if(gameState.winner == null) {
202-
myTurn?.let {
203-
if(it == gameState.playerAtTurn) onEvent(GameEvent.UpdateGame(pos))
204-
} ?: onEvent(GameEvent.UpdateGame(pos))
205-
}
206-
},
207-
modifier = Modifier
208-
.padding(32.dp)
209-
)
199+
if((isOnlineGame && gameState.gameCode == null) || !isOnlineGame) {
200+
TicTacToeField(
201+
state = gameState,
202+
onTap = { pos->
203+
if(gameState.winner == null) {
204+
myTurn?.let {
205+
if(it == gameState.playerAtTurn) onEvent(GameEvent.UpdateGame(pos))
206+
} ?: onEvent(GameEvent.UpdateGame(pos))
207+
}
208+
},
209+
modifier = Modifier
210+
.padding(32.dp)
211+
)
212+
}
210213

211214
Button(
212215
onClick = { onEvent(GameEvent.ResetGame) },
@@ -434,6 +437,7 @@ fun GameBoard(
434437
) {
435438
val lineLength = remember { Animatable(0f) }
436439
LaunchedEffect(key1 = Unit) {
440+
delay(300)
437441
lineLength.animateTo(targetValue = 1f, animationSpec = tween(animationDuration))
438442
}
439443
Canvas(modifier = modifier) {
@@ -549,6 +553,7 @@ private fun GameScreenPreview() {
549553
avatar = R.drawable.boy_avatar_2
550554
)
551555
),
556+
isOnlineGame = false,
552557
onEvent = {}
553558
)
554559
}

0 commit comments

Comments
 (0)