Skip to content

Commit c40cc66

Browse files
committed
Fix Export & Import notes
- Change ActivityResultContracts MIME type from "application/json" to "application/zip"
1 parent 9bacfb1 commit c40cc66

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

app/src/main/java/com/vladshurakov/jetnotesapp/feature_settings/presenter/screen/SettingsScreen.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ fun SettingsScreen(
5757
) {
5858

5959
val exportDataLauncher = rememberLauncherForActivityResult(
60-
ActivityResultContracts.CreateDocument("application/json")
60+
ActivityResultContracts.CreateDocument("application/zip")
6161
) { uri ->
6262
if (uri == null) return@rememberLauncherForActivityResult
63-
val jsonString = Gson().toJson(settingsViewModel.getAll())
63+
val json = Gson().toJson(settingsViewModel.getAllNotes())
6464
navController.context.contentResolver.openOutputStream(uri).use {
6565
it?.bufferedWriter()?.apply {
66-
write(jsonString)
66+
write(json)
6767
flush()
6868
}
6969
}
@@ -73,11 +73,15 @@ fun SettingsScreen(
7373
ActivityResultContracts.OpenDocument()
7474
) { uri ->
7575
if (uri == null) return@rememberLauncherForActivityResult
76-
val jsonString = navController.context.contentResolver.openInputStream(uri).use {
76+
val json = navController.context.contentResolver.openInputStream(uri).use {
7777
it?.bufferedReader()?.readText() ?: "[]"
7878
}
7979
val type: Type = object : TypeToken<List<Note>>() {}.type
80-
val notes = Gson().fromJson<List<Note>>(jsonString, type).toMutableList()
80+
val notes = Gson().fromJson<List<Note>>(json, type).toMutableList()
81+
/*
82+
* It changes id to null to auto-generate new id
83+
* (will be duplicated without it)
84+
*/
8185
notes.onEachIndexed { index, note ->
8286
notes[index] = note.copy(id = null)
8387
}
@@ -277,7 +281,7 @@ fun SettingsScreen(
277281
}
278282

279283
TextButton(
280-
onClick = { importDataLauncher.launch(arrayOf("application/json")) },
284+
onClick = { importDataLauncher.launch(arrayOf("application/zip")) },
281285
shape = RectangleShape,
282286
contentPadding = PaddingValues(start = 18.dp),
283287
modifier = Modifier

app/src/main/java/com/vladshurakov/jetnotesapp/feature_settings/presenter/viewmodel/SettingsViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class SettingsViewModel @Inject constructor(
3939
}
4040
}
4141

42-
fun getAll(): List<Note> {
42+
fun getAllNotes(): List<Note> {
4343
return notesUseCases.getAllNotes.invoke()
4444
}
4545

0 commit comments

Comments
 (0)