Skip to content

Commit

Permalink
Improvements to copy feature
Browse files Browse the repository at this point in the history
- Remove openUrl from context
- use skiko ClipboardManager
- FIx URL building missing host
- Fix launching app outside UWP environment
  • Loading branch information
DRSchlaubi committed Aug 18, 2023
1 parent f9f30fe commit f81dc41
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static long getAppdataFolderLength() throws Throwable {
* Tries to retrieve the current UWP app data folder.
*
* @return the absolute path to the folder
* @throws Throwable if an error occurrs
* @throws Throwable if an error occurs
*/
@Nullable
public static String getAppdataFolder() throws Throwable {
Expand Down
3 changes: 0 additions & 3 deletions app/desktop/src/main/kotlin/ConfigBasedAppContext.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package dev.schlaubi.tonbrett.app.desktop

import dev.schlaubi.tonbrett.app.api.AppContext
import java.net.URI

abstract class ConfigBasedAppContext : AppContext() {
override var token: String
get() = getConfig().sessionToken ?: error("Please sign in")
set(value) = saveConfig(Config(sessionToken = value))

override fun openUrl(url: String) = browseUrl(URI.create(url))
}
2 changes: 1 addition & 1 deletion app/desktop/src/main/kotlin/URIUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.awt.Desktop
import java.net.URI

fun browseUrl(url: URI) {
if (System.getProperty("os.name").contains("windows", ignoreCase = true)) {
if (System.getProperty("os.name").contains("windows", ignoreCase = true) && windowsAppDataFolder != null) {
NativeUtil.launchUri(url)
} else {
val desktop = Desktop.getDesktop()
Expand Down
2 changes: 0 additions & 2 deletions app/shared/src/commonMain/kotlin/api/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ abstract class AppContextBase {
fun resetApi() {
apiState.value = Tonbrett(token, getUrl(), onTokenRefresh = { token = it })
}

open fun openUrl(url: String) = Unit
}

expect open class AppContext : AppContextBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ import androidx.compose.foundation.ContextMenuItem
import androidx.compose.runtime.Composable
import cafe.adriel.lyricist.LocalStrings
import dev.schlaubi.tonbrett.app.api.LocalContext
import dev.schlaubi.tonbrett.client.href
import dev.schlaubi.tonbrett.common.Id
import dev.schlaubi.tonbrett.common.Route
import dev.schlaubi.tonbrett.common.Sound
import io.ktor.http.*
import org.jetbrains.skiko.ClipboardManager

@Composable
private fun CopySoundUrl(id: Id<Sound>): ContextMenuItem {
val context = LocalContext.current
val api = LocalContext.current.api
return ContextMenuItem(LocalStrings.current.copyUrl) {
context.openUrl(
href(Route.Sounds.Sound.Audio(id.toString(), ContentType.Audio.MPEG))
)
ClipboardManager().setText(api.href(Route.Sounds.Sound.Audio(id.toString(), ContentType.Audio.MPEG)))
}
}

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {

allprojects {
group = "dev.schlaubi.tonbrett"
version = "1.12.10"
version = "1.12.11"

repositories {
mavenCentral()
Expand Down
10 changes: 7 additions & 3 deletions client/src/commonMain/kotlin/Tonbrett.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import io.ktor.serialization.*
import io.ktor.serialization.kotlinx.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.util.*
import io.ktor.utils.io.core.*
import io.ktor.websocket.*
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
Expand All @@ -41,7 +39,7 @@ private var HttpRequestBuilder.useUnicode: Boolean

class ReauthorizationRequiredException : Exception()

class Tonbrett(initialToken: String, private val baseUrl: Url, private val onTokenRefresh: (String) -> Unit = {}) {
class Tonbrett(initialToken: String, @PublishedApi internal val baseUrl: Url, private val onTokenRefresh: (String) -> Unit = {}) {
private val eventFlow = MutableSharedFlow<Event>()
private val json = Json {
serializersModule = TonbrettSerializersModule
Expand Down Expand Up @@ -116,6 +114,12 @@ class Tonbrett(initialToken: String, private val baseUrl: Url, private val onTok

suspend fun stop(): Unit = client.post(Route.StopPlayer()).body()

inline fun <reified T> href(resource: T): String {
val builder = URLBuilder(baseUrl)
href(resource, builder)
return builder.toString()
}

@OptIn(DelicateCoroutinesApi::class)
suspend fun connect(useUnicode: Boolean = false) {
client.retryingWebSocket({ this.useUnicode = useUnicode }) {
Expand Down

0 comments on commit f81dc41

Please sign in to comment.