From 60bf0bffc8bec814f926ca08f09e9b7a30f1ea42 Mon Sep 17 00:00:00 2001 From: DatLag Date: Tue, 14 May 2024 19:44:47 +0200 Subject: [PATCH] update progress after edit --- .../commonMain/graphql/EditMutation.graphql | 1 + .../screen/medium/MediumComponent.kt | 2 +- .../screen/medium/MediumScreenComponent.kt | 69 ++++++++++++++----- .../screen/medium/component/FABContent.kt | 2 +- .../screen/medium/dialog/edit/EditState.kt | 2 +- 5 files changed, 56 insertions(+), 20 deletions(-) diff --git a/anilist/src/commonMain/graphql/EditMutation.graphql b/anilist/src/commonMain/graphql/EditMutation.graphql index 60472e0..7659b95 100644 --- a/anilist/src/commonMain/graphql/EditMutation.graphql +++ b/anilist/src/commonMain/graphql/EditMutation.graphql @@ -12,6 +12,7 @@ mutation EditMutation( ) { progress, progressVolumes, + repeat, status, startedAt { year, diff --git a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/MediumComponent.kt b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/MediumComponent.kt index b31ba92..07d08d1 100644 --- a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/MediumComponent.kt +++ b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/MediumComponent.kt @@ -57,7 +57,7 @@ interface MediumComponent : ContentHolderComponent { val siteUrl: Flow val type: Flow - val listStatus: Flow + val listStatus: StateFlow val dialog: Value> diff --git a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/MediumScreenComponent.kt b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/MediumScreenComponent.kt index 60af7ff..d20f824 100644 --- a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/MediumScreenComponent.kt +++ b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/MediumScreenComponent.kt @@ -179,9 +179,12 @@ class MediumScreenComponent( } @OptIn(ExperimentalCoroutinesApi::class) - override val listStatus: Flow = mediumSuccessState.mapLatest { + override val listStatus: MutableStateFlow = mediumSuccessState.mapLatest { it.medium.entry?.status ?: MediaListStatus.UNKNOWN__ - } + }.mutableStateIn( + scope = ioScope(), + initialValue = initialMedium.entry?.status ?: MediaListStatus.UNKNOWN__ + ) @OptIn(ExperimentalCoroutinesApi::class) override val chapters: Flow = mediumSuccessState.mapLatest { @@ -221,6 +224,22 @@ class MediumScreenComponent( search.ifEmpty { default } } + @OptIn(ExperimentalCoroutinesApi::class) + private val watchProgress = mediumSuccessState.mapLatest { + it.medium.entry?.progress + }.mutableStateIn( + scope = ioScope(), + initialValue = initialMedium.entry?.progress + ) + + @OptIn(ExperimentalCoroutinesApi::class) + private val watchRepeat = mediumSuccessState.mapLatest { + it.medium.entry?.repeatCount + }.mutableStateIn( + scope = ioScope(), + initialValue = initialMedium.entry?.repeatCount + ) + private val dialogNavigation = SlotNavigation() @OptIn(ExperimentalCoroutinesApi::class) override val dialog: Value> = childSlot( @@ -238,28 +257,16 @@ class MediumScreenComponent( componentContext = context, di = di, episodes = episodes, - progress = mediumSuccessState.mapLatest { - it.medium.entry?.progress - }, + progress = watchProgress, listStatus = listStatus, - repeatCount = mediumSuccessState.mapLatest { - it.medium.entry?.repeatCount - }, + repeatCount = watchRepeat, episodeStartDate = mediumSuccessState.mapLatest { it.medium.startDate }, onDismiss = dialogNavigation::dismiss, onSave = { status, progress, repeat -> dialogNavigation.dismiss { - val call = mediumRepository.updateEditCall( - status = status, - progress = progress, - repeat = repeat - ).fetchPolicy(FetchPolicy.NetworkOnly) - - launchIO { - call.execute() - } + editSave(status, progress, repeat) } } ) @@ -301,6 +308,34 @@ class MediumScreenComponent( } } + private fun editSave( + state: MediaListStatus, + progress: Int, + repeat: Int, + ) { + val newData = mediumRepository + .updateEditCall( + status = state, + progress = progress, + repeat = repeat + ) + .fetchPolicy(FetchPolicy.NetworkOnly) + .toFlow() + .mapNotNull { + it.data?.SaveMediaListEntry + } + + launchIO { + listStatus.emitAll(newData.mapNotNull { it.status }) + } + launchIO { + watchProgress.emitAll(newData.mapNotNull { it.progress }) + } + launchIO { + watchRepeat.emitAll(newData.mapNotNull { it.repeat }) + } + } + override fun descriptionTranslation(text: String?) { translatedDescription.update { text } } diff --git a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/component/FABContent.kt b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/component/FABContent.kt index accf996..bc35f1c 100644 --- a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/component/FABContent.kt +++ b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/component/FABContent.kt @@ -62,7 +62,7 @@ fun FABContent( if (!notReleased) { val loggedIn by component.isLoggedIn.collectAsStateWithLifecycle(false) - val status by component.listStatus.collectAsStateWithLifecycle(component.initialMedium.entry?.status ?: MediaListStatus.UNKNOWN__) + val status by component.listStatus.collectAsStateWithLifecycle() val type by component.type.collectAsStateWithLifecycle(component.initialMedium.type) val uriHandler = LocalUriHandler.current val speedDialFABState = rememberSpeedDialState() diff --git a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/dialog/edit/EditState.kt b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/dialog/edit/EditState.kt index 85776dd..169a300 100644 --- a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/dialog/edit/EditState.kt +++ b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/dialog/edit/EditState.kt @@ -112,7 +112,7 @@ class EditState( MediaListStatus.CURRENT } } else { - if (it == MediaListStatus.COMPLETED) { + if (it == MediaListStatus.COMPLETED || it == MediaListStatus.UNKNOWN__) { MediaListStatus.CURRENT } else { it