Skip to content

Commit

Permalink
Cleanup NativeUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Sep 25, 2023
1 parent add1575 commit 2704fad
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 45 deletions.
13 changes: 2 additions & 11 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 1 addition & 28 deletions app/desktop/src/commonMain/kotlin/ConfigFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,13 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.json.encodeToStream
import java.nio.file.Path
import java.nio.file.StandardOpenOption
import kotlin.io.path.*

val windowsAppDataFolder: String? by lazy {
runCatching { getAppDataRoaming() }
.onFailure(Throwable::printStackTrace)
.getOrNull()
}

@Serializable
data class Config(@EncodeDefault(EncodeDefault.Mode.NEVER) val sessionToken: String? = null)

fun getAppDataFolder(): Path {
val os = System.getProperty("os.name")
val basePath = when {
os.contains("windows", ignoreCase = true) -> {
val uwpFolder = windowsAppDataFolder
if (uwpFolder == null) {
Path(System.getenv("APPDATA"))
} else {
Path(uwpFolder)
}
}

os.contains("mac", ignoreCase = true) ->
Path(System.getenv("HOME")) / "Library" / "Application Support"

else -> Path(System.getProperty("user.home"))
}
return basePath / "Tonbrett"
}

fun getConfigFile() = getAppDataFolder() / "config.json"
fun getConfigFile() = getAppDataRoaming() / "config.json"

fun getConfig(): Config {
val file = getConfigFile()
Expand Down
3 changes: 2 additions & 1 deletion app/desktop/src/commonMain/kotlin/NativeUtil.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.schlaubi.tonbrett.app.desktop

import java.net.URI
import java.nio.file.Path

/**
* Tries to launch the URI using the UWP `Launcher`.
Expand All @@ -14,4 +15,4 @@ expect fun launchUri(uri: URI)
*
* @return the absolute path to the folder
*/
expect fun getAppDataRoaming(): String
expect fun getAppDataRoaming(): Path
15 changes: 13 additions & 2 deletions app/desktop/src/nonWindowsMain/kotlin/NativeUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package dev.schlaubi.tonbrett.app.desktop

import java.awt.Desktop
import java.net.URI
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.div

actual fun launchUri(uri: URI) {
val desktop = Desktop.getDesktop()
Expand All @@ -12,5 +15,13 @@ actual fun launchUri(uri: URI) {
}
}

actual fun getAppDataRoaming(): String =
throw UnsupportedOperationException("This function is only supported on Windows")
actual fun getAppDataRoaming(): Path {
val os = System.getProperty("os.name")
val basePath = when {
os.contains("windows", ignoreCase = true) -> Path(System.getenv("APPDATA"))
os.contains("mac", ignoreCase = true) ->
Path(System.getenv("HOME")) / "Library" / "Application Support"
else -> Path(System.getProperty("user.home"))
}
return basePath / "Tonbrett"
}
5 changes: 3 additions & 2 deletions app/desktop/src/windowsMain/kotlin/NativeUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import dev.schlaubi.tonbrett.app.desktop.uwp_helper.AppDataRoamingResult
import dev.schlaubi.tonbrett.app.desktop.uwp_helper.UwpHelper.*
import java.lang.foreign.Arena
import java.net.URI
import java.nio.file.Path

actual fun launchUri(uri: URI): Unit = Arena.openConfined().use { arena ->
val url = arena.allocateUtf8String(uri.toString())
launch_uri(url)
}

actual fun getAppDataRoaming(): String =
actual fun getAppDataRoaming(): Path =
Arena.openConfined().use { arena ->
val result = get_app_data_roaming(arena)
val isError = AppDataRoamingResult.`is_error$get`(result)
Expand All @@ -23,5 +24,5 @@ actual fun getAppDataRoaming(): String =
charArray[index] = short.toInt().toChar()
}
val string = String(charArray)
if (isError) throw Exception(string) else string
if (isError) throw Exception(string) else Path(string)
}

0 comments on commit 2704fad

Please sign in to comment.