Skip to content

Commit

Permalink
prepare edit dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 10, 2024
1 parent e97509f commit 070e271
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 98 deletions.
3 changes: 2 additions & 1 deletion anilist/src/commonMain/graphql/AiringQuery.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ query AiringQuery(
}
},
mediaListEntry {
score(format: POINT_5)
score(format: POINT_5),
status
},
trailer {
id,
Expand Down
3 changes: 2 additions & 1 deletion anilist/src/commonMain/graphql/MediaListEntryQuery.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
query MediaListEntryQuery($id: Int) {
MediaList(mediaId: $id) {
score(format: POINT_5)
score(format: POINT_5),
status
}
}
3 changes: 2 additions & 1 deletion anilist/src/commonMain/graphql/MediumQuery.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ query MediumQuery($id: Int, $statusVersion: Int, $html: Boolean) {
}
},
mediaListEntry {
score(format: POINT_5)
score(format: POINT_5),
status
},
trailer {
id,
Expand Down
3 changes: 2 additions & 1 deletion anilist/src/commonMain/graphql/SeasonQuery.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ query SeasonQuery(
}
},
mediaListEntry {
score(format: POINT_5)
score(format: POINT_5),
status
},
trailer {
id,
Expand Down
3 changes: 2 additions & 1 deletion anilist/src/commonMain/graphql/TrendingQuery.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ query TrendingQuery(
}
},
mediaListEntry {
score(format: POINT_5)
score(format: POINT_5),
status
},
trailer {
id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package dev.datlag.aniflow.anilist.model
import dev.datlag.aniflow.anilist.*
import dev.datlag.aniflow.anilist.AdultContent
import dev.datlag.aniflow.anilist.common.lastMonth
import dev.datlag.aniflow.anilist.type.MediaFormat
import dev.datlag.aniflow.anilist.type.MediaRankType
import dev.datlag.aniflow.anilist.type.MediaStatus
import dev.datlag.aniflow.anilist.type.MediaType
import dev.datlag.aniflow.anilist.type.*
import dev.datlag.aniflow.model.ifValue
import dev.datlag.aniflow.model.toInt
import kotlinx.datetime.Clock
Expand Down Expand Up @@ -379,22 +376,27 @@ data class Medium(

@Serializable
data class Entry(
val score: Double?
val score: Double?,
val status: MediaListStatus
) {
constructor(entry: MediumQuery.MediaListEntry) : this(
score = entry.score
score = entry.score,
status = entry.status ?: MediaListStatus.UNKNOWN__
)

constructor(entry: TrendingQuery.MediaListEntry) : this(
score = entry.score
score = entry.score,
status = entry.status ?: MediaListStatus.UNKNOWN__
)

constructor(entry: AiringQuery.MediaListEntry) : this(
score = entry.score
score = entry.score,
status = entry.status ?: MediaListStatus.UNKNOWN__
)

constructor(entry: SeasonQuery.MediaListEntry) : this(
score = entry.score
score = entry.score,
status = entry.status ?: MediaListStatus.UNKNOWN__
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package dev.datlag.aniflow.common

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import dev.datlag.aniflow.LocalDI
import dev.datlag.aniflow.SharedRes
import dev.datlag.aniflow.anilist.model.Character
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.type.MediaFormat
import dev.datlag.aniflow.anilist.type.MediaRankType
import dev.datlag.aniflow.anilist.type.MediaStatus
import dev.datlag.aniflow.anilist.type.*
import dev.datlag.aniflow.settings.Settings
import dev.datlag.aniflow.settings.model.AppSettings
import dev.datlag.aniflow.trace.model.SearchResponse
Expand Down Expand Up @@ -149,4 +149,26 @@ fun SearchResponse.Result.AniList.asMedium(): Medium {
_isAdult = this.isAdult,
title = this.title.asMediumTitle()
)
}
}

fun MediaListStatus.icon() = when (this) {
MediaListStatus.CURRENT -> Icons.Rounded.Edit
MediaListStatus.COMPLETED -> Icons.Rounded.Check
MediaListStatus.PAUSED -> Icons.Rounded.Pause
MediaListStatus.DROPPED -> Icons.Rounded.Close
MediaListStatus.PLANNING -> Icons.Rounded.WatchLater
MediaListStatus.REPEATING -> Icons.Rounded.Replay
else -> Icons.Rounded.Add
}

fun MediaListStatus.stringRes(isManga: Boolean) = when (this) {
MediaListStatus.CURRENT -> if (isManga) SharedRes.strings.reading else SharedRes.strings.watching
MediaListStatus.COMPLETED -> SharedRes.strings.completed
MediaListStatus.PAUSED -> SharedRes.strings.paused
MediaListStatus.DROPPED -> SharedRes.strings.dropped
MediaListStatus.PLANNING -> SharedRes.strings.planning
MediaListStatus.REPEATING -> SharedRes.strings.repeating
else -> SharedRes.strings.add
}

fun MediaListStatus.stringRes(type: MediaType) = this.stringRes(type == MediaType.MANGA)
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ data object NetworkModule {
}
bindSingleton<CharacterRepository> {
CharacterRepository(
client = instance(Constants.AniList.APOLLO_CLIENT),
fallbackClient = instance(Constants.AniList.FALLBACK_APOLLO_CLIENT),
client = instance<ApolloClient>(Constants.AniList.APOLLO_CLIENT).newBuilder().fetchPolicy(FetchPolicy.NetworkFirst).build(),
fallbackClient = instance<ApolloClient>(Constants.AniList.FALLBACK_APOLLO_CLIENT).newBuilder().fetchPolicy(FetchPolicy.NetworkFirst).build(),
)
}
bindSingleton<MediumRepository> {
MediumRepository(
client = instance(Constants.AniList.APOLLO_CLIENT),
fallbackClient = instance(Constants.AniList.FALLBACK_APOLLO_CLIENT)
client = instance<ApolloClient>(Constants.AniList.APOLLO_CLIENT).newBuilder().fetchPolicy(FetchPolicy.NetworkFirst).build(),
fallbackClient = instance<ApolloClient>(Constants.AniList.FALLBACK_APOLLO_CLIENT).newBuilder().fetchPolicy(FetchPolicy.NetworkFirst).build()
)
}
bindSingleton<TraceRepository> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ sealed class DialogConfig {
data class Character(
val initial: Char
) : DialogConfig()

@Serializable
data object Edit : DialogConfig()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import dev.datlag.aniflow.anilist.MediumRepository
import dev.datlag.aniflow.anilist.model.Character
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.type.MediaFormat
import dev.datlag.aniflow.anilist.type.MediaListStatus
import dev.datlag.aniflow.anilist.type.MediaStatus
import dev.datlag.aniflow.anilist.type.MediaType
import dev.datlag.aniflow.other.Series
import dev.datlag.aniflow.settings.model.AppSettings
import dev.datlag.aniflow.ui.navigation.Component
Expand Down Expand Up @@ -50,8 +52,8 @@ interface MediumComponent : ContentHolderComponent {
val isFavoriteBlocked: Flow<Boolean>
val siteUrl: Flow<String>

val bsAvailable: Boolean
val bsOptions: Flow<Collection<Series>>
val type: Flow<MediaType>
val listStatus: Flow<MediaListStatus>

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

Expand All @@ -64,4 +66,5 @@ interface MediumComponent : ContentHolderComponent {
fun descriptionTranslation(text: String?)
fun showCharacter(character: Character)
fun toggleFavorite()
fun edit()
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import dev.datlag.aniflow.LocalDI
import dev.datlag.aniflow.LocalHaze
import dev.datlag.aniflow.LocalPaddingValues
import dev.datlag.aniflow.SharedRes
import dev.datlag.aniflow.anilist.type.MediaListStatus
import dev.datlag.aniflow.anilist.type.MediaStatus
import dev.datlag.aniflow.common.*
import dev.datlag.aniflow.other.StateSaver
Expand All @@ -46,6 +47,7 @@ import dev.datlag.aniflow.ui.custom.EditFAB
import dev.datlag.aniflow.ui.navigation.screen.medium.component.*
import dev.datlag.tooling.decompose.lifecycle.collectAsStateWithLifecycle
import dev.icerock.moko.resources.compose.painterResource
import dev.icerock.moko.resources.compose.stringResource
import io.github.aakira.napier.Napier
import kotlinx.coroutines.launch
import org.kodein.di.instance
Expand Down Expand Up @@ -92,79 +94,24 @@ fun MediumScreen(component: MediumComponent) {
)
},
floatingActionButton = {
val userRating by component.rating.collectAsStateWithLifecycle(-1)
val ratingState = rememberUseCaseState()
val bsState = rememberUseCaseState()

val bsOptions by component.bsOptions.collectAsStateWithLifecycle(emptySet())

val alreadyAdded by component.alreadyAdded.collectAsStateWithLifecycle(
component.initialMedium.entry != null
)
val notReleased by component.status.mapCollect(component.initialMedium.status) {
it == MediaStatus.UNKNOWN__ || it == MediaStatus.NOT_YET_RELEASED
}

RatingDialog(
state = ratingState,
selection = RatingSelection(
onSelectRating = { rating, _ ->
component.rate(rating)
}
),
header = Header.Default(
title = "Rate this Anime",
icon = IconSource(Icons.Filled.Star)
),
body = RatingBody.Default(
bodyText = ""
),
config = RatingConfig(
ratingOptionsCount = 5,
ratingOptionsSelected = userRating.takeIf { it > 0 },
ratingZeroValid = true
)
)

OptionDialog(
state = bsState,
selection = OptionSelection.Single(
options = bsOptions.map {
Option(
titleText = it.title
)
},
onSelectOption = { option, _ ->
Napier.e("Selected: ${bsOptions.toList()[option]}")
}
),
header = Header.Default(
icon = IconSource(
painter = painterResource(SharedRes.images.bs)
),
title = "Connect with BS"
),
config = OptionConfig(
mode = DisplayMode.LIST
)
)

if (!notReleased) {
val uriHandler = LocalUriHandler.current
val userHelper by LocalDI.current.instance<UserHelper>()
val status by component.listStatus.collectAsStateWithLifecycle(component.initialMedium.entry?.status ?: MediaListStatus.UNKNOWN__)
val type by component.type.collectAsStateWithLifecycle(component.initialMedium.type)

EditFAB(
displayAdd = !alreadyAdded,
bsAvailable = component.bsAvailable,
expanded = listState.isScrollingUp(),
onBS = {
bsState.show()
},
onRate = {
uriHandler.openUri(userHelper.loginUrl)
ExtendedFloatingActionButton(
onClick = { component.edit() },
icon = {
Icon(
imageVector = status.icon(),
contentDescription = null,
)
},
onProgress = {
// ratingState.show()
text = {
Text(text = stringResource(status.stringRes(type)))
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import dev.datlag.aniflow.anilist.*
import dev.datlag.aniflow.anilist.model.Character
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.type.MediaFormat
import dev.datlag.aniflow.anilist.type.MediaListStatus
import dev.datlag.aniflow.anilist.type.MediaStatus
import dev.datlag.aniflow.anilist.type.MediaType
import dev.datlag.aniflow.common.*
Expand All @@ -26,6 +27,7 @@ import dev.datlag.aniflow.settings.model.AppSettings
import dev.datlag.aniflow.settings.model.CharLanguage
import dev.datlag.aniflow.ui.navigation.DialogComponent
import dev.datlag.aniflow.ui.navigation.screen.medium.dialog.character.CharacterDialogComponent
import dev.datlag.aniflow.ui.navigation.screen.medium.dialog.edit.EditDialogComponent
import dev.datlag.tooling.alsoTrue
import dev.datlag.tooling.async.suspendCatching
import dev.datlag.tooling.compose.ioDispatcher
Expand Down Expand Up @@ -70,7 +72,7 @@ class MediumScreenComponent(
override val isAdultAllowed: Flow<Boolean> = appSettings.adultContent

@OptIn(ExperimentalCoroutinesApi::class)
private val type: Flow<MediaType> = mediumSuccessState.mapLatest {
override val type: Flow<MediaType> = mediumSuccessState.mapLatest {
it.medium.type
}

Expand Down Expand Up @@ -178,14 +180,9 @@ class MediumScreenComponent(
it.medium.siteUrl
}

private val burningSeriesResolver by di.instance<BurningSeriesResolver>()

override val bsAvailable: Boolean
get() = burningSeriesResolver.isAvailable

@OptIn(ExperimentalCoroutinesApi::class)
override val bsOptions = title.mapLatest {
burningSeriesResolver.resolveByName(it.english, it.romaji)
override val listStatus: Flow<MediaListStatus> = mediumSuccessState.mapLatest {
it.medium.entry?.status ?: MediaListStatus.UNKNOWN__
}

private val dialogNavigation = SlotNavigation<DialogConfig>()
Expand All @@ -200,6 +197,12 @@ class MediumScreenComponent(
initialChar = config.initial,
onDismiss = dialogNavigation::dismiss
)
is DialogConfig.Edit -> EditDialogComponent(
componentContext = context,
di = di,
titleFlow = title,
onDismiss = dialogNavigation::dismiss
)
}
}

Expand Down Expand Up @@ -294,4 +297,8 @@ class MediumScreenComponent(
aniListClient.mutation(mutation).execute()
}
}

override fun edit() {
dialogNavigation.activate(DialogConfig.Edit)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.datlag.aniflow.ui.navigation.screen.medium.dialog.edit

import dev.datlag.aniflow.other.Series
import dev.datlag.aniflow.ui.navigation.DialogComponent
import kotlinx.coroutines.flow.Flow

interface EditComponent : DialogComponent {

val bsAvailable: Boolean
val bsOptions: Flow<Collection<Series>>
}
Loading

0 comments on commit 070e271

Please sign in to comment.