From 0b0fd8f5af46a732898d30ae28280455d13fe298 Mon Sep 17 00:00:00 2001 From: Daniele Verducci Date: Wed, 15 Jan 2025 08:08:26 +0100 Subject: [PATCH] Refactoring FileLogbookRepository --- .../lunatracker/repository/FileLogbookRepository.kt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 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 5d239da..8026a27 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/repository/FileLogbookRepository.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/repository/FileLogbookRepository.kt @@ -31,7 +31,7 @@ class FileLogbookRepository: LogbookRepository { fun loadLogbook(context: Context, name: String): Logbook { val logbook = Logbook(name) - val fileName = "$FILE_NAME_START{${if (name.isNotEmpty()) "_" else ""}{$name}$FILE_NAME_END" + val fileName = getFileName(name) val file = File(context.getFilesDir(), fileName) val json = FileInputStream(file).bufferedReader().use { it.readText() } val ja = JSONArray(json) @@ -57,8 +57,7 @@ class FileLogbookRepository: LogbookRepository { } fun saveLogbook(context: Context, logbook: Logbook) { - val name = logbook.name - val fileName = "$FILE_NAME_START${if (name.isNotEmpty()) "_" else ""}${name}$FILE_NAME_END" + val fileName = getFileName(logbook.name) val file = File(context.getFilesDir(), fileName) val ja = JSONArray() for (l in logbook.logs) { @@ -92,4 +91,8 @@ class FileLogbookRepository: LogbookRepository { } return logbooksNames } + + private fun getFileName(name: String): String { + return "$FILE_NAME_START${if (name.isNotEmpty()) "_" else ""}${name}$FILE_NAME_END" + } } \ No newline at end of file