Skip to content

Commit 7a3edfc

Browse files
committed
wip
1 parent c859ea8 commit 7a3edfc

File tree

6 files changed

+42
-39
lines changed

6 files changed

+42
-39
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ dependencies {
8585
implementation(libs.androidx.room.runtime)
8686
implementation(libs.vico.compose)
8787
implementation(libs.vico.compose.m3)
88+
implementation(libs.gson)
8889
annotationProcessor(libs.androidx.room.compiler)
8990
ksp(libs.androidx.room.compiler)
9091
testImplementation(libs.junit)

app/src/main/java/com/waldo121/pongstats/MainActivity.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import androidx.compose.ui.unit.dp
7171
import androidx.lifecycle.compose.collectAsStateWithLifecycle
7272
import androidx.lifecycle.viewmodel.MutableCreationExtras
7373
import androidx.lifecycle.viewmodel.compose.viewModel
74+
import androidx.lifecycle.lifecycleScope
7475
import com.patrykandpatrick.vico.compose.cartesian.CartesianChartHost
7576
import com.patrykandpatrick.vico.compose.cartesian.axis.rememberAxisTickComponent
7677
import com.patrykandpatrick.vico.compose.cartesian.axis.rememberBottom
@@ -95,8 +96,10 @@ import com.waldo121.pongstats.ui.theme.PingPongWood
9596
import com.waldo121.pongstats.ui.theme.PongStatsTheme
9697
import com.waldo121.pongstats.viewModel.MatchRecordViewModel
9798
import com.waldo121.pongstats.viewModel.StatisticsViewModel
99+
import com.waldo121.pongstats.util.ExportRoomDatabase
98100
import kotlinx.coroutines.Dispatchers
99101
import kotlinx.coroutines.launch
102+
import kotlinx.coroutines.withContext
100103
import java.time.LocalDate
101104
import java.time.ZoneId
102105
import java.time.format.DateTimeFormatter
@@ -137,8 +140,10 @@ class MainActivity : ComponentActivity() {
137140
appDatabase = MatchRecordsDatabase.getDatabase(this)
138141

139142
lifecycleScope.launch {
140-
val path = exportRoomDatabase(this@MainActivity, appDatabase)
141-
Toast.makeText(this@MainActivity, "Export terminé : $path", Toast.LENGTH_LONG).show()
143+
val path = ExportRoomDatabase(this@MainActivity, appDatabase)
144+
withContext(Dispatchers.Main) {
145+
Toast.makeText(this@MainActivity, "Export terminé : $path", Toast.LENGTH_LONG).show()
146+
}
142147
}
143148

144149
enableEdgeToEdge()

app/src/main/java/com/waldo121/pongstats/data/repository/MatchRecordRepository.kt

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.waldo121.pongstats.data.repository
22

3-
43
import com.waldo121.pongstats.data.local.MatchRecordsDatabase
54
import com.waldo121.pongstats.data.local.entities.DoubleMatchRecordEntity
65
import com.waldo121.pongstats.data.local.entities.SingleMatchRecordEntity
@@ -15,9 +14,10 @@ import kotlinx.coroutines.flow.map
1514
import kotlinx.coroutines.withContext
1615
import java.sql.SQLException
1716

18-
class MatchRecordRepository (
17+
class MatchRecordRepository(
1918
private val matchRecordLocalDatabase: MatchRecordsDatabase
2019
) {
20+
2121
@Throws(SQLException::class)
2222
suspend fun createSingleMatchRecord(record: SingleMatchRecord) {
2323
withContext(Dispatchers.IO) {
@@ -28,12 +28,9 @@ class MatchRecordRepository (
2828
}
2929

3030
@Throws(SQLException::class)
31-
suspend fun getAllSingleMatchRecords(): Flow<List<SingleMatchRecord>> {
32-
return withContext(Dispatchers.IO) {
33-
val dao = matchRecordLocalDatabase.singleMatchRecordDao()
34-
return@withContext dao.getAll()
35-
.map { entities: List<SingleMatchRecordEntity> -> entities.map { it.toDomain() } }
36-
}
31+
fun getAllSingleMatchRecords(): Flow<List<SingleMatchRecord>> {
32+
return matchRecordLocalDatabase.singleMatchRecordDao().getAll()
33+
.map { entities: List<SingleMatchRecordEntity> -> entities.map { it.toDomain() } }
3734
}
3835

3936
@Throws(SQLException::class)
@@ -46,14 +43,9 @@ class MatchRecordRepository (
4643
}
4744

4845
@Throws(SQLException::class)
49-
suspend fun getAllDoubleMatchRecords(): Flow<List<DoubleMatchRecord>> {
50-
return withContext(Dispatchers.IO) {
51-
val dao = matchRecordLocalDatabase.doubleMatchRecordDao()
52-
53-
return@withContext dao.getAll()
54-
.map { entities: List<DoubleMatchRecordEntity> -> entities.map { it.toDomain() } }
55-
56-
}
46+
fun getAllDoubleMatchRecords(): Flow<List<DoubleMatchRecord>> {
47+
return matchRecordLocalDatabase.doubleMatchRecordDao().getAll()
48+
.map { entities: List<DoubleMatchRecordEntity> -> entities.map { it.toDomain() } }
5749
}
5850

5951
fun getAllUniquePlayerNames(): Flow<List<String>> {

app/src/main/java/com/waldo121/pongstats/viewModel/PlayerProfileViewModel.kt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@ package com.waldo121.pongstats.viewModel
22

33
import androidx.lifecycle.ViewModel
44
import androidx.lifecycle.viewModelScope
5-
import com.waldo121.pongstats.data.model.DoubleMatchRecord
65
import com.waldo121.pongstats.data.model.SingleMatchRecord
76
import com.waldo121.pongstats.data.repository.MatchRecordRepository
87
import com.waldo121.pongstats.ui.screens.PlayerStatsUi
98
import kotlinx.coroutines.flow.MutableStateFlow
109
import kotlinx.coroutines.flow.StateFlow
1110
import kotlinx.coroutines.flow.asStateFlow
12-
import kotlinx.coroutines.flow.combine
1311
import kotlinx.coroutines.launch
12+
import kotlinx.coroutines.flow.map
13+
import kotlinx.coroutines.flow.stateIn
14+
import kotlinx.coroutines.flow.SharingStarted
15+
16+
1417

1518
class PlayerProfileViewModel(
1619
private val repository: MatchRecordRepository,
1720
private val playerName: String
1821
) : ViewModel() {
19-
private val _stats = MutableStateFlow(PlayerStatsUi(0, 0, 0))
20-
val stats: StateFlow<PlayerStatsUi> = _stats.asStateFlow()
21-
22-
init {
23-
viewModelScope.launch {
24-
combine(
25-
repository.getAllSingleMatchRecords()
26-
) { singleMatches, doubleMatches ->
27-
val single = singleMatches.filter { it.opponentName == playerName }
28-
val totalWins = single.sumOf { it.numberOfWins }
29-
val totalDefeats = single.sumOf { it.numberOfDefeats }
30-
val totalMatches = totalWins + totalDefeats
31-
val winRate = if (totalMatches > 0) (totalWins * 100 / totalMatches) else 0
32-
PlayerStatsUi(totalWins, totalDefeats, winRate)
33-
}.collect { _stats.value = it }
22+
val stats: StateFlow<PlayerStatsUi> = repository.getAllSingleMatchRecords()
23+
.map { singleMatches ->
24+
val single = singleMatches.filter { it.opponentName == playerName }
25+
val totalWins = single.sumOf { it.numberOfWins }
26+
val totalDefeats = single.sumOf { it.numberOfDefeats }
27+
val totalMatches = totalWins + totalDefeats
28+
val winRate = if (totalMatches > 0) (totalWins * 100 / totalMatches) else 0
29+
PlayerStatsUi(totalWins, totalDefeats, winRate)
3430
}
35-
}
31+
.stateIn(
32+
scope = viewModelScope,
33+
started = kotlinx.coroutines.flow.SharingStarted.Lazily,
34+
initialValue = PlayerStatsUi(0, 0, 0)
35+
)
3636
}

app/src/main/java/com/waldo121/util/ExportDatabase.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ import com.google.gson.GsonBuilder
77
import kotlinx.coroutines.Dispatchers
88
import kotlinx.coroutines.withContext
99
import java.io.File
10+
import com.waldo121.pongstats.data.local.dao.DoubleMatchRecordDao
11+
import com.waldo121.pongstats.data.local.dao.SingleMatchRecordDao
12+
import com.waldo121.pongstats.data.local.MatchRecordsDatabase
1013

1114
// Fonction d'export complète
12-
suspend fun exportRoomDatabase(context: Context, db: MatchRecordsDatabase) = withContext(Dispatchers.IO) {
15+
suspend fun ExportRoomDatabase(context: Context, db: MatchRecordsDatabase) = withContext(Dispatchers.IO) {
1316
val gson = GsonBuilder().setPrettyPrinting().create()
1417

1518
// 🔹 Récupère les données de chaque DAO
1619
val singleMatches = db.singleMatchRecordDao().getAll()
17-
val doubleMatches = db.DoubleMatchRecordDao().getAll()
20+
val doubleMatches = db.doubleMatchRecordDao().getAll()
1821

1922
// 🔹 Mets tout dans un seul objet
20-
val exportData = mapOf(
23+
val exportData: Map<String, Any> = mapOf(
2124
"single_match_records" to singleMatches,
2225
"double_match_records" to doubleMatches,
2326
)

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ navigationCompose = "2.8.9"
1212
room = "2.7.1"
1313
ksp = "2.0.0-1.0.24"
1414
vico = "2.2.0-alpha.1"
15+
gson = "2.10.1"
1516

1617
[libraries]
1718
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@@ -33,6 +34,7 @@ androidx-room-runtime = { group = "androidx.room", name = "room-runtime", versio
3334
androidx-room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" }
3435
vico-compose = { group = "com.patrykandpatrick.vico", name = "compose", version.ref = "vico" }
3536
vico-compose-m3 = { group = "com.patrykandpatrick.vico", name = "compose-m3", version.ref = "vico" }
37+
gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
3638

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

0 commit comments

Comments
 (0)