Skip to content

Commit

Permalink
update with Compose typed navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
gbrah committed Oct 6, 2024
1 parent e089297 commit c44dfe7
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 62 deletions.
164 changes: 144 additions & 20 deletions composeApp/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,21 @@ import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.toRoute
import screens.questionScreen
import screens.scoreScreen
import screens.welcomeScreen
import kotlin.random.Random
import kotlinx.serialization.Serializable

@Serializable
object WelcomeRoute

@Serializable
object QuizRoute

@Serializable
data class ScoreRoute(val score: Int, val questionSize: Int)

@Composable
fun App(
Expand All @@ -21,28 +32,141 @@ fun App(
MaterialTheme {
NavHost(
navController = navController,
startDestination = "/welcome",
startDestination = WelcomeRoute,
) {
composable(route = "/welcome") {
composable<WelcomeRoute>() {
welcomeScreen(
onStartButtonPushed = {
navController.navigate(route = "/quiz")
navController.navigate(route = QuizRoute)
}
)
}
composable(route = "/quiz") {
composable<QuizRoute>() {
val questions by viewModel.questionState.collectAsState()
// random manga character names
val nicknames = listOf("Naruto", "Goku", "Luffy", "Ichigo", "Saitama", "Kenshin", "Yusuke", "Gon", "Killua", "Natsu",
"Gon", "Gintoki", "Koro-sensei", "Kakashi", "Vegeta", "Sasuke", "Zoro", "Sanji", "Shanks", "Ace", "Kaido", "Katakuri",
"Law", "Deku", "Bakugo", "Todoroki", "All Might", "Asta", "Yuno", "Yami", "Julius", "Licht", "Nero", "Grimm", "Gordon",
"Yami", "Magna", "Luck", "Noelle", "Mimosa", "Vanessa", "Finral", "Charmy", "Gordon", "Grey", "Gimodelo", "Zora", "Mereoleona",
"Fuegoleon", "Leopold", "Kahono", "Kiato", "Yuno", "Yami", "Julius", "Licht", "Nero", "Grimm", "Gordon", "Yami", "Magna",
"Luck", "Noelle", "Mimosa", "Vanessa", "Finral", "Charmy", "Gordon", "Grey", "Gimodelo", "Zora", "Mereoleona", "Fuegoleon",
"Leopold", "Kahono", "Kiato", "Yuno", "Yami", "Julius", "Licht", "Nero", "Grimm", "Gordon", "Yami", "Magna", "Luck", "Noelle",
"Mimosa", "Vanessa", "Finral", "Charmy", "Gordon", "Grey", "Gimodelo", "Zora", "Mereoleona", "Fuegoleon", "Leopold", "Kahono",
"Kiato", "Yuno", "Yami", "Julius", "Licht", "Nero", "Grimm", "Gordon", "Yami", "Magna", "Luck", "Noelle", "Mimosa", "Vanessa",
"Finral", "Charmy", "Gordon", "Grey", "Gimodelo", "Zora", "Mereoleona", "Fuegoleon")
val nicknames = listOf(
"Naruto",
"Goku",
"Luffy",
"Ichigo",
"Saitama",
"Kenshin",
"Yusuke",
"Gon",
"Killua",
"Natsu",
"Gon",
"Gintoki",
"Koro-sensei",
"Kakashi",
"Vegeta",
"Sasuke",
"Zoro",
"Sanji",
"Shanks",
"Ace",
"Kaido",
"Katakuri",
"Law",
"Deku",
"Bakugo",
"Todoroki",
"All Might",
"Asta",
"Yuno",
"Yami",
"Julius",
"Licht",
"Nero",
"Grimm",
"Gordon",
"Yami",
"Magna",
"Luck",
"Noelle",
"Mimosa",
"Vanessa",
"Finral",
"Charmy",
"Gordon",
"Grey",
"Gimodelo",
"Zora",
"Mereoleona",
"Fuegoleon",
"Leopold",
"Kahono",
"Kiato",
"Yuno",
"Yami",
"Julius",
"Licht",
"Nero",
"Grimm",
"Gordon",
"Yami",
"Magna",
"Luck",
"Noelle",
"Mimosa",
"Vanessa",
"Finral",
"Charmy",
"Gordon",
"Grey",
"Gimodelo",
"Zora",
"Mereoleona",
"Fuegoleon",
"Leopold",
"Kahono",
"Kiato",
"Yuno",
"Yami",
"Julius",
"Licht",
"Nero",
"Grimm",
"Gordon",
"Yami",
"Magna",
"Luck",
"Noelle",
"Mimosa",
"Vanessa",
"Finral",
"Charmy",
"Gordon",
"Grey",
"Gimodelo",
"Zora",
"Mereoleona",
"Fuegoleon",
"Leopold",
"Kahono",
"Kiato",
"Yuno",
"Yami",
"Julius",
"Licht",
"Nero",
"Grimm",
"Gordon",
"Yami",
"Magna",
"Luck",
"Noelle",
"Mimosa",
"Vanessa",
"Finral",
"Charmy",
"Gordon",
"Grey",
"Gimodelo",
"Zora",
"Mereoleona",
"Fuegoleon"
)

if (questions.isNotEmpty()) {
questionScreen(
Expand All @@ -52,7 +176,7 @@ fun App(
score,
"${nicknames.random()}-${Random.nextInt(1000)}"
)
navController.navigate(route = "/score/$score/$questionSize")
navController.navigate(route = ScoreRoute(score, questionSize))
},
/* FOR SPEAKER TALK DEMO ON WEB APP */
onSaveStatQuestion = { id: Long, question: String, answerId: Long, correctAnswerId: Long, answer: String ->
Expand All @@ -61,16 +185,16 @@ fun App(
)
}
}
composable(route = "/score/{score}/{total}") {
composable<ScoreRoute> { backStackEntry ->
val scoreRoute: ScoreRoute = backStackEntry.toRoute<ScoreRoute>()
scoreScreen(
score = it.arguments?.getString("score").toString(),
total = it.arguments?.getString("total").toString(),
score = scoreRoute.score,
total = scoreRoute.questionSize,
onResetButtonPushed = {
navController.navigate(route = "/quiz")
navController.navigate(route = QuizRoute)
}
)
}

}
}
}
19 changes: 10 additions & 9 deletions composeApp/src/commonMain/kotlin/data/QuizRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@ import data.datasources.QuizKStoreDataSource
import data.datasources.StatsApiDatasource
import kotlinx.datetime.Clock

class QuizRepository {
class QuizRepository {

private val mockDataSource = MockDataSource()
private val quizApiDatasource = QuizApiDatasource()
private var quizKStoreDataSource = QuizKStoreDataSource()

private suspend fun fetchQuiz(): List<Question> = quizApiDatasource.getAllQuestions().questions

/* FOR SPEAKER TALK DEMO ON WEB APP */ private val statsDataSource = StatsApiDatasource()
/* FOR SPEAKER TALK DEMO ON WEB APP */ private val statsDataSource = StatsApiDatasource()

private suspend fun fetchAndStoreQuiz(): List<Question>{
private suspend fun fetchAndStoreQuiz(): List<Question> {
quizKStoreDataSource.resetQuizKstore()
val questions = fetchQuiz()
val questions = fetchQuiz()
quizKStoreDataSource.insertQuestions(questions)
quizKStoreDataSource.setUpdateTimeStamp(Clock.System.now().epochSeconds)
return questions
}
suspend fun updateQuiz():List<Question>{

suspend fun updateQuiz(): List<Question> {
try {
val lastRequest = quizKStoreDataSource.getUpdateTimeStamp()
return if(lastRequest == 0L || lastRequest - Clock.System.now().epochSeconds > 300000){
return if (lastRequest == 0L || lastRequest - Clock.System.now().epochSeconds > 300000) {
fetchAndStoreQuiz()
}else{
} else {
quizKStoreDataSource.getAllQuestions()
}
} catch (e: NullPointerException) {
Expand All @@ -42,7 +43,7 @@ class QuizRepository {
}

/* FOR SPEAKER TALK DEMO ON WEB APP */
suspend fun storeStats(nickname:String, score: Int, responses : List<QuestionStats>){
statsDataSource.postQuestionStats(score,nickname,responses)
suspend fun storeStats(nickname: String, score: Int, responses: List<QuestionStats>) {
statsDataSource.postQuestionStats(score, nickname, responses)
}
}
8 changes: 4 additions & 4 deletions composeApp/src/commonMain/kotlin/screens/ScoreScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import org.jetbrains.compose.ui.tooling.preview.Preview
@Composable
internal fun scoreScreenPreview() {
val onResetButtonPushed = { }
scoreScreen(onResetButtonPushed, score = "10", total = "10")
scoreScreen(onResetButtonPushed, score = 10, total = 10)
}

@Composable
internal fun scoreScreen(onResetButtonPushed: () -> Unit, score: String, total: String) {
internal fun scoreScreen(onResetButtonPushed: () -> Unit, score: Int, total: Int) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.fillMaxWidth().fillMaxHeight()
Expand All @@ -45,7 +45,7 @@ internal fun scoreScreen(onResetButtonPushed: () -> Unit, score: String, total:
fontSize = 30.sp,
text = "$score/$total",
)

Button(
modifier = Modifier.padding(all = 20.dp),
onClick = {
Expand All @@ -61,7 +61,7 @@ internal fun scoreScreen(onResetButtonPushed: () -> Unit, score: String, total:
}
}

private fun generateScoringColor(score: String, total: String): Color {
private fun generateScoringColor(score: Int, total: Int): Color {
val percentage = (score.toFloat() / total.toFloat()) * 100
return when {
percentage <= 40 -> Color.Red // red
Expand Down
57 changes: 29 additions & 28 deletions composeApp/src/commonMain/kotlin/screens/WelcomeScreen.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package screens

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
Expand All @@ -21,12 +22,12 @@ import quiz.composeapp.generated.resources.question
@Preview
@Composable
internal fun welcomeScreenPreview() {
val onStartButtonPushed = { }
val onStartButtonPushed = { }
welcomeScreen(onStartButtonPushed)
}

@Composable
internal fun welcomeScreen(onStartButtonPushed: () -> Unit) {
fun welcomeScreen(onStartButtonPushed: () -> Unit) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.fillMaxWidth().fillMaxHeight()
Expand All @@ -35,33 +36,33 @@ internal fun welcomeScreen(onStartButtonPushed: () -> Unit) {
shape = RoundedCornerShape(8.dp),
modifier = Modifier.padding(10.dp),
) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Image(
modifier = Modifier.size(70.dp),
painter = painterResource(Res.drawable.question),
contentDescription = null
)
Text(
text = "Quiz",
fontSize = 30.sp,
modifier = Modifier.padding(all = 10.dp)
)
Text(
modifier = Modifier.padding(all = 10.dp),
text = "A simple Quiz to discovers KMP and compose.",
)
Button(
modifier = Modifier.padding(all = 10.dp),
onClick = { onStartButtonPushed() }
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Image(
modifier = Modifier.size(70.dp),
painter = painterResource(Res.drawable.question),
contentDescription = null
)
Text(
text = "Quiz",
fontSize = 30.sp,
modifier = Modifier.padding(all = 10.dp)
)
Text(
modifier = Modifier.padding(all = 10.dp),
text = "A simple Quiz to discovers KMP and compose.",
)
Button(
modifier = Modifier.padding(all = 10.dp),
onClick = { onStartButtonPushed() }

) {
Icon(
Icons.AutoMirrored.Filled.ArrowForward,
contentDescription = "Localized description",
Modifier.padding(end = 15.dp)
)
Text("Start the Quiz")
}
) {
Icon(
Icons.AutoMirrored.Filled.ArrowForward,
contentDescription = "Localized description",
Modifier.padding(end = 15.dp)
)
Text("Start the Quiz")
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion composeApp/src/desktopMain/kotlin/Platform.jvm.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import data.dataclasses.Quiz
import io.github.xxfast.kstore.KStore
import io.github.xxfast.kstore.file.storeOf
import net.harawata.appdirs.AppDirsFactory
import okio.Path.Companion.toPath

class JVMPlatform : Platform {
Expand Down

0 comments on commit c44dfe7

Please sign in to comment.