Michael Paus
11/03/2022, 12:06 PMfun readText(): String
but
fun readText(context: Context): String
on Android. On any other platform than Android there is no Context. Am I supposed to somehow wrap that again in some expect/actual code of my own, or what? I just don’t get it.Javier
11/03/2022, 12:13 PMJavier
11/03/2022, 12:15 PMViewModel
.
https://medium.com/androiddevelopers/locale-changes-and-the-androidviewmodel-antipattern-84eb677660d9Javier
11/03/2022, 12:20 PMLocalContext
. But it would be accessing in the view tooMichael Paus
11/03/2022, 12:52 PMJavier
11/03/2022, 1:05 PMJavier
11/03/2022, 1:07 PMJavier
11/03/2022, 1:10 PMContext
in a singleton and with that initializer change it to the context provided, so you can access it in your actual without passing it as parameter
var globalContext: Context? = null
class FileInitializer: Initializer<Context> {
override fun create(context: Context): FileInitializer {
globalContext = context
return ...
}
override fun dependencies(): List<Class<out Initializer<*>>> = ...
}
Kirill Kharkov
11/07/2022, 2:14 AMexpect class TextReader {
fun readText(): String
}
then have a platform specific class that might have platform specific fields
Android:
actual class TextReader(private val context: Context) {
fun readText(): String {
return context.getString(...)
}
}
and iOS
actual class TextReader {
fun readText(): String {
// iOS implementation goes here
}
}
Similar concept is described in docs here