Skip to content

Instantly share code, notes, and snippets.

View Raiden18's full-sized avatar
🎯
Focusing

Paul Karpukhin Raiden18

🎯
Focusing
View GitHub Profile
fun assertThat(someViewModel: SomeViewModel): ViewModelStateVerifier { // inspired by Hamcrest
return ViewModelStateVerifier(someViewModel)
}
class ViewModelStateVerifier(private val someViewModel: SomeViewModel) {
fun loaderIsShown(): ViewModelStateVerifier {
assertTrue(someViewModel.getLoaderState().value)
return this
}
@Raiden18
Raiden18 / SomeViewModelTest.kt
Last active July 21, 2022 19:10
SomeViewModelTest for states
class SomeViewModelTest {
private lateinit var someViewModel: SomeViewModel
private val someInteractor: SomeInteractor = mockk(relaxed = true)
private val dataToLoad: String = "Data to load"
@Before
fun setUp() {
clearAllMocks()
}
@Raiden18
Raiden18 / SomeViewModel.kt
Last active July 21, 2022 19:06
SomeViewModel example
sealed class ContentState {
object Hidden : ContentState()
data class Shown(val data: String) : ContentState()
}
class SomeViewModel(
private val someInteractor: SomeInteractor,
private val dispatchersProvider: DispatchersProvider
) : ViewModel() {
sealed class Icon {
data class Resources(val name: String)
data class Url(val url: String)
}
data class IconResponse(
@SerializedName("type")
val type: IconTypeEnum,
@SerializedName("name")
val name: String?,
@SerializedName("url")
val url: String?
)
// Icon from android resource
{
"icon": {
type: "resources"
"name": "resources_name"
}
}
// Icon from url
{
data class YourUiModel(
val title: String,
val description: String,
val visibility: Int // View.GONE, etc
)
class YourViewHolder(
view: View
): RecyclerView.ViewHolder(view) {
//...
data class YourModelResponse(
@SerializedName("field1")
val field1: String,
@SerializedName("field2")
val field2: String
)
data class YourDomainDto(
val field1: String,
val field2: String
)
sealed class ButtonSize() {
object Small : ButtonSize()
object Medium : ButtonSize()
object Large : ButtonSize()
object Huge : ButtonSize()
data class Custom(
val heightDpInt: Int,
val color: Color,
val textResId: Int
) : ButtonSize()