-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
resources/src/androidMain/kotlin/dev/icerock/moko/resources/FileResource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package dev.icerock.moko.resources | ||
|
||
import java.io.File | ||
import java.net.URI | ||
|
||
actual class FileResource( | ||
val assetsPath: String | ||
) { | ||
actual fun readText(): String { | ||
val assetUri: URI = URI.create("file:///android_asset/$assetsPath") | ||
val file: File = File(assetUri) | ||
return file.readText() | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
resources/src/commonMain/kotlin/dev/icerock/moko/resources/FileResource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package dev.icerock.moko.resources | ||
|
||
expect class FileResource { | ||
fun readText(): String | ||
} |
42 changes: 42 additions & 0 deletions
42
resources/src/iosMain/kotlin/dev/icerock/moko/resources/FileResource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package dev.icerock.moko.resources | ||
|
||
import kotlinx.cinterop.ObjCObjectVar | ||
import kotlinx.cinterop.alloc | ||
import kotlinx.cinterop.memScoped | ||
import kotlinx.cinterop.ptr | ||
import kotlinx.cinterop.value | ||
import platform.Foundation.NSBundle | ||
import platform.Foundation.NSError | ||
import platform.Foundation.NSString | ||
import platform.Foundation.NSURL | ||
import platform.Foundation.NSUTF8StringEncoding | ||
import platform.Foundation.stringWithContentsOfFile | ||
|
||
actual class FileResource( | ||
val fileName: String, | ||
val bundle: NSBundle = NSBundle.mainBundle | ||
) { | ||
val path: String get() = bundle.pathForResource(name = fileName, ofType = null, inDirectory = "files")!! | ||
val url: NSURL get() = bundle.URLForResource(name = fileName, withExtension = null, subdirectory = "files")!! | ||
|
||
actual fun readText(): String { | ||
val (result: String?, error: NSError?) = memScoped { | ||
val p = alloc<ObjCObjectVar<NSError?>>() | ||
val result: String? = runCatching { | ||
NSString.stringWithContentsOfFile( | ||
path = path, | ||
encoding = NSUTF8StringEncoding, | ||
error = p.ptr | ||
) | ||
}.getOrNull() | ||
result to p.value | ||
} | ||
|
||
if (error != null) throw RuntimeException(error.localizedDescription) | ||
else return result!! | ||
} | ||
} |