Viewmodel에 대한 preview를 compose는 제공하지 않는다.
따라서 interface나 abstract class를 사용하여 fake Viewmodel을 구성해주어야한다.
사용예제는 아래와 같다
1. Viewmodel 구성
interface UserViewModelInterface {
var userFid : String
var userBasicData : MutableLiveData<UserPublic>
}
object FakeUserViewModel : UserViewModelInterface {
override var userFid: String = "fakeFid"
override var userBasicData: MutableLiveData<UserPublic> = MutableLiveData<UserPublic>()
}
@HiltViewModel
class UserViewModel @Inject constructor() : ViewModel(), UserViewModelInterface {
override var userFid = ""
override var userBasicData = MutableLiveData<UserPublic>()
2. Compose 구현 예
@OptIn(ExperimentalCoroutinesApi::class)
@Preview(showBackground = true)
@Composable
fun WritingSketchPreview(modifier: Modifier = Modifier) {
Writing(userViewModel = FakeUserViewModel)
}
@OptIn(ExperimentalCoroutinesApi::class)
@Composable
fun Writing(modifier: Modifier = Modifier, userViewModel : UserViewModelInterface) {
'Android Dev > Compose' 카테고리의 다른 글
Compose Dialog resize 안되는 문제 (0) | 2022.06.23 |
---|---|
Compose - Text outlined color 설정 (0) | 2022.06.05 |
Dagger Hilt Testing Error - error: cannot find symbol @ScopeMetadata, @QualifierMetadata (0) | 2022.03.10 |
jcenter() deprecated에 따른 repository파일 부재(bad gate way) 문제 해결(ex : ucrop) (0) | 2022.03.04 |
Classes trial, variable initializing (0) | 2022.02.28 |