Skip to content

Commit

Permalink
Add a helper to create partial updates from RHMILists
Browse files Browse the repository at this point in the history
  • Loading branch information
hufman committed May 5, 2024
1 parent 91928f7 commit 5377c9c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,41 @@ abstract class RHMIModel protected constructor(val id: Int) {
abstract val endIndex: Int

/**
* Return the number of actual rows in this RHMList
* Return the number of actual rows in this RHMIList
*/
abstract val height: Int

fun getWindowList(startIndex: Int, numRows: Int): RHMIList {
return RHMIListPartial(this, startIndex, startIndex + numRows)
}

fun getWindow(startIndex: Int, numRows: Int): Array<Array<Any>> {
val lastIndex = min(startIndex + numRows, endIndex)
val actualNumRows = max(0, lastIndex - startIndex)
return Array(actualNumRows) { index -> this[startIndex + index] ?: emptyArray() }
}
}

/**
* An RHMIList that provides a smaller view into another RHMIList
* to create partial updates
*/
class RHMIListPartial(val base: RHMIList, override var startIndex: Int = 0, endIndex: Int = 0): RHMIList(base.width) {
override val width: Int = base.width

private val desiredEndIndex = endIndex
override val endIndex: Int
get() = min(desiredEndIndex, base.endIndex)
override val height: Int
get() = base.height

override fun get(index: Int): Array<Any>? = base[index]
}

/**
* An RHMIList that has a concrete storage, which can be added and set
* A starting index can optionally be set, to create partial updates without materializing the entire list
*/
class RHMIListConcrete(override var width: Int, override var startIndex: Int = 0, endIndex: Int = 0): RHMIList(width) {
private var realData = ArrayList<Array<Any>>()
override fun get(index: Int): Array<Any> {
Expand All @@ -84,7 +109,7 @@ abstract class RHMIModel protected constructor(val id: Int) {
override val endIndex: Int
get() = max(forcedEndIndex, startIndex + realData.size)
override val height: Int
get() = realData.size
get() = endIndex

fun clear() {
realData.clear()
Expand Down Expand Up @@ -112,6 +137,10 @@ abstract class RHMIModel protected constructor(val id: Int) {
realData[index - startIndex] = row
}
}

/**
* An RHMIList that wraps an existing List object, by default with toString() for each item
*/
open class RHMIListAdapter<T>(width: Int, val realData: List<T>) : RHMIModel.RaListModel.RHMIList(width) {
override val startIndex: Int = 0
override val endIndex: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class RHMIModelLive {
get() = _getValue()
set(value) {
if (value != null) {
setValue(value, value.startIndex, value.height, value.endIndex)
setValue(value, value.startIndex, value.endIndex - value.startIndex, value.height)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ class TestXMLParsing {

@Test fun listPartial() {
val list = RHMIModel.RaListModel.RHMIListConcrete(3, 10)
assertEquals(0, list.height)
assertEquals(10, list.height)
assertEquals(10, list.startIndex)
assertEquals(10, list.endIndex)
val atTheEnd = list.getWindow(10, 5)
Expand All @@ -677,7 +677,7 @@ class TestXMLParsing {

// add a row at the end
list.addRow(arrayOf(10))
assertEquals(1, list.height)
assertEquals(11, list.height)
assertEquals(10, list.startIndex)
assertEquals(11, list.endIndex)
val atTheEnd2 = list.getWindow(10, 5)
Expand All @@ -695,7 +695,7 @@ class TestXMLParsing {

// set a row in the middle
list[8] = arrayOf(8)
assertEquals(3, list.height) // (8, _, 10)
assertEquals(11, list.height) // (8, _, 10)
assertEquals(8, list.startIndex)
assertEquals(11, list.endIndex)

Expand All @@ -704,7 +704,7 @@ class TestXMLParsing {
assertArrayEquals(arrayOf(emptyArray<Any>(), emptyArray<Any>(), arrayOf(8),
emptyArray<Any>(), arrayOf(10)), atTheEnd3)
}
@Test fun listModelPartial() {
@Test fun listModelPartialConcrete() {
val model = RHMIModel.loadFromXML(app, models.getChildNamed("raListModel") as Node) as RHMIModel.RaListModel

val list = RHMIModel.RaListModel.RHMIListConcrete(3, 10, 15)
Expand All @@ -713,15 +713,60 @@ class TestXMLParsing {

val sentList = app.modelData[7] as BMWRemoting.RHMIDataTable
assertEquals(10, sentList.fromRow)
assertEquals(1, sentList.numRows)
assertEquals(5, sentList.numRows)
assertEquals(15, sentList.totalRows)
assertArrayEquals(arrayOf(arrayOf(1)), sentList.data)
assertArrayEquals(arrayOf(arrayOf(1), emptyArray(), emptyArray(), emptyArray(), emptyArray()), sentList.data)

val retrievedList = model.value as RHMIModel.RaListModel.RHMIList
assertEquals(10, retrievedList.startIndex)
assertEquals(1, retrievedList.height)
assertEquals(15, retrievedList.height)
assertEquals(15, retrievedList.endIndex)
}
@Test fun listModelPartialHelper() {
val model = RHMIModel.loadFromXML(app, models.getChildNamed("raListModel") as Node) as RHMIModel.RaListModel
val list = RHMIModel.RaListModel.RHMIListConcrete(3, 0)
(1 .. 15).forEach { list.addRow(arrayOf(it)) }
model.value = list

{
val sentList = app.modelData[7] as BMWRemoting.RHMIDataTable
assertEquals(0, sentList.fromRow)
assertEquals(15, sentList.numRows)
assertEquals(15, sentList.totalRows)
assertArrayEquals(arrayOf(1), sentList.data[0])
assertArrayEquals(arrayOf(15), sentList.data[14])
}

val partialList = list.getWindowList(10, 3)
assertEquals(10, partialList.startIndex)
assertEquals(13, partialList.endIndex)
assertEquals(15, partialList.height)

model.value = partialList
{
val sentPartialList = app.modelData[7] as BMWRemoting.RHMIDataTable
assertEquals(10, sentPartialList.fromRow)
assertEquals(3, sentPartialList.numRows)
assertEquals(15, sentPartialList.totalRows)
assertArrayEquals(arrayOf(11), sentPartialList.data[0])
assertArrayEquals(arrayOf(13), sentPartialList.data[2])
}

val overflowList = list.getWindowList(12, 8)
assertEquals(12, overflowList.startIndex)
assertEquals(15, overflowList.endIndex)
assertEquals(15, overflowList.height)

model.value = overflowList
{
val sentPartialList = app.modelData[7] as BMWRemoting.RHMIDataTable
assertEquals(12, sentPartialList.fromRow)
assertEquals(3, sentPartialList.numRows)
assertEquals(15, sentPartialList.totalRows)
assertArrayEquals(arrayOf(13), sentPartialList.data[0])
assertArrayEquals(arrayOf(15), sentPartialList.data[2])
}
}

@Test fun checkbox() {
val component = RHMIComponent.loadFromXML(app, components.getChildNamed("checkbox") as Node)
Expand Down

0 comments on commit 5377c9c

Please sign in to comment.