Skip to content

Commit d03bed0

Browse files
committed
Introduces AlertOverlay
First concrete use of `Overlay`. Updates `hellobackbutton` sample. Note the commented out deprecations. Turns out its impossible to suppress deprecation warnings in `typealias` calls like the ones our sample apps are still using, and those can't be ported until we have a replacement for `ModalViewContainer`. Tracked in #589.
1 parent acc7336 commit d03bed0

File tree

18 files changed

+218
-45
lines changed

18 files changed

+218
-45
lines changed

samples/containers/common/src/main/java/com/squareup/sample/container/panel/PanelContainerScreen.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:Suppress("DEPRECATION")
2+
13
package com.squareup.sample.container.panel
24

35
import com.squareup.workflow1.ui.Screen

samples/containers/hello-back-button/src/main/java/com/squareup/sample/hellobackbutton/AreYouSureWorkflow.kt

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@ import com.squareup.workflow1.StatefulWorkflow
1111
import com.squareup.workflow1.WorkflowAction.Companion.noAction
1212
import com.squareup.workflow1.action
1313
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
14-
import com.squareup.workflow1.ui.modal.AlertContainerScreen
15-
import com.squareup.workflow1.ui.modal.AlertScreen
16-
import com.squareup.workflow1.ui.modal.AlertScreen.Button.NEGATIVE
17-
import com.squareup.workflow1.ui.modal.AlertScreen.Button.POSITIVE
18-
import com.squareup.workflow1.ui.modal.AlertScreen.Event.ButtonClicked
19-
import com.squareup.workflow1.ui.modal.AlertScreen.Event.Canceled
14+
import com.squareup.workflow1.ui.container.AlertOverlay
15+
import com.squareup.workflow1.ui.container.AlertOverlay.Button.NEGATIVE
16+
import com.squareup.workflow1.ui.container.AlertOverlay.Button.POSITIVE
17+
import com.squareup.workflow1.ui.container.AlertOverlay.Event.ButtonClicked
18+
import com.squareup.workflow1.ui.container.AlertOverlay.Event.Canceled
19+
import com.squareup.workflow1.ui.container.BodyAndModalsScreen
2020
import com.squareup.workflow1.ui.toParcelable
2121
import com.squareup.workflow1.ui.toSnapshot
2222
import kotlinx.parcelize.Parcelize
2323

2424
/**
25-
* Wraps [HelloBackButtonWorkflow] to (sometimes) pop a confirmation dialog when the back
25+
* Wraps [HelloBackButtonWorkflow] to (sometimes) pop a confirmation alert when the back
2626
* button is pressed.
2727
*/
2828
@OptIn(WorkflowUiExperimentalApi::class)
29-
object AreYouSureWorkflow : StatefulWorkflow<Unit, State, Finished, AlertContainerScreen<*>>() {
29+
object AreYouSureWorkflow :
30+
StatefulWorkflow<Unit, State, Finished, BodyAndModalsScreen<*, AlertOverlay>>() {
3031
override fun initialState(
3132
props: Unit,
3233
snapshot: Snapshot?
@@ -44,42 +45,42 @@ object AreYouSureWorkflow : StatefulWorkflow<Unit, State, Finished, AlertContain
4445
renderProps: Unit,
4546
renderState: State,
4647
context: RenderContext
47-
): AlertContainerScreen<*> {
48+
): BodyAndModalsScreen<*, AlertOverlay> {
4849
val ableBakerCharlie = context.renderChild(HelloBackButtonWorkflow, Unit) { noAction() }
4950

5051
return when (renderState) {
5152
Running -> {
52-
AlertContainerScreen(
53-
BackButtonScreen(ableBakerCharlie) {
54-
// While we always provide a back button handler, by default the view code
55-
// associated with BackButtonScreen ignores ours if the view created for the
56-
// wrapped rendering sets a handler of its own. (Set BackButtonScreen.override
57-
// to change this precedence.)
58-
context.actionSink.send(maybeQuit)
59-
}
53+
BodyAndModalsScreen(
54+
BackButtonScreen(ableBakerCharlie) {
55+
// While we always provide a back button handler, by default the view code
56+
// associated with BackButtonScreen ignores ours if the view created for the
57+
// wrapped rendering sets a handler of its own. (Set BackButtonScreen.override
58+
// to change this precedence.)
59+
context.actionSink.send(maybeQuit)
60+
}
6061
)
6162
}
6263
Quitting -> {
63-
val dialog = AlertScreen(
64-
buttons = mapOf(
65-
POSITIVE to "I'm Positive",
66-
NEGATIVE to "Negatory"
67-
),
68-
message = "Are you sure you want to do this thing?",
69-
onEvent = { alertEvent ->
70-
context.actionSink.send(
71-
when (alertEvent) {
72-
is ButtonClicked -> when (alertEvent.button) {
73-
POSITIVE -> confirmQuit
74-
else -> cancelQuit
75-
}
76-
Canceled -> cancelQuit
77-
}
78-
)
79-
}
64+
val alert = AlertOverlay(
65+
buttons = mapOf(
66+
POSITIVE to "I'm Positive",
67+
NEGATIVE to "Negatory"
68+
),
69+
message = "Are you sure you want to do this thing?",
70+
onEvent = { alertEvent ->
71+
context.actionSink.send(
72+
when (alertEvent) {
73+
is ButtonClicked -> when (alertEvent.button) {
74+
POSITIVE -> confirmQuit
75+
else -> cancelQuit
76+
}
77+
Canceled -> cancelQuit
78+
}
79+
)
80+
}
8081
)
8182

82-
AlertContainerScreen(ableBakerCharlie, dialog)
83+
BodyAndModalsScreen(ableBakerCharlie, alert)
8384
}
8485
}
8586
}

samples/containers/hello-back-button/src/main/java/com/squareup/sample/hellobackbutton/HelloBackButtonActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@file:Suppress("DEPRECATION")
12
@file:OptIn(WorkflowUiExperimentalApi::class)
23

34
package com.squareup.sample.hellobackbutton

samples/dungeon/app/src/main/java/com/squareup/sample/dungeon/Component.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:Suppress("DEPRECATION")
2+
13
package com.squareup.sample.dungeon
24

35
import android.content.Context.VIBRATOR_SERVICE

samples/dungeon/app/src/main/java/com/squareup/sample/dungeon/DungeonAppWorkflow.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:Suppress("DEPRECATION")
2+
13
package com.squareup.sample.dungeon
24

35
import com.squareup.sample.dungeon.DungeonAppWorkflow.Props

samples/dungeon/app/src/main/java/com/squareup/sample/dungeon/GameSessionWorkflow.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:Suppress("DEPRECATION")
2+
13
package com.squareup.sample.dungeon
24

35
import android.os.Vibrator

samples/tictactoe/app/src/main/java/com/squareup/sample/mainactivity/TicTacToeActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:Suppress("DEPRECATION")
2+
13
package com.squareup.sample.mainactivity
24

35
import android.os.Bundle

workflow-ui/container-android/src/main/java/com/squareup/workflow1/ui/modal/AlertContainer.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.squareup.workflow1.ui.modal.AlertScreen.Event.Canceled
2727
* Renders the [AlertScreen]s of an [AlertContainerScreen] as [AlertDialog]s.
2828
*/
2929
@WorkflowUiExperimentalApi
30+
@Deprecated("Use BodyAndModalsContainer and the built in OverlayDialogFactory for AlertOverlay")
3031
public class AlertContainer @JvmOverloads constructor(
3132
context: Context,
3233
attributeSet: AttributeSet? = null,

workflow-ui/container-common/src/main/java/com/squareup/workflow1/ui/modal/AlertContainerScreen.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:Suppress("DEPRECATION")
2+
13
package com.squareup.workflow1.ui.modal
24

35
import com.squareup.workflow1.ui.Screen
@@ -9,6 +11,16 @@ import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
911
* @param B the type of [beneathModals]
1012
*/
1113
@WorkflowUiExperimentalApi
14+
// Can't quite deprecate this yet, because such warnings are impossible to suppress
15+
// in the typealias uses in the Tic Tac Toe sample. Will uncomment before merging to main.
16+
// https://github.com/square/workflow-kotlin/issues/589
17+
// @Deprecated(
18+
// "Use BodyAndModalsScreen",
19+
// ReplaceWith(
20+
// "BodyAndModalsScreen<B>(beneathModals, modals)",
21+
// "com.squareup.workflow1.ui.container.BodyAndModalsScreen"
22+
// )
23+
// )
1224
public data class AlertContainerScreen<B : Any>(
1325
override val beneathModals: B,
1426
override val modals: List<AlertScreen> = emptyList()

workflow-ui/container-common/src/main/java/com/squareup/workflow1/ui/modal/AlertScreen.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:Suppress("DEPRECATION")
2+
13
package com.squareup.workflow1.ui.modal
24

35
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
@@ -6,6 +8,16 @@ import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
68
* Models a typical "You sure about that?" alert box.
79
*/
810
@WorkflowUiExperimentalApi
11+
// Can't quite deprecate this yet, because such warnings are impossible to suppress
12+
// in the typealias uses in the Tic Tac Toe sample. Will uncomment before merging to main.
13+
// https://github.com/square/workflow-kotlin/issues/589
14+
// @Deprecated(
15+
// "Use AlertOverlay",
16+
// ReplaceWith(
17+
// "AlertOverlay(buttons, message, title, cancelable, onEvent)",
18+
// "com.squareup.workflow1.ui.container.AlertOverlay"
19+
// )
20+
// )
921
public data class AlertScreen(
1022
val buttons: Map<Button, String> = emptyMap(),
1123
val message: String = "",
@@ -32,9 +44,9 @@ public data class AlertScreen(
3244
other as AlertScreen
3345

3446
return buttons == other.buttons &&
35-
message == other.message &&
36-
title == other.title &&
37-
cancelable == other.cancelable
47+
message == other.message &&
48+
title == other.title &&
49+
cancelable == other.cancelable
3850
}
3951

4052
override fun hashCode(): Int {

0 commit comments

Comments
 (0)