mirror of
https://github.com/InsanusMokrassar/PlaguPoster.git
synced 2024-11-25 08:58:45 +00:00
add settings template
This commit is contained in:
parent
aa9f187203
commit
463a590861
@ -6,6 +6,7 @@ String[] includes = [
|
||||
":posts_registrar",
|
||||
":ratings",
|
||||
":triggers:command",
|
||||
":settings",
|
||||
":runner"
|
||||
]
|
||||
|
||||
|
21
settings/build.gradle
Normal file
21
settings/build.gradle
Normal file
@ -0,0 +1,21 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
id "com.android.library"
|
||||
}
|
||||
|
||||
apply from: "$mppProjectWithSerializationPresetPath"
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
api project(":plaguposter.common")
|
||||
}
|
||||
}
|
||||
jvmMain {
|
||||
dependencies {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
1
settings/src/commonMain/kotlin/PackageInfo.kt
Normal file
1
settings/src/commonMain/kotlin/PackageInfo.kt
Normal file
@ -0,0 +1 @@
|
||||
package dev.inmo.plaguposter.settings
|
6
settings/src/commonMain/kotlin/repo/ReadSettingsRepo.kt
Normal file
6
settings/src/commonMain/kotlin/repo/ReadSettingsRepo.kt
Normal file
@ -0,0 +1,6 @@
|
||||
package dev.inmo.plaguposter.settings.repo
|
||||
|
||||
import dev.inmo.micro_utils.repos.ReadKeyValueRepo
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
|
||||
interface ReadSettingsRepo<T> : ReadKeyValueRepo<ChatId, T>
|
13
settings/src/commonMain/kotlin/repo/Set.kt
Normal file
13
settings/src/commonMain/kotlin/repo/Set.kt
Normal file
@ -0,0 +1,13 @@
|
||||
package dev.inmo.plaguposter.settings.repo
|
||||
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import kotlinx.serialization.json.buildJsonObject
|
||||
|
||||
suspend inline fun <reified T : Any> SettingsRepo.set(
|
||||
chatId: ChatId,
|
||||
settings: T
|
||||
) {
|
||||
val oldSettings = get(chatId) ?: buildJsonObject { }
|
||||
|
||||
|
||||
}
|
8
settings/src/commonMain/kotlin/repo/SettingsRepo.kt
Normal file
8
settings/src/commonMain/kotlin/repo/SettingsRepo.kt
Normal file
@ -0,0 +1,8 @@
|
||||
package dev.inmo.plaguposter.settings.repo
|
||||
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import kotlinx.serialization.SerializationStrategy
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
|
||||
interface SettingsRepo<T> : KeyValueRepo<ChatId, T>, ReadSettingsRepo<T>, WriteSettingsRepo<T>
|
8
settings/src/commonMain/kotlin/repo/WriteSettingsRepo.kt
Normal file
8
settings/src/commonMain/kotlin/repo/WriteSettingsRepo.kt
Normal file
@ -0,0 +1,8 @@
|
||||
package dev.inmo.plaguposter.settings.repo
|
||||
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.utils.RiskFeature
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
|
||||
interface WriteSettingsRepo<T> : WriteKeyValueRepo<ChatId, T>
|
60
settings/src/jvmMain/kotlin/exposed/ExposedSettingsRepo.kt
Normal file
60
settings/src/jvmMain/kotlin/exposed/ExposedSettingsRepo.kt
Normal file
@ -0,0 +1,60 @@
|
||||
package dev.inmo.plaguposter.settings.exposed
|
||||
|
||||
import dev.inmo.micro_utils.repos.KeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.mappers.withMapper
|
||||
import dev.inmo.plaguposter.settings.repo.SettingsRepo
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
|
||||
class ExposedSettingsRepo<T>(
|
||||
parent: KeyValueRepo<ChatId, T>
|
||||
) : SettingsRepo<T>, KeyValueRepo<ChatId, T> by parent
|
||||
|
||||
@OptIn(InternalSerializationApi::class)
|
||||
inline fun <reified T : Any> ExposedSettingsRepo(
|
||||
parent: KeyValueRepo<Long, String>,
|
||||
json: Json,
|
||||
serializer: KSerializer<T> = T::class.serializer()
|
||||
) = ExposedSettingsRepo<T>(
|
||||
parent.withMapper<ChatId, T, Long, String>(
|
||||
{ chatId },
|
||||
{ json.encodeToString(serializer, this) },
|
||||
{ ChatId(this) },
|
||||
{ json.decodeFromString(serializer, this) }
|
||||
)
|
||||
)
|
||||
|
||||
@OptIn(InternalSerializationApi::class)
|
||||
inline fun <reified T : Any> ExposedSettingsRepo(
|
||||
database: Database,
|
||||
json: Json,
|
||||
serializer: KSerializer<T> = T::class.serializer(),
|
||||
tableName: String = "settings_${T::class.simpleName!!}"
|
||||
) = ExposedSettingsRepo<T>(
|
||||
ExposedKeyValueRepo(
|
||||
database,
|
||||
{ long("chat_id") },
|
||||
{ text("settings") },
|
||||
tableName
|
||||
),
|
||||
json,
|
||||
serializer
|
||||
)
|
||||
|
||||
@OptIn(InternalSerializationApi::class)
|
||||
inline fun <reified T : Any> SettingsRepo(
|
||||
parent: KeyValueRepo<Long, String>,
|
||||
json: Json,
|
||||
serializer: KSerializer<T> = T::class.serializer()
|
||||
) = ExposedSettingsRepo<T>(parent, json, serializer)
|
||||
|
||||
@OptIn(InternalSerializationApi::class)
|
||||
inline fun <reified T : Any> SettingsRepo(
|
||||
database: Database,
|
||||
json: Json,
|
||||
serializer: KSerializer<T> = T::class.serializer(),
|
||||
tableName: String = "settings_${T::class.simpleName!!}"
|
||||
) = ExposedSettingsRepo(database, json, serializer, tableName)
|
1
settings/src/main/AndroidManifest.xml
Normal file
1
settings/src/main/AndroidManifest.xml
Normal file
@ -0,0 +1 @@
|
||||
<manifest package="dev.inmo.plaguposter.settings"/>
|
Loading…
Reference in New Issue
Block a user