Skip to content

Commit

Permalink
update progress after edit
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 14, 2024
1 parent 999528b commit 60bf0bf
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
1 change: 1 addition & 0 deletions anilist/src/commonMain/graphql/EditMutation.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mutation EditMutation(
) {
progress,
progressVolumes,
repeat,
status,
startedAt {
year,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ interface MediumComponent : ContentHolderComponent {
val siteUrl: Flow<String>

val type: Flow<MediaType>
val listStatus: Flow<MediaListStatus>
val listStatus: StateFlow<MediaListStatus>

val dialog: Value<ChildSlot<DialogConfig, DialogComponent>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,12 @@ class MediumScreenComponent(
}

@OptIn(ExperimentalCoroutinesApi::class)
override val listStatus: Flow<MediaListStatus> = mediumSuccessState.mapLatest {
override val listStatus: MutableStateFlow<MediaListStatus> = mediumSuccessState.mapLatest {
it.medium.entry?.status ?: MediaListStatus.UNKNOWN__
}
}.mutableStateIn(
scope = ioScope(),
initialValue = initialMedium.entry?.status ?: MediaListStatus.UNKNOWN__
)

@OptIn(ExperimentalCoroutinesApi::class)
override val chapters: Flow<Int> = mediumSuccessState.mapLatest {
Expand Down Expand Up @@ -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<DialogConfig>()
@OptIn(ExperimentalCoroutinesApi::class)
override val dialog: Value<ChildSlot<DialogConfig, DialogComponent>> = childSlot(
Expand All @@ -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)
}
}
)
Expand Down Expand Up @@ -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 }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class EditState(
MediaListStatus.CURRENT
}
} else {
if (it == MediaListStatus.COMPLETED) {
if (it == MediaListStatus.COMPLETED || it == MediaListStatus.UNKNOWN__) {
MediaListStatus.CURRENT
} else {
it
Expand Down

0 comments on commit 60bf0bf

Please sign in to comment.