What to Expect When You're Expecting... Jetpack Compose
Mark Murphy, CommonsWare
mmurphy@commonsware.com
New Talk, Who Dis?
A Brief Compose History
Because Compose Hasn't Been Around Long
- Announced at Google I|O 2019
- First bits released fall 2019
- Role: replacement UI framework
-
Timeline
- Now: developer preview
- "Summer 2020": alpha
- 2021: stable
A Bit Of Composin'
@Composable
fun NewsStory() {
val image = imageResource(R.drawable.header)
Column(modifier = Modifier.padding(16.dp)) {
val imageModifier = Modifier
.preferredHeightIn(maxHeight = 180.dp)
.fillMaxWidth()
Image(image, modifier = imageModifier,
contentScale = ContentScale.Crop)
HeightSpacer(16.dp)
Text("A day in Shark Fin Cove")
Text("Davenport, California")
Text("December 2018")
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NewsStory()
}
}
}
Revisiting Google and Kotlin
Those Who Do Not Learn From History...
- 2017: Oh, you're interested in Kotlin?
- 2018: We support Kotlin!
-
2019: Kotlin first!
- But we still support Java!
- (hint: they said that once about Eclipse)
Talent Acquisition
Developers Don't Grow on Trees (Usually)
- Hiring Android talent has been tough... until 2020 upended matters
- Being considered "behind the times" makes that tougher
- How long before you can't land talent because you're using Java?
- Reasonably likely that Compose will have a similar effect
Why We Care Now
Versus, Y'know, Later
- Do you remember how that Kotlin migration went?
-
Migrating to Compose will be narrower yet more complicated
- Narrower: effects grow closer you get to the UI rendering
- Complicated: less automatic tool support, most likely
- Perfect Planning Prevents Poor Production... Perhaps
What We Want
Besides Fewer Murder Hornets
- Minimize transition pain... with minimal extraneous prep work, in case Compose fizzles
- Identify opportunities to improve the apps... as Compose itself is largely a lateral move
Kotlinize!
All The Things! Mostly!
- Compose relies upon a Kotlin compiler plugin to perform its magic
- Net: Java ain't walkin' through that door
- By the time Compose is mainstream, Java will be tech debt
Clean Up Your Layouts
They're Probably Crufty
-
Move everything you can into styles, themes, dimension resources, etc.
- Those still have roles in Compose
- More tools/libraries to assist, such as Accompanist
- Objective: minimize what is in the layouts, since those get rewritten
Go Easy on Data Binding
Can't Make an Omelette...
- Data binding is tied to layout resources
- Layout resources are going away (mostly)
- Net: your binding expressions will need to get replaced
- Maybe don't change what you have, but perhaps don't add more to the pile
A Bit More Composin'
private data class AgreementViewState(
val terms: Boolean,
val privacy: Boolean
) {
fun canProceed() = terms && privacy
}
@Composable
fun Agreement() {
Column(
verticalArrangement = Arrangement.SpaceAround,
horizontalGravity = Alignment.CenterHorizontally
) {
val viewState = state { AgreementViewState(terms = false, privacy = false) }
Column(horizontalGravity = Alignment.Start) {
Row(horizontalArrangement = Arrangement.Start) {
Checkbox(
checked = viewState.value.terms,
onCheckedChange = { viewState.value = viewState.value.copy(terms = it) }
)
Spacer(modifier = Modifier.preferredWidth(4.dp))
Text(text = "I agree to the terms and conditions")
}
Spacer(modifier = Modifier.preferredHeight(8.dp))
Row(horizontalArrangement = Arrangement.Start) {
Checkbox(
checked = viewState.value.privacy,
onCheckedChange = { viewState.value = viewState.value.copy(privacy = it) }
)
Spacer(modifier = Modifier.preferredWidth(4.dp))
Text(text = "I agree to the privacy policy")
}
Spacer(modifier = Modifier.preferredHeight(8.dp))
Text(text = if (viewState.value.canProceed()) "You may proceed!" else "Please indicate your agreement")
}
}
}
Even More Composin'
(Insert Android Studio Here)
Clean Up Your Architectures
Yes, All of Them
-
Compose designed for unidirectional data flows
- State flows down
- Events flow up
- Widgets are not stateful
- May require some tweaking of your approaches (e.g., two-way data binding)
- Consistent architecture will simplify migration
Identify Tactical UI Improvements
Make Google Happy! Users Too!
- Landscape?
- Dark mode?
- Split-screen?
Ponder Moonshots
Crew Dragon Has Touchscreens, So...
- Things that are "game changers" that Compose might make easier to achieve
-
Example: Desktop via
ui-desktop
- Kotlin/Multiplatform
- Windows/macOS/Linux via the JVM
- Can you expand your market with modest re-engineering?
Budget Play Time
Skin Your Knees Early!
- By the time Compose reaches beta, try to start experimenting
- Partly, so you can learn and use that knowledge to better steer your plans
-
Partly, so you can tell us what you need to succeed
- Tell your favorite GDE
- Tell your favorite Compose library maintainer
- Tell
#compose
on Kotlinlang Slack (or DM me there)
Prepare Arguments for Management
Or Perhaps Just Gentle Conversations
- Why are we bothering with Compose? (recruiting)
- Will this interrupt our delivery schedules? (hopefully not, using progressive replacement)
- How does this help our users? (tactical UI changes plus moontshots)
- How does this help the CEO's golf game? (you're on your own for that one)
R-E-L-A-X
What? Wrong QB?
- Expect a significant hype train
- All the cool kids will be slinging Compose code
- You will get your turn too
- Do right by your users and your team, and don't Compose without a plan
Where To Get This Presentation
https://commonsware.com/presos/2020-06-Compose/