nrobi
09/03/2020, 9:26 AMApplicationContext
won’t work with all kinds of resources, while view specific context
would probably cause leaks.
How are you guys approaching this?Javier
09/03/2020, 10:58 AMnrobi
09/03/2020, 11:14 AMUiModels
in the VM, which the View just “renders” (we currently do this directly from databinding or in the fragment/activity).
At that point in order to make the VM platform agnostic you’d need some kind of ResourceMapper as I see italex009
09/03/2020, 11:17 AMStringDesc
from https://github.com/icerockdev/moko-resourcesJavier
09/03/2020, 11:28 AMnrobi
09/03/2020, 11:32 AMJavier
09/03/2020, 11:33 AMnrobi
09/03/2020, 11:33 AMBindingAdapters
, right @alex009?Javier
09/03/2020, 11:34 AMAndroidViewModel
alex009
09/03/2020, 11:35 AMnrobi
09/03/2020, 11:35 AMstreetsofboston
09/03/2020, 1:13 PM/**
* Provides all the exposed resources as platform-specific values.
*/
interface ResourceProvider {
/**
* Provides all the exposed [StringResource]s.
*/
val Strings: Strings
/**
* Provides all the exposed [PluralResource]s.
*/
val Plurals: Plurals
/**
* Provides all the exposed [ColorResource]s.
*/
val Colors: Colors
/**
* Provides all the exposed [ImageResource]s.
*/
val Images: Images
}
/**
* Represents a string-resource
*/
expect class StringResource
/**
* Wraps a [StringResource] around a plain [String].
*/
expect val String.asStringResource: StringResource
/**
* Represents a plural-resource
*/
expect class PluralResource {
internal val quantity: Int
}
/**
* Represents a color-resource
*/
expect class ColorResource
/**
* Represents an image-resource
*/
expect class ImageResource
/**
* Provides all the exposed resources for this shared common module.
* E.g. val uiMsg = Resources.Strings.WelcomeMessage
*/
internal val Resources: ResourceProvider get() = Application.resourceProvider
commonMain then defines the actual Resources it needs for its common code: E.g.
interface Strings {
val WelcomeMessage: StringResource
fun FullName(first: String, last: String): StringResource
...
}
interface Images {
val AppIcon: DrawableResource
...
}
Common `actual`s:
The androidMain and iosMain will the create the actuals
for the StringResource
, DrawableResource
, etc. Android will wrap `Int`s as resource-ids; iOS will wrap `String`s as dict-keys.
Platform Code:
The actual Android app
(Java/Kotlin) and iOS iOS
(Swift) modules will provide and inject (Koin) the actual implementations of the ResourceProvider
interface and assigns it to the Application.resourceProvider
.alex009
09/03/2020, 1:16 PMdarkmoon_uk
09/04/2020, 3:00 AM