Skip to content

Commit cc29669

Browse files
committed
cp samples/{hellow-workflow,nested-overlays}
1 parent 51d5936 commit cc29669

File tree

10 files changed

+218
-0
lines changed

10 files changed

+218
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
plugins {
2+
id("com.android.application")
3+
`kotlin-android`
4+
`android-sample-app`
5+
`android-ui-tests`
6+
}
7+
8+
android {
9+
defaultConfig {
10+
applicationId = "com.squareup.sample.nestedoverlays"
11+
}
12+
namespace = "com.squareup.sample.nestedoverlays"
13+
}
14+
15+
dependencies {
16+
debugImplementation(libs.squareup.leakcanary.android)
17+
18+
implementation(libs.androidx.activity.ktx)
19+
implementation(libs.androidx.lifecycle.viewmodel.ktx)
20+
implementation(libs.androidx.lifecycle.viewmodel.savedstate)
21+
implementation(libs.androidx.viewbinding)
22+
23+
implementation(project(":workflow-ui:core-android"))
24+
implementation(project(":workflow-ui:core-common"))
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.squareup.sample.nestedoverlays
2+
3+
import androidx.test.espresso.action.ViewActions.click
4+
import androidx.test.espresso.assertion.ViewAssertions.matches
5+
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
6+
import androidx.test.espresso.matcher.ViewMatchers.withText
7+
import androidx.test.ext.junit.rules.ActivityScenarioRule
8+
import androidx.test.ext.junit.runners.AndroidJUnit4
9+
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
10+
import com.squareup.workflow1.ui.internal.test.IdlingDispatcherRule
11+
import com.squareup.workflow1.ui.internal.test.inAnyView
12+
import leakcanary.DetectLeaksAfterTestSuccess
13+
import org.junit.Rule
14+
import org.junit.Test
15+
import org.junit.rules.RuleChain
16+
import org.junit.runner.RunWith
17+
18+
@RunWith(AndroidJUnit4::class)
19+
@OptIn(WorkflowUiExperimentalApi::class)
20+
class NestedOverlaysAppTest {
21+
22+
private val scenarioRule = ActivityScenarioRule(NestedOverlaysActivity::class.java)
23+
24+
@get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess())
25+
.around(scenarioRule)
26+
.around(IdlingDispatcherRule)
27+
28+
@Test fun togglesHelloAndGoodbye() {
29+
inAnyView(withText("Hello"))
30+
.check(matches(isDisplayed()))
31+
.perform(click())
32+
33+
inAnyView(withText("Goodbye"))
34+
.check(matches(isDisplayed()))
35+
.perform(click())
36+
37+
inAnyView(withText("Hello"))
38+
.check(matches(isDisplayed()))
39+
}
40+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<application
6+
android:allowBackup="false"
7+
android:label="@string/app_name"
8+
android:theme="@style/AppTheme"
9+
tools:ignore="GoogleAppIndexingWarning,MissingApplicationIcon"
10+
>
11+
12+
<activity android:name="com.squareup.sample.nestedoverlays.NestedOverlaysActivity"
13+
android:exported="true">
14+
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
<category android:name="android.intent.category.LAUNCHER" />
18+
</intent-filter>
19+
20+
</activity>
21+
22+
</application>
23+
</manifest>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.squareup.sample.nestedoverlays
2+
3+
import com.squareup.sample.nestedoverlays.databinding.HelloGoodbyeLayoutBinding
4+
import com.squareup.workflow1.ui.AndroidScreen
5+
import com.squareup.workflow1.ui.ScreenViewFactory
6+
import com.squareup.workflow1.ui.ScreenViewFactory.Companion.fromViewBinding
7+
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
8+
9+
@OptIn(WorkflowUiExperimentalApi::class)
10+
data class HelloRendering(
11+
val message: String,
12+
val onClick: () -> Unit
13+
) : AndroidScreen<HelloRendering> {
14+
override val viewFactory: ScreenViewFactory<HelloRendering> =
15+
fromViewBinding(HelloGoodbyeLayoutBinding::inflate) { r, _ ->
16+
helloMessage.text = r.message
17+
helloMessage.setOnClickListener { r.onClick() }
18+
}
19+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
@file:OptIn(WorkflowExperimentalRuntime::class)
2+
3+
package com.squareup.sample.nestedoverlays
4+
5+
import android.os.Bundle
6+
import androidx.activity.viewModels
7+
import androidx.appcompat.app.AppCompatActivity
8+
import androidx.lifecycle.SavedStateHandle
9+
import androidx.lifecycle.ViewModel
10+
import androidx.lifecycle.viewModelScope
11+
import com.squareup.workflow1.WorkflowExperimentalRuntime
12+
import com.squareup.workflow1.config.AndroidRuntimeConfigTools
13+
import com.squareup.workflow1.ui.WorkflowLayout
14+
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
15+
import com.squareup.workflow1.ui.renderWorkflowIn
16+
import kotlinx.coroutines.flow.StateFlow
17+
18+
@OptIn(WorkflowUiExperimentalApi::class)
19+
class NestedOverlaysActivity : AppCompatActivity() {
20+
override fun onCreate(savedInstanceState: Bundle?) {
21+
super.onCreate(savedInstanceState)
22+
23+
// This ViewModel will survive configuration changes. It's instantiated
24+
// by the first call to viewModels(), and that original instance is returned by
25+
// succeeding calls.
26+
val model: HelloViewModel by viewModels()
27+
setContentView(
28+
WorkflowLayout(this).apply { take(lifecycle, model.renderings) }
29+
)
30+
}
31+
}
32+
33+
class HelloViewModel(savedState: SavedStateHandle) : ViewModel() {
34+
@OptIn(WorkflowUiExperimentalApi::class)
35+
val renderings: StateFlow<HelloRendering> by lazy {
36+
renderWorkflowIn(
37+
workflow = NestedOverlaysWorkflow,
38+
scope = viewModelScope,
39+
savedStateHandle = savedState,
40+
runtimeConfig = AndroidRuntimeConfigTools.getAppWorkflowRuntimeConfig()
41+
)
42+
}
43+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.squareup.sample.nestedoverlays
2+
3+
import com.squareup.sample.nestedoverlays.NestedOverlaysWorkflow.State
4+
import com.squareup.sample.nestedoverlays.NestedOverlaysWorkflow.State.Goodbye
5+
import com.squareup.sample.nestedoverlays.NestedOverlaysWorkflow.State.Hello
6+
import com.squareup.workflow1.Snapshot
7+
import com.squareup.workflow1.StatefulWorkflow
8+
import com.squareup.workflow1.action
9+
import com.squareup.workflow1.parse
10+
11+
object NestedOverlaysWorkflow : StatefulWorkflow<Unit, State, Nothing, HelloRendering>() {
12+
enum class State {
13+
Hello,
14+
Goodbye
15+
}
16+
17+
override fun initialState(
18+
props: Unit,
19+
snapshot: Snapshot?
20+
): State = snapshot?.bytes?.parse { source -> if (source.readInt() == 1) Hello else Goodbye }
21+
?: Hello
22+
23+
override fun render(
24+
renderProps: Unit,
25+
renderState: State,
26+
context: RenderContext
27+
): HelloRendering {
28+
return HelloRendering(
29+
message = renderState.name,
30+
onClick = { context.actionSink.send(helloAction) }
31+
)
32+
}
33+
34+
override fun snapshotState(state: State): Snapshot = Snapshot.of(if (state == Hello) 1 else 0)
35+
36+
private val helloAction = action {
37+
state = when (state) {
38+
Hello -> Goodbye
39+
Goodbye -> Hello
40+
}
41+
}
42+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="vertical"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent">
6+
7+
<TextView
8+
android:id="@+id/hello_message"
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent"
11+
android:gravity="center"
12+
/>
13+
14+
</LinearLayout>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">Nested Overlays</string>
3+
</resources>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<resources>
2+
3+
<!-- Base application theme. -->
4+
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
5+
<!-- Customize your theme here. -->
6+
</style>
7+
8+
</resources>

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ include(
5555
":samples:hello-terminal:todo-terminal-app",
5656
":samples:hello-workflow",
5757
":samples:hello-workflow-fragment",
58+
":samples:nested-overlays",
5859
":samples:stub-visibility",
5960
":samples:tictactoe:app",
6061
":samples:tictactoe:common",

0 commit comments

Comments
 (0)