Android Weekly Issue #476
Tricky refactoring of Jetpack Compose code — be careful with side effects
輸入框和snackbar的一個例子.
抽了一個方法之後, 結果就不對了.
最後最好的解決方案是這樣:
@Composable
fun LaunchedEffectWrapper(state: SnackbarHostState, text: String) {
val mostRecentText = rememberUpdatedState(text) // Wrap text
LaunchedEffect(Unit) {
...
// Use mostRecentText instead of the text
}
}
其中rememberUpdatedState
的實現是:
val state = remember { mutableStateOf(newValue) }
state.value = newState
Guidelines for writing better tests
- 可讀性: 可以適當重複.
- Arrange-Act-Assert.
- Test a single behavior.
- 外部依賴.
Optimizing Your Kotlin Build
優化Kotlin build.
Beyond preferences
老的preferences api已經過時了, 現在我們要切換到Jetpack Datastore.
Using Exoplayer in LazyColumn
在一個List裏播放Video.
技術方案: ExoPlayer
+ Compose
.
其中針對生命週期的清理部分是這樣做的:
val lifecycleOwner by rememberUpdatedState(LocalLifecycleOwner.current)
DisposableEffect(lifecycleOwner) {
val lifecycle = lifecycleOwner.lifecycle
val observer = LifecycleEventObserver { _, event ->
when (event) {
Lifecycle.Event.ON_PAUSE -> {
exoPlayer.playWhenReady = false
}
Lifecycle.Event.ON_RESUME -> {
exoPlayer.playWhenReady = true
}
Lifecycle.Event.ON_DESTROY -> {
exoPlayer.run {
stop()
release()
}
}
}
}
lifecycle.addObserver(observer)
onDispose {
lifecycle.removeObserver(observer)
}
}
How much QA is too much QA?
這裏頭有個插圖網站好搞笑: https://www.monkeyuser.com/2018/the-struggle/
Lifecycle-Aware Components Using Android Jetpack
用的是這個api: https://spoonacular.com/food-api/console#Dashboard
得註冊, 有apiKey.
Lifecycle Observer
要觀察Lifecycle, 實現接口LifecycleObserver
:
class NetworkMonitor @Inject constructor(private val context: Context) : LifecycleObserver {
// Code to observe changes in the network connection.
}
Lifecycle
類有兩個枚舉:
- Event: 對應生命週期事件.
- State: Lifecycle owner現在的狀態.
Lifecycle Owner
誰是LifecycleOwner? -> 實現了LifecycleOwner
的類.
Android提供了: ComponentActivity
, Fragment
.
如果要響應應用的生命週期變化, 用ProcessLifecycleOwner
.
創建自定義的Lifecycle Owner
@Singleton
class UnavailableConnectionLifecycleOwner @Inject constructor() : LifecycleOwner {
private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
override fun getLifecycle() = lifecycleRegistry
fun onConnectionLost() {
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
}
fun onConnectionAvailable() {
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
}
fun addObserver(lifecycleObserver: LifecycleObserver) {
lifecycleRegistry.addObserver(lifecycleObserver)
}
}
後面還有關於lifecycle的測試以及LiveData的例子.
Create Bitmaps From Jetpack Composables
創建一個bitmap.
So, how do I write a Kotlin Symbol Processor (KSP)?
KSP的sample: https://github.com/Morfly/ksp-sample
有很詳細的代碼生成例子.
Updating your widget for Android 12
Android 12對Widgets API的更新.
Material You
Focus in Jetpack Compose
Compose中的Focus.
Inside Code Transparency: The Verification Process
Code transparency是用App Bundle發佈時可選的一種簽名和驗證機制.
作者之前還有一篇文章: https://commonsware.com/blog/2021/07/11/inside-code-transparency-jwt-file.html
Building an Enterprise IntelliJ Plugin for Android Developers
如何編寫Intellij插件.
Destructuring Declarations in Kotlin
解構以及使用場景.
Code
- https://github.com/akshay2211/PixImagePicker 圖片選擇器.
- https://github.com/adrielcafe/voyager Compose的輕量導航庫.
- https://github.com/CaelumF/FigmaToCompose Figma轉Compose.