Skip to content

Commit

Permalink
Merge pull request #247 from imashnake0/user-manga
Browse files Browse the repository at this point in the history
User Manga Lists
  • Loading branch information
imashnake0 authored Jan 15, 2025
2 parents affdfc0 + 89677b6 commit f09dac9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
5 changes: 2 additions & 3 deletions api/anilist/src/main/graphql/UserQuery.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ query Viewer {
}
}

query UserMediaListQuery($userId: Int) {
# TODO: Support MANGA type.
mediaListCollection: MediaListCollection(userId: $userId, type: ANIME) {
query UserMediaListQuery($userId: Int, $type: MediaType) {
mediaListCollection: MediaListCollection(userId: $userId, type: $type) {
lists {
name
entries {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.apollographql.apollo3.api.Optional
import com.apollographql.apollo3.cache.normalized.FetchPolicy
import com.apollographql.apollo3.cache.normalized.fetchPolicy
import com.imashnake.animite.api.anilist.sanitize.profile.User
import com.imashnake.animite.api.anilist.type.MediaType
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

Expand All @@ -27,9 +28,14 @@ class AnilistUserRepository @Inject constructor(
}

/** @param id The id of the user. */
fun fetchUserMediaList(id: Int?): Flow<Result<User.MediaCollection>> =
fun fetchUserMediaList(id: Int?, type: MediaType?): Flow<Result<User.MediaCollection>> =
apolloClient
.query(UserMediaListQuery(Optional.presentIfNotNull(id)))
.query(
UserMediaListQuery(
userId = Optional.presentIfNotNull(id),
type = Optional.presentIfNotNull(type)
)
)
.fetchPolicy(FetchPolicy.CacheFirst)
.toFlow()
.asResult { User.MediaCollection(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ data class Media(
/** @see MediaSmall.id */
val id: Int,
/** @see MediaSmall.type */
val type: Type?,
val type: Type,
/** @see MediaSmall.title */
val title: String?,
/** @see MediaSmall.coverImage */
Expand All @@ -141,12 +141,13 @@ data class Media(
/** @see MediaSmall.type */
enum class Type(val type: String) {
ANIME("Anime"),
MANGA("Manga")
MANGA("Manga"),
UNKNOWN("Unknown"),
}

internal constructor(query: MediaSmall) : this(
id = query.id,
type = query.type?.name?.let { Type.valueOf(it) },
type = query.type?.name?.let { Type.valueOf(it) } ?: Type.UNKNOWN,
coverImage = query.coverImage?.extraLarge ?: query.coverImage?.large,
title = query.title?.romaji ?: query.title?.english ?: query.title?.native
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import com.imashnake.animite.core.ui.NestedScrollableContent
import com.imashnake.animite.core.ui.layouts.BannerLayout
import com.imashnake.animite.media.MediaPage
import com.imashnake.animite.profile.tabs.AboutTab
import com.imashnake.animite.profile.tabs.AnimeTab
import com.imashnake.animite.profile.tabs.MediaTab
import com.imashnake.animite.profile.tabs.ProfileTab
import kotlinx.coroutines.launch
import com.imashnake.animite.navigation.R as navigationR
Expand All @@ -62,7 +62,8 @@ fun ProfileScreen(
) {
val isLoggedIn by viewModel.isLoggedIn.collectAsState(initial = false)
val viewer by viewModel.viewer.collectAsState()
val viewerMediaLists by viewModel.viewerMediaList.collectAsState(initial = null)
val viewerAnimeLists by viewModel.viewerAnimeLists.collectAsState(initial = null)
val viewerMangaLists by viewModel.viewerMangaLists.collectAsState(initial = null)

Box(
contentAlignment = Alignment.Center,
Expand Down Expand Up @@ -115,7 +116,8 @@ fun ProfileScreen(
Spacer(Modifier.size(LocalPaddings.current.medium))
UserTabs(
user = this@run,
mediaCollection = viewerMediaLists?.data,
animeCollection = viewerAnimeLists?.data,
mangaCollection = viewerMangaLists?.data,
onNavigateToMediaItem = onNavigateToMediaItem,
sharedTransitionScope = sharedTransitionScope,
animatedVisibilityScope = animatedVisibilityScope,
Expand Down Expand Up @@ -159,7 +161,8 @@ private fun UserDescription(description: String?, modifier: Modifier = Modifier)
@Composable
private fun UserTabs(
user: User,
mediaCollection: User.MediaCollection?,
animeCollection: User.MediaCollection?,
mangaCollection: User.MediaCollection?,
onNavigateToMediaItem: (MediaPage) -> Unit,
sharedTransitionScope: SharedTransitionScope,
animatedVisibilityScope: AnimatedVisibilityScope,
Expand Down Expand Up @@ -219,8 +222,14 @@ private fun UserTabs(
Box(Modifier.fillMaxSize()) {
when (ProfileTab.entries[page]) {
ProfileTab.ABOUT -> AboutTab(user)
ProfileTab.ANIME -> AnimeTab(
mediaCollection = mediaCollection,
ProfileTab.ANIME -> MediaTab(
mediaCollection = animeCollection,
onNavigateToMediaItem = onNavigateToMediaItem,
sharedTransitionScope = sharedTransitionScope,
animatedVisibilityScope = animatedVisibilityScope,
)
ProfileTab.MANGA -> MediaTab(
mediaCollection = mangaCollection,
onNavigateToMediaItem = onNavigateToMediaItem,
sharedTransitionScope = sharedTransitionScope,
animatedVisibilityScope = animatedVisibilityScope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.navigation.toRoute
import com.imashnake.animite.api.anilist.AnilistUserRepository
import com.imashnake.animite.api.anilist.type.MediaType
import com.imashnake.animite.api.preferences.PreferencesRepository
import com.imashnake.animite.core.data.Resource
import com.imashnake.animite.core.data.Resource.Companion.asResource
Expand Down Expand Up @@ -44,9 +45,16 @@ class ProfileViewModel @Inject constructor(
}
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(1000), Resource.loading())

val viewerMediaList = preferencesRepository.viewerId.flatMapLatest {
val viewerAnimeLists = preferencesRepository.viewerId.flatMapLatest {
userRepository
.fetchUserMediaList(it?.toIntOrNull())
.fetchUserMediaList(it?.toIntOrNull(), MediaType.ANIME)
.asResource()
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(1000), Resource.loading())
}

val viewerMangaLists = preferencesRepository.viewerId.flatMapLatest {
userRepository
.fetchUserMediaList(it?.toIntOrNull(), MediaType.MANGA)
.asResource()
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(1000), Resource.loading())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.util.fastForEach
import com.imashnake.animite.api.anilist.sanitize.profile.User
import com.imashnake.animite.api.anilist.type.MediaType
import com.imashnake.animite.core.ui.LocalPaddings
import com.imashnake.animite.core.ui.MediaSmall
import com.imashnake.animite.core.ui.MediaSmallRow
Expand All @@ -25,7 +24,7 @@ import com.imashnake.animite.navigation.SharedContentKey.Component.Text
import com.imashnake.animite.core.R as coreR

@Composable
fun AnimeTab(
fun MediaTab(
mediaCollection: User.MediaCollection?,
onNavigateToMediaItem: (MediaPage) -> Unit,
sharedTransitionScope: SharedTransitionScope,
Expand Down Expand Up @@ -76,7 +75,7 @@ private fun UserMediaList(
id = media.id,
// TODO: We can use the list's index instead.
source = namedList.name.orEmpty(),
mediaType = MediaType.ANIME.rawValue,
mediaType = media.type.name,
)
)
},
Expand Down

0 comments on commit f09dac9

Please sign in to comment.