Skip to content

Commit

Permalink
Merge branch 'trunk' into veyndan/2023-09-28/TreehouseLayoutId
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton authored Sep 30, 2023
2 parents 570e085 + 68b5332 commit 4a9f7fb
Show file tree
Hide file tree
Showing 19 changed files with 240 additions and 132 deletions.
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", ve
kotlin-serializationPlugin = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin" }
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }

kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
Expand All @@ -27,7 +28,7 @@ xmlutil-serialization = 'io.github.pdvrieze.xmlutil:serialization:0.86.2'

atomicFuPlugin = "org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.22.0"
dokkaPlugin = "org.jetbrains.dokka:dokka-gradle-plugin:1.9.0"
spotlessPlugin = "com.diffplug.spotless:spotless-plugin-gradle:6.21.0"
spotlessPlugin = "com.diffplug.spotless:spotless-plugin-gradle:6.22.0"
gradleMavenPublishPlugin = "com.vanniktech:gradle-maven-publish-plugin:0.25.3"
buildConfigPlugin = "com.github.gmazzo:gradle-buildconfig-plugin:3.1.0"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class ComposeUiFlexContainerTest(

override fun flexContainer(direction: FlexDirection) = ComposeTestFlexContainer(direction)

override fun widget(text: String, modifier: RedwoodModifier) = object : Text<@Composable () -> Unit> {
private var text by mutableStateOf(text)
override fun widget() = object : Text<@Composable () -> Unit> {
private var text by mutableStateOf("")

override val value = @Composable {
BasicText(
Expand All @@ -65,7 +65,7 @@ class ComposeUiFlexContainerTest(
)
}

override var modifier = modifier
override var modifier: RedwoodModifier = RedwoodModifier

override fun text(text: String) {
this.text = text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import app.cash.paparazzi.DeviceConfig
import app.cash.paparazzi.Paparazzi
import app.cash.redwood.Modifier
import app.cash.redwood.layout.AbstractSpacerTest
import app.cash.redwood.layout.widget.Spacer
import app.cash.redwood.ui.dp
import app.cash.redwood.widget.Widget
import com.android.ide.common.rendering.api.SessionParams
import org.junit.Rule
Expand All @@ -41,15 +39,7 @@ class ComposeUiSpacerTest : AbstractSpacerTest<@Composable () -> Unit>() {
renderingMode = SessionParams.RenderingMode.SHRINK,
)

override fun widget(
width: Int,
height: Int,
modifier: Modifier,
): Spacer<@Composable () -> Unit> = ComposeUiSpacer().apply {
this.modifier = modifier
width(width.dp)
height(height.dp)
}
override fun widget(): Spacer<@Composable () -> Unit> = ComposeUiSpacer()

override fun wrap(widget: Widget<@Composable () -> Unit>, horizontal: Boolean) = @Composable {
if (horizontal) {
Expand Down
8 changes: 2 additions & 6 deletions redwood-layout-shared-test/build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
apply plugin: 'com.android.library'
apply plugin: 'org.jetbrains.kotlin.android'
apply plugin: 'org.jetbrains.kotlin.jvm'

dependencies {
api projects.redwoodLayoutApi
api projects.redwoodLayoutModifiers
api projects.redwoodLayoutWidget
api projects.redwoodRuntime
api projects.redwoodWidget
api projects.redwoodYoga
api libs.junit
api libs.testParameterInjector
}

android {
namespace 'app.cash.redwood.redwoodlayoutsharedtest'
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ import org.junit.Test
@Suppress("JUnitMalformedDeclaration")
abstract class AbstractFlexContainerTest<T : Any> {
abstract fun flexContainer(direction: FlexDirection): TestFlexContainer<T>
abstract fun widget(text: String, modifier: Modifier = Modifier): Text<T>
abstract fun widget(): Text<T>
abstract fun verifySnapshot(container: TestFlexContainer<T>, name: String? = null)

private fun widget(text: String, modifier: Modifier = Modifier): Text<T> = widget().apply {
text(text)
this.modifier = modifier
}

@Test fun emptyLayout(
@TestParameter flexDirectionEnum: FlexDirectionEnum,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@
*/
package app.cash.redwood.layout

import app.cash.redwood.Modifier
import app.cash.redwood.layout.widget.Spacer
import app.cash.redwood.ui.dp
import app.cash.redwood.widget.Widget
import org.junit.Test

abstract class AbstractSpacerTest<T : Any> {

abstract fun widget(
width: Int = 0,
height: Int = 0,
modifier: Modifier = Modifier,
): Widget<T>
abstract fun widget(): Spacer<T>

abstract fun wrap(widget: Widget<T>, horizontal: Boolean): T

abstract fun verifySnapshot(value: T)

private fun widget(width: Int, height: Int): Spacer<T> = widget().apply {
width(width.dp)
height(height.dp)
}

@Test fun zeroSpacer() {
val widget = widget(width = 0, height = 0)
verifySnapshot(wrap(widget, horizontal = true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,15 @@ class ViewFlexContainerTest(
return ViewTestFlexContainer(paparazzi.context, direction)
}

override fun widget(text: String, modifier: Modifier) = object : Text<View> {
override fun widget() = object : Text<View> {
override val value = TextView(paparazzi.context).apply {
background = ColorDrawable(Color.GREEN)
textSize = 18f
textDirection = View.TEXT_DIRECTION_LOCALE
setTextColor(Color.BLACK)
this.text = text
}

override var modifier = modifier
override var modifier: Modifier = Modifier

override fun text(text: String) {
value.text = text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ import android.widget.LinearLayout.VERTICAL
import android.widget.TextView
import app.cash.paparazzi.DeviceConfig
import app.cash.paparazzi.Paparazzi
import app.cash.redwood.Modifier
import app.cash.redwood.layout.AbstractSpacerTest
import app.cash.redwood.layout.widget.Spacer
import app.cash.redwood.ui.dp
import app.cash.redwood.widget.Widget
import com.android.ide.common.rendering.api.SessionParams
import org.junit.Rule
Expand All @@ -42,15 +40,7 @@ class ViewSpacerTest : AbstractSpacerTest<View>() {
renderingMode = SessionParams.RenderingMode.SHRINK,
)

override fun widget(
width: Int,
height: Int,
modifier: Modifier,
): Spacer<View> = ViewSpacer(paparazzi.context).apply {
this.modifier = modifier
width(width.dp)
height(height.dp)
}
override fun widget(): Spacer<View> = ViewSpacer(paparazzi.context)

override fun wrap(widget: Widget<View>, horizontal: Boolean): View {
return LinearLayout(paparazzi.context).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class ComposeUiLazyListTest(

override fun flexContainer(direction: FlexDirection) = ComposeTestFlexContainer(direction)

override fun widget(text: String, modifier: RedwoodModifier) = object : Text<@Composable () -> Unit> {
private var text by mutableStateOf(text)
override fun widget() = object : Text<@Composable () -> Unit> {
private var text by mutableStateOf("")

override val value = @Composable {
BasicText(
Expand All @@ -67,7 +67,7 @@ class ComposeUiLazyListTest(
)
}

override var modifier = modifier
override var modifier: RedwoodModifier = RedwoodModifier

override fun text(text: String) {
this.text = text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ class ViewLazyListTest(

override fun flexContainer(direction: FlexDirection) = ViewTestFlexContainer(paparazzi.context, direction)

override fun widget(text: String, modifier: Modifier) = object : Text<View> {
override fun widget() = object : Text<View> {
override val value = TextView(paparazzi.context).apply {
background = ColorDrawable(Color.GREEN)
textSize = 18f
setTextColor(Color.BLACK)
this.text = text
}

override var modifier = modifier
override var modifier: Modifier = Modifier

override fun text(text: String) {
value.text = text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public fun <A : AppService> TreehouseContent(
widgetSystem: WidgetSystem<@Composable () -> Unit>,
codeListener: CodeListener = CodeListener(),
contentSource: TreehouseContentSource<A>,
modifier: Modifier = Modifier,
) {
val onBackPressedDispatcher = platformOnBackPressedDispatcher()

Expand Down Expand Up @@ -87,7 +88,7 @@ public fun <A : AppService> TreehouseContent(
}

Box(
modifier = Modifier.onSizeChanged { size ->
modifier = modifier.onSizeChanged { size ->
viewportSize = with(Density(density.density.toDouble())) {
Size(size.width.toDp().value.redwoodDp, size.height.toDp().value.redwoodDp)
}
Expand Down
7 changes: 1 addition & 6 deletions redwood-treehouse-host/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ dependencies {

// The kotlin.test library provides JVM variants for multiple testing frameworks. The Kotlin
// plugin does not apply the capability to configurations created by the Android plugin.
androidTestImplementation(libs.kotlin.test) {
capabilities {
requireCapability(
"org.jetbrains.kotlin:kotlin-test-framework-junit:${libs.versions.kotlin.get()}")
}
}
androidTestImplementation libs.kotlin.test.junit
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import app.cash.redwood.ui.OnBackPressedCallback
import app.cash.redwood.ui.OnBackPressedDispatcher
import app.cash.redwood.ui.UiConfiguration
import app.cash.redwood.widget.Widget
import app.cash.zipline.ZiplineApiMismatchException
import app.cash.zipline.ZiplineScope
import app.cash.zipline.withScope
import kotlinx.coroutines.CancellationException
Expand Down Expand Up @@ -388,35 +389,49 @@ private class ViewContentCodeBinding<A : AppService>(
treehouseUiOrNull = treehouseUi
val restoredId = viewOrNull?.stateSnapshotId
val restoredState = if (restoredId != null) app.stateStore.get(restoredId.value.orEmpty()) else null
treehouseUi.start(
changesSink = this@ViewContentCodeBinding,
onBackPressedDispatcher = object : OnBackPressedDispatcherService {
override fun addCallback(onBackPressedCallback: OnBackPressedCallbackService): CancellableService {
app.dispatchers.checkZipline()
val cancellable = onBackPressedDispatcher.addCallback(
object : OnBackPressedCallback(onBackPressedCallback.isEnabled) {
override fun handleOnBackPressed() {
bindingScope.launch(app.dispatchers.zipline) {
onBackPressedCallback.handleOnBackPressed()
}
}
},
)
return object : CancellableService {
override fun cancel() {
app.dispatchers.checkZipline()
cancellable.cancel()
}

override fun close() {
cancel()
}
try {
treehouseUi.start(
changesSink = this@ViewContentCodeBinding,
onBackPressedDispatcher = onBackPressedDispatcherService,
uiConfigurations = uiConfigurationFlow,
stateSnapshot = restoredState,
)
} catch (e: ZiplineApiMismatchException) {
// Fall back to calling the function that doesn't have a back pressed dispatcher.
treehouseUi.start(
changesSink = this@ViewContentCodeBinding,
uiConfigurations = uiConfigurationFlow,
stateSnapshot = restoredState,
)
}
}
}

private val onBackPressedDispatcherService = object : OnBackPressedDispatcherService {
override fun addCallback(
onBackPressedCallback: OnBackPressedCallbackService,
): CancellableService {
app.dispatchers.checkZipline()
val cancellable = onBackPressedDispatcher.addCallback(
object : OnBackPressedCallback(onBackPressedCallback.isEnabled) {
override fun handleOnBackPressed() {
bindingScope.launch(app.dispatchers.zipline) {
onBackPressedCallback.handleOnBackPressed()
}
}
},
uiConfigurations = uiConfigurationFlow,
stateSnapshot = restoredState,
)

return object : CancellableService {
override fun cancel() {
app.dispatchers.checkZipline()
cancellable.cancel()
}

override fun close() {
cancel()
}
}
}
}

Expand Down
31 changes: 14 additions & 17 deletions redwood-widget-testing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,20 @@ kotlin {
api projects.redwoodWidget
}
}
}
}

dependencies {
// The kotlin.test library provides JVM variants for multiple testing frameworks. When used
// as a test dependency this selection is transparent. But since we are publishing a library
// we need to select one ourselves at compilation time.
def configurationNames = [
"jvmMainImplementation",
"debugImplementation",
"releaseImplementation",
]
for (configurationName in configurationNames) {
add(configurationName, libs.kotlin.test) {
capabilities {
requireCapability(
"org.jetbrains.kotlin:kotlin-test-framework-junit:${libs.versions.kotlin.get()}")
androidMain {
dependencies {
// The kotlin.test library provides JVM variants for multiple testing frameworks. When used
// as a test dependency this selection is transparent. But since we are publishing a library
// we need to select one ourselves at compilation time.
api libs.kotlin.test.junit
}
}
jvmMain {
dependencies {
// The kotlin.test library provides JVM variants for multiple testing frameworks. When used
// as a test dependency this selection is transparent. But since we are publishing a library
// we need to select one ourselves at compilation time.
api libs.kotlin.test.junit
}
}
}
Expand Down
Loading

0 comments on commit 4a9f7fb

Please sign in to comment.