diff options
author | Robert Jaros <rjaros@finn.pl> | 2018-05-05 22:39:26 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2018-05-05 22:39:26 +0200 |
commit | 8067c5ae27822e019dde8bbba5763e9334a86fb0 (patch) | |
tree | 980f89fbaf6b474fd494c5d75a379b97be63bf60 /src/main/kotlin/pl | |
parent | fc1ee9bd5d09a6a932e62e8652a8dafed1515d09 (diff) | |
download | kvision-8067c5ae27822e019dde8bbba5763e9334a86fb0.tar.gz kvision-8067c5ae27822e019dde8bbba5763e9334a86fb0.tar.bz2 kvision-8067c5ae27822e019dde8bbba5763e9334a86fb0.zip |
Function for observable list synchronization
Diffstat (limited to 'src/main/kotlin/pl')
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/utils/Utils.kt | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/utils/Utils.kt b/src/main/kotlin/pl/treksoft/kvision/utils/Utils.kt index a1901c35..203fac98 100644 --- a/src/main/kotlin/pl/treksoft/kvision/utils/Utils.kt +++ b/src/main/kotlin/pl/treksoft/kvision/utils/Utils.kt @@ -23,6 +23,7 @@ package pl.treksoft.kvision.utils +import com.lightningkite.kotlin.observable.list.ObservableList import kotlinx.coroutines.experimental.suspendCancellableCoroutine import org.w3c.files.File import org.w3c.files.FileReader @@ -225,3 +226,21 @@ suspend fun File.getContent(): String = suspendCancellableCoroutine { cont -> } reader.readAsDataURL(this@getContent) } + +/** + * Utility extension function to synchronise elements of the ObservableList. + */ +fun <T> ObservableList<T>.syncWithList(list: List<T>) { + if (list.isEmpty()) { + this.clear() + } else { + for (pos in (this.size - 1) downTo list.size) this.removeAt(pos) + list.forEachIndexed { index, element -> + if (index < this.size) { + if (this[index] != element) this[index] = element + } else { + this.add(element) + } + } + } +} |