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

feat: Renames NimbusTheme #43

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
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
38 changes: 19 additions & 19 deletions compose/src/main/java/br/zup/com/nimbus/compose/Nimbus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.platform.LocalContext
import br.zup.com.nimbus.compose.Nimbus.Companion.staticState
import br.zup.com.nimbus.compose.internal.NimbusNavHostHelper
import br.zup.com.nimbus.compose.ui.NimbusComposeUILibrary
import br.zup.com.nimbus.compose.ui.components.ErrorDefault
Expand Down Expand Up @@ -59,7 +60,22 @@ class Nimbus(
httpClient = httpClient,
viewClient = viewClient,
idManager = idManager
))
)) {
companion object {
@get:Synchronized
@set:Synchronized
var staticState: NimbusComposeStaticState? = null
internal set

val instance: NimbusCompose
@Composable
get() = LocalNimbus.current

val navigatorInstance: NimbusNavigatorState
@Composable
get() = LocalNavigator.current
}
}

private val LocalNimbus = staticCompositionLocalOf<NimbusCompose> {
error("No Nimbus provided")
Expand All @@ -86,8 +102,8 @@ fun ProvideNimbus(
}

private fun configureStaticState(applicationContext: Context) {
if (NimbusTheme.nimbusStaticState == null) {
NimbusTheme.nimbusStaticState =
if (staticState == null) {
staticState =
NimbusComposeStaticState(applicationContext = applicationContext)
}
}
Expand All @@ -113,19 +129,3 @@ internal fun ProvideNavigatorState(
* Should only expose singleton properties here
*/
class NimbusComposeStaticState(val applicationContext: Context)

object NimbusTheme {

@get:Synchronized
@set:Synchronized
var nimbusStaticState: NimbusComposeStaticState? = null
internal set

val nimbus: NimbusCompose
@Composable
get() = LocalNimbus.current

val nimbusNavigatorState: NimbusNavigatorState
@Composable
get() = LocalNavigator.current
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import android.app.Activity
import androidx.activity.compose.BackHandler
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import br.zup.com.nimbus.compose.NimbusTheme
import br.zup.com.nimbus.compose.Nimbus

@Composable
internal fun NimbusBackHandler(onDismiss: ()-> Unit = {}) {
val activity = LocalContext.current as? Activity
val navHostHelper = NimbusTheme.nimbusNavigatorState.navHostHelper
val navHostHelper = Nimbus.navigatorInstance.navHostHelper
BackHandler(enabled = true) {
if (!navHostHelper.pop()) {
activity?.finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import br.zup.com.nimbus.compose.JSON
import br.zup.com.nimbus.compose.Nimbus
import br.zup.com.nimbus.compose.NimbusTheme.nimbus
import br.zup.com.nimbus.compose.ProvideNavigatorState
import br.zup.com.nimbus.compose.SHOW_VIEW_DESTINATION
import br.zup.com.nimbus.compose.JSON
import br.zup.com.nimbus.compose.NimbusTheme
import br.zup.com.nimbus.compose.VIEW_URL
import br.zup.com.nimbus.compose.model.NimbusPageState
import br.zup.com.nimbus.compose.model.Page
import com.zup.nimbus.core.network.ViewRequest
import java.util.UUID

Expand All @@ -31,7 +27,7 @@ internal fun NimbusNavHost(
navController: NavHostController = rememberNavController(),
viewModelKey: String = UUID.randomUUID().toString(),
viewRequest: ViewRequest? = null,
nimbusConfig: Nimbus = nimbus,
nimbusConfig: Nimbus = Nimbus.instance,
nimbusViewModel: NimbusViewModel = viewModel(
//Creates a new viewmodel for each unique key
key = viewModelKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import androidx.compose.runtime.key
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import br.zup.com.nimbus.compose.ComponentData
import br.zup.com.nimbus.compose.Nimbus
import br.zup.com.nimbus.compose.NimbusMode
import br.zup.com.nimbus.compose.NimbusTheme
import br.zup.com.nimbus.compose.ui.getComponent

@Composable
internal fun ComponentNotFound(name: String) {
val nimbus = NimbusTheme.nimbus
val nimbus = Nimbus.instance
val message = "Could not find any component named \"$name\"."
nimbus.logger.error(message)
if (nimbus.mode == NimbusMode.Development) Text(message, color = Color.Red)
Expand All @@ -23,7 +23,7 @@ internal fun ComponentNotFound(name: String) {
fun RenderedNode(flow: NodeFlow) {
val state = flow.collectAsState()
val (node, children) = state.value
val ui = NimbusTheme.nimbus.uiLibraryManager
val ui = Nimbus.instance.uiLibraryManager
val handler = remember(node.component) { ui.getComponent(node.component) }

DisposableEffect(Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package br.zup.com.nimbus.compose.model
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import br.zup.com.nimbus.compose.NimbusTheme
import br.zup.com.nimbus.compose.Nimbus.Companion.instance
import br.zup.com.nimbus.compose.internal.HandleNimbusPageState
import com.zup.nimbus.core.ServerDrivenView
import com.zup.nimbus.core.tree.ServerDrivenNode
Expand Down Expand Up @@ -54,7 +54,7 @@ data class Page(
@Composable
fun Compose() {
val localState: NimbusPageState by state.collectAsState()
localState.HandleNimbusPageState(NimbusTheme.nimbus.loadingView, NimbusTheme.nimbus.errorView)
localState.HandleNimbusPageState(instance.loadingView, instance.errorView)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object ClassNames {
val Column = ClassName(PackageNames.composeLayout, "Column")
val Color = ClassName(PackageNames.composeGraphics, "Color")
val ComponentData = ClassName(PackageNames.nimbusCompose, "ComponentData")
val NimbusTheme = ClassName(PackageNames.nimbusCompose, "NimbusTheme")
val Nimbus = ClassName(PackageNames.nimbusCompose, "Nimbus")
val NimbusMode = ClassName(PackageNames.nimbusCompose, "NimbusMode")
val AnyServerDrivenData = ClassName(
"${PackageNames.nimbusCore}.deserialization",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal object ComponentWriter: ComponentActionWriter() {
override val imports = setOf(
ClassNames.DeserializationContext,
ClassNames.AnyServerDrivenData,
ClassNames.NimbusTheme,
ClassNames.Nimbus,
ClassNames.NimbusMode,
ClassNames.Text,
ClassNames.Color,
Expand All @@ -48,8 +48,8 @@ internal object ComponentWriter: ComponentActionWriter() {
| $name(
| ${ParameterUtils.buildParameterAssignments(properties).joinToString(",\n ")}
| )
|} else if (NimbusTheme.nimbus.mode == NimbusMode.Development) {
| NimbusTheme.nimbus.logger.error(
|} else if (Nimbus.instance.mode == NimbusMode.Development) {
| Nimbus.instance.logger.error(
| "Can't deserialize properties of the component with id ${'$'}{$COMPONENT_REF.node.id} " +
| "into the composable $name. See the errors below:" +
| $PROPERTIES_REF.errorsAsString()
Expand Down