-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Implementing the ConfigManager (#16)
- Loading branch information
1 parent
18dd599
commit 7042d43
Showing
16 changed files
with
243 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
composeApp/src/commonMain/kotlin/com/clipevery/config/ConfigManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.clipevery.config | ||
|
||
import com.clipevery.AppConfig | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
abstract class ConfigManager(private val ioScope: CoroutineScope) { | ||
|
||
lateinit var config: AppConfig | ||
|
||
fun initConfig(): ConfigManager { | ||
config = try { | ||
loadConfig() ?: AppConfig() | ||
} catch (e: Exception) { | ||
AppConfig() | ||
} | ||
return this | ||
} | ||
|
||
abstract fun loadConfig(): AppConfig? | ||
|
||
@Synchronized | ||
fun updateBindingState(bindingState: Boolean) { | ||
config = config.copy(bindingState = bindingState) | ||
ioScope.launch { | ||
saveConfig(config) | ||
} | ||
} | ||
|
||
@Synchronized | ||
fun saveConfig(config: AppConfig) { | ||
saveConfigImpl(config) | ||
} | ||
|
||
protected abstract fun saveConfigImpl(config: AppConfig) | ||
} |
6 changes: 6 additions & 0 deletions
6
composeApp/src/commonMain/kotlin/com/clipevery/config/ConfigType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.clipevery.config | ||
|
||
enum class ConfigType { | ||
USER, | ||
SYSTEM | ||
} |
5 changes: 0 additions & 5 deletions
5
composeApp/src/commonMain/kotlin/com/clipevery/net/ClipServer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
package com.clipevery.net | ||
|
||
import com.clipevery.AppConfig | ||
|
||
interface ClipServer { | ||
} | ||
|
||
interface ConfigManager { | ||
val config: AppConfig | ||
} |
17 changes: 17 additions & 0 deletions
17
composeApp/src/commonMain/kotlin/com/clipevery/path/PathProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.clipevery.path | ||
|
||
import com.clipevery.config.ConfigType | ||
import java.nio.file.Path | ||
|
||
interface PathProvider { | ||
fun resolve(configName: String, configType: ConfigType): Path { | ||
return when (configType) { | ||
ConfigType.USER -> resolveUser(configName) | ||
ConfigType.SYSTEM -> resolveApp(configName) | ||
} | ||
} | ||
|
||
fun resolveUser(configName: String): Path | ||
|
||
fun resolveApp(configName: String): Path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
composeApp/src/commonMain/kotlin/com/clipevery/presist/FilePersist.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.clipevery.presist | ||
|
||
import com.clipevery.config.ConfigType | ||
import com.clipevery.path.PathProvider | ||
import java.nio.file.Path | ||
|
||
interface FilePersist { | ||
|
||
val pathProvider: PathProvider | ||
|
||
fun getPersist(configName: String, configType: ConfigType): OneFilePersist { | ||
return createOneFilePersist(pathProvider.resolve(configName, configType)) | ||
} | ||
|
||
fun createOneFilePersist(path: Path): OneFilePersist | ||
} |
8 changes: 8 additions & 0 deletions
8
composeApp/src/commonMain/kotlin/com/clipevery/presist/OneFilePersist.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.clipevery.presist | ||
|
||
import kotlin.reflect.KClass | ||
|
||
interface OneFilePersist { | ||
fun <T : Any> readAs(clazz: KClass<T>): T? | ||
fun <T> save(config: T) | ||
} |
60 changes: 60 additions & 0 deletions
60
composeApp/src/desktopMain/kotlin/com/clipevery/path/DesktopPathProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.clipevery.path | ||
|
||
import com.clipevery.platform.currentPlatform | ||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
|
||
fun getPathProvider(): PathProvider { | ||
return if (currentPlatform().isWindows()) { | ||
WindowsPathProvider() | ||
} else if (currentPlatform().isMacos()) { | ||
MacosPathProvider() | ||
} else if (currentPlatform().isLinux()) { | ||
LinuxPathProvider() | ||
} else { | ||
throw IllegalStateException("Unknown platform: ${currentPlatform().name}") | ||
} | ||
} | ||
|
||
|
||
class WindowsPathProvider: PathProvider { | ||
|
||
private val userHomePath = System.getProperty("user.home") | ||
|
||
private val userDir = System.getProperty("user.dir") | ||
|
||
override fun resolveUser(configName: String): Path { | ||
return Paths.get(userHomePath).resolve(".clipevery").resolve(configName) | ||
} | ||
|
||
override fun resolveApp(configName: String): Path { | ||
return Paths.get(userDir).resolve(configName) | ||
} | ||
} | ||
|
||
|
||
class MacosPathProvider: PathProvider { | ||
|
||
private val userHomePath = System.getProperty("user.home") | ||
|
||
private val userDir = System.getProperty("user.dir") | ||
|
||
override fun resolveUser(configName: String): Path { | ||
return Paths.get(userHomePath).resolve(".clipevery").resolve(configName) | ||
} | ||
|
||
override fun resolveApp(configName: String): Path { | ||
return Paths.get(userDir).resolve(configName) | ||
} | ||
} | ||
|
||
class LinuxPathProvider: PathProvider { | ||
|
||
override fun resolveUser(configName: String): Path { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun resolveApp(configName: String): Path { | ||
TODO("Not yet implemented") | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
composeApp/src/desktopMain/kotlin/com/clipevery/presist/DesktopOneFilePersist.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.clipevery.presist | ||
|
||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.serializer | ||
import java.nio.file.Path | ||
import kotlin.reflect.KClass | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
class DesktopOneFilePersist(private val path: Path) : OneFilePersist { | ||
override fun <T: Any> readAs(clazz: KClass<T>): T? { | ||
val file = path.toFile() | ||
return if (file.exists()) { | ||
val serializer = Json.serializersModule.serializer(clazz.java) | ||
Json.decodeFromString(serializer, file.readText()) as T | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
override fun <T> save(config: T) { | ||
val kClass = config!!::class | ||
val serializer = Json.serializersModule.serializer(kClass.java) | ||
val json = Json.encodeToString(serializer, config) | ||
val file = path.toFile() | ||
file.parentFile?.mkdirs() | ||
file.writeText(json) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
composeApp/src/desktopTest/kotlin/com/clipevery/path/PathProviderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.clipevery.path | ||
|
||
import org.junit.Test | ||
import kotlin.test.assertEquals | ||
|
||
class PathProviderTest { | ||
|
||
@Test | ||
fun testPathProvider() { | ||
val pathProvider = getPathProvider() | ||
|
||
val configPath = pathProvider.resolveUser("test.config") | ||
|
||
assertEquals("test.config", configPath.fileName.toString()) | ||
assertEquals(".clipevery", configPath.parent.fileName.toString()) | ||
} | ||
} |
3 changes: 1 addition & 2 deletions
3
...Test/kotlin/com/clipevery/PlatformTest.kt → ...in/com/clipevery/platform/PlatformTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters