Skip to content

Commit

Permalink
make resources android preview friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
vpodlesnyak committed Nov 6, 2024
1 parent d314047 commit 9ca4d68
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 40 deletions.
63 changes: 29 additions & 34 deletions base/src/androidMain/kotlin/ApplicationHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,37 @@ package com.splendo.kaluga.base

import android.app.Application
import android.content.Context
import com.splendo.kaluga.base.ApplicationHolder.Companion.application

/**
* Class holding reference to the [Application] running Kaluga
* set [application] to your Application so default constructors work with the proper [Context]
* Class holding reference to the [Application] [Context] running Kaluga
* set [applicationContext] to your Application so default constructors work with the proper [Context]
*/
class ApplicationHolder {
companion object {

/**
* The [Application] running Kaluga
*/
var application: Application? = null
set(application) {
check(field == null) { "Application object can only be set once." }
field = application
object ApplicationHolder {

private var _applicationContext: Context? = null
set(value) {
check(field == null) { "Application object can only be set once." }
field = value
}

/**
* Indicates whether [Application] [Context] was set
*/
val isInitialized: Boolean get() = _applicationContext != null

/**
* The [Context] of the [Application] running Kaluga
*/
var applicationContext: Context
get() {
val context = _applicationContext
checkNotNull(context) {
"You've used ApplicationHolder.applicationContext without setting it on this holder " +
"(you should do this from Application.onCreate() or in your test)"
}

/**
* The [Context] of the [application]
*/
val applicationContext: Context
get() {
val application = this.application
checkNotNull(application) {
"You've used ApplicationHolder.applicationContext without setting the Application on this holder " +
"(you should do this from Application.onCreate() or in your test)"
}

val context = application.applicationContext
checkNotNull(context) {
"ApplicationContext is null, this should not happen during runtime if you set a real Application instance on this holder." +
"For testing make sure you set an Application with a real or mock ApplicationContext available."
}

return context
}
}
return context
}
set(value) {
_applicationContext = value.applicationContext
}
}
11 changes: 5 additions & 6 deletions resources/src/androidMain/kotlin/Resources.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ import android.graphics.Typeface
import android.os.Handler
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import com.splendo.kaluga.base.ApplicationHolder.Companion.application
import com.splendo.kaluga.base.ApplicationHolder.Companion.applicationContext
import com.splendo.kaluga.base.ApplicationHolder
import kotlinx.coroutines.CompletableDeferred

/**
* Default implementation of a [StringLoader].
* @param context the [Context] from which to load the string resources
*/
actual class DefaultStringLoader(private val context: Context?) : StringLoader {
actual constructor() : this(if (application != null) applicationContext else null)
actual constructor() : this(if (ApplicationHolder.isInitialized) ApplicationHolder.applicationContext else null)
actual override fun loadString(identifier: String, defaultValue: String): String {
if (context == null) {
return defaultValue
Expand Down Expand Up @@ -63,7 +62,7 @@ actual class DefaultStringLoader(private val context: Context?) : StringLoader {
* @param context the [Context] from which to load the color resources
*/
actual class DefaultColorLoader(private val context: Context?) : KalugaColorLoader {
actual constructor() : this(if (application != null) applicationContext else null)
actual constructor() : this(if (ApplicationHolder.isInitialized) ApplicationHolder.applicationContext else null)
actual override fun loadColor(identifier: String, defaultValue: KalugaColor?): KalugaColor? {
if (context == null) {
return defaultValue
Expand Down Expand Up @@ -93,7 +92,7 @@ actual class DefaultColorLoader(private val context: Context?) : KalugaColorLoad
* @param context the [Context] from which to load the image resources
*/
actual class DefaultImageLoader(private val context: Context?) : ImageLoader {
actual constructor() : this(if (application != null) applicationContext else null)
actual constructor() : this(if (ApplicationHolder.isInitialized) ApplicationHolder.applicationContext else null)
actual override fun loadImage(identifier: String, defaultValue: KalugaImage?): KalugaImage? {
if (context == null) {
return defaultValue
Expand All @@ -113,7 +112,7 @@ actual class DefaultImageLoader(private val context: Context?) : ImageLoader {
* @param handler a [Handler] for the thread the completion of loading the font should called on. If `null`, the UI thread will be used.
*/
actual class DefaultFontLoader(private val context: Context?, private val handler: Handler?) : FontLoader {
actual constructor() : this(if (application != null) applicationContext else null, null)
actual constructor() : this(if (ApplicationHolder.isInitialized) ApplicationHolder.applicationContext else null, null)
actual override suspend fun loadFont(identifier: String, defaultValue: KalugaFont?): KalugaFont? {
if (context == null) {
return defaultValue
Expand Down

0 comments on commit 9ca4d68

Please sign in to comment.