diff --git a/composeApp/src/androidMain/kotlin/resources/AppFile.android.kt b/composeApp/src/androidMain/kotlin/resources/AppFile.android.kt index c6c70a9..64bfeb6 100644 --- a/composeApp/src/androidMain/kotlin/resources/AppFile.android.kt +++ b/composeApp/src/androidMain/kotlin/resources/AppFile.android.kt @@ -17,6 +17,12 @@ actual class AppFile actual constructor(val fileName: String) { return context?.deleteFile(fileName) ?: false } + actual fun readText(): String? { + return context?.openFileInput(fileName)?.bufferedReader()?.use { + it.readText() + } + } + companion object { var context: android.content.Context? = null } diff --git a/composeApp/src/commonMain/kotlin/resources/AppFile.kt b/composeApp/src/commonMain/kotlin/resources/AppFile.kt index de83034..5481d37 100644 --- a/composeApp/src/commonMain/kotlin/resources/AppFile.kt +++ b/composeApp/src/commonMain/kotlin/resources/AppFile.kt @@ -5,4 +5,5 @@ expect class AppFile(fileName: String) { fun exists(): Boolean fun create(): Boolean fun delete(): Boolean + fun readText(): String? } diff --git a/composeApp/src/desktopMain/kotlin/resources/AppFile.desktop.kt b/composeApp/src/desktopMain/kotlin/resources/AppFile.desktop.kt index 6950402..8290b1d 100644 --- a/composeApp/src/desktopMain/kotlin/resources/AppFile.desktop.kt +++ b/composeApp/src/desktopMain/kotlin/resources/AppFile.desktop.kt @@ -15,6 +15,14 @@ actual class AppFile actual constructor(fileName: String) { } actual fun delete(): Boolean { + if (!file.exists()) + return false return file.delete() } + + actual fun readText(): String? { + if (!file.exists()) + return null + return file.readText() + } } \ No newline at end of file diff --git a/composeApp/src/desktopTest/kotlin/resources/ReadAppFileTest.kt b/composeApp/src/desktopTest/kotlin/resources/ReadAppFileTest.kt new file mode 100644 index 0000000..576f7bd --- /dev/null +++ b/composeApp/src/desktopTest/kotlin/resources/ReadAppFileTest.kt @@ -0,0 +1,44 @@ +package resources + +import io.kotest.matchers.nulls.shouldBeNull +import io.kotest.matchers.nulls.shouldNotBeNull +import io.kotest.matchers.string.shouldBeEmpty +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test + +class ReadAppFileTest { + + val appFile = AppFile("appFile.json") + + @BeforeTest + fun setup() { + if(appFile.exists()) { + appFile.delete() + } + } + + @AfterTest + fun teardown() { + if(appFile.exists()) { + appFile.delete() + } + } + + @Test + fun `Non-existent App File Reads null`() { + val appFile = AppFile("appFile.json") + appFile.readText() + .shouldBeNull() + } + + @Test + fun `Reads Empty App File`() { + val appFile = AppFile("appFile.json") + appFile.create() + + appFile.readText() + .shouldNotBeNull() + .shouldBeEmpty() + } +} \ No newline at end of file diff --git a/composeApp/src/nativeMain/kotlin/resources/AppFile.native.kt b/composeApp/src/nativeMain/kotlin/resources/AppFile.native.kt index 04b09b5..1ea4678 100644 --- a/composeApp/src/nativeMain/kotlin/resources/AppFile.native.kt +++ b/composeApp/src/nativeMain/kotlin/resources/AppFile.native.kt @@ -13,4 +13,8 @@ actual class AppFile actual constructor(fileName: String) { actual fun delete(): Boolean { TODO("Not yet implemented") } + + actual fun readText(): String? { + TODO("Not yet implemented") + } } \ No newline at end of file