From 5d1f5168ade697d5203480825fd8b190b756fbdc Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Sun, 31 Mar 2019 16:54:43 -0500 Subject: [PATCH] Create Observable class --- .../isoron/platform/concurrency/Observable.kt | 36 ++++++++++++++ .../uhabits/backend/MainScreenDataSource.kt | 12 +---- .../org/isoron/platform/io/StringsTest.kt | 4 +- .../platform/concurrency/ObservableTest.kt | 48 +++++++++++++++++++ .../org/isoron/uhabits/backend/BackendTest.kt | 2 +- 5 files changed, 88 insertions(+), 14 deletions(-) create mode 100644 core/src/commonMain/kotlin/org/isoron/platform/concurrency/Observable.kt create mode 100644 core/src/jvmTest/kotlin/org/isoron/platform/concurrency/ObservableTest.kt diff --git a/core/src/commonMain/kotlin/org/isoron/platform/concurrency/Observable.kt b/core/src/commonMain/kotlin/org/isoron/platform/concurrency/Observable.kt new file mode 100644 index 000000000..9aa11a5e6 --- /dev/null +++ b/core/src/commonMain/kotlin/org/isoron/platform/concurrency/Observable.kt @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2016-2019 Álinson Santos Xavier + * + * 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 . + */ + +package org.isoron.platform.concurrency + +class Observable { + private val listeners = mutableListOf() + + 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) + } +} \ No newline at end of file diff --git a/core/src/commonMain/kotlin/org/isoron/uhabits/backend/MainScreenDataSource.kt b/core/src/commonMain/kotlin/org/isoron/uhabits/backend/MainScreenDataSource.kt index 4f03d11a5..043806bb7 100644 --- a/core/src/commonMain/kotlin/org/isoron/uhabits/backend/MainScreenDataSource.kt +++ b/core/src/commonMain/kotlin/org/isoron/uhabits/backend/MainScreenDataSource.kt @@ -36,15 +36,7 @@ class MainScreenDataSource(val habits: MutableMap, val colors: List, val checkmarks: List>) - private val listeners = mutableListOf() - - fun addListener(listener: Listener) { - listeners.add(listener) - } - - fun removeListener(listener: Listener) { - listeners.remove(listener) - } + val observable = Observable() interface Listener { fun onDataChanged(newData: Data) @@ -64,7 +56,7 @@ class MainScreenDataSource(val habits: MutableMap, } val data = Data(ids, scores, names, colors, ck) taskRunner.runInForeground { - listeners.forEach { listener -> + observable.notifyListeners { listener -> listener.onDataChanged(data) } } diff --git a/core/src/commonTest/kotlin/org/isoron/platform/io/StringsTest.kt b/core/src/commonTest/kotlin/org/isoron/platform/io/StringsTest.kt index 1191452e7..3ae79f7fe 100644 --- a/core/src/commonTest/kotlin/org/isoron/platform/io/StringsTest.kt +++ b/core/src/commonTest/kotlin/org/isoron/platform/io/StringsTest.kt @@ -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 diff --git a/core/src/jvmTest/kotlin/org/isoron/platform/concurrency/ObservableTest.kt b/core/src/jvmTest/kotlin/org/isoron/platform/concurrency/ObservableTest.kt new file mode 100644 index 000000000..7c6333040 --- /dev/null +++ b/core/src/jvmTest/kotlin/org/isoron/platform/concurrency/ObservableTest.kt @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2016-2019 Álinson Santos Xavier + * + * 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 . + */ + +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() + observable.addListener(listener) + observable.notifyListeners { l -> + l.onDataChanged(42) + } + observable.removeListener(listener) + assertTrue(latch.await(3, TimeUnit.SECONDS)) + } +} \ No newline at end of file diff --git a/core/src/jvmTest/kotlin/org/isoron/uhabits/backend/BackendTest.kt b/core/src/jvmTest/kotlin/org/isoron/uhabits/backend/BackendTest.kt index 3b9a4ff07..85d16cc3f 100644 --- a/core/src/jvmTest/kotlin/org/isoron/uhabits/backend/BackendTest.kt +++ b/core/src/jvmTest/kotlin/org/isoron/uhabits/backend/BackendTest.kt @@ -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)) }