Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

feat: Add context as a scope variable #56

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions compose/src/main/java/br/com/zup/nimbus/compose/Nimbus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.platform.LocalContext
import br.com.zup.nimbus.compose.Nimbus.Companion.staticState
import br.com.zup.nimbus.compose.internal.NimbusNavHostHelper
import br.com.zup.nimbus.compose.ui.NimbusComposeUILibrary
import br.com.zup.nimbus.compose.ui.components.ErrorDefault
Expand All @@ -35,6 +34,7 @@ import br.com.zup.nimbus.core.log.Logger
import br.com.zup.nimbus.core.network.HttpClient
import br.com.zup.nimbus.core.network.UrlBuilder
import br.com.zup.nimbus.core.network.ViewClient
import br.com.zup.nimbus.core.scope.CommonScope
import br.com.zup.nimbus.core.tree.IdManager
import br.com.zup.nimbus.compose.Nimbus as NimbusCompose

Expand All @@ -43,6 +43,7 @@ typealias LoadingHandler = @Composable () -> Unit
typealias ErrorHandler = @Composable (throwable: Throwable, retry: () -> Unit) -> Unit

const val PLATFORM_NAME = "android"
private const val NIMBUS_LOCAL_CONTEXT_KEY = "NIMBUS_LOCAL_CONTEXT_KEY"

@Stable
class NimbusNavigatorState(
Expand Down Expand Up @@ -78,10 +79,6 @@ class Nimbus(
idManager = idManager
)) {
companion object {
@get:Synchronized
@set:Synchronized
var staticState: NimbusComposeStaticState? = null
internal set

val instance: NimbusCompose
@Composable
Expand All @@ -104,10 +101,10 @@ private val LocalNavigator = staticCompositionLocalOf<NimbusNavigatorState> {
@Composable
fun ProvideNimbus(
nimbus: NimbusCompose,
applicationContext: Context = LocalContext.current.applicationContext,
context: Context = LocalContext.current,
content: @Composable () -> Unit,
) {
configureStaticState(applicationContext)
nimbus.configureScope(context)

val nimbusComposeState = remember(
nimbus
Expand All @@ -117,11 +114,15 @@ fun ProvideNimbus(
CompositionLocalProvider(LocalNimbus provides nimbusComposeState, content = content)
}

private fun configureStaticState(applicationContext: Context) {
if (staticState == null) {
staticState =
NimbusComposeStaticState(applicationContext = applicationContext)
}
private fun NimbusCompose.configureScope(context: Context) {
set(
key = NIMBUS_LOCAL_CONTEXT_KEY,
value = context
)
}

fun CommonScope.context(): Context? {
Copy link
Contributor

@Tiagoperes Tiagoperes Dec 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coloca isso em Scope ao invés CommonScope.

return get(key = NIMBUS_LOCAL_CONTEXT_KEY) as? Context
}

@Composable
Expand All @@ -139,9 +140,3 @@ internal fun ProvideNavigatorState(
}
CompositionLocalProvider(LocalNavigator provides nimbusNavigatorState, content = content)
}

/**
* Exposes a singleton state for non compose functions.
* Should only expose singleton properties here
*/
class NimbusComposeStaticState(val applicationContext: Context)