Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.compose)
kotlin("plugin.serialization") version "2.0.20"
}

android {
Expand Down Expand Up @@ -40,7 +41,8 @@ android {
}

dependencies {
implementation("com.google.code.gson:gson:2.8.9")
implementation(libs.kotlinx.datetime)
implementation(libs.kotlinx.serialization.json)
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import com.example.dadflex.preferences.HighscoreEntry
import com.example.dadflex.preferences.PreferencesHelper
import com.example.dadflex.ui.theme.DadflexTheme
import kotlinx.coroutines.delay
import java.util.Date
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant


@Composable
Expand Down Expand Up @@ -109,7 +110,7 @@ fun GameScreen(
val newHighScoreAdded = highscore.checkAndAddHighscore(HighscoreEntry(
name = name,
reactionTime = result,
date = Date()
date = Clock.System.now()
))

if (newHighScoreAdded){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.example.dadflex.preferences

import java.util.Date
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable

@Serializable
data class HighscoreEntry(
val name : String,
val reactionTime : Long,
val date : Date
val date : Instant
)

@Serializable
class Highscore {
private val highscoreList = mutableListOf<HighscoreEntry>()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.dadflex.preferences

import com.google.gson.Gson

import android.content.Context
import android.content.SharedPreferences
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

object PreferencesHelper {
private const val PREFS_NAME = "dadflex_prefs"
Expand All @@ -25,17 +25,24 @@ object PreferencesHelper {
}

fun saveHighscore(context: Context, highscore: Highscore) {
val gson = Gson()
val highscoreJson = gson.toJson(highscore)
// Convert to JSON with kotlin serialization
val highscoreJson = Json.encodeToString(highscore)

val editor = getPreferences(context).edit()

editor.putString(KEY_HIGHSCORE, highscoreJson)
editor.apply()
}

fun getHighscore(context: Context): Highscore {
val gson = Gson()
val json = getPreferences(context).getString(KEY_HIGHSCORE, null) ?: return Highscore()
return gson.fromJson(json, Highscore::class.java)
// Decode with kotlin serialization

return try {
Json.decodeFromString(json)
} catch (e: Exception) {
// Old java.util.Date format e.g 'Dec 10, 2024 4:42:44 PM' can't be parsed :(
Highscore()
}
}
}
21 changes: 13 additions & 8 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,28 @@ lifecycleRuntimeKtx = "2.6.1"
activityCompose = "1.8.0"
composeBom = "2024.04.01"
navigationCompose = "2.8.4"
kotlinxDatetime = "0.4.0"
kotlinxSerializationJson = "1.7.3"


[libraries]
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "navigationCompose" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "navigationCompose" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
kotlinx-datetime = { group = "org.jetbrains.kotlinx", name = "kotlinx-datetime", version.ref = "kotlinxDatetime" }
kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down