Android Weekly Notes #467

Android Weekly Issue #467

Jetpack Compose: Styles and Themes (Part II)

Puppy App的theme设置.

改状态栏, 在theme里:

@color/grey
?attr/statusBarBackground
?android:attr/windowBackground
view raw

EXPLORING THE MATERIAL NAVIGATION RAIL

竖直放置的navigation bar.


Migrating from LiveData to Kotlin’s Flow

从LiveData迁移到Flow, 几种情况的代码举例.

还讨论了StateFlow的stateIn和收集操作符.

StateFlow使用总结
从ViewModel暴露数据到UI, 用StateFlow的两种方式:

  1. 暴露一个StateFlow属性, 用WhileSubscribed加上一个timeout.
class MyViewModel(...) : ViewModel() {
    val result = userId.mapLatest { newUserId ->
        repository.observeItem(newUserId)
    }.stateIn(
        scope = viewModelScope, 
        started = WhileSubscribed(5000), 
        initialValue = Result.Loading
    )
}
  1. repeatOnLifecycle收集.
onCreateView(...) {
    viewLifecycleOwner.lifecycleScope.launch {
        viewLifecycleOwner.lifecycle.repeatOnLifecycle(STARTED) {
            myViewModel.myUiState.collect { ... }
        }
    }
}

其他的组合都会保持上游的活跃, 浪费资源.

  • WhileSubscribed暴露属性, 在lifecycleScope.launch/launchWhenX里收集.
  • 通过Lazily/Eagerly暴露, 用repeatOnLifecycle收集.

推荐阅读.

Gradle: Version Catalogs

写一个依赖的文件.
然后用法类似于这样:

 implementation(libs.androidx.core.ktx)

Code

  • https://github.com/google/iosched
  • https://github.com/MackHartley/RoundedProgressBar
  • https://github.com/AzimoLabs/ConditionWatcher
  • https://github.com/skydoves/chamber 可以用来在不同的Scope之间通信.
  • https://github.com/drewhamilton/Poko

你可能感兴趣的:(Android Weekly Notes #467)