mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Create Observable class
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.platform.concurrency
|
||||
|
||||
class Observable<T> {
|
||||
private val listeners = mutableListOf<T>()
|
||||
|
||||
fun addListener(listener: T) {
|
||||
listeners.add(listener)
|
||||
}
|
||||
|
||||
fun notifyListeners(action: (T) -> Unit) {
|
||||
for (l in listeners) action.invoke(l)
|
||||
}
|
||||
|
||||
fun removeListener(listener: T) {
|
||||
listeners.remove(listener)
|
||||
}
|
||||
}
|
||||
@@ -36,15 +36,7 @@ class MainScreenDataSource(val habits: MutableMap<Int, Habit>,
|
||||
val colors: List<PaletteColor>,
|
||||
val checkmarks: List<List<Int>>)
|
||||
|
||||
private val listeners = mutableListOf<Listener>()
|
||||
|
||||
fun addListener(listener: Listener) {
|
||||
listeners.add(listener)
|
||||
}
|
||||
|
||||
fun removeListener(listener: Listener) {
|
||||
listeners.remove(listener)
|
||||
}
|
||||
val observable = Observable<Listener>()
|
||||
|
||||
interface Listener {
|
||||
fun onDataChanged(newData: Data)
|
||||
@@ -64,7 +56,7 @@ class MainScreenDataSource(val habits: MutableMap<Int, Habit>,
|
||||
}
|
||||
val data = Data(ids, scores, names, colors, ck)
|
||||
taskRunner.runInForeground {
|
||||
listeners.forEach { listener ->
|
||||
observable.notifyListeners { listener ->
|
||||
listener.onDataChanged(data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,9 +19,7 @@
|
||||
|
||||
package org.isoron.platform.io
|
||||
|
||||
import org.isoron.platform.io.*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.*
|
||||
|
||||
class StringsTest {
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.platform.concurrency
|
||||
|
||||
import java.util.concurrent.*
|
||||
import kotlin.test.*
|
||||
|
||||
class ObservableTest {
|
||||
|
||||
interface TestListener {
|
||||
fun onDataChanged(data: Int)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test() {
|
||||
val latch = CountDownLatch(1)
|
||||
val listener = object : TestListener {
|
||||
override fun onDataChanged(data: Int) {
|
||||
assertEquals(42, data)
|
||||
latch.countDown()
|
||||
}
|
||||
}
|
||||
val observable = Observable<TestListener>()
|
||||
observable.addListener(listener)
|
||||
observable.notifyListeners { l ->
|
||||
l.onDataChanged(42)
|
||||
}
|
||||
observable.removeListener(listener)
|
||||
assertTrue(latch.await(3, TimeUnit.SECONDS))
|
||||
}
|
||||
}
|
||||
@@ -82,7 +82,7 @@ class BackendTest : BaseTest() {
|
||||
latch.countDown()
|
||||
}
|
||||
}
|
||||
backend.mainScreenDataSource.addListener(listener)
|
||||
backend.mainScreenDataSource.observable.addListener(listener)
|
||||
backend.mainScreenDataSource.requestData()
|
||||
assertTrue(latch.await(3, TimeUnit.SECONDS))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user