-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose HostConfiguration
type to Compose through Treehouse
#393
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (C) 2022 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package app.cash.redwood.treehouse | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.serialization.Contextual | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
public data class FlowWithInitialValue<T>( | ||
val initial: T, | ||
@Contextual val flow: Flow<T>, | ||
) | ||
|
||
public fun <T> StateFlow<T>.toFlowWithInitialValue(): FlowWithInitialValue<T> = | ||
FlowWithInitialValue(value, this) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (C) 2022 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package app.cash.redwood.treehouse | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
public data class HostConfiguration( | ||
val darkMode: Boolean = false, | ||
) { | ||
public companion object | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. weeeird |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (C) 2022 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package app.cash.redwood.treehouse | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.ProvidableCompositionLocal | ||
import androidx.compose.runtime.ReadOnlyComposable | ||
import androidx.compose.runtime.compositionLocalOf | ||
import app.cash.redwood.compose.WidgetVersion | ||
|
||
/** | ||
* Provide the configuration of the host display. | ||
* This value will be bound automatically when Treehouse is used. | ||
* Custom values should only be provided into a composition for testing purposes! | ||
* | ||
* @see WidgetVersion | ||
*/ | ||
public val LocalHostConfiguration: ProvidableCompositionLocal<HostConfiguration> = | ||
compositionLocalOf { | ||
throw AssertionError("HostConfiguration was not provided!") | ||
} | ||
|
||
/** | ||
* Expose various configuration properties of the host. | ||
* | ||
* @see HostConfiguration | ||
*/ | ||
@Suppress("unused") // Emulating a CompositionLocal. | ||
public val HostConfiguration.Companion.current: HostConfiguration | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahhh there it is |
||
@Composable | ||
@ReadOnlyComposable | ||
get() = LocalHostConfiguration.current |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ | |
package app.cash.redwood.treehouse | ||
|
||
import androidx.compose.runtime.BroadcastFrameClock | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import app.cash.redwood.protocol.Event | ||
import app.cash.redwood.protocol.compose.DiffProducingWidget | ||
import app.cash.redwood.protocol.compose.ProtocolRedwoodComposition | ||
|
@@ -37,37 +40,39 @@ public fun TreehouseUi.asZiplineTreehouseUi( | |
factory = factory, | ||
widgetVersion = widgetVersion, | ||
) | ||
|
||
return composition.asZiplineTreehouseUi { | ||
composition.setContent { | ||
Show() | ||
} | ||
} | ||
return RedwoodZiplineTreehouseUi(composition, this) | ||
} | ||
|
||
private fun ProtocolRedwoodComposition.asZiplineTreehouseUi( | ||
startSignal: () -> Unit = {}, | ||
): ZiplineTreehouseUi { | ||
val delegate = this | ||
private class RedwoodZiplineTreehouseUi( | ||
private val composition: ProtocolRedwoodComposition, | ||
private val treehouseUi: TreehouseUi, | ||
) : ZiplineTreehouseUi { | ||
private var diffSinkToClose: DiffSinkService? = null | ||
|
||
return object : ZiplineTreehouseUi { | ||
var diffSinkToClose: DiffSinkService? = null | ||
override fun sendEvent(event: Event) { | ||
composition.sendEvent(event) | ||
} | ||
|
||
override fun sendEvent(event: Event) { | ||
delegate.sendEvent(event) | ||
} | ||
override fun start( | ||
diffSink: DiffSinkService, | ||
hostConfigurations: FlowWithInitialValue<HostConfiguration>, | ||
) { | ||
check(diffSinkToClose == null) | ||
diffSinkToClose = diffSink | ||
composition.start(diffSink) | ||
|
||
override fun start(diffSink: DiffSinkService) { | ||
check(diffSinkToClose == null) | ||
diffSinkToClose = diffSink | ||
delegate.start(diffSink) | ||
startSignal() | ||
val (initialHostConfiguration, hostConfigurationFlow) = hostConfigurations | ||
composition.setContent { | ||
val hostConfiguration by hostConfigurationFlow.collectAsState(initialHostConfiguration) | ||
CompositionLocalProvider(LocalHostConfiguration provides hostConfiguration) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. infix functions so weird There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed |
||
treehouseUi.Show() | ||
} | ||
} | ||
} | ||
|
||
override fun close() { | ||
delegate.cancel() | ||
diffSinkToClose?.close() | ||
} | ||
override fun close() { | ||
composition.cancel() | ||
diffSinkToClose?.close() | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wants Kotlin/KEEP#278