Android Weekly Issue #450
Using Hilt’s ViewModelComponent
在ViewModelComponent
被引入之前, ViewModel的依賴scope只能是: 沒有, singleton, activity這三種情況.
在類型上使用注解的時候:
- 如果所有的ViewModels共享實例, 用
@ActivityRetainedScoped
. - 如果和ViewModel保持一致, 能夠configuration change的時候survive, 或者被navigation graph控制, 用
@ViewModelScoped
. - 如果scope和Activity/Fragment一致, configuration change的時候不survive, 用
@ActivityScoped
或者@FragmentScoped
.
在這個例子中:
@ViewModelScoped // Scopes type to the ViewModel
class UserInputAuthData(
private val handle: SavedStateHandle // Default binding in ViewModelComponent
) { /* Cached data and logic here */ }
class RegistrationViewModel(
private val userInputAuthData: UserInputAuthData,
private val validateUsernameUseCase: ValidateUsernameUseCase,
private val validatePasswordUseCase: ValidatePasswordUseCase
) : ViewModel() { /* ... */ }
class LoginViewModel(
private val userInputAuthData: UserInputAuthData,
private val validateUsernameUseCase: ValidateUsernameUseCase,
private val validatePasswordUseCase: ValidatePasswordUseCase
) : ViewModel() { /* ... */ }
class ValidateUsernameUseCase(
private val userInputAuthData: UserInputAuthData,
private val repository: UserRepository
) { /* ... */ }
class ValidatePasswordUseCase(
private val userInputAuthData: UserInputAuthData,
private val repository: UserRepository
) { /* ... */ }
- 每個ViewModel擁有自己的
UserInputAuthData
實例. - 每個ViewModel的UseCase實例依賴的
UserInputAuthData
(間接依賴), 是各自的ViewModel中的那個實例.
Android Parcelable: There's a better way
介紹這個庫: https://github.com/chRyNaN/parcelable
依賴了kotlin serialization來把數(shù)據(jù)序列化成Parcels.
Why we use Kotlin Multiplatform and Redux
為什么使用了Kotlin Multiplatform和Redux來做一個小app.
文章里的圖看起來挺好的(就是沒仔細看).
How do we handle multi-modules navigation on our Android app
多module應用的導航.
官方的navigation也支持多module:
https://developer.android.com/guide/navigation/navigation-dynamic?authuser=1
但是和Fragment強相關(guān).
這篇文章提供的解決方案和依賴注入相關(guān).
demo在這里: https://github.com/PhilippeBoisney/android-multi-modules-navigation-demo
Ktor and GraphQL
如何用Ktor寫一個GraphQL的server.
Helping You Understand The Syntax of Jetpack Compose
幫你讀懂Jetpack Compose的語法.
Epoxy—Build Declarative & Reusable UI Components
- 每一個View都是一個
EpoxyModel
. -
EpoxyController
用來連接models, 類似于Adapter.
優(yōu)勢: ui元素和controller獨立, ui元素可以復用.
How runBlocking May Surprise You
關(guān)于協(xié)程和主線程的一些有趣的討論, 圖不錯.
What’s new in Hilt and Dagger 2.31
Hilt的新特性:
- HiltAndroidApp.
-
ApplicationComponent
改名為SingleComponent
. - 引入
ViewModelComponent
和ViewModelScoped
. - Assisted Injection: 一些依賴由依賴注入框架提供, 另一些創(chuàng)建時提供.
- Hilt和Navigation以及Compose結(jié)合.
- HiltWorker.
Sealed goodies coming in Kotlin 1.5
Kotlin1.5推出了sealed interface.
Language Injections in Android Studio / IntelliJ IDEA
同一個文件用不同的語言.
比如Room中的sql語句.
Advanced FP for the Enterprise Bee: Higher Kinded Types
函數(shù)式編程系列文章的第三篇.
Code
- https://github.com/CRED-CLUB/synth-android 這個樣式庫的效果還挺好看的.
- https://github.com/chRyNaN/serialization-parcelable Android Parcelable support for the Kotlinx Serialization library.
- https://github.com/dreipol/multiplatform-redux-sample