-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #232
- Loading branch information
Showing
5 changed files
with
106 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ android { | |
lintOptions { | ||
warningsAsErrors true | ||
abortOnError true | ||
disable "GradleDependency" | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
BrickKit/bricks/src/androidTest/java/com/wayfair/brickkit/brick/ViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
91 changes: 0 additions & 91 deletions
91
BrickKit/bricks/src/main/java/com/wayfair/brickkit/brick/ViewModel.java
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
BrickKit/bricks/src/main/java/com/wayfair/brickkit/brick/ViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |