Skip to content

Commit

Permalink
Convert ViewModel to kotlin (#233)
Browse files Browse the repository at this point in the history
Fixes #232
  • Loading branch information
thusson13 authored Feb 4, 2021
1 parent add1216 commit 21923c3
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 92 deletions.
1 change: 1 addition & 0 deletions BrickKit/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ android {
lintOptions {
warningsAsErrors true
abortOnError true
disable "GradleDependency"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,13 @@ class ViewModelBrickTest {
.build()

val countDownLatch = CountDownLatch(1)
viewModel.addUpdateListener { countDownLatch.countDown() }
viewModel.addUpdateListener(
object : ViewModel.ViewModelUpdateListener {
override fun onChange() {
countDownLatch.countDown()
}
}
)

assertFalse(viewModelBrick.isHidden)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.wayfair.brickkit.brick

import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test

class ViewModelTest {
private val dataModel: DataModel = mock()
private lateinit var viewModel: ViewModel<DataModel>

@Before
fun setup() {
viewModel = object : ViewModel<DataModel>(dataModel) { }

verify(dataModel).addUpdateListener(viewModel)
}

@Test
fun testNotifyChange() {
val updateListener = mock<ViewModel.ViewModelUpdateListener>()

viewModel.addUpdateListener(updateListener)

viewModel.notifyChange()

verify(updateListener).onChange()
}

@Test
fun testIsDataModelReady_dataModelReady() {
whenever(dataModel.isReady).thenReturn(true)

assertTrue(viewModel.isDataModelReady)
}

@Test
fun testIsDataModelReady_dataModelNotReady() {
whenever(dataModel.isReady).thenReturn(false)

assertFalse(viewModel.isDataModelReady)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.wayfair.brickkit.brick

import androidx.databinding.BaseObservable
import com.wayfair.brickkit.brick.DataModel.DataModelUpdateListener
import java.io.Serializable

/**
* The object used to bind information in a [ViewModelBrick].
*/
abstract class ViewModel<DM : DataModel> protected constructor(
open val dataModel: DM
) : BaseObservable(), DataModelUpdateListener, Serializable {

@Transient
private var updateListeners = mutableSetOf<ViewModelUpdateListener>()

init {
dataModel.addUpdateListener(this)
}

/**
* Add an [ViewModelUpdateListener] to the list of items watching for changes.
*
* @param updateListener the object that is watching
*/
open fun addUpdateListener(updateListener: ViewModelUpdateListener) {
updateListeners.add(updateListener)
}

override fun notifyChange() {
super.notifyChange()
updateListeners.forEach { updateListener -> updateListener.onChange() }
}

/**
* Determines if the [DataModel] is ready, meaning the ViewModel is ready.
*
* @return if the data model is ready, IE fully populated
*/
open val isDataModelReady: Boolean
get() = dataModel.isReady

/**
* Interface for listening to changes.
*/
interface ViewModelUpdateListener {
/**
* Called when notify change is called.
*/
fun onChange()
}
}

0 comments on commit 21923c3

Please sign in to comment.