temporal update of markups system

This commit is contained in:
InsanusMokrassar 2020-08-05 00:23:30 +06:00
parent e35d578490
commit 8686e60fbc
5 changed files with 87 additions and 24 deletions

View File

@ -2,34 +2,26 @@ package com.insanusmokrassar.postssystem.markups.core
import com.insanusmokrassar.postssystem.core.post.PostId import com.insanusmokrassar.postssystem.core.post.PostId
typealias MarkupPluginName = String
typealias MarkupPluginIdentifier = String
/** /**
* That is the plugin for platform, which is able to be drawn on the [MarkupTarget]. * That is the plugin for platform, which is able to be drawn on the [MarkupTarget].
* *
* * Plugin could be some Telegram plugin * * Plugin could be some Telegram plugin
* * [MarkupTarget] could be web. * * [MarkupTarget] could be web.
*/ */
interface MarkupPlugin<MarkupTarget> { interface MarkupPlugin {
/** /**
* This name will be used in `select`-questions on site * This name will be used in `select`-questions on site
*/ */
val name: String val pluginName: MarkupPluginName
val pluginId: MarkupPluginIdentifier
get() = pluginName
/** /**
* Link to icon which will be used for links onto this plugin * Link to icon which will be used for links onto this plugin
*/ */
val iconLink: String? val iconLink: String?
/**
* Drawing on [MarkupTarget].
*
* As a result, plugin must use [MarkupTarget] as a canvas to create the builder of markup. This fun
* will be implemented by creators like:
*
* * Telegram
* * Web
* * etc.
*
* For example, it could be some creator like "TelegramForHTMLMarkupBuilder"
*/
suspend fun drawMarkupBuilder(on: MarkupTarget, postId: PostId)
} }

View File

@ -0,0 +1,47 @@
package com.insanusmokrassar.postssystem.markups.core
import com.insanusmokrassar.postssystem.markups.core.utils.*
import com.insanusmokrassar.postssystem.utils.repos.ReadStandardCRUDRepo
import com.insanusmokrassar.postssystem.utils.repos.pagination.*
import kotlin.math.min
abstract class MarkupsSystem(
protected val plugins: List<MarkupPlugin>
) : ReadStandardCRUDRepo<MarkupPlugin, MarkupPluginIdentifier> {
private val pluginsMap: Map<MarkupPluginIdentifier, MarkupPlugin> = plugins.associateBy { it.pluginId }
override suspend fun contains(id: MarkupPluginIdentifier): Boolean = pluginsMap.keys.contains(id)
override suspend fun getById(id: MarkupPluginIdentifier): MarkupPlugin? = pluginsMap[id]
override suspend fun getByPagination(pagination: Pagination): PaginationResult<MarkupPlugin> {
if (pagination.firstIndex > plugins.lastIndex) {
return emptyList<MarkupPlugin>().createPaginationResult(
pagination,
plugins.size.toLong()
)
}
val lastExclusiveIndex = min(pagination.lastExclusiveIndex, plugins.size)
return plugins.subList(pagination.firstIndex, lastExclusiveIndex).createPaginationResult(
pagination,
plugins.size.toLong()
)
}
fun drawPluginsNamedList(
listBuilder: ListBuilder
) {
listBuilder.createLinkedItems(
plugins.map {
it.pluginName to it.pluginId
}
)
}
fun drawPluginsIconsList(
listBuilder: ListBuilder
) {
listBuilder.createLinkedIconItems(
plugins.mapNotNull {
(it.iconLink ?: return@mapNotNull null) to it.pluginId
}
)
}
}

View File

@ -0,0 +1,18 @@
package com.insanusmokrassar.postssystem.markups.core.utils
import com.insanusmokrassar.postssystem.markups.core.MarkupPluginIdentifier
import com.insanusmokrassar.postssystem.markups.core.MarkupPluginName
typealias TitledListItem = Pair<MarkupPluginName, MarkupPluginIdentifier>
typealias IconLinkListItem = Pair<String, MarkupPluginIdentifier>
interface ListBuilder {
/**
* Accept the list of titled items with their ids
*/
fun createLinkedItems(data: List<TitledListItem>)
/**
* Accept the list of items with their icons links and ids
*/
fun createLinkedIconItems(data: List<IconLinkListItem>)
}

View File

@ -4,8 +4,7 @@ import com.insanusmokrassar.postssystem.utils.repos.pagination.Pagination
import com.insanusmokrassar.postssystem.utils.repos.pagination.PaginationResult import com.insanusmokrassar.postssystem.utils.repos.pagination.PaginationResult
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
interface ReadStandardCRUDRepo<ObjectType, IdType> : interface ReadStandardCRUDRepo<ObjectType, IdType> : Repo {
Repo {
suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType> suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType>
suspend fun getById(id: IdType): ObjectType? suspend fun getById(id: IdType): ObjectType?
suspend fun contains(id: IdType): Boolean suspend fun contains(id: IdType): Boolean
@ -17,8 +16,7 @@ val <IdType> UpdatedValuePair<IdType, *>.id
val <ValueType> UpdatedValuePair<*, ValueType>.value val <ValueType> UpdatedValuePair<*, ValueType>.value
get() = second get() = second
interface WriteStandardCRUDRepo<ObjectType, IdType, InputValueType> : interface WriteStandardCRUDRepo<ObjectType, IdType, InputValueType> : Repo {
Repo {
val newObjectsFlow: Flow<ObjectType> val newObjectsFlow: Flow<ObjectType>
val updatedObjectsFlow: Flow<ObjectType> val updatedObjectsFlow: Flow<ObjectType>
val deletedObjectsIdsFlow: Flow<IdType> val deletedObjectsIdsFlow: Flow<IdType>
@ -29,6 +27,5 @@ interface WriteStandardCRUDRepo<ObjectType, IdType, InputValueType> :
suspend fun deleteById(vararg ids: IdType) suspend fun deleteById(vararg ids: IdType)
} }
interface StandardCRUDRepo<ObjectType, IdType, InputValueType> interface StandardCRUDRepo<ObjectType, IdType, InputValueType> : ReadStandardCRUDRepo<ObjectType, IdType>,
: ReadStandardCRUDRepo<ObjectType, IdType>,
WriteStandardCRUDRepo<ObjectType, IdType, InputValueType> WriteStandardCRUDRepo<ObjectType, IdType, InputValueType>

View File

@ -27,6 +27,15 @@ interface Pagination {
val Pagination.firstIndex: Int val Pagination.firstIndex: Int
get() = page * size get() = page * size
/**
* Last exclusive number in index of objects
*
* For [Pagination] with [Pagination.size] == 10 and [Pagination.page] == 1
* you will retrieve [Pagination.firstIndex] == 10 and [Pagination.lastExclusiveIndex] == 20.
*/
val Pagination.lastExclusiveIndex: Int
get() = firstIndex + size
/** /**
* Last number in index of objects. In fact, one [Pagination] object represent data in next range: * Last number in index of objects. In fact, one [Pagination] object represent data in next range:
* *
@ -34,7 +43,7 @@ val Pagination.firstIndex: Int
* you will retrieve [Pagination.firstIndex] == 10 and [Pagination.lastIndex] == 19. * you will retrieve [Pagination.firstIndex] == 10 and [Pagination.lastIndex] == 19.
*/ */
val Pagination.lastIndex: Int val Pagination.lastIndex: Int
get() = firstIndex + size - 1 get() = lastExclusiveIndex - 1
/** /**
* Calculates pages count for given [datasetSize] * Calculates pages count for given [datasetSize]