diff --git a/pom.xml b/pom.xml index c75f416..0258ca9 100644 --- a/pom.xml +++ b/pom.xml @@ -15,8 +15,8 @@ ${java.version} ${java.version} UTF-8 - 6.6.2 - 6.6.2-alpha + 6.6.3 + 6.6.3-alpha 2.2.20 2.0.16 1.5.12 diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/components/fragments/SessionFragment.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/components/fragments/SessionFragment.kt index 116ee0e..1507e9d 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/components/fragments/SessionFragment.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/components/fragments/SessionFragment.kt @@ -63,6 +63,9 @@ class SessionFragment : Fragment("Change Session") { addClass(Styles.ACCENT) isDefaultButton = true action { + runBlocking { + GuiEventBus.publishEvent(GuiEventBus.ChangingSession) + } val selectedToggle = toggleGroup.selectedToggle runLater { find().replaceWith(find()) @@ -80,7 +83,6 @@ class SessionFragment : Fragment("Change Session") { else -> VripperGuiApplication.APP_INSTANCE.stop() } runBlocking { - GuiEventBus.publishEvent(GuiEventBus.ChangingSession) GuiEventBus.publishEvent(GuiEventBus.ApplicationInitialized(emptyList())) } close() diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/ActionBarView.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/ActionBarView.kt index d7ed707..d54601e 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/ActionBarView.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/ActionBarView.kt @@ -13,7 +13,6 @@ import me.vripper.gui.components.fragments.SettingsFragment import me.vripper.gui.controller.ActionBarController import me.vripper.gui.controller.PostController import me.vripper.gui.controller.SettingsController -import me.vripper.gui.event.GuiEventBus import org.kordamp.ikonli.feather.Feather import org.kordamp.ikonli.javafx.FontIcon import tornadofx.* @@ -121,27 +120,9 @@ class ActionBarView : View() { downloadActiveProperty.bind(running.greaterThan(0)) coroutineScope.launch { - var job: Job? = null - GuiEventBus.events.collect { event -> - when (event) { - GuiEventBus.LocalSession, GuiEventBus.RemoteSession -> { - job = launch { - actionBarController.onQueueStateUpdate.collect { - runLater { - running.set(it.running) - } - } - } - } - - GuiEventBus.ChangingSession -> { - job?.cancelAndJoin() - runLater { - running.set(0) - } - } - - else -> {} + actionBarController.onQueueStateUpdate.collect { + runLater { + running.set(it.running) } } } diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/LogTableView.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/LogTableView.kt index f7a8779..3a3a420 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/LogTableView.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/LogTableView.kt @@ -153,13 +153,33 @@ class LogTableView : View() { tableView.placeholder = Label("Loading") tableView.sortOrder.add(tableView.columns.first { it.id == "time" }) + logController.newLogs.let { flow -> + coroutineScope.launch { + flow.collect { + runLater { + items.sortWith(Comparator.comparing { it.sequence }) + while (items.isNotEmpty() && (items.size >= maxLogEvent)) { + items.removeFirst() + } + items.add(it) + tableView.sort() + } + } + } + } + + logController.updateSettings.let { flow -> + coroutineScope.launch { + flow.collect { + maxLogEvent = it.systemSettings.maxEventLog + } + } + } coroutineScope.launch { - val jobs = mutableListOf() - GuiEventBus.events.collect { event -> - when (event) { + GuiEventBus.events.collect { + when (it) { GuiEventBus.LocalSession, GuiEventBus.RemoteSession -> { - println("Collecting $event from LogTableView") while (isActive) { val result = runCatching { logController.getMaxEventLog() } if (result.isSuccess) { @@ -173,31 +193,11 @@ class LogTableView : View() { break } } - launch { - logController.newLogs.collect { - runLater { - items.sortWith(Comparator.comparing { it.sequence }) - while (items.isNotEmpty() && (items.size >= maxLogEvent)) { - items.removeFirst() - } - items.add(it) - tableView.sort() - } - } - }.also { jobs.add(it) } - launch { - logController.updateSettings.collect { - maxLogEvent = it.systemSettings.maxEventLog - } - }.also { jobs.add(it) } } - GuiEventBus.ChangingSession -> { - jobs.forEach { it.cancelAndJoin() } - runLater { - items.clear() - tableView.placeholder = Label("Loading") - } + GuiEventBus.ChangingSession -> runLater { + items.clear() + tableView.placeholder = Label("Loading") } else -> {} diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/MenuBarView.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/MenuBarView.kt index 7b66672..7815589 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/MenuBarView.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/MenuBarView.kt @@ -17,7 +17,6 @@ import me.vripper.gui.controller.ActionBarController import me.vripper.gui.controller.PostController import me.vripper.gui.controller.SettingsController import me.vripper.gui.controller.WidgetsController -import me.vripper.gui.event.GuiEventBus import me.vripper.gui.utils.openLink import me.vripper.services.IAppEndpointService import me.vripper.utilities.ApplicationProperties @@ -270,31 +269,15 @@ class MenuBarView : View() { } } downloadActiveProperty.bind(running.greaterThan(0)) - - coroutineScope.launch { - var job: Job? = null - GuiEventBus.events.collect { event -> - when (event) { - GuiEventBus.LocalSession, GuiEventBus.RemoteSession -> { - job = launch { - actionBarController.onQueueStateUpdate.collect { - runLater { - running.set(it.running) - } - } - } + actionBarController.onQueueStateUpdate.let { flow -> + coroutineScope.launch { + flow.collect { + runLater { + running.set(it.running) } - - GuiEventBus.ChangingSession -> { - job?.cancelAndJoin() - runLater { - running.set(0) - } - } - - else -> {} } } } + } } \ No newline at end of file diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/PostInfoView.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/PostInfoView.kt index 9ea53b2..c667aec 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/PostInfoView.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/PostInfoView.kt @@ -2,8 +2,11 @@ package me.vripper.gui.components.views import javafx.collections.FXCollections import javafx.scene.control.TabPane -import kotlinx.coroutines.* +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.launch import me.vripper.gui.controller.PostController import me.vripper.gui.model.PostModel import org.kordamp.ikonli.feather.Feather @@ -17,8 +20,6 @@ class PostInfoView : View() { private val postModel: PostModel = PostModel( -1, -1, "", 0.0, "", "", 0, 0, "", "", "*", "", "", "", emptyList(), emptyList(), "", 0 ) - private var updatePostJob: Job? = null - private var updateMetadataJob: Job? = null override val root = tabpane() @@ -77,10 +78,6 @@ class PostInfoView : View() { } fun setPostId(id: Long?) { - runBlocking { - updatePostJob?.cancelAndJoin() - updateMetadataJob?.cancelAndJoin() - } imagesTableView.setPostId(id) if (id == null) { postModel.apply { @@ -130,32 +127,37 @@ class PostInfoView : View() { } } } - updatePostJob = coroutineScope.launch { - postController.updatePostsFlow.filter { - it.id == postModel.id - }.collect { post -> - runLater { - postModel.status = post.status.stringValue.lowercase().replaceFirstChar { it.uppercase() } - postModel.progressCount = postController.progressCount( - post.total, post.done, post.downloaded - ) - postModel.done = post.done - postModel.progress = postController.progress( - post.total, post.done - ) - postModel.path = post.getDownloadFolder() - postModel.folderName = post.folderName + + postController.updatePostsFlow.let { flow -> + coroutineScope.launch { + flow.filter { + it.id == postModel.id + }.collect { post -> + runLater { + postModel.status = post.status.stringValue.lowercase().replaceFirstChar { it.uppercase() } + postModel.progressCount = postController.progressCount( + post.total, post.done, post.downloaded + ) + postModel.done = post.done + postModel.progress = postController.progress( + post.total, post.done + ) + postModel.path = post.getDownloadFolder() + postModel.folderName = post.folderName + } } } } - updateMetadataJob = coroutineScope.launch { - postController.updateMetadataFlow.filter { - it.postIdRef == postModel.id - }.collect { - runLater { - postModel.altTitles = FXCollections.observableArrayList(it.data.resolvedNames) - postModel.postedBy = it.data.postedBy + postController.updateMetadataFlow.let { flow -> + coroutineScope.launch { + flow.filter { + it.postIdRef == postModel.id + }.collect { + runLater { + postModel.altTitles = FXCollections.observableArrayList(it.data.resolvedNames) + postModel.postedBy = it.data.postedBy + } } } } diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/PostsTableView.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/PostsTableView.kt index 424bf7d..b2b7ee1 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/PostsTableView.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/PostsTableView.kt @@ -14,7 +14,6 @@ import javafx.scene.input.MouseButton import javafx.util.Callback import kotlinx.coroutines.* import kotlinx.coroutines.flow.debounce -import kotlinx.coroutines.flow.toList import kotlinx.coroutines.javafx.asFlow import me.vripper.gui.components.Shared import me.vripper.gui.components.cells.PreviewTableCell @@ -51,12 +50,10 @@ class PostsTableView : View() { init { items.filterWhen(Shared.searchInput) { query, item -> - item.title.contains(query, ignoreCase = true) - || item.vgPostId.toString().contains(query) - || item.vgThreadId.toString().contains(query) - || item.hosts.contains(query, ignoreCase = true) - || item.status.contains(query, ignoreCase = true) - || item.path.contains(query, ignoreCase = true) + item.title.contains(query, ignoreCase = true) || item.vgPostId.toString() + .contains(query) || item.vgThreadId.toString().contains(query) || item.hosts.contains( + query, ignoreCase = true + ) || item.status.contains(query, ignoreCase = true) || item.path.contains(query, ignoreCase = true) } with(root) { @@ -237,8 +234,7 @@ class PostsTableView : View() { preview.cleanup() if (cell.tableRow.item != null && cell.tableRow.item.previewList.isNotEmpty()) { preview.display( - cell.tableRow.item.vgThreadId, - cell.tableRow.item.previewList + cell.tableRow.item.vgThreadId, cell.tableRow.item.previewList ) preview.previewPopup.apply { x = mouseEvent.screenX + 20 @@ -406,13 +402,75 @@ class PostsTableView : View() { tableView.prefHeightProperty().bind(root.heightProperty()) tableView.placeholder = Label("Loading") + postController.updateMetadataFlow.let { flow -> + coroutineScope.launch { + flow.collect { metadataEntity -> + runLater { + val postModel = items.find { it.id == metadataEntity.postIdRef } ?: return@runLater + + postModel.altTitles = FXCollections.observableArrayList(metadataEntity.data.resolvedNames) + postModel.postedBy = metadataEntity.data.postedBy + } + } + } + } + + postController.deletedPostsFlow.let { flow -> + coroutineScope.launch { + flow.collect { + runLater { + items.items.removeIf { p -> p.id == it } + tableView.sort() + } + } + } + } + + postController.updatePostsFlow.let { flow -> + coroutineScope.launch { + flow.collect { post -> + runLater { + val postModel = items.find { it.id == post.id } ?: return@runLater + + postModel.status = post.status.name + postModel.progressCount = postController.progressCount( + post.total, post.done, post.downloaded + ) + postModel.done = post.done + postModel.progress = postController.progress( + post.total, post.done + ) + postModel.path = post.getDownloadFolder() + postModel.folderName = post.folderName + } + } + } + } + + postController.queueStateUpdate.let { flow -> + coroutineScope.launch { + flow.collect { + updateQueueState(it) + } + } + } + + postController.newPostsFlow.let { flow -> + coroutineScope.launch { + flow.collect { + runLater { + items.addAll(it) + tableView.sort() + } + } + } + } + coroutineScope.launch { - val jobs = mutableListOf() - GuiEventBus.events.collect { event -> - when (event) { + GuiEventBus.events.collect { + when (it) { GuiEventBus.LocalSession, GuiEventBus.RemoteSession -> { - println("Collecting $event from PostsTableView") - val postModelList = postController.findAllPosts().toList() + val postModelList = postController.findAllPosts() val queueState = postController.getQueueState() runLater { items.addAll(postModelList) @@ -420,64 +478,11 @@ class PostsTableView : View() { tableView.placeholder = Label("No content in table") updateQueueState(queueState) } - launch { - postController.updateMetadataFlow.collect { metadataEntity -> - runLater { - val postModel = items.find { it.id == metadataEntity.postIdRef } ?: return@runLater - - postModel.altTitles = - FXCollections.observableArrayList(metadataEntity.data.resolvedNames) - postModel.postedBy = metadataEntity.data.postedBy - } - } - }.also { jobs.add(it) } - launch { - postController.deletedPostsFlow.collect { - runLater { - items.items.removeIf { p -> p.id == it } - tableView.sort() - } - } - }.also { jobs.add(it) } - launch { - postController.updatePostsFlow.collect { post -> - runLater { - val postModel = items.find { it.id == post.id } ?: return@runLater - - postModel.status = post.status.name - postModel.progressCount = postController.progressCount( - post.total, post.done, post.downloaded - ) - postModel.done = post.done - postModel.progress = postController.progress( - post.total, post.done - ) - postModel.path = post.getDownloadFolder() - postModel.folderName = post.folderName - } - } - }.also { jobs.add(it) } - launch { - postController.queueStateUpdate.collect { queueState -> - updateQueueState(queueState) - } - }.also { jobs.add(it) } - launch { - postController.newPostsFlow.collect { - runLater { - items.addAll(it) - tableView.sort() - } - } - }.also { jobs.add(it) } } - GuiEventBus.ChangingSession -> { - jobs.forEach { it.cancelAndJoin() } - runLater { - tableView.placeholder = Label("Loading") - items.clear() - } + GuiEventBus.ChangingSession -> runLater { + tableView.placeholder = Label("Loading") + items.clear() } else -> {} diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/StatusBarView.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/StatusBarView.kt index 3d49cf9..079c73c 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/StatusBarView.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/StatusBarView.kt @@ -26,9 +26,8 @@ class StatusBarView : View("Status bar") { init { coroutineScope.launch { - val jobs = mutableListOf() - GuiEventBus.events.collect { event -> - when (event) { + GuiEventBus.events.collect { + when (it) { GuiEventBus.LocalSession, GuiEventBus.RemoteSession -> { while (isActive) { val result = runCatching { statusBarController.loggedInUser() } @@ -40,54 +39,6 @@ class StatusBarView : View("Status bar") { } delay(1000) } - launch { - statusBarController.vgUserUpdate.collect { - runLater { - loggedUser.set(it) - } - } - }.also { jobs.add(it) } - launch { - statusBarController.tasksRunning.collect { - runLater { - tasksRunning.set(it) - } - } - }.also { jobs.add(it) } - launch { - statusBarController.downloadSpeed.collect { - runLater { - downloadSpeed.set(it.speed.formatSI()) - } - } - }.also { jobs.add(it) } - launch { - statusBarController.queueStateUpdate.collect { - runLater { - running.set(it.running) - pending.set(it.remaining) - } - } - }.also { jobs.add(it) } - launch { - statusBarController.errorCountUpdate.collect { - runLater { - error.set(it.count) - } - } - }.also { jobs.add(it) } - } - - GuiEventBus.ChangingSession -> { - jobs.forEach { it.cancelAndJoin() } - runLater { - loggedUser.set("") - tasksRunning.set(false) - downloadSpeed.set(0L.formatSI()) - running.set(0) - pending.set(0) - error.set(0) - } } else -> {} @@ -95,6 +46,57 @@ class StatusBarView : View("Status bar") { } } + statusBarController.vgUserUpdate.let { flow -> + coroutineScope.launch { + flow.collect { + runLater { + loggedUser.set(it) + } + } + } + } + + statusBarController.tasksRunning.let { flow -> + coroutineScope.launch { + flow.collect { + runLater { + tasksRunning.set(it) + } + } + } + } + + statusBarController.downloadSpeed.let { flow -> + coroutineScope.launch { + flow.collect { + runLater { + downloadSpeed.set(it.speed.formatSI()) + } + } + } + } + + statusBarController.queueStateUpdate.let { flow -> + coroutineScope.launch { + flow.collect { + runLater { + running.set(it.running) + pending.set(it.remaining) + } + } + } + } + + statusBarController.errorCountUpdate.let { flow -> + coroutineScope.launch { + flow.collect { + runLater { + error.set(it.count) + } + } + } + } + coroutineScope.launch { while (isActive) { val text = when (statusBarController.connectionState()) { diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/ThreadTableView.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/ThreadTableView.kt index bc49e9d..1c92833 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/ThreadTableView.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/components/views/ThreadTableView.kt @@ -7,10 +7,12 @@ import javafx.collections.ObservableList import javafx.scene.control.* import javafx.scene.input.KeyCode import javafx.scene.input.KeyEvent -import kotlinx.coroutines.* +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.flow.debounce -import kotlinx.coroutines.flow.toList import kotlinx.coroutines.javafx.asFlow +import kotlinx.coroutines.launch import me.vripper.gui.components.fragments.ThreadSelectionTableFragment import me.vripper.gui.controller.ThreadController import me.vripper.gui.controller.WidgetsController @@ -153,57 +155,63 @@ class ThreadTableView : View() { tableView.prefHeightProperty().bind(root.heightProperty()) tableView.placeholder = Label("Loading") + threadController.newThread.let { flow -> + coroutineScope.launch { + flow.collect { + runLater { + items.add(it) + } + } + } + } + + threadController.updateThread.let { flow -> + coroutineScope.launch { + flow.collect { thread -> + runLater { + val threadModel = items.find { it.threadId == thread.threadId } ?: return@runLater + threadModel.total = thread.total + threadModel.title = thread.title + } + } + } + } + + threadController.deleteThread.let { flow -> + coroutineScope.launch { + flow.collect { threadId -> + runLater { + tableView.items.removeIf { it.threadId == threadId } + } + } + } + } + + threadController.clearThreads.let { + coroutineScope.launch { + it.collect { + runLater { + tableView.items.clear() + } + } + } + } coroutineScope.launch { - val jobs = mutableListOf() - GuiEventBus.events.collect { event -> - when (event) { + GuiEventBus.events.collect { + when (it) { GuiEventBus.LocalSession, GuiEventBus.RemoteSession -> { - println("Collecting $event from ThreadTableView") - val list = threadController.findAll().toList() + val list = threadController.findAll() runLater { items.clear() items.addAll(list) tableView.placeholder = Label("No content in table") } - launch { - threadController.newThread.collect { - runLater { - items.add(it) - } - } - }.also { jobs.add(it) } - launch { - threadController.updateThread.collect { thread -> - runLater { - val threadModel = items.find { it.threadId == thread.threadId } ?: return@runLater - threadModel.total = thread.total - threadModel.title = thread.title - } - } - }.also { jobs.add(it) } - launch { - threadController.deleteThread.collect { threadId -> - runLater { - tableView.items.removeIf { it.threadId == threadId } - } - } - }.also { jobs.add(it) } - launch { - threadController.clearThreads.collect { - runLater { - tableView.items.clear() - } - } - }.also { jobs.add(it) } } - GuiEventBus.ChangingSession -> { - jobs.forEach { it.cancelAndJoin() } - runLater { - tableView.placeholder = Label("Loading") - items.clear() - } + GuiEventBus.ChangingSession -> runLater { + tableView.placeholder = Label("Loading") + items.clear() } else -> {} diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/controller/ActionBarController.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/controller/ActionBarController.kt index 0a5041d..0c8760a 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/controller/ActionBarController.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/controller/ActionBarController.kt @@ -1,11 +1,14 @@ package me.vripper.gui.controller -import kotlinx.coroutines.flow.cancellable -import me.vripper.gui.utils.AppEndpointManager.currentAppEndpointService +import me.vripper.gui.utils.AppEndpointManager.localAppEndpointService +import me.vripper.gui.utils.AppEndpointManager.remoteAppEndpointService +import me.vripper.gui.utils.ChannelFlowBuilder import tornadofx.Controller class ActionBarController : Controller() { - val onQueueStateUpdate = - currentAppEndpointService().onQueueStateUpdate().cancellable() + val onQueueStateUpdate = ChannelFlowBuilder.build( + localAppEndpointService::onQueueStateUpdate, + remoteAppEndpointService::onQueueStateUpdate + ) } \ No newline at end of file diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/controller/LogController.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/controller/LogController.kt index a599aa9..84e9d25 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/controller/LogController.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/controller/LogController.kt @@ -1,17 +1,26 @@ package me.vripper.gui.controller -import kotlinx.coroutines.flow.cancellable import kotlinx.coroutines.flow.map import me.vripper.gui.model.LogModel import me.vripper.gui.utils.AppEndpointManager.currentAppEndpointService +import me.vripper.gui.utils.AppEndpointManager.localAppEndpointService +import me.vripper.gui.utils.AppEndpointManager.remoteAppEndpointService +import me.vripper.gui.utils.ChannelFlowBuilder import me.vripper.model.LogEntry import tornadofx.Controller class LogController : Controller() { - val newLogs = currentAppEndpointService().onNewLog().map(::mapper).cancellable() + val newLogs = ChannelFlowBuilder.build( + { localAppEndpointService.onNewLog().map(::mapper) }, + { remoteAppEndpointService.onNewLog().map(::mapper) } + ) - val updateSettings = currentAppEndpointService().onUpdateSettings().cancellable() + val updateSettings = + ChannelFlowBuilder.build( + localAppEndpointService::onUpdateSettings, + remoteAppEndpointService::onUpdateSettings + ) private fun mapper(it: LogEntry): LogModel { return LogModel( diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/controller/PostController.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/controller/PostController.kt index 8c69203..d630a2f 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/controller/PostController.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/controller/PostController.kt @@ -1,11 +1,11 @@ package me.vripper.gui.controller -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.cancellable import kotlinx.coroutines.flow.map import me.vripper.gui.model.PostModel import me.vripper.gui.utils.AppEndpointManager.currentAppEndpointService -import me.vripper.gui.utils.ChannelFlowBuilder.toFlow +import me.vripper.gui.utils.AppEndpointManager.localAppEndpointService +import me.vripper.gui.utils.AppEndpointManager.remoteAppEndpointService +import me.vripper.gui.utils.ChannelFlowBuilder import me.vripper.model.Post import me.vripper.model.QueueState import me.vripper.services.download.MovePosition @@ -17,15 +17,39 @@ class PostController : Controller() { private val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") - val updatePostsFlow = currentAppEndpointService().onUpdatePosts().cancellable() + val updatePostsFlow = + ChannelFlowBuilder.build( + localAppEndpointService::onUpdatePosts, + remoteAppEndpointService::onUpdatePosts + ) - val newPostsFlow = currentAppEndpointService().onNewPosts().map(::mapper).cancellable() + val newPostsFlow = ChannelFlowBuilder.build( + { + localAppEndpointService.onNewPosts().map { post -> + mapper(post) + } + }, { + remoteAppEndpointService.onNewPosts().map { post -> + mapper(post) + } + } + ) - val deletedPostsFlow = currentAppEndpointService().onDeletePosts().cancellable() + val deletedPostsFlow = + ChannelFlowBuilder.build( + localAppEndpointService::onDeletePosts, + remoteAppEndpointService::onDeletePosts + ) - val updateMetadataFlow = currentAppEndpointService().onUpdateMetadata().cancellable() + val updateMetadataFlow = ChannelFlowBuilder.build( + localAppEndpointService::onUpdateMetadata, + remoteAppEndpointService::onUpdateMetadata, + ) - val queueStateUpdate = currentAppEndpointService().onQueueStateUpdate().cancellable() + val queueStateUpdate = ChannelFlowBuilder.build( + localAppEndpointService::onQueueStateUpdate, + remoteAppEndpointService::onQueueStateUpdate, + ) suspend fun scan(postLinks: String) { runCatching { currentAppEndpointService().scanLinks(postLinks) } @@ -59,8 +83,8 @@ class PostController : Controller() { return runCatching { mapper(currentAppEndpointService().findPost(postEntityId)) }.getOrNull() } - fun findAllPosts(): Flow { - return toFlow { currentAppEndpointService().findAllPosts().map(::mapper) } + suspend fun findAllPosts(): List { + return currentAppEndpointService().findAllPosts().map(::mapper) } suspend fun getQueueState(): QueueState { diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/controller/StatusBarController.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/controller/StatusBarController.kt index 235da1c..7d86d02 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/controller/StatusBarController.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/controller/StatusBarController.kt @@ -1,7 +1,9 @@ package me.vripper.gui.controller -import kotlinx.coroutines.flow.cancellable import me.vripper.gui.utils.AppEndpointManager.currentAppEndpointService +import me.vripper.gui.utils.AppEndpointManager.localAppEndpointService +import me.vripper.gui.utils.AppEndpointManager.remoteAppEndpointService +import me.vripper.gui.utils.ChannelFlowBuilder import tornadofx.Controller class StatusBarController : Controller() { @@ -17,13 +19,28 @@ class StatusBarController : Controller() { return currentAppEndpointService().getVersion() } - val vgUserUpdate = currentAppEndpointService().onVGUserUpdate().cancellable() + val vgUserUpdate = ChannelFlowBuilder.build( + localAppEndpointService::onVGUserUpdate, + remoteAppEndpointService::onVGUserUpdate, + ) - val tasksRunning = currentAppEndpointService().onTasksRunning().cancellable() + val tasksRunning = ChannelFlowBuilder.build( + localAppEndpointService::onTasksRunning, + remoteAppEndpointService::onTasksRunning, + ) - val downloadSpeed = currentAppEndpointService().onDownloadSpeed().cancellable() + val downloadSpeed = ChannelFlowBuilder.build( + localAppEndpointService::onDownloadSpeed, + remoteAppEndpointService::onDownloadSpeed, + ) - val queueStateUpdate = currentAppEndpointService().onQueueStateUpdate().cancellable() + val queueStateUpdate = ChannelFlowBuilder.build( + localAppEndpointService::onQueueStateUpdate, + remoteAppEndpointService::onQueueStateUpdate, + ) - val errorCountUpdate = currentAppEndpointService().onErrorCountUpdate().cancellable() + val errorCountUpdate = ChannelFlowBuilder.build( + localAppEndpointService::onErrorCountUpdate, + remoteAppEndpointService::onErrorCountUpdate + ) } \ No newline at end of file diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/controller/ThreadController.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/controller/ThreadController.kt index 40962f4..9ee046f 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/controller/ThreadController.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/controller/ThreadController.kt @@ -1,29 +1,45 @@ package me.vripper.gui.controller -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.cancellable import kotlinx.coroutines.flow.map import me.vripper.entities.ThreadEntity import me.vripper.gui.model.ThreadModel import me.vripper.gui.model.ThreadSelectionModel import me.vripper.gui.utils.AppEndpointManager.currentAppEndpointService -import me.vripper.gui.utils.ChannelFlowBuilder.toFlow +import me.vripper.gui.utils.AppEndpointManager.localAppEndpointService +import me.vripper.gui.utils.AppEndpointManager.remoteAppEndpointService +import me.vripper.gui.utils.ChannelFlowBuilder import me.vripper.model.ThreadPostId import org.koin.core.component.KoinComponent import tornadofx.Controller class ThreadController : KoinComponent, Controller() { - val newThread = currentAppEndpointService().onNewThread().map(::threadModelMapper).cancellable() + val newThread = ChannelFlowBuilder.build( + { + localAppEndpointService.onNewThread().map(::threadModelMapper) + }, + { + remoteAppEndpointService.onNewThread().map(::threadModelMapper) + }, + ) - val updateThread = currentAppEndpointService().onUpdateThread().cancellable() + val updateThread = ChannelFlowBuilder.build( + localAppEndpointService::onUpdateThread, + remoteAppEndpointService::onUpdateThread, + ) - val deleteThread = currentAppEndpointService().onDeleteThread().cancellable() + val deleteThread = ChannelFlowBuilder.build( + localAppEndpointService::onDeleteThread, + remoteAppEndpointService::onDeleteThread, + ) - val clearThreads = currentAppEndpointService().onClearThreads().cancellable() + val clearThreads = ChannelFlowBuilder.build( + localAppEndpointService::onClearThreads, + remoteAppEndpointService::onClearThreads, + ) - fun findAll(): Flow { - return toFlow { currentAppEndpointService().findAllThreads().map(::threadModelMapper) } + suspend fun findAll(): List { + return currentAppEndpointService().findAllThreads().map(::threadModelMapper) } private fun threadModelMapper(it: ThreadEntity): ThreadModel { diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/utils/ChannelFlowBuilder.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/utils/ChannelFlowBuilder.kt index eb7d782..b7e54ba 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/utils/ChannelFlowBuilder.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/utils/ChannelFlowBuilder.kt @@ -1,16 +1,47 @@ package me.vripper.gui.utils -import kotlinx.coroutines.delay +import kotlinx.coroutines.* import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.cancellable import kotlinx.coroutines.flow.channelFlow import kotlinx.coroutines.flow.retryWhen -import kotlinx.coroutines.isActive +import me.vripper.gui.event.GuiEventBus object ChannelFlowBuilder { - fun toFlow(source: suspend () -> List): Flow { + fun build(localFlow: () -> Flow, remoteFlow: () -> Flow): Flow { return channelFlow { - source().forEach { if (isActive) send(it) } - }.retryWhen { _, _ -> delay(1000); true } + var job: Job? = null + + fun collect(localSession: Boolean) { + job = if (localSession) { + launch { + localFlow().cancellable().collect { if (isActive) send(it) } + } + } else { + launch { + remoteFlow().cancellable().retryWhen { _, _ -> + delay(1000) + true + }.collect { if (isActive) send(it) } + } + } + } + + launch { + GuiEventBus + .events + .collect { + if (job != null && job.isActive) { + job.cancelAndJoin() + } + when (it) { + GuiEventBus.LocalSession -> collect(true) + GuiEventBus.RemoteSession -> collect(false) + else -> {} + } + } + } + } } } diff --git a/vripper-gui/src/main/kotlin/me/vripper/gui/utils/ClipboardManager.kt b/vripper-gui/src/main/kotlin/me/vripper/gui/utils/ClipboardManager.kt index c4eeb08..9566761 100644 --- a/vripper-gui/src/main/kotlin/me/vripper/gui/utils/ClipboardManager.kt +++ b/vripper-gui/src/main/kotlin/me/vripper/gui/utils/ClipboardManager.kt @@ -18,8 +18,8 @@ object ClipboardManager : KoinComponent { fun init() { coroutineScope.launch { - GuiEventBus.events.collect { event -> - when (event) { + GuiEventBus.events.collect { + when (it) { GuiEventBus.LocalSession, GuiEventBus.RemoteSession -> { logger.info("Clipboard manager initialized") while (isActive) { @@ -30,6 +30,7 @@ object ClipboardManager : KoinComponent { } delay(1000) } + settingsUpdateJob?.cancelAndJoin() settingsUpdateJob = launch { AppEndpointManager.currentAppEndpointService().onUpdateSettings() .retryWhen { _, _ -> delay(1000); true }.collect { @@ -37,9 +38,7 @@ object ClipboardManager : KoinComponent { } } } - GuiEventBus.ChangingSession -> { - settingsUpdateJob?.cancelAndJoin() - } + else -> {} } }