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来把数据序列化成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强相关.
这篇文章提供的解决方案和依赖注入相关.
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.
优势: ui元素和controller独立, ui元素可以复用.
How runBlocking May Surprise You
关于协程和主线程的一些有趣的讨论, 图不错.
What’s new in Hilt and Dagger 2.31
Hilt的新特性:
- HiltAndroidApp.
-
ApplicationComponent
改名为SingleComponent
. - 引入
ViewModelComponent
和ViewModelScoped
. - Assisted Injection: 一些依赖由依赖注入框架提供, 另一些创建时提供.
- Hilt和Navigation以及Compose结合.
- 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
函数式编程系列文章的第三篇.
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
网友评论