add settings template

This commit is contained in:
InsanusMokrassar 2022-08-21 18:00:51 +06:00
parent aa9f187203
commit 463a590861
9 changed files with 119 additions and 0 deletions

View File

@ -6,6 +6,7 @@ String[] includes = [
":posts_registrar", ":posts_registrar",
":ratings", ":ratings",
":triggers:command", ":triggers:command",
":settings",
":runner" ":runner"
] ]

21
settings/build.gradle Normal file
View 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 {
}
}
}
}

View File

@ -0,0 +1 @@
package dev.inmo.plaguposter.settings

View 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>

View 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 { }
}

View 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>

View 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>

View 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)

View File

@ -0,0 +1 @@
<manifest package="dev.inmo.plaguposter.settings"/>