TelegramBotApiLibraries/cache/admins/micro_utils/src/commonMain/kotlin/dev/inmo/tgbotapi/libraries/cache/admins/micro_utils/DynamicAdminsCacheSettingsA...

32 lines
1.2 KiB
Kotlin
Raw Normal View History

2021-02-22 12:11:12 +00:00
package dev.inmo.tgbotapi.libraries.cache.admins.micro_utils
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.repos.*
import dev.inmo.tgbotapi.libraries.cache.admins.*
2022-11-10 15:25:31 +00:00
import dev.inmo.tgbotapi.types.IdChatIdentifier
2021-02-22 12:11:12 +00:00
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.*
class DynamicAdminsCacheSettingsAPI(
2022-11-10 15:25:31 +00:00
private val repo: KeyValueRepo<IdChatIdentifier, AdminsCacheSettings>,
2021-02-22 12:11:12 +00:00
private val scope: CoroutineScope
) : AdminsCacheSettingsAPI, MutableAdminsCacheSettingsAPI {
2022-11-10 15:25:31 +00:00
override val chatSettingsUpdatedFlow: SharedFlow<Pair<IdChatIdentifier, AdminsCacheSettings>>
2021-02-22 12:11:12 +00:00
get() = repo.onNewValue.shareIn(scope, SharingStarted.Eagerly)
2022-11-10 15:25:31 +00:00
override suspend fun setChatSettings(chatId: IdChatIdentifier, settings: AdminsCacheSettings) {
2021-02-22 12:11:12 +00:00
repo.set(chatId, settings)
}
2022-11-10 15:25:31 +00:00
override suspend fun getChatSettings(chatId: IdChatIdentifier): AdminsCacheSettings {
2021-02-22 12:11:12 +00:00
val settings = repo.get(chatId)
return if (settings == null) {
val newSettings = AdminsCacheSettings()
setChatSettings(chatId, newSettings)
newSettings
} else {
settings
}
}
2022-11-10 15:25:31 +00:00
}