From 0f6798e69f6367072f89f7718466ab333bd94e67 Mon Sep 17 00:00:00 2001 From: Xerus <27jf@web.de> Date: Thu, 13 Sep 2018 16:06:16 +0200 Subject: [PATCH] Update Coroutines --- build.gradle.kts | 8 +++----- src/archive/Downloader.kt | 2 +- src/main/xerus/monstercat/Main.kt | 8 +++----- src/main/xerus/monstercat/MonsterUtilities.kt | 7 ++++--- src/main/xerus/monstercat/api/DiscordRPC.kt | 3 ++- src/main/xerus/monstercat/api/Player.kt | 7 ++++--- src/main/xerus/monstercat/downloader/SongViews.kt | 3 ++- .../xerus/monstercat/downloader/TabDownloader.kt | 12 ++++++------ 8 files changed, 25 insertions(+), 25 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index a6b8e83..287cadb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -47,14 +47,12 @@ repositories { } dependencies { - compile("com.github.Xerus2000", "util", "master-SNAPSHOT") - compile(kotlin("stdlib-jdk8")) compile(kotlin("reflect")) + compile("com.github.Xerus2000.util", "javafx", "-SNAPSHOT") compile("org.controlsfx", "controlsfx", "8.40.14") - compile("be.bluexin", "drpc4k", "0.6-SNAPSHOT") - + compile("com.github.Bluexin", "drpc4k", "-SNAPSHOT") compile("org.apache.httpcomponents", "httpmime", "4.5.5") compile("com.google.apis", "google-api-services-sheets", "v4-rev527-1.23.0") @@ -112,7 +110,7 @@ tasks { setDelete(file(".").listFiles { f -> f.name.run { startsWith("MonsterUtilities-") && endsWith("jar") && this != file } }) } - "test"(Test::class) { + withType { useJUnitPlatform() } diff --git a/src/archive/Downloader.kt b/src/archive/Downloader.kt index f0d6150..5e7a533 100644 --- a/src/archive/Downloader.kt +++ b/src/archive/Downloader.kt @@ -44,7 +44,7 @@ class DownloaderSwing : BasePanel() { } init { - launch { + GlobalScope.launch { logger.fine("DownloadWorker started") var limit = LIMIT.int diff --git a/src/main/xerus/monstercat/Main.kt b/src/main/xerus/monstercat/Main.kt index 1608abb..c2584d2 100644 --- a/src/main/xerus/monstercat/Main.kt +++ b/src/main/xerus/monstercat/Main.kt @@ -4,9 +4,7 @@ import com.google.api.client.googleapis.auth.oauth2.GoogleCredential import com.google.api.services.sheets.v4.SheetsScopes import javafx.scene.Scene import javafx.scene.image.Image -import kotlinx.coroutines.experimental.asCoroutineDispatcher -import kotlinx.coroutines.experimental.delay -import kotlinx.coroutines.experimental.launch +import kotlinx.coroutines.experimental.* import xerus.ktutil.* import xerus.ktutil.javafx.applySkin import xerus.ktutil.javafx.ui.App @@ -44,7 +42,7 @@ fun main(args: Array) { try { XerusLogger.logToFile(logfile) logger.config("Logging to $logfile") - launch { + GlobalScope.launch { val logs = logDir.listFiles() if (logs.size > 10) { logs.asSequence().sortedByDescending { it.name }.drop(5).filter { @@ -82,7 +80,7 @@ fun main(args: Array) { } fun showErrorSafe(error: Throwable, title: String = "Error") { - launch { + GlobalScope.launch { var i = 0 while (i < 100 && !::monsterUtilities.isInitialized) { delay(200) diff --git a/src/main/xerus/monstercat/MonsterUtilities.kt b/src/main/xerus/monstercat/MonsterUtilities.kt index 8a922de..0b66314 100644 --- a/src/main/xerus/monstercat/MonsterUtilities.kt +++ b/src/main/xerus/monstercat/MonsterUtilities.kt @@ -6,6 +6,7 @@ import javafx.scene.control.* import javafx.scene.image.Image import javafx.scene.image.ImageView import javafx.scene.layout.VBox +import kotlinx.coroutines.experimental.GlobalScope import kotlinx.coroutines.experimental.launch import org.controlsfx.dialog.ExceptionDialog import xerus.ktutil.* @@ -42,7 +43,7 @@ class MonsterUtilities : VBox(), JFXMessageDisplay { fun addTab(tabClass: KClass) { try { - val baseTab = tabClass.java.newInstance() + val baseTab = tabClass.java.getDeclaredConstructor().newInstance() logger.finer("New Tab: $baseTab") tabs.add(baseTab) val tab = Tab(baseTab.tabName, baseTab.asNode()) @@ -65,7 +66,7 @@ class MonsterUtilities : VBox(), JFXMessageDisplay { showIntro() Settings.LASTVERSION.put(VERSION) } else { - launch { + GlobalScope.launch { logger.fine("New version! Now running $VERSION, previously " + Settings.LASTVERSION()) val f = Settings.DELETE() if (f.exists()) { @@ -97,7 +98,7 @@ class MonsterUtilities : VBox(), JFXMessageDisplay { inline fun tabsByClass() = tabs.mapNotNull { it as? T } fun checkForUpdate(userControlled: Boolean = false, unstable: Boolean = isUnstable) { - launch { + GlobalScope.launch { try { val latestVersion = URL("http://monsterutilities.bplaced.net/downloads/" + if (unstable) "unstable" else "latest").openConnection().getInputStream().reader().readLines().firstOrNull() logger.fine("Latest version: $latestVersion") diff --git a/src/main/xerus/monstercat/api/DiscordRPC.kt b/src/main/xerus/monstercat/api/DiscordRPC.kt index c80388b..5da8168 100644 --- a/src/main/xerus/monstercat/api/DiscordRPC.kt +++ b/src/main/xerus/monstercat/api/DiscordRPC.kt @@ -2,6 +2,7 @@ package xerus.monstercat.api import be.bluexin.drpc4k.jna.DiscordRichPresence import be.bluexin.drpc4k.jna.RPCHandler +import kotlinx.coroutines.experimental.GlobalScope import kotlinx.coroutines.experimental.delay import kotlinx.coroutines.experimental.launch import xerus.ktutil.getResource @@ -23,7 +24,7 @@ object DiscordRPC { } fun connect(delay: Int = 0) { - launch { + GlobalScope.launch { delay(delay) if (!RPCHandler.connected.get()) { RPCHandler.onReady = { diff --git a/src/main/xerus/monstercat/api/Player.kt b/src/main/xerus/monstercat/api/Player.kt index b875e88..bff6a28 100644 --- a/src/main/xerus/monstercat/api/Player.kt +++ b/src/main/xerus/monstercat/api/Player.kt @@ -9,6 +9,7 @@ import javafx.scene.layout.VBox import javafx.scene.media.Media import javafx.scene.media.MediaPlayer import javafx.util.Duration +import kotlinx.coroutines.experimental.GlobalScope import kotlinx.coroutines.experimental.delay import kotlinx.coroutines.experimental.launch import xerus.ktutil.javafx.* @@ -91,7 +92,7 @@ object Player : FadingHBox(true, targetHeight = 25) { /** hides the Player and appears again displaying the latest Release */ fun reset() { fadeOut() - launch { + GlobalScope.launch { val latest = Releases.getReleases().lastOrNull() ?: return@launch while (fading) delay(50) showText("Latest Release: $latest") @@ -177,7 +178,7 @@ object Player : FadingHBox(true, targetHeight = 25) { /** Finds the best match for the given [title] and [artists] and starts playing it */ fun play(title: String, artists: String) { - launch { + GlobalScope.launch { showText("Searching for \"$title\"...") disposePlayer() val track = API.find(title, artists) @@ -193,7 +194,7 @@ object Player : FadingHBox(true, targetHeight = 25) { /** Plays this [release], creating an internal playlist when it has multiple Tracks */ fun play(release: Release) { checkFx { showText("Searching for $release") } - launch { + GlobalScope.launch { val results = APIConnection("catalog", "release", release.id, "tracks").getTracks()?.takeUnless { it.isEmpty() } ?: run { showBack("No tracks found for Release $release") diff --git a/src/main/xerus/monstercat/downloader/SongViews.kt b/src/main/xerus/monstercat/downloader/SongViews.kt index 5c92a31..78f927f 100644 --- a/src/main/xerus/monstercat/downloader/SongViews.kt +++ b/src/main/xerus/monstercat/downloader/SongViews.kt @@ -1,5 +1,6 @@ package xerus.monstercat.downloader +import kotlinx.coroutines.experimental.GlobalScope import kotlinx.coroutines.experimental.launch import xerus.ktutil.javafx.controlsfx.FilterableCheckTreeView import xerus.ktutil.javafx.onFx @@ -31,7 +32,7 @@ abstract class SongView(root: T) : FilterableCheckTreeView(roo } fun load() { - launch { + GlobalScope.launch { fetchItems() onFx { onReady() diff --git a/src/main/xerus/monstercat/downloader/TabDownloader.kt b/src/main/xerus/monstercat/downloader/TabDownloader.kt index 6832ef4..5c339c7 100644 --- a/src/main/xerus/monstercat/downloader/TabDownloader.kt +++ b/src/main/xerus/monstercat/downloader/TabDownloader.kt @@ -205,7 +205,7 @@ class TabDownloader : VTab() { .also { it.selectedProperty().listen { if (it) { - launch { + GlobalScope.launch { awaitReady() releaseView.roots.forEach { it.value.internalChildren.removeIf { @@ -218,7 +218,7 @@ class TabDownloader : VTab() { releaseView.root.internalChildren.clear() releaseView.roots.clear() trackView.root.internalChildren.clear() - launch { + GlobalScope.launch { releaseView.load() trackView.load() } @@ -236,7 +236,7 @@ class TabDownloader : VTab() { val dont = arrayOf("Album", "EP", "Single") val deferred = (albums.flatMap { it.children } + releaseView.roots.filterNot { it.value.value.title in dont }.flatMap { it.value.internalChildren }.filterNot { it.value.isMulti }) .map { - async(context) { + GlobalScope.async(context) { if (!isActive) return@async null APIConnection("catalog", "release", it.value.id, "tracks").getTracks()?.map { it.toString().normalised } } @@ -284,7 +284,7 @@ class TabDownloader : VTab() { private fun refreshDownloadButton(button: Button) { button.text = "Checking..." - launch { + GlobalScope.launch { var valid = false val text = when (APIConnection.checkCookie()) { CookieValidity.NOCONNECTION -> "No connection" @@ -359,7 +359,7 @@ class TabDownloader : VTab() { if (done == total) progressLabel.text = "$done / $total Errors: $e" else - counter = launch { + counter = GlobalScope.launch { val estimate = ((estimatedLength / lengths.sum() + total / done - 2) * timer.time() / 1000).roundToLong() time = if (time > 0) (time * 9 + estimate) / 10 else estimate logger.finest("Estimate: ${formatTimeDynamic(estimate, estimate.coerceAtLeast(60))} Weighed: ${formatTimeDynamic(time, time.coerceAtLeast(60))}") @@ -398,7 +398,7 @@ class TabDownloader : VTab() { private val success = SimpleIntegerProperty() private val errors = SimpleIntegerProperty() private fun startDownload() { - downloader = launch { + downloader = GlobalScope.launch { log("Download started") for (item in items) { val download = item.downloadTask()