From 5222b48fc6116a98afaeeabe27eedec5cd2cc9ed Mon Sep 17 00:00:00 2001 From: Daniele Verducci Date: Mon, 18 Nov 2024 07:51:47 +0100 Subject: [PATCH] Working local file save --- .../lunatracker/repository/FileLogbookRepository.kt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/it/danieleverducci/lunatracker/repository/FileLogbookRepository.kt b/app/src/main/java/it/danieleverducci/lunatracker/repository/FileLogbookRepository.kt index f56a8d1..fc4741d 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/repository/FileLogbookRepository.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/repository/FileLogbookRepository.kt @@ -18,7 +18,10 @@ class FileLogbookRepository: LogbookRepository { try { listener.onLogbookLoaded(loadLogbook(context)) } catch (e: FileNotFoundException) { - Log.d(TAG, "No logbook file found, will be created at first save") + Log.d(TAG, "No logbook file found, create one") + val newLogbook = Logbook() + saveLogbook(context, newLogbook) + listener.onLogbookLoaded(newLogbook) } } @@ -40,12 +43,16 @@ class FileLogbookRepository: LogbookRepository { logbook: Logbook, listener: LogbookSavedListener ) { + saveLogbook(context, logbook) + listener.onLogbookSaved() + } + + fun saveLogbook(context: Context, logbook: Logbook) { val file = File(context.getFilesDir(), "data.json") val ja = JSONArray() for (l in logbook.logs) { ja.put(l.toJson()) } file.writeText(ja.toString()) - listener.onLogbookSaved() } } \ No newline at end of file