From 0c8cf5c2bcf32ad72618620c1b49a6aef75070c3 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sat, 6 Jul 2019 13:30:34 +0200 Subject: New implementation of ObservableList --- .../pl/treksoft/kvision/utils/ObservableList.kt | 206 +++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 src/main/kotlin/pl/treksoft/kvision/utils/ObservableList.kt (limited to 'src/main/kotlin/pl') diff --git a/src/main/kotlin/pl/treksoft/kvision/utils/ObservableList.kt b/src/main/kotlin/pl/treksoft/kvision/utils/ObservableList.kt new file mode 100644 index 00000000..193cd575 --- /dev/null +++ b/src/main/kotlin/pl/treksoft/kvision/utils/ObservableList.kt @@ -0,0 +1,206 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package pl.treksoft.kvision.utils + +/** + * Observable list interface. + */ +interface ObservableList : MutableList { + val onUpdate: MutableCollection<(MutableList) -> Unit> +} + +/** + * Simple observable list implementation. + */ +class ObservableListWrapper(val mutableList: MutableList = mutableListOf()) : MutableList, ObservableList { + + override val onUpdate: MutableCollection<(MutableList) -> Unit> = mutableListOf() + + override val size: Int + get() = mutableList.size + + override fun contains(element: T): Boolean { + return mutableList.contains(element) + } + + override fun containsAll(elements: Collection): Boolean { + return mutableList.containsAll(elements) + } + + override fun get(index: Int): T { + return mutableList[index] + } + + override fun indexOf(element: T): Int { + return mutableList.indexOf(element) + } + + override fun isEmpty(): Boolean { + return mutableList.isEmpty() + } + + override fun iterator(): MutableIterator = object : MutableIterator { + val inner = mutableList.iterator() + override fun hasNext(): Boolean = inner.hasNext() + override fun next(): T = inner.next() + override fun remove() { + inner.remove() + onUpdate.forEach { it(this@ObservableListWrapper) } + } + } + + override fun lastIndexOf(element: T): Int { + return mutableList.lastIndexOf(element) + } + + override fun listIterator(): MutableListIterator = object : MutableListIterator { + val inner = mutableList.listIterator() + + override fun hasNext() = inner.hasNext() + override fun next() = inner.next() + override fun hasPrevious() = inner.hasPrevious() + override fun nextIndex() = inner.nextIndex() + override fun previous() = inner.previous() + override fun previousIndex() = inner.previousIndex() + + override fun add(element: T) { + inner.add(element) + onUpdate.forEach { it(this@ObservableListWrapper) } + } + + override fun set(element: T) { + inner.set(element) + onUpdate.forEach { it(this@ObservableListWrapper) } + } + + override fun remove() { + inner.remove() + onUpdate.forEach { it(this@ObservableListWrapper) } + } + } + + override fun listIterator(index: Int): MutableListIterator = object : MutableListIterator { + val inner = mutableList.listIterator(index) + + override fun hasNext() = inner.hasNext() + override fun next() = inner.next() + override fun hasPrevious() = inner.hasPrevious() + override fun nextIndex() = inner.nextIndex() + override fun previous() = inner.previous() + override fun previousIndex() = inner.previousIndex() + + override fun add(element: T) { + inner.add(element) + onUpdate.forEach { it(this@ObservableListWrapper) } + } + + override fun set(element: T) { + inner.set(element) + onUpdate.forEach { it(this@ObservableListWrapper) } + } + + override fun remove() { + inner.remove() + onUpdate.forEach { it(this@ObservableListWrapper) } + } + } + + override fun subList(fromIndex: Int, toIndex: Int): MutableList { + return mutableList.subList(fromIndex, toIndex) + } + + override fun add(element: T): Boolean { + val result = mutableList.add(element) + if (result) { + onUpdate.forEach { it(this) } + } + return result + } + + override fun add(index: Int, element: T) { + mutableList.add(index, element) + onUpdate.forEach { it(this) } + } + + override fun addAll(elements: Collection): Boolean { + val result = mutableList.addAll(elements) + if (result) { + onUpdate.forEach { it(this) } + } + return result + } + + override fun addAll(index: Int, elements: Collection): Boolean { + val result = mutableList.addAll(index, elements) + if (result) { + onUpdate.forEach { it(this) } + } + return result + } + + override fun clear() { + mutableList.clear() + onUpdate.forEach { it(this) } + } + + override fun remove(element: T): Boolean { + val result = mutableList.remove(element) + if (result) { + onUpdate.forEach { it(this) } + } + return result + } + + override fun removeAt(index: Int): T { + val result = mutableList.removeAt(index) + onUpdate.forEach { it(this) } + return result + } + + override fun removeAll(elements: Collection): Boolean { + val result = mutableList.removeAll(elements) + if (result) { + onUpdate.forEach { it(this) } + } + return result + } + + override fun retainAll(elements: Collection): Boolean { + val result = mutableList.retainAll(elements) + if (result) { + onUpdate.forEach { it(this) } + } + return result + } + + override fun set(index: Int, element: T): T { + val result = mutableList.set(index, element) + onUpdate.forEach { it(this) } + return result + } +} + +/** + * Creates an instance of ObservableList + */ +fun observableListOf(vararg items: T) = ObservableListWrapper(items.toMutableList()) -- cgit