Skip to content

Introduce renderComposable. #1271

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.ui.graphics.Color
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.squareup.workflow1.SimpleLoggingWorkflowInterceptor
import com.squareup.workflow1.WorkflowExperimentalRuntime
import com.squareup.workflow1.config.AndroidRuntimeConfigTools
import com.squareup.workflow1.mapRendering
Expand Down Expand Up @@ -47,7 +48,8 @@ class NestedRenderingsActivity : AppCompatActivity() {
workflow = RecursiveWorkflow.mapRendering { it.withEnvironment(viewEnvironment) },
scope = viewModelScope,
savedStateHandle = savedState,
runtimeConfig = AndroidRuntimeConfigTools.getAppWorkflowRuntimeConfig()
runtimeConfig = AndroidRuntimeConfigTools.getAppWorkflowRuntimeConfig(),
interceptors = listOf(SimpleLoggingWorkflowInterceptor())
)
}
}
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pluginManagement {
google()
// For binary compatibility validator.
maven { url = uri("https://kotlin.bintray.com/kotlinx") }
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
includeBuild("build-logic")
}
Expand Down
3 changes: 3 additions & 0 deletions workflow-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import com.squareup.workflow1.buildsrc.iosWithSimulatorArm64
plugins {
id("kotlin-multiplatform")
id("published")
id("org.jetbrains.compose") version "1.7.3"
}

kotlin {
Expand All @@ -23,6 +24,8 @@ dependencies {
commonMainApi(libs.kotlinx.coroutines.core)
// For Snapshot.
commonMainApi(libs.squareup.okio)
commonMainApi("org.jetbrains.compose.runtime:runtime:1.7.3")
commonMainApi("org.jetbrains.compose.runtime:runtime-saveable:1.7.3")

commonTestImplementation(libs.kotlinx.atomicfu)
commonTestImplementation(libs.kotlinx.coroutines.test.common)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ public fun <PropsT, StateT, OutputT, ChildRenderingT>
key: String = ""
): ChildRenderingT = renderChild(child, Unit, key) { noAction() }

// /**
// * TODO
// */
// @WorkflowExperimentalApi
// public fun <PropsT, StateT, OutputT, ChildRenderingT>
// BaseRenderContext<PropsT, StateT, OutputT>.renderComposable(
// key: String = "",
// content: @WorkflowComposable @Composable () -> ChildRenderingT
// ): ChildRenderingT = renderComposable<Nothing, ChildRenderingT>(
// key = key,
// handler = { noAction() },
// content = { content() }
// )

/**
* Ensures a [LifecycleWorker] is running. Since [worker] can't emit anything,
* it can't trigger any [WorkflowAction]s.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.squareup.workflow1

import com.squareup.workflow1.compose.ComposeWorkflow

/**
* If your Workflow caches its [WorkflowIdentifier] (to avoid frequent lookups) then implement
* this interface. Note that [StatefulWorkflow] and [StatelessWorkflow] already implement this,
* so you only need to do so if you do not extend one of those classes.
* this interface. Note that built-in workflow types ([StatefulWorkflow], [StatelessWorkflow],
* [ComposeWorkflow] etc.) already implement this, so you only need to do so if you do not extend
* one of those classes.
*
* Your Workflow can just assign null to this value as the [identifier] extension will use it
* for caching.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.squareup.workflow1

import com.squareup.workflow1.RuntimeConfigOptions.STABLE_EVENT_HANDLERS
import com.squareup.workflow1.StatefulWorkflow.RenderContext
import kotlinx.coroutines.CoroutineScope
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.squareup.workflow1.compose

import androidx.compose.runtime.Composable
import com.squareup.workflow1.IdCacheable
import com.squareup.workflow1.StatefulWorkflow
import com.squareup.workflow1.Workflow
import com.squareup.workflow1.WorkflowExperimentalApi

/**
* TODO
*/
@WorkflowExperimentalApi
public abstract class ComposeWorkflow<PropsT, OutputT, out RenderingT> :
Workflow<PropsT, OutputT, RenderingT>,
IdCacheable {

/**
* TODO
*/
@WorkflowComposable
@Composable
protected abstract fun produceRendering(
props: PropsT,
emitOutput: (OutputT) -> Unit
): RenderingT

final override fun asStatefulWorkflow(): StatefulWorkflow<PropsT, *, OutputT, RenderingT> {
throw UnsupportedOperationException(
"This version of the Compose runtime does not support ComposeWorkflow. " +
"Please upgrade your workflow-runtime."
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.squareup.workflow1.compose

import androidx.compose.runtime.ComposableTargetMarker
import com.squareup.workflow1.WorkflowExperimentalApi
import kotlin.annotation.AnnotationRetention.BINARY
import kotlin.annotation.AnnotationTarget.FILE
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.PROPERTY_GETTER
import kotlin.annotation.AnnotationTarget.TYPE
import kotlin.annotation.AnnotationTarget.TYPE_PARAMETER

/**
* An annotation that can be used to mark a composable function as being expected to be use in a
* composable function that is also marked or inferred to be marked as a [WorkflowComposable], i.e.
* that can be called from [BaseRenderContext.renderComposable].
*
* Using this annotation explicitly is rarely necessary as the Compose compiler plugin will infer
* the necessary equivalent annotations automatically. See
* [androidx.compose.runtime.ComposableTarget] for details.
*/
@WorkflowExperimentalApi
@ComposableTargetMarker(description = "Workflow Composable")
@Target(FILE, FUNCTION, PROPERTY_GETTER, TYPE, TYPE_PARAMETER)
@Retention(BINARY)
public annotation class WorkflowComposable
8 changes: 8 additions & 0 deletions workflow-runtime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import com.squareup.workflow1.buildsrc.iosWithSimulatorArm64
plugins {
id("kotlin-multiplatform")
id("published")
id("org.jetbrains.compose") version "1.7.3"
}

kotlin {
Expand All @@ -16,6 +17,13 @@ kotlin {
if (targets == "kmp" || targets == "js") {
js(IR) { browser() }
}
// sourceSets {
// getByName("commonMain") {
// dependencies {
// implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
// }
// }
// }
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.squareup.workflow1.WorkflowInterceptor.RenderPassesComplete
import com.squareup.workflow1.internal.WorkflowRunner
import com.squareup.workflow1.internal.chained
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@file:OptIn(WorkflowExperimentalApi::class)

package com.squareup.workflow1

import com.squareup.workflow1.WorkflowInterceptor.RenderContextInterceptor
import com.squareup.workflow1.WorkflowInterceptor.RuntimeLoopOutcome
import com.squareup.workflow1.WorkflowInterceptor.WorkflowSession
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -430,6 +433,21 @@ private class InterceptedRenderContext<P, S, O>(
baseRenderContext.renderChild(iChild, iProps, iKey, iHandler)
}

// override fun <ChildPropsT, ChildOutputT, ChildRenderingT> renderChild(
// child: ComposeWorkflow<ChildPropsT, ChildOutputT, ChildRenderingT>,
// props: ChildPropsT,
// key: String,
// handler: (ChildOutputT) -> WorkflowAction<P, S, O>
// ): ChildRenderingT =
// interceptor.onRenderChild(child, props, key, handler) { iChild, iProps, iKey, iHandler ->
// // Explicitly dispatch to the ComposeWorkflow overload if necessary.
// if (iChild is ComposeWorkflow) {
// baseRenderContext.renderChild(iChild, iProps, iKey, iHandler)
// } else {
// baseRenderContext.renderChild(iChild, iProps, iKey, iHandler)
// }
// }

override fun runningSideEffect(
key: String,
sideEffect: suspend CoroutineScope.() -> Unit
Expand Down Expand Up @@ -459,6 +477,23 @@ private class InterceptedRenderContext<P, S, O>(
}
}

// @OptIn(WorkflowExperimentalApi::class)
// override fun <ChildOutputT, ChildRenderingT> renderComposable(
// key: String,
// handler: (ChildOutputT) -> WorkflowAction<P, S, O>,
// content: @WorkflowComposable @Composable (emitOutput: (ChildOutputT) -> Unit) -> ChildRenderingT
// ): ChildRenderingT = interceptor.onRenderComposable(
// key = key,
// content = content,
// proceed = { iKey, iContent ->
// baseRenderContext.renderComposable(
// key = iKey,
// handler = handler,
// content = iContent
// )
// }
// )

/**
* In a block with a CoroutineScope receiver, calls to `coroutineContext` bind
* to `CoroutineScope.coroutineContext` instead of `suspend val coroutineContext`.
Expand Down
Loading
Loading