Skip to content

Commit f831ad0

Browse files
committed
Add database export for debug purposes
1 parent da02553 commit f831ad0

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
<category android:name="android.intent.category.LAUNCHER" />
2424
</intent-filter>
2525
</activity>
26+
<provider
27+
android:name="androidx.core.content.FileProvider"
28+
android:authorities="${applicationId}.fileprovider"
29+
android:exported="false"
30+
android:grantUriPermissions="true">
31+
<meta-data
32+
android:name="android.support.FILE_PROVIDER_PATHS"
33+
android:resource="@xml/file_paths" />
34+
</provider>
35+
2636
</application>
2737

2838
</manifest>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,20 @@ import androidx.compose.ui.unit.DpOffset
127127
import androidx.compose.foundation.lazy.LazyColumn
128128
import androidx.compose.foundation.lazy.items
129129
import androidx.compose.foundation.layout.heightIn
130+
import android.widget.Toast
131+
130132

131133
class MainActivity : ComponentActivity() {
132134
private lateinit var appDatabase: MatchRecordsDatabase
133135
override fun onCreate(savedInstanceState: Bundle?) {
134136
super.onCreate(savedInstanceState)
135137
appDatabase = MatchRecordsDatabase.getDatabase(this)
138+
139+
lifecycleScope.launch {
140+
val path = exportRoomDatabase(this@MainActivity, MyRoomDatabase.getInstance(this@MainActivity))
141+
Toast.makeText(this@MainActivity, "Export terminé : $path", Toast.LENGTH_LONG).show()
142+
}
143+
136144
enableEdgeToEdge()
137145
setContent {
138146
App(
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.waldo121.pongstats.util
2+
3+
import android.content.Context
4+
import android.content.Intent
5+
import androidx.core.content.FileProvider
6+
import com.google.gson.GsonBuilder
7+
import kotlinx.coroutines.Dispatchers
8+
import kotlinx.coroutines.withContext
9+
import java.io.File
10+
11+
// Fonction d'export complète
12+
suspend fun exportRoomDatabase(context: Context, db: MyRoomDatabase) = withContext(Dispatchers.IO) {
13+
val gson = GsonBuilder().setPrettyPrinting().create()
14+
15+
// 🔹 Récupère les données de chaque DAO
16+
val users = db.userDao().getAll()
17+
val orders = db.orderDao().getAll()
18+
val products = db.productDao().getAll()
19+
20+
// 🔹 Mets tout dans un seul objet
21+
val exportData = mapOf(
22+
"single_match_records" to singleMatches,
23+
"double_match_records" to doubleMatches,
24+
)
25+
26+
// 🔹 Sérialise en JSON
27+
val json = gson.toJson(exportData)
28+
29+
// 🔹 Sauvegarde dans un fichier
30+
val exportFile = File(
31+
context.getExternalFilesDir(null),
32+
"room_export_${System.currentTimeMillis()}.json"
33+
)
34+
exportFile.writeText(json)
35+
36+
// 🔹 Optionnel : crée un intent pour partager
37+
val uri = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", exportFile)
38+
val shareIntent = Intent(Intent.ACTION_SEND).apply {
39+
type = "application/json"
40+
putExtra(Intent.EXTRA_STREAM, uri)
41+
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
42+
}
43+
44+
// Lance le sélecteur de partage
45+
withContext(Dispatchers.Main) {
46+
context.startActivity(Intent.createChooser(shareIntent, "Partager la base exportée"))
47+
}
48+
49+
exportFile.absolutePath // Retourne le chemin du fichier
50+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<paths xmlns:android="http://schemas.android.com/apk/res/android">
3+
<external-files-path name="export" path="." />
4+
</paths>

0 commit comments

Comments
 (0)