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