Skip to content

Commit

Permalink
✨ Impl the app information model (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
guiyanakuang authored Nov 21, 2023
1 parent 99701b3 commit 529e83d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
7 changes: 7 additions & 0 deletions composeApp/src/commonMain/kotlin/com/clipevery/AppInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.clipevery

data class AppInfo(
val appName: String = "Clipevery",
val appVersion: String,
val userName: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.clipevery

interface AppInfoFactory {
fun createAppInfo(): AppInfo
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.clipevery

import com.clipevery.platform.currentPlatform
import io.github.oshai.kotlinlogging.KotlinLogging
import java.io.IOException
import java.nio.file.Paths
import java.util.Properties


val logger = KotlinLogging.logger {}

fun getFactory(): AppInfoFactory {
val platform = currentPlatform()
return if (platform.isMacos()) {
MacosAppInfoFactory()
} else if (platform.isWindows()) {
WindowsAppInfoFactory()
} else {
throw IllegalStateException("Unknown platform: ${platform.name}")
}
}

class MacosAppInfoFactory: AppInfoFactory {
override fun createAppInfo(): AppInfo {
return AppInfo(appVersion = getVersion(), userName = getUserName())
}
}

class WindowsAppInfoFactory: AppInfoFactory {
override fun createAppInfo(): AppInfo {
return AppInfo(appVersion = getVersion(), userName = getUserName())
}
}

fun getVersion(): String {
val properties = Properties()
try {
properties.load( Thread.currentThread().contextClassLoader
.getResourceAsStream("version.properties"))
return properties.getProperty("version", "Unknown")
} catch (e: IOException) {
logger.error(e) { "Failed to read version" }
}
return "Unknown"
}

fun getUserName(): String {
val userHome = System.getProperty("user.home")
return Paths.get(userHome).toFile().name
}
1 change: 1 addition & 0 deletions composeApp/src/desktopMain/resources/version.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version=1.0.0

0 comments on commit 529e83d

Please sign in to comment.