diff --git a/kotlin/buildSrc/src/main/java/Dependencies.kt b/kotlin/buildSrc/src/main/java/Dependencies.kt index 53b1554b4..f5e5f9cfb 100644 --- a/kotlin/buildSrc/src/main/java/Dependencies.kt +++ b/kotlin/buildSrc/src/main/java/Dependencies.kt @@ -4,7 +4,6 @@ import java.util.Locale.US import kotlin.reflect.full.declaredMembers object Versions { - const val compose = "0.1.0-dev10" const val coroutines = "1.3.4" const val kotlin = "1.3.71" const val targetSdk = 29 @@ -38,14 +37,6 @@ object Dependencies { const val viewbinding = "androidx.databinding:viewbinding:3.6.2" } - object Compose { - const val foundation = "androidx.ui:ui-foundation:${Versions.compose}" - const val layout = "androidx.ui:ui-layout:${Versions.compose}" - const val material = "androidx.ui:ui-material:${Versions.compose}" - const val test = "androidx.ui:ui-test:${Versions.compose}" - const val tooling = "androidx.ui:ui-tooling:${Versions.compose}" - } - const val cycler = "com.squareup.cycler:cycler:0.1.3" // Required for Dungeon Crawler sample. diff --git a/kotlin/samples/hello-compose-binding/build.gradle.kts b/kotlin/samples/hello-compose-binding/build.gradle.kts deleted file mode 100644 index dd5982138..000000000 --- a/kotlin/samples/hello-compose-binding/build.gradle.kts +++ /dev/null @@ -1,48 +0,0 @@ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile - -/* - * Copyright 2019 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. - */ -plugins { - id("com.android.application") - kotlin("android") -} - -apply(from = rootProject.file(".buildscript/android-sample-app.gradle")) -apply(from = rootProject.file(".buildscript/android-ui-tests.gradle")) - -android { - defaultConfig { - applicationId = "com.squareup.sample.hellocomposebinding" - } -} - -apply(from = rootProject.file(".buildscript/configure-compose.gradle")) -tasks.withType { - kotlinOptions.apiVersion = "1.3" -} - -dependencies { - implementation(project(":workflow-ui:core-compose")) - - implementation(Dependencies.Compose.layout) - implementation(Dependencies.Compose.material) - implementation(Dependencies.Compose.tooling) - implementation(Dependencies.Compose.foundation) - implementation(Dependencies.RxJava2.rxjava2) - - androidTestImplementation(Dependencies.Compose.test) - androidTestImplementation(Dependencies.Test.junit) -} diff --git a/kotlin/samples/hello-compose-binding/src/androidTest/java/com/squareup/sample/hellocomposebinding/HelloBindingTest.kt b/kotlin/samples/hello-compose-binding/src/androidTest/java/com/squareup/sample/hellocomposebinding/HelloBindingTest.kt deleted file mode 100644 index 9723cbf5d..000000000 --- a/kotlin/samples/hello-compose-binding/src/androidTest/java/com/squareup/sample/hellocomposebinding/HelloBindingTest.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2020 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 com.squareup.sample.hellocomposebinding - -import androidx.test.ext.junit.runners.AndroidJUnit4 -import androidx.ui.test.android.AndroidComposeTestRule -import androidx.ui.test.assertIsDisplayed -import androidx.ui.test.doClick -import androidx.ui.test.findByText -import org.junit.Rule -import org.junit.Test -import org.junit.runner.RunWith - -@RunWith(AndroidJUnit4::class) -class HelloBindingTest { - - // Launches the activity. - @Rule @JvmField val composeRule = AndroidComposeTestRule() - - @Test fun togglesBetweenStates() { - findByText("Hello") - .assertIsDisplayed() - .doClick() - findByText("Goodbye") - .assertIsDisplayed() - .doClick() - findByText("Hello") - .assertIsDisplayed() - } -} diff --git a/kotlin/samples/hello-compose-binding/src/main/AndroidManifest.xml b/kotlin/samples/hello-compose-binding/src/main/AndroidManifest.xml deleted file mode 100644 index 557bdb0d0..000000000 --- a/kotlin/samples/hello-compose-binding/src/main/AndroidManifest.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/kotlin/samples/hello-compose-binding/src/main/java/com/squareup/sample/hellocomposebinding/HelloBinding.kt b/kotlin/samples/hello-compose-binding/src/main/java/com/squareup/sample/hellocomposebinding/HelloBinding.kt deleted file mode 100644 index 1ab0b009c..000000000 --- a/kotlin/samples/hello-compose-binding/src/main/java/com/squareup/sample/hellocomposebinding/HelloBinding.kt +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2019 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 com.squareup.sample.hellocomposebinding - -import androidx.compose.Composable -import androidx.ui.core.Alignment -import androidx.ui.core.Modifier -import androidx.ui.foundation.Clickable -import androidx.ui.foundation.Text -import androidx.ui.layout.fillMaxSize -import androidx.ui.layout.wrapContentSize -import androidx.ui.material.MaterialTheme -import androidx.ui.material.Surface -import androidx.ui.material.ripple.ripple -import androidx.ui.tooling.preview.Preview -import com.squareup.sample.hellocomposebinding.HelloWorkflow.Rendering -import com.squareup.workflow.ui.compose.bindCompose - -val HelloBinding = bindCompose { rendering, _ -> - MaterialTheme { - DrawHelloRendering(rendering) - } -} - -@Composable -private fun DrawHelloRendering(rendering: Rendering) { - Clickable( - modifier = Modifier.fillMaxSize() - .ripple(bounded = true), - onClick = { rendering.onClick() } - ) { - Text(rendering.message, modifier = Modifier.wrapContentSize(Alignment.Center)) - } -} - -@Preview(heightDp = 150) -@Composable -fun DrawHelloRenderingPreview() { - MaterialTheme { - Surface { - DrawHelloRendering(Rendering("Hello!", onClick = {})) - } - } -} diff --git a/kotlin/samples/hello-compose-binding/src/main/java/com/squareup/sample/hellocomposebinding/HelloBindingActivity.kt b/kotlin/samples/hello-compose-binding/src/main/java/com/squareup/sample/hellocomposebinding/HelloBindingActivity.kt deleted file mode 100644 index 4f6302e61..000000000 --- a/kotlin/samples/hello-compose-binding/src/main/java/com/squareup/sample/hellocomposebinding/HelloBindingActivity.kt +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2019 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 com.squareup.sample.hellocomposebinding - -import android.os.Bundle -import androidx.appcompat.app.AppCompatActivity -import com.squareup.workflow.diagnostic.SimpleLoggingDiagnosticListener -import com.squareup.workflow.ui.ViewRegistry -import com.squareup.workflow.ui.WorkflowRunner -import com.squareup.workflow.ui.setContentWorkflow - -private val viewRegistry = ViewRegistry(HelloBinding) - -class HelloBindingActivity : AppCompatActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentWorkflow(viewRegistry) { - WorkflowRunner.Config( - HelloWorkflow, - diagnosticListener = SimpleLoggingDiagnosticListener() - ) - } - } -} diff --git a/kotlin/samples/hello-compose-binding/src/main/java/com/squareup/sample/hellocomposebinding/HelloWorkflow.kt b/kotlin/samples/hello-compose-binding/src/main/java/com/squareup/sample/hellocomposebinding/HelloWorkflow.kt deleted file mode 100644 index 42a7e8bc0..000000000 --- a/kotlin/samples/hello-compose-binding/src/main/java/com/squareup/sample/hellocomposebinding/HelloWorkflow.kt +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2019 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 com.squareup.sample.hellocomposebinding - -import com.squareup.sample.hellocomposebinding.HelloWorkflow.Rendering -import com.squareup.sample.hellocomposebinding.HelloWorkflow.State -import com.squareup.sample.hellocomposebinding.HelloWorkflow.State.Goodbye -import com.squareup.sample.hellocomposebinding.HelloWorkflow.State.Hello -import com.squareup.workflow.RenderContext -import com.squareup.workflow.Snapshot -import com.squareup.workflow.StatefulWorkflow -import com.squareup.workflow.action -import com.squareup.workflow.parse - -object HelloWorkflow : StatefulWorkflow() { - enum class State { - Hello, - Goodbye; - - fun theOtherState(): State = when (this) { - Hello -> Goodbye - Goodbye -> Hello - } - } - - data class Rendering( - val message: String, - val onClick: () -> Unit - ) - - private val helloAction = action { - nextState = nextState.theOtherState() - } - - override fun initialState( - props: Unit, - snapshot: Snapshot? - ): State = snapshot?.bytes?.parse { source -> if (source.readInt() == 1) Hello else Goodbye } - ?: Hello - - override fun render( - props: Unit, - state: State, - context: RenderContext - ): Rendering { - return Rendering( - message = state.name, - onClick = { context.actionSink.send(helloAction) } - ) - } - - override fun snapshotState(state: State): Snapshot = Snapshot.of(if (state == Hello) 1 else 0) -} diff --git a/kotlin/samples/hello-compose-binding/src/main/res/values/strings.xml b/kotlin/samples/hello-compose-binding/src/main/res/values/strings.xml deleted file mode 100644 index a2cc91abd..000000000 --- a/kotlin/samples/hello-compose-binding/src/main/res/values/strings.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - Hello Compose Binding - diff --git a/kotlin/samples/hello-compose-binding/src/main/res/values/styles.xml b/kotlin/samples/hello-compose-binding/src/main/res/values/styles.xml deleted file mode 100644 index d5b0c9026..000000000 --- a/kotlin/samples/hello-compose-binding/src/main/res/values/styles.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - diff --git a/kotlin/workflow-ui/core-compose/README.md b/kotlin/workflow-ui/core-compose/README.md deleted file mode 100644 index 5235c1ed9..000000000 --- a/kotlin/workflow-ui/core-compose/README.md +++ /dev/null @@ -1,68 +0,0 @@ -# workflow-ui-core-compose - -This module provides experimental support for [Jetpack Compose UI][1] with workflows. - -The only integration that is currently supported is the ability to define [ViewFactories][2] that -are implemented as `@Composable` functions. See the `hello-compose-binding` sample in `samples` for -an example of how to use. - ----- - -## Pre-Alpha - -**DO NOT USE this module in your production apps!** - -Jetpack Compose is in pre-alpha, developer preview stage. The API is incomplete and changes -_very frequently_. This integration module exists as a proof-of-concept, to show what's possible, -and to experiment with various ways to integrate Compose with Workflow. - ----- - -## Usage - -To get started, you must be using the latest Android Gradle Plugin 4.x version. Then, you need to -enable Compose support in your `build.gradle`: - -```groovy -android { - buildFeatures { - compose true - } - composeOptions { - kotlinCompilerVersion "1.3.70-dev-withExperimentalGoogleExtensions-20200424" - kotlinCompilerExtensionVersion "${compose_version}" - } -} -``` - -You may also need to set the Kotlin API version to 1.3: - -```groovy -tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach { - kotlinOptions.apiVersion = "1.3" -} -``` - -To create a `ViewFactory`, call `bindCompose`. The lambda passed to `bindCompose` is a `@Composable` -function. - -```kotlin -val HelloBinding = bindCompose { rendering -> - MaterialTheme { - Clickable(onClick = { rendering.onClick() }) { - Text(rendering.message) - } - } -} -``` - -The `bindCompose` function returns a regular [`ViewFactory`][2] which can be added to a -[`ViewRegistry`][3] like any other: - -```kotlin -val viewRegistry = ViewRegistry(HelloBinding) -``` - -[1]: https://developer.android.com/jetpack/compose -[2]: https://square.github.io/workflow/kotlin/api/workflow/com.squareup.workflow.ui/-view-factory/ -[3]: https://square.github.io/workflow/kotlin/api/workflow/com.squareup.workflow.ui/-view-registry/ diff --git a/kotlin/workflow-ui/core-compose/api/core-compose.api b/kotlin/workflow-ui/core-compose/api/core-compose.api deleted file mode 100644 index 2e9e15f95..000000000 --- a/kotlin/workflow-ui/core-compose/api/core-compose.api +++ /dev/null @@ -1,15 +0,0 @@ -public final class com/squareup/workflow/ui/compose/ComposeViewFactory : com/squareup/workflow/ui/ViewFactory { - public fun (Lkotlin/reflect/KClass;Lkotlin/jvm/functions/Function3;)V - public fun buildView (Ljava/lang/Object;Lcom/squareup/workflow/ui/ViewEnvironment;Landroid/content/Context;Landroid/view/ViewGroup;)Landroid/view/View; - public fun getType ()Lkotlin/reflect/KClass; -} - -public final class com/squareup/workflow/ui/core/compose/BuildConfig { - public static final field BUILD_TYPE Ljava/lang/String; - public static final field DEBUG Z - public static final field LIBRARY_PACKAGE_NAME Ljava/lang/String; - public static final field VERSION_CODE I - public static final field VERSION_NAME Ljava/lang/String; - public fun ()V -} - diff --git a/kotlin/workflow-ui/core-compose/build.gradle.kts b/kotlin/workflow-ui/core-compose/build.gradle.kts deleted file mode 100644 index 359c6257e..000000000 --- a/kotlin/workflow-ui/core-compose/build.gradle.kts +++ /dev/null @@ -1,47 +0,0 @@ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile - -/* - * Copyright 2019 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. - */ -plugins { - id("com.android.library") - kotlin("android") -} - -java { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 -} - -apply(from = rootProject.file(".buildscript/configure-maven-publish.gradle")) -apply(from = rootProject.file(".buildscript/configure-android-defaults.gradle")) - -apply(from = rootProject.file(".buildscript/configure-compose.gradle")) -tasks.withType { - kotlinOptions.apiVersion = "1.3" -} - -dependencies { - api(project(":workflow-ui:core-android")) - api(Dependencies.Kotlin.Stdlib.jdk8) - - implementation(project(":workflow-runtime")) - implementation(Dependencies.Compose.foundation) - implementation(Dependencies.Compose.layout) - implementation(Dependencies.Compose.tooling) - - testImplementation(Dependencies.Test.junit) - testImplementation(Dependencies.Test.truth) -} diff --git a/kotlin/workflow-ui/core-compose/gradle.properties b/kotlin/workflow-ui/core-compose/gradle.properties deleted file mode 100644 index 293cedcf2..000000000 --- a/kotlin/workflow-ui/core-compose/gradle.properties +++ /dev/null @@ -1,18 +0,0 @@ -# -# Copyright 2019 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. -# -POM_ARTIFACT_ID=workflow-ui-core-compose -POM_NAME=Workflow UI Core Compose -POM_PACKAGING=aar diff --git a/kotlin/workflow-ui/core-compose/src/main/AndroidManifest.xml b/kotlin/workflow-ui/core-compose/src/main/AndroidManifest.xml deleted file mode 100644 index 4b26d2a70..000000000 --- a/kotlin/workflow-ui/core-compose/src/main/AndroidManifest.xml +++ /dev/null @@ -1,16 +0,0 @@ - - diff --git a/kotlin/workflow-ui/core-compose/src/main/java/com/squareup/workflow/ui/compose/ComposeViewFactory.kt b/kotlin/workflow-ui/core-compose/src/main/java/com/squareup/workflow/ui/compose/ComposeViewFactory.kt deleted file mode 100644 index b095f296f..000000000 --- a/kotlin/workflow-ui/core-compose/src/main/java/com/squareup/workflow/ui/compose/ComposeViewFactory.kt +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2019 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. - */ -// See https://youtrack.jetbrains.com/issue/KT-31734 -@file:Suppress("RemoveEmptyParenthesesFromAnnotationEntry") - -package com.squareup.workflow.ui.compose - -import android.content.Context -import android.view.View -import android.view.ViewGroup -import android.widget.FrameLayout -import androidx.compose.Composable -import androidx.compose.Recomposer -import androidx.compose.StructurallyEqual -import androidx.compose.mutableStateOf -import androidx.ui.core.setContent -import com.squareup.workflow.ui.ViewEnvironment -import com.squareup.workflow.ui.ViewFactory -import com.squareup.workflow.ui.bindShowRendering -import kotlin.reflect.KClass - -/** - * Creates a [ViewFactory] that uses a [Composable] function to display the rendering. - * - * Note that the function you pass in will not have any `MaterialTheme` applied, so views that rely - * on Material theme attributes must be explicitly wrapped with `MaterialTheme`. - * - * Simple usage: - * - * ``` - * // Function references to @Composable functions aren't supported yet. - * val FooBinding = bindCompose { showFoo(it) } - * - * @Composable - * private fun showFoo(foo: FooRendering) { - * MaterialTheme { - * Text(foo.message) - * } - * } - * - * … - * - * val viewRegistry = ViewRegistry(FooBinding, …) - * ``` - */ -inline fun bindCompose( - noinline showRendering: @Composable() (RenderingT, ViewEnvironment) -> Unit -): ViewFactory = ComposeViewFactory(RenderingT::class) { rendering, environment -> - showRendering(rendering, environment) -} - -@PublishedApi -internal class ComposeViewFactory( - override val type: KClass, - private val showRendering: @Composable() (RenderingT, ViewEnvironment) -> Unit -) : ViewFactory { - - override fun buildView( - initialRendering: RenderingT, - initialViewEnvironment: ViewEnvironment, - contextForNewView: Context, - container: ViewGroup? - ): View { - // There is currently no way to automatically generate an Android View directly from a - // Composable function, so we need to use ViewGroup.setContent. - val composeContainer = FrameLayout(contextForNewView) - - // This model will be used to feed state updates into the composition. - val renderState = mutableStateOf?>( - value = null, - areEquivalent = StructurallyEqual - ) - - // Entry point to the composition. - composeContainer.setContent(Recomposer.current()) { - // Don't compose anything until we have the first value (which should happen in the initial - // frame). - val (rendering, environment) = renderState.value ?: return@setContent - showRendering(rendering, environment) - } - - composeContainer.bindShowRendering( - initialRendering, - initialViewEnvironment - ) { rendering, environment -> - renderState.value = Pair(rendering, environment) - } - return composeContainer - } -}