Share
Embed
Transcript
111 slides
-
About me • Luca • Italian • Android Engineer @Babylon
Health • Based in London @luca_nicolett -
MVI ACTIONS ACTION RELAY TRANSFORMERS REDUCER STATE UPDATES SOMETHING ACTIONS
EVENTS STATE STATE RELAY -
MVI We also added middleware • Works as a glue
• Binds actions to transformers • Transformations to reducers • Or actions to reducers directly -
MVI perform("load logged in patient data") .on( LifecycleAction.Created::class.java, ProfileScreenAction.RetryConnection::class.java, ProfileScreenAction.MonitorReady::class.java
) .transform { transformers.loadPatient(this) } .withReducer(reducers::reduceLoggedInPatient) -
MVI perform("load logged in patient") .on( LifecycleAction.Created::class.java, ProfileScreenAction.RetryConnection::class.java, ProfileScreenAction.MonitorReady::class.java )
.transform { transformers.loadPatient(this) } .withReducer(reducers::reduceLoggedInPatient) -
MVI private fun render(state: HomeScreenRedesignState) { loading_container .show(state.loadingState == LoadingState.Loading)
unable_to_connect_error_container .show(state.loadingState == LoadingState.Error) /* ... */ } -
MVI fun View.show(isVisible: Boolean = true) { this.visibility = if
(isVisible) View.VISIBLE else View.GONE } -
Jetpack Compose • Declarative UI • Concise and idiomatic •
Stateless or Stateful components • Reusable components • Compatible -
Jetpack Compose @Composable @GenerateView fun Greetings(name: String) { /* ...
*/ } <GreetingsView android:id="@+id/greetings" app:name="Luca" /> val greetingsView = findViewById<GreetingsView>(R.id.greetings) greetingsView.name = "Luca" -
Jetpack Compose @Composable @GenerateView fun Greetings(name: String) { /* ...
*/ } <GreetingsView android:id="@+id/greetings" app:name="Luca" /> val greetingsView = findViewById<GreetingsView>(R.id.greetings) greetingsView.name = "Luca" -
Jetpack Compose @Composable @GenerateView fun Greetings(name: String) { /* ...
*/ } <GreetingsView android:id="@+id/greetings" app:name="Luca" /> val greetingsView = findViewById<GreetingsView>(R.id.greetings) greetingsView.name = "Luca" -
Jetpack Compose • Declarative UI • Concise and idiomatic •
Stateless or Stateful components • Reusable components • Compatible • Unbundled from OS -
Jetpack Compose mkdir androidx-master-dev cd androidx-master-dev repo init -u https://android.googlesource.com/platform/manifest
-b androidx-master-dev repo sync -j8 -c -
Jetpack Compose mkdir androidx-master-dev cd androidx-master-dev repo init -u https://android.googlesource.com/platform/manifest
-b androidx-master-dev repo sync -j8 -c Download the code (and grab a coffee while we pull down 6GB) -
class MyActivity: Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)
setContent { MyComposableFunction() } } } Jetpack Compose -
fun Activity.setContent(content: @Composable() () -> Unit) : CompositionContext? { /*
... */ } Jetpack Compose -
fun Activity.setContent(content: @Composable() () -> Unit) : CompositionContext? { /*
... */ } Jetpack Compose -
val craneView = window.decorView .findViewById<ViewGroup>(android.R.id.content) .getChildAt(0) as? AndroidCraneView ?: AndroidCraneView(this).also
{ setContentView(it) } Jetpack Compose -
val coroutineContext = Dispatchers.Main return Compose.composeInto(craneView.root, this) { WrapWithAmbients(craneView, this,
coroutineContext) { content() } } Jetpack Compose -
@Composable fun Text(/* ... */) { /* ... */ Draw
{ canvas, _ -> internalSelection.value?.let { textDelegate.paintBackground( it.min, it.max, selectionColor, canvas ) } textDelegate.paint(canvas) } /* ... */ } Jetpack Compose -
@Composable fun TextField(/* ... */) { // States val hasFocus
= +state { false } val coords = +state<LayoutCoordinates?> { null } /* ... */ } Jetpack Compose -
@CheckResult("+") /* inline */ fun <T> state(init: () -> T)
= memo { State(init()) } Jetpack Compose -
/** * The State class is an @Model class meant
to * wrap around a single value. * It is used in the `+state` and `+stateFor` effects. */ @Model class State<T> @PublishedApi internal constructor(value:T) : Framed { /* ... */ } Jetpack Compose -
/** * [Model] can be applied to a class which
represents your * application's data model, and will cause instances of the * class to become observable, such that a read of a property * of an instance of this class during the invocation of a * composable function will cause that component to be * "subscribed" to mutations of that instance. Composable * functions which directly or indirectly read properties of the * model class, the composables will be recomposed whenever any * properties of the the model are written to. */ Jetpack Compose -
@Model data class TaskModel( var isDone: Boolean, val description: String
): BaseModel() Jetpack Compose -
@Model data class TaskModel( var isDone: Boolean, val description: String
): BaseModel() TaskModel.kt: (14, 3): Model objects do not support inheritance Jetpack Compose -
@Composable fun RallyBody() { Padding(padding = 16.dp) { Column {
// TODO: scrolling container RallyAlertCard() HeightSpacer(height = 10.dp) RallyAccountsCard() } } } Jetpack Compose -
@Composable fun RallyBody() { Padding(padding = 16.dp) { VerticalScroller {
Column { RallyAlertCard() HeightSpacer(height = 10.dp) RallyAccountsCard() } } } } Jetpack Compose -
@Composable fun DrawSeekBar(x: Float) { var paint = +memo {
Paint() } Draw { canvas, parentSize -> /* ... */ canvas.drawRect(Rect(/* ... */), paint) canvas.drawRect(Rect(/* ... */), paint) canvas.drawCircle(/* ... */, paint) } } Jetpack Compose -
Text(text = "Row") ContainerWithBackground( width = ExampleSize, color = lightGrey
) { Row { PurpleSquare() CyanSquare() } } Jetpack Compose -
Jetpack Compose ContainerWithBackground( width = ExampleSize, color = lightGrey )
{ Row( mainAxisAlignment = MainAxisAlignment.End ) { PurpleSquare() CyanSquare() } } -
Text(text = "Column") ContainerWithBackground( height = ExampleSize, color = lightGrey
) { Row { Column { PurpleSquare() CyanSquare() } /* ... */ } } Jetpack Compose -
Jetpack Compose ContainerWithBackground( height = ExampleSize, color = lightGrey )
{ Column( crossAxisAlignment = CrossAxisAlignment.End ) { PurpleSquare() CyanSquare() } } -
@Composable fun RippleRect() { val toState = +state{ ButtonStatus.Initial }
val onPress:(PxPosition) -> Unit = { p -> down.x = p.x.value down.y = p.y.value toState.value = ButtonStatus.Pressed } /* ... */ } Jetpack Compose -
@Composable fun RippleRect() { val toState = +state{ ButtonStatus.Initial }
val onPress:(PxPosition) -> Unit = { p -> down.x = p.x.value down.y = p.y.value toState.value = ButtonStatus.Pressed } /* ... */ } Jetpack Compose -
@Composable fun RippleRect() { /* ... */ val onRelease: ()
-> Unit = { toState.value = ButtonStatus.Released } /* ... */ } Jetpack Compose -
PressGestureDetector(onPress, onRelease) { Container(expanded = true) { Transition(toState = toState.value)
{ s-> RippleRectFromState(s) } } } Jetpack Compose -
@Composable fun RippleRectFromState(state: TransitionState) { Draw { canvas, _ ->
canvas.drawCircle( Offset(x, y), radius, paint ) } } Jetpack Compose -
@Composable fun AlertDialog( onCloseRequest: () -> Unit, title: (@Composable() ()
-> Unit), text: (@Composable() () -> Unit), confirmButton: (@Composable() () -> Unit), dismissButton: (@Composable() () -> Unit), buttonLayout: AlertDialogButtonLayout ) { /* ... */ } Jetpack Compose -
Jetpack Compose @Composable fun Dialog( onCloseRequest: () -> Unit, children:
@Composable() () -> Unit ) { val context = +ambient(ContextAmbient) val dialog = +memo { DialogWrapper(context, onCloseRequest) } } -
Jetpack Compose @Composable fun Dialog( onCloseRequest: () -> Unit, children:
@Composable() () -> Unit ) { +onActive { dialog.show() onDispose { dialog.dismiss() dialog.disposeComposition() } } } -
Jetpack Compose @Composable fun Dialog( onCloseRequest: () -> Unit, children:
@Composable() () -> Unit ) { +onCommit { dialog.setContent(children) } } -
Jetpack Compose @Test fun topAppBar_expandsToScreen() { val dm = composeTestRule.displayMetrics
composeTestRule .setMaterialContentAndCollectSizes { TopAppBar<Nothing>() } .assertHeightEqualsTo(appBarHeight) .assertWidthEqualsTo { dm.widthPixels.ipx } } -
Jetpack Compose @Test fun topAppBar_expandsToScreen() { val dm = composeTestRule.displayMetrics
composeTestRule .setMaterialContentAndCollectSizes { TopAppBar<Nothing>() } .assertHeightEqualsTo(appBarHeight) .assertWidthEqualsTo { dm.widthPixels.ipx } } -
Jetpack Compose @Test fun topAppBar_expandsToScreen() { val dm = composeTestRule.displayMetrics
composeTestRule .setMaterialContentAndCollectSizes { TopAppBar<Nothing>() } .assertHeightEqualsTo(appBarHeight) .assertWidthEqualsTo { dm.widthPixels.ipx } } -
All together object InProgress : BaseViewState(true, null) { @Composable override
fun buildUI() { Container(expanded = true) { CircularProgressIndicator() } } } -
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) viewModel = /* view
model init */ setContent { CustomTheme { render(viewModel.states()) } } /* .. */ } All together -
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) viewModel = /* view
model init */ setContent { CustomTheme { render(viewModel.states()) } } /* .. */ } All together -
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) viewModel = /* view
model init */ setContent { CustomTheme { render(viewModel.states()) } } /* .. */ } All together -
override fun render( observableState: Observable<BaseViewState> ) { val state =
+observe(ViewState.Idle, observableState) state.buildUI() } All together -
override fun render( observableState: Observable<BaseViewState> ) { val state =
+observe(ViewState.Idle, observableState) state.buildUI() } All together -
override fun render( observableState: Observable<BaseViewState> ) { val state =
+observe(ViewState.Idle, observableState) state.buildUI() } All together -
fun <T> observe(initialState: T, data: Observable<T>) = effectOf<T> { val
result = +state { initialState } +onActive { val disposable = data.subscribe { newValue -> result.value = newValue } onDispose { disposable.dispose() } } return result.value } All together -
fun <T> observe(initialState: T, data: Observable<T>) = effectOf<T> { val
result = +state { initialState } +onActive { val disposable = data.subscribe { newValue -> result.value = newValue } onDispose { disposable.dispose() } } return result.value } All together -
fun <T> observe(initialState: T, data: Observable<T>) = effectOf<T> { val
result = +state { initialState } +onActive { val disposable = data.subscribe { newValue -> result.value = newValue } onDispose { disposable.dispose() } } return result.value } All together -
fun <T> observe(initialState: T, data: Observable<T>) = effectOf<T> { val
result = +state { initialState } +onActive { val disposable = data.subscribe { newValue -> result.value = newValue } onDispose { disposable.dispose() } } return result.value } All together -
fun <T> observe(initialState: T, data: Observable<T>) = effectOf<T> { val
result = +state { initialState } +onActive { val disposable = data.subscribe { newValue -> result.value = newValue } onDispose { disposable.dispose() } } return result.value } All together -
All together override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // viewModel
= /* ... */ not needed anymore setContent { PostListScreen( processIntents(intents()), reloadPostListIntentPublisher ) } } -
All together override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // viewModel
= /* ... */ not needed anymore setContent { PostListScreen( processIntents(intents()), reloadPostListIntentPublisher ) } } -
All together override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // viewModel
= /* ... */ not needed anymore setContent { PostListScreen( processIntents(intents()), reloadPostListIntentPublisher ) } } -
All together override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // viewModel
= /* ... */ not needed anymore setContent { PostListScreen( processIntents(intents()), reloadPostListIntentPublisher ) } } -
All together fun modelState() = +effectOf<PostListViewState> { val result =
+state { PostListViewState.Idle } var disposable: Disposable? = null +onActive { /* ... */ } +onDispose { disposable?.dispose() } result.value } -
All together fun modelState() = +effectOf<PostListViewState> { val result =
+state { PostListViewState.Idle } var disposable: Disposable? = null +onActive { /* ... */ } +onDispose { disposable?.dispose() } result.value } -
All together fun modelState() = +effectOf<PostListViewState> { val result =
+state { PostListViewState.Idle } var disposable: Disposable? = null +onActive { /* ... */ } +onDispose { disposable?.dispose() } result.value } -
All together fun modelState() = +effectOf<PostListViewState> { val result =
+state { PostListViewState.Idle } var disposable: Disposable? = null +onActive { /* ... */ } +onDispose { disposable?.dispose() } result.value } -
All together +onActive { disposable = intentsObserver .applyBusinessLogic() .subscribe {
result.value = it } } -
All together override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // viewModel
= /* ... */ not needed anymore setContent { PostListScreen( processIntents(intents()) ) } } -
All together @Composable fun PostListScreen( state: PostListViewState ) { when
(state) { PostListViewState.InProgress -> { /* ... */ } } }