temporal potentially working variant (bot not building

This commit is contained in:
2022-03-26 14:19:20 +06:00
parent 51cdfb320b
commit 37114f0ddb
17 changed files with 216 additions and 31 deletions

View File

@@ -14,9 +14,6 @@ val loader = AuthorizedModuleLoader {
WritePostsService::class
)
UIFSMStateSerializer.include("posts_create", PostsCreateUIFSMState.serializer())
UIFSMStateSerializer.include("posts_list", PostsListUIFSMState.serializer())
factory<PostCreateUIModel> { DefaultPostCreateUIModel(get(), get()) }
factory { PostCreateUIViewModel(get()) }
}

View File

@@ -1,9 +1,7 @@
package dev.inmo.postssystem.services.posts.client.ui.create
import dev.inmo.postssystem.features.common.common.ui.fsm.UIFSMState
import kotlinx.serialization.Serializable
@Serializable
data class PostsCreateUIFSMState(
override val from: UIFSMState? = null,
override val context: String = "main"

View File

@@ -1,9 +1,7 @@
package dev.inmo.postssystem.services.posts.client.ui.list
import dev.inmo.postssystem.features.common.common.ui.fsm.UIFSMState
import kotlinx.serialization.Serializable
@Serializable
data class PostsListUIFSMState(
override val from: UIFSMState? = null,
override val context: String = "main"

View File

@@ -1,6 +1,6 @@
package dev.inmo.postssystem.services.posts.client.ui.list
import dev.inmo.postssystem.features.posts.common.Post
import dev.inmo.postssystem.features.posts.common.RegisteredPostWithContent
import kotlinx.serialization.Serializable
@Serializable
@@ -10,7 +10,7 @@ sealed class PostsListUIState {
@Serializable
data class Show(
val posts: List<Post>
val posts: List<RegisteredPostWithContent>
) : PostsListUIState()
}

View File

@@ -0,0 +1,11 @@
package dev.inmo.postssystem.services.posts.client.ui.list
import dev.inmo.postssystem.features.common.common.ui.UIViewModel
import kotlinx.coroutines.flow.StateFlow
class PostsListUIViewModel(
private val model: PostsListUIModel
) : UIViewModel<PostsListUIState> {
override val currentState: StateFlow<PostsListUIState>
get() = model.currentState
}

View File

@@ -0,0 +1,90 @@
package dev.inmo.postssystem.services.posts.client
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import dev.inmo.jsuikit.elements.*
import dev.inmo.micro_utils.common.applyDiff
import dev.inmo.micro_utils.coroutines.compose.renderComposableAndLinkToContextAndRoot
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.fsm.common.StatesMachine
import dev.inmo.postssystem.features.auth.client.registerAfterAuthHandler
import dev.inmo.postssystem.features.common.common.*
import dev.inmo.postssystem.features.common.common.ui.DateTimeView
import dev.inmo.postssystem.features.common.common.ui.JSView
import dev.inmo.postssystem.features.common.common.ui.fsm.*
import dev.inmo.postssystem.features.content.client.ContentClientProvider
import dev.inmo.postssystem.features.posts.common.RegisteredPostWithContent
import dev.inmo.postssystem.services.posts.client.ui.create.PostsCreateUIFSMState
import dev.inmo.postssystem.services.posts.client.ui.list.*
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import org.w3c.dom.HTMLElement
val postsListViewLoader = DefaultModuleLoader {
factory { PostsListView(get(), getAllDistinct(), get(), getAllDistinct()) }
singleWithRandomQualifier <UIFSMHandler.Registrator> {
UIFSMHandler.Registrator {
registerAfterAuthHandler(getKoin(), PostsListView::class)
}
}
}
class PostsListView(
private val model: PostsListUIViewModel,
private val contentClientProviders: List<ContentClientProvider>,
private val uiScope: CoroutineScope,
defaultExceptionsHandlers: Iterable<UIFSMExceptionHandler>
) : JSView<PostsCreateUIFSMState>(defaultExceptionsHandlers) {
override suspend fun StatesMachine<in UIFSMState>.safeHandleState(
htmlElement: HTMLElement,
state: PostsCreateUIFSMState
): UIFSMState? {
val result = CompletableDeferred<UIFSMState?>()
val loadingState = mutableStateOf(true)
val postsState = mutableStateListOf<RegisteredPostWithContent>()
renderComposableAndLinkToContextAndRoot(htmlElement) {
if (loadingState.value) {
Spinner()
} else {
postsState.forEach {
Card(
header = {
CardTitle {
DateTimeView.Simple(it.post.creationDate)
}
Icon.App.Plus.drawAsButton {
uiScope.launchSafelyWithoutExceptions {
startChain(PostsCreateUIFSMState(state))
}
}
}
) {
it.content.forEach {
for (provider in contentClientProviders) {
if (provider.renderPreview(it)) {
break
}
}
}
}
}
}
}
model.currentState.subscribeSafelyWithoutExceptions(uiScope) {
loadingState.value = it == PostsListUIState.Loading
when (it) {
PostsListUIState.Loading -> {}
is PostsListUIState.Show -> {
postsState.applyDiff(it.posts)
}
}
}
return result.await()
}
}