Starting
What is Preview? Itβs a functionality that gives your an opportunity for view a composable UI during development without the need to build and run on an emulator. (If you use the Android Studio obviously)
Problem
In fact you must have a some data that you wanna see in preview. But sometimes itβs impossible.
Imagine, you have a screen like this:
@Composable
fun SomeScreen(
viewModel: SomeViewModel = hiltViewModel(),
){
//some UI
}
And view model of that screen like this:
class SomeViewModel @Inject constructor(
private val someUseCase1: someUseCase1,
private val someUseCase2: someUseCase2
//etc
) : ViewModel() {
val someDataFLow1 = someUseCase1.getSomeData()
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
initia...
Starting
What is Preview? Itβs a functionality that gives your an opportunity for view a composable UI during development without the need to build and run on an emulator. (If you use the Android Studio obviously)
Problem
In fact you must have a some data that you wanna see in preview. But sometimes itβs impossible.
Imagine, you have a screen like this:
@Composable
fun SomeScreen(
viewModel: SomeViewModel = hiltViewModel(),
){
//some UI
}
And view model of that screen like this:
class SomeViewModel @Inject constructor(
private val someUseCase1: someUseCase1,
private val someUseCase2: someUseCase2
//etc
) : ViewModel() {
val someDataFLow1 = someUseCase1.getSomeData()
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = null
)
//etc
}
And thatβs all. How can you make a preview? - Directly, no how.
Solution
Use MockK library. Itβs an open-source and excellent library for testing. Okay, itβs for testing... But why canβt we use it for debugging and preview? - Of course, we can!
First of all we have to add dependencies
//Compose
implementation(platform("androidx.compose:compose-bom:<bom-version>"))
//other compose dependencies
//Preview
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-tooling-preview")
//MockK
debugImplementation("io.mockk:mockk:<mockk-version>")
Hereβs our project structure
βββ src/
βββ main/
β βββ kotlin/
β βββ yourpackage/
β βββ SomeScreen.kt
β βββ SomeViewModel.kt
β
βββ debug/
βββ kotlin/
βββ yourpackage/
βββ preview/
βββ SomeScreenPreview.kt
βββ MockEntities.kt
We have to add a mock ViewModel by MockK library and setup itβs behavior:
internal object MockEntities {
private val mockData = listOf(
SomeDataItem(id = 1),
SomeDataItem(id = 2),
SomeDataItem(id = 3),
SomeDataItem(id = 4),
)
private val mockDataFlow1 = MutableStateFlow(mockData)
val someViewModel = mockk<SomeViewModel>().apply {
every { someDataFLow1 } returns mockDataFlow1
}
}
And a final function is a Preview itself:
@Composable
@Preview
private fun SomeScreenPreview() {
YourTheme {
SomeScreen(
viewModel = MockEntities.someViewModel
)
}
}
So, that was a little trick. I hope it helps you in your work :)