-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add back handling for iOS as well as make iOS sample work for non-Pre…
…view and View related items
- Loading branch information
Showing
16 changed files
with
357 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
samples/compose-samples/iosApp/iosApp/ContainerViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import SwiftUI | ||
|
||
class ContainerViewController: UIViewController { | ||
private let onTouchDown: (CGPoint) -> Void | ||
|
||
init(child: UIViewController, onTouchDown: @escaping (CGPoint) -> Void) { | ||
self.onTouchDown = onTouchDown | ||
super.init(nibName: nil, bundle: nil) | ||
addChild(child) | ||
child.view.frame = view.frame | ||
view.addSubview(child.view) | ||
child.didMove(toParent: self) | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | ||
super.touchesBegan(touches, with: event) | ||
if let startPoint = touches.first?.location(in: nil) { | ||
onTouchDown(startPoint) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,3 @@ private val viewEnvironment = defaultViewEnvironment() | |
) | ||
} | ||
} | ||
|
16 changes: 16 additions & 0 deletions
16
samples/compose-samples/src/iosMain/kotlin/com/squareup/sample/compose/DefaultsIos.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.squareup.sample.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import store | ||
|
||
@Composable | ||
actual fun BackHandler(isEnabled: Boolean, onBack: () -> Unit) { | ||
LaunchedEffect(isEnabled) { | ||
if (isEnabled) { | ||
store.events.collect { | ||
onBack() | ||
} | ||
} | ||
} | ||
} |
31 changes: 29 additions & 2 deletions
31
samples/compose-samples/src/iosMain/kotlin/com/squareup/sample/compose/MainViewController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,37 @@ | ||
import androidx.compose.foundation.text.BasicText | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.window.ComposeUIViewController | ||
import com.squareup.sample.compose.defaultViewEnvironment | ||
import com.squareup.sample.compose.launcher.SampleWorkflow | ||
import com.squareup.sample.compose.states.Action | ||
import com.squareup.sample.compose.states.Store | ||
import com.squareup.sample.compose.states.createStore | ||
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi | ||
import com.squareup.workflow1.ui.compose.WorkflowRendering | ||
import com.squareup.workflow1.ui.compose.renderAsState | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.withContext | ||
|
||
fun MainViewController() = ComposeUIViewController { App() } | ||
|
||
val store: Store = CoroutineScope(SupervisorJob()).createStore() | ||
|
||
@OptIn(WorkflowUiExperimentalApi::class) | ||
@Composable | ||
fun App() { | ||
BasicText("Hello World") | ||
MaterialTheme { | ||
val rendering by SampleWorkflow.renderAsState(Unit) {} | ||
|
||
WorkflowRendering( | ||
rendering, | ||
defaultViewEnvironment() | ||
) | ||
} | ||
} | ||
|
||
fun onBackGesture() { | ||
store.send(Action.OnBackPressed) | ||
} |
15 changes: 15 additions & 0 deletions
15
...ples/src/iosMain/kotlin/com/squareup/sample/compose/inlinerendering/InlineRenderingApp.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.squareup.sample.compose.inlinerendering | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import com.squareup.sample.compose.defaultViewEnvironment | ||
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi | ||
import com.squareup.workflow1.ui.compose.WorkflowRendering | ||
import com.squareup.workflow1.ui.compose.renderAsState | ||
|
||
@OptIn(WorkflowUiExperimentalApi::class) | ||
@Composable | ||
fun InlineRenderingApp() { | ||
val rendering by InlineRenderingWorkflow.renderAsState(Unit) {} | ||
WorkflowRendering(rendering, defaultViewEnvironment()) | ||
} |
44 changes: 44 additions & 0 deletions
44
...s/compose-samples/src/iosMain/kotlin/com/squareup/sample/compose/launcher/SampleScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
@file:OptIn(WorkflowUiExperimentalApi::class) | ||
|
||
package com.squareup.sample.compose.launcher | ||
|
||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.material.Card | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import com.squareup.workflow1.ui.ViewEnvironment | ||
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi | ||
import com.squareup.workflow1.ui.compose.ComposeScreen | ||
|
||
data class SampleScreen(val onSampleClicked: (Sample) -> Unit) : ComposeScreen { | ||
@Composable | ||
override fun Content(viewEnvironment: ViewEnvironment) { | ||
LazyColumn( | ||
verticalArrangement = Arrangement.spacedBy(16.dp), | ||
modifier = Modifier.fillMaxSize().padding(16.dp) | ||
) { | ||
items(samples) { sample -> | ||
Card( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.clickable { onSampleClicked(sample) } | ||
) { | ||
Column(modifier = Modifier.padding(16.dp)) { | ||
Text(sample.name, style = MaterialTheme.typography.h6) | ||
Text(sample.description, style = MaterialTheme.typography.body1) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...compose-samples/src/iosMain/kotlin/com/squareup/sample/compose/launcher/SampleWorkflow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
@file:OptIn(WorkflowUiExperimentalApi::class) | ||
|
||
package com.squareup.sample.compose.launcher | ||
|
||
import androidx.compose.runtime.Composable | ||
import com.squareup.sample.compose.BackHandler | ||
import com.squareup.sample.compose.hellocomposebinding.HelloWorkflow | ||
import com.squareup.sample.compose.hellocomposebinding.viewEnvironment | ||
import com.squareup.sample.compose.hellocomposeworkflow.HelloComposeWorkflow | ||
import com.squareup.workflow1.Snapshot | ||
import com.squareup.workflow1.StatefulWorkflow | ||
import com.squareup.workflow1.Workflow | ||
import com.squareup.workflow1.WorkflowAction.Companion.noAction | ||
import com.squareup.workflow1.mapRendering | ||
import com.squareup.workflow1.renderChild | ||
import com.squareup.workflow1.ui.Screen | ||
import com.squareup.workflow1.ui.ViewEnvironment | ||
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi | ||
import com.squareup.workflow1.ui.compose.ComposeScreen | ||
import com.squareup.workflow1.ui.compose.WorkflowRendering | ||
import com.squareup.workflow1.ui.withEnvironment | ||
|
||
object SampleWorkflow : StatefulWorkflow<Unit, Sample?, Nothing, Screen>() { | ||
override fun render( | ||
renderProps: Unit, | ||
renderState: Sample?, | ||
context: RenderContext | ||
): Screen { | ||
val screen = when (val workflow = renderState?.workflow) { | ||
null -> SampleScreen(context.eventHandler { sample -> state = sample }) | ||
is HelloComposeWorkflow -> context.renderChild( | ||
child = workflow, | ||
props = "Hello", | ||
) { noAction() } | ||
|
||
is HelloWorkflow -> context.renderChild( | ||
child = workflow.mapRendering { it.withEnvironment(viewEnvironment) } | ||
) { noAction() } | ||
|
||
else -> context.renderChild(child = workflow as Workflow<Unit, Nothing, Screen>) | ||
} | ||
|
||
return BackHandlerScreen(screen, onBack = context.eventHandler { state = null }) | ||
} | ||
|
||
override fun initialState( | ||
props: Unit, | ||
snapshot: Snapshot? | ||
) = null | ||
|
||
override fun snapshotState(state: Sample?): Snapshot? = null | ||
} | ||
|
||
data class BackHandlerScreen(val screen: Screen, val onBack: () -> Unit) : ComposeScreen { | ||
@Composable | ||
override fun Content(viewEnvironment: ViewEnvironment) { | ||
BackHandler(onBack = onBack) | ||
WorkflowRendering(screen, viewEnvironment) | ||
} | ||
} |
Oops, something went wrong.