diff --git a/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/FxScheduler.kt b/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/FxScheduler.kt new file mode 100644 index 00000000..5dd0b8c6 --- /dev/null +++ b/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/FxScheduler.kt @@ -0,0 +1,6 @@ +package me.mnlr.vripper.view + +import javafx.application.Platform +import reactor.core.scheduler.Schedulers + +val FxScheduler = Schedulers.fromExecutor(Platform::runLater) \ No newline at end of file diff --git a/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/ImagesTableView.kt b/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/ImagesTableView.kt index bd1002b2..cebf913d 100644 --- a/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/ImagesTableView.kt +++ b/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/ImagesTableView.kt @@ -9,6 +9,7 @@ import me.mnlr.vripper.controller.ImageController import me.mnlr.vripper.event.Event import me.mnlr.vripper.event.EventBus import me.mnlr.vripper.model.ImageModel +import me.mnlr.vripper.view.FxScheduler import tornadofx.* class ImagesTableView : Fragment("Photos") { @@ -27,31 +28,31 @@ class ImagesTableView : Fragment("Photos") { runLater { items.addAll(imageController.findImages(postId)) tableView.sort() + } - val disposable = eventBus.flux() - .filter { it!!.kind == Event.Kind.IMAGE_UPDATE } - .subscribe { event -> - imageController - .findImageById(event!!.data as Long) - .filter { it.postId == postId } - .ifPresent { - // search - val find = items - .find { image -> image.id == it.id } - if (find != null) { - find.apply { - progress = it.progress - status = it.status - } - } else { - items.add(it) - } - } + val disposable = eventBus + .flux() + .filter { it!!.kind == Event.Kind.IMAGE_UPDATE } + .map { imageController.findImageById(it!!.data as Long) } + .filter { it.isPresent } + .map { it.get() } + .filter { it.postId == postId } + .publishOn(FxScheduler) + .subscribe { + val find = items + .find { image -> image.id == it.id } + if (find != null) { + find.apply { + progress = it.progress + status = it.status + } + } else { + items.add(it) } - - whenUndocked { - disposable.dispose() } + + whenUndocked { + disposable.dispose() } } diff --git a/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/LogTableView.kt b/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/LogTableView.kt index b9212ad3..ad14ad68 100644 --- a/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/LogTableView.kt +++ b/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/LogTableView.kt @@ -8,6 +8,7 @@ import me.mnlr.vripper.controller.LogController import me.mnlr.vripper.event.Event import me.mnlr.vripper.event.EventBus import me.mnlr.vripper.model.LogModel +import me.mnlr.vripper.view.FxScheduler import tornadofx.* class LogTableView : View() { @@ -29,28 +30,33 @@ class LogTableView : View() { }) items.addAll(logController.findAll()) - eventBus.flux() - .doOnNext { event -> - if (event!!.kind.equals(Event.Kind.LOG_EVENT_UPDATE)) { - logController.find(event.data as Long) - .ifPresent { - // search - val find = items - .find { threadModel -> threadModel.id == it.id } - if (find != null) { - find.apply { - status = it.status - message = it.message - } - } else { - items.add(it) - } - } - } else if (event.kind.equals(Event.Kind.LOG_EVENT_REMOVE)) { - items.removeIf { it.id == event.data } + eventBus + .flux() + .filter { it!!.kind == Event.Kind.LOG_EVENT_UPDATE } + .map { logController.find(it.data as Long) } + .filter { it.isPresent } + .map { it.get() } + .publishOn(FxScheduler) + .subscribe { + val find = items + .find { threadModel -> threadModel.id == it.id } + if (find != null) { + find.apply { + status = it.status + message = it.message + } + } else { + items.add(it) } } - .subscribe() + + eventBus + .flux() + .filter { it.kind == Event.Kind.LOG_EVENT_REMOVE } + .publishOn(FxScheduler) + .subscribe { event -> + items.removeIf { it.id == event.data } + } } override fun onDock() { diff --git a/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/PostsTableView.kt b/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/PostsTableView.kt index c6d28f9c..722e78ba 100644 --- a/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/PostsTableView.kt +++ b/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/PostsTableView.kt @@ -11,6 +11,7 @@ import me.mnlr.vripper.event.Event import me.mnlr.vripper.event.EventBus import me.mnlr.vripper.model.PostModel import me.mnlr.vripper.utils.Shell32 +import me.mnlr.vripper.view.FxScheduler import tornadofx.* @@ -32,11 +33,14 @@ class PostsTableView : View() { }) items.addAll(postController.findAllPosts()) - eventBus.flux().filter { - it!!.kind == Event.Kind.POST_UPDATE || it.kind == Event.Kind.METADATA_UPDATE - }.subscribe { event -> - postController.findById(event!!.data as Long).ifPresent { - // search + eventBus + .flux() + .filter { it!!.kind == Event.Kind.POST_UPDATE || it.kind == Event.Kind.METADATA_UPDATE } + .map { postController.findById(it!!.data as Long) } + .filter { it.isPresent } + .map { it.get() } + .publishOn(FxScheduler) + .subscribe { val find = items.find { postModel -> postModel.postId == it.postId } if (find != null) { find.apply { @@ -49,19 +53,17 @@ class PostsTableView : View() { } } else { items.add(it) - runLater { - this.tableView.refresh() - } + this.tableView.refresh() } } - } - - eventBus.flux().filter { - it!!.kind == Event.Kind.POST_REMOVE - }.subscribe { event -> - items.removeIf { p -> p.postId == event.data as String } - } + eventBus + .flux() + .filter { it!!.kind == Event.Kind.POST_REMOVE } + .publishOn(FxScheduler) + .subscribe { + items.removeIf { p -> p.postId == it.data as String } + } } override fun onDock() { diff --git a/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/ThreadTableView.kt b/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/ThreadTableView.kt index 899f909a..b5d42846 100644 --- a/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/ThreadTableView.kt +++ b/vripper-gui/src/main/kotlin/me/mnlr/vripper/view/tables/ThreadTableView.kt @@ -8,6 +8,7 @@ import me.mnlr.vripper.controller.ThreadController import me.mnlr.vripper.event.Event import me.mnlr.vripper.event.EventBus import me.mnlr.vripper.model.ThreadModel +import me.mnlr.vripper.view.FxScheduler import tornadofx.* class ThreadTableView : View() { @@ -29,35 +30,39 @@ class ThreadTableView : View() { }) items.addAll(threadController.findAll()) - eventBus.flux() - .filter { it!!.kind == Event.Kind.THREAD_UPDATE || it.kind == Event.Kind.THREAD_REMOVE || it.kind == Event.Kind.THREAD_CLEAR } - .subscribe { event -> - when (event!!.kind) { - Event.Kind.THREAD_UPDATE -> { - threadController.find(event.data as Long).ifPresent { - // search - val find = - items.find { threadModel -> threadModel.threadId == it.threadId } - if (find != null) { - find.apply { - total = it.total - } - } else { - items.add(it) - } - } - } - - Event.Kind.THREAD_REMOVE -> { - tableView.items.removeIf { it.threadId == event.data as String } + eventBus + .flux() + .filter { it!!.kind == Event.Kind.THREAD_UPDATE } + .map { threadController.find(it.data as Long) } + .filter { it.isPresent } + .map { it.get() } + .publishOn(FxScheduler) + .subscribe { + val find = + items.find { threadModel -> threadModel.threadId == it.threadId } + if (find != null) { + find.apply { + total = it.total } + } else { + items.add(it) + } + } - Event.Kind.THREAD_CLEAR -> { - tableView.items.clear() - } + eventBus + .flux() + .filter { it.kind == Event.Kind.THREAD_REMOVE } + .publishOn(FxScheduler) + .subscribe { event -> + tableView.items.removeIf { it.threadId == event.data as String } + } - else -> {} - } + eventBus + .flux() + .filter { it.kind == Event.Kind.THREAD_CLEAR } + .publishOn(FxScheduler) + .subscribe { + tableView.items.clear() } }