美文网首页Android Weekly Notes
Android Weekly Notes #476

Android Weekly Notes #476

作者: 圣骑士wind | 来源:发表于2021-08-31 00:08 被阅读0次

    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

    相关文章

      网友评论

        本文标题:Android Weekly Notes #476

        本文链接:https://www.haomeiwen.com/subject/yluailtx.html