From 26cf21774ae2eca7f483236cfc4e9bd50a5ca9b5 Mon Sep 17 00:00:00 2001 From: Douglas Ronne Date: Wed, 10 Jul 2024 10:48:30 -0400 Subject: [PATCH] AppFile create, check existence, delete, works for android --- .../kotlin/resources/CreatesAppFileTest.kt | 34 +++++++++++++++++++ .../kotlin/resources/AppFile.android.kt | 15 +++++--- .../kotlin/resources/CreatesAppFileTest.kt | 0 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 composeApp/src/androidInstrumentedTest/kotlin/resources/CreatesAppFileTest.kt rename composeApp/src/{commonTest => desktopTest}/kotlin/resources/CreatesAppFileTest.kt (100%) diff --git a/composeApp/src/androidInstrumentedTest/kotlin/resources/CreatesAppFileTest.kt b/composeApp/src/androidInstrumentedTest/kotlin/resources/CreatesAppFileTest.kt new file mode 100644 index 0000000..0ac9566 --- /dev/null +++ b/composeApp/src/androidInstrumentedTest/kotlin/resources/CreatesAppFileTest.kt @@ -0,0 +1,34 @@ +package resources + +import androidx.test.platform.app.InstrumentationRegistry +import io.kotest.matchers.booleans.shouldBeFalse +import io.kotest.matchers.booleans.shouldBeTrue +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test + +class CreatesAppFileTest { + val saveFile = AppFile("saveFile.json") + + @BeforeTest + fun setup() { + AppFile.context = InstrumentationRegistry.getInstrumentation().targetContext + if(saveFile.exists()) { + saveFile.delete() + } + } + + @AfterTest + fun teardown() { + if(saveFile.exists()) { + saveFile.delete() + } + } + + @Test + fun createAppFile() { + saveFile.exists().shouldBeFalse() + saveFile.create().shouldBeTrue() + saveFile.exists().shouldBeTrue() + } +} \ No newline at end of file diff --git a/composeApp/src/androidMain/kotlin/resources/AppFile.android.kt b/composeApp/src/androidMain/kotlin/resources/AppFile.android.kt index 04b09b5..c6c70a9 100644 --- a/composeApp/src/androidMain/kotlin/resources/AppFile.android.kt +++ b/composeApp/src/androidMain/kotlin/resources/AppFile.android.kt @@ -1,16 +1,23 @@ package resources @Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING") -actual class AppFile actual constructor(fileName: String) { +actual class AppFile actual constructor(val fileName: String) { actual fun exists(): Boolean { - TODO("Not yet implemented") + return context?.getFileStreamPath(fileName)?.exists() ?: false } actual fun create(): Boolean { - TODO("Not yet implemented") + return context?.openFileOutput(fileName, android.content.Context.MODE_PRIVATE)?.use { + it.write(byteArrayOf()) + true + } ?: false } actual fun delete(): Boolean { - TODO("Not yet implemented") + return context?.deleteFile(fileName) ?: false + } + + companion object { + var context: android.content.Context? = null } } \ No newline at end of file diff --git a/composeApp/src/commonTest/kotlin/resources/CreatesAppFileTest.kt b/composeApp/src/desktopTest/kotlin/resources/CreatesAppFileTest.kt similarity index 100% rename from composeApp/src/commonTest/kotlin/resources/CreatesAppFileTest.kt rename to composeApp/src/desktopTest/kotlin/resources/CreatesAppFileTest.kt