Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/anilist/src/main/graphql/fragments/User.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fragment User on User {
}
}
mediaListOptions {
scoreFormat
animeList {
sectionOrder
}
Expand Down
3 changes: 3 additions & 0 deletions api/anilist/src/main/graphql/mutations/UpdateUser.graphql
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
mutation UpdateUser(
$profileColor: String,
$scoreFormat: ScoreFormat,
$animeListOptions: MediaListOptionsInput,
$mangaListOptions: MediaListOptionsInput,
) {
UpdateUser(
profileColor: $profileColor,
scoreFormat: $scoreFormat,
animeListOptions: $animeListOptions
mangaListOptions: $mangaListOptions
) {
options {
profileColor
}
mediaListOptions {
scoreFormat
animeList {
sectionOrder
}
Expand Down
4 changes: 2 additions & 2 deletions api/anilist/src/main/graphql/queries/UserQuery.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ query Viewer {
}
}

query UserMediaListQuery($userId: Int, $type: MediaType) {
query UserMediaListQuery($userId: Int, $type: MediaType, $scoreFormat: ScoreFormat) {
mediaListCollection: MediaListCollection(userId: $userId, type: $type) {
lists {
name
entries {
status
progress
score(format: POINT_10_DECIMAL)
score(format: $scoreFormat)
media {
...MediaTracking
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.imashnake.animite.api.anilist.sanitize.media.Media
import com.imashnake.animite.api.anilist.sanitize.profile.User
import com.imashnake.animite.api.anilist.type.MediaListOptionsInput
import com.imashnake.animite.api.anilist.type.MediaType
import com.imashnake.animite.api.anilist.type.ScoreFormat
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filter

Expand Down Expand Up @@ -42,12 +43,14 @@ class AnilistUserRepository(
type: MediaType?,
useNetwork: Boolean,
language: Media.Language = Media.Language.DEFAULT,
scoreFormat: ScoreFormat,
mediaListOrder: List<String>
): Flow<Result<User.MediaCollection>> {
return apolloClient.query(
UserMediaListQuery(
userId = Optional.presentIfNotNull(id),
type = Optional.presentIfNotNull(type)
type = Optional.presentIfNotNull(type),
scoreFormat = Optional.presentIfNotNull(scoreFormat)
)
)
.fetchPolicy(
Expand All @@ -57,18 +60,20 @@ class AnilistUserRepository(
)
.toFlow()
.filter { it.exception == null }
.asResult { User.MediaCollection(it, type, language, mediaListOrder) }
.asResult { User.MediaCollection(it, type, language, scoreFormat, mediaListOrder) }
}

fun updateUser(
profileColor: String? = null,
scoreFormat: ScoreFormat? = null,
animeSectionOrder: List<String>? = null,
mangaSectionOrder: List<String>? = null,
): Flow<Result<UpdateUserMutation.UpdateUser?>> {
return apolloClient
.mutation(
UpdateUserMutation(
profileColor = Optional.presentIfNotNull(profileColor),
scoreFormat = Optional.presentIfNotNull(scoreFormat),
animeListOptions = Optional.presentIfNotNull(
MediaListOptionsInput(
sectionOrder = Optional.presentIfNotNull(animeSectionOrder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.imashnake.animite.api.anilist.type.MediaSort
import com.imashnake.animite.api.anilist.type.MediaSource
import com.imashnake.animite.api.anilist.type.MediaStatus
import com.imashnake.animite.api.anilist.type.ScoreFormat
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList
Expand All @@ -32,6 +33,7 @@
import java.util.Locale
import kotlin.collections.mapNotNull
import kotlin.collections.orEmpty
import kotlin.math.roundToInt
import kotlin.time.Clock
import kotlin.time.Instant

Expand Down Expand Up @@ -737,6 +739,7 @@
query: MediaTracking,
progress: Int?,
score: Float?,
scoreFormat: ScoreFormat,
status: TrackingStatus,
language: Language
) : this(
Expand All @@ -755,28 +758,46 @@
format = query.format?.sanitize(),
segments = query.episodes ?: query.nextAiringEpisode?.episode ?: query.chapters,
progress = progress,
score = score?.let { Score(it) },
score = score?.let { Score(it, scoreFormat) },
status = status
)
}

@Immutable
data class Score(
val value: Float,
val color: Long,
val format: ScoreFormat,

Check warning

Code scanning / detekt

Public properties require documentation. Warning

The property format is missing documentation.
val color: Long

Check warning

Code scanning / detekt

Public properties require documentation. Warning

The property color is missing documentation.
) {
companion object {
private const val RED = 0xffff7770
private const val ORANGE = 0xffffc863
private const val LIME = 0xffb8ff70
private const val GREEN = 0xff63ff88

fun getColor(normalizedScore: Float) = when {

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function getColor is missing documentation.
normalizedScore < 5f -> RED

Check warning

Code scanning / detekt

Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers. Warning

This expression contains a magic number. Consider defining it to a well named constant.
normalizedScore < 7f -> ORANGE

Check warning

Code scanning / detekt

Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers. Warning

This expression contains a magic number. Consider defining it to a well named constant.
normalizedScore < 9f -> LIME

Check warning

Code scanning / detekt

Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers. Warning

This expression contains a magic number. Consider defining it to a well named constant.
else -> GREEN
}
}

internal constructor(score: Float) : this(
internal constructor(score: Float, scoreFormat: ScoreFormat) : this(
value = score,
color = when {
score < 5f -> RED
score < 7f -> ORANGE
score < 9f -> LIME
format = scoreFormat,
color = when(scoreFormat) {
ScoreFormat.POINT_100 -> getColor(score / 10f)

Check warning

Code scanning / detekt

Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers. Warning

This expression contains a magic number. Consider defining it to a well named constant.
ScoreFormat.POINT_10_DECIMAL,
ScoreFormat.POINT_10 -> getColor(score)
ScoreFormat.POINT_5 -> getColor(score * 2f)
ScoreFormat.POINT_3 -> when (score.roundToInt()) {
0 -> RED
1 -> ORANGE
2 -> LIME
3 -> GREEN
else -> GREEN
}
else -> GREEN
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.imashnake.animite.api.anilist.sanitize.profile.User.TrackingStatus.Companion.toTrackingStatus
import com.imashnake.animite.api.anilist.type.MediaListStatus
import com.imashnake.animite.api.anilist.type.MediaType
import com.imashnake.animite.api.anilist.type.ScoreFormat
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import kotlin.time.Duration.Companion.minutes
Expand Down Expand Up @@ -44,6 +45,8 @@
val banner: String?,
/** @see User.Options.profileColor */
val color: String?,
/** @see User.MediaListOptions.scoreFormat */
val scoreFormat: ScoreFormat?,

// region About
/** User Stats */
Expand Down Expand Up @@ -96,6 +99,7 @@
) {
internal constructor(
query: UserMediaListQuery.List,
scoreFormat: ScoreFormat,
language: Language
) : this(
name = query.name,
Expand All @@ -105,6 +109,7 @@
query = it?.media?.mediaTracking ?: return@mapNotNull null,
progress = it.progress,
score = it.score?.toFloat(),
scoreFormat = scoreFormat,
language = language,
status = it.status.toTrackingStatus(
type = it.media.mediaTracking.type?.name?.let { type ->
Expand All @@ -120,14 +125,15 @@
query: UserMediaListQuery.Data,
type: MediaType?,
language: Language,
scoreFormat: ScoreFormat,
mediaListOrder: List<String>
) : this(
type = type?.name?.let { Type.valueOf(it) } ?: Type.UNKNOWN,
namedLists = query.mediaListCollection?.lists.orEmpty().sortedBy {
mediaListOrder.indexOf(it?.name)
}.mapNotNull {
NamedTrackingList(it ?: return@mapNotNull null, language)
}.toImmutableList()
NamedTrackingList(it ?: return@mapNotNull null, scoreFormat, language)
}.toImmutableList(),
)
}

Expand Down Expand Up @@ -180,6 +186,7 @@
avatar = query.avatar?.large,
banner = query.bannerImage,
color = query.options?.profileColor,
scoreFormat = query.mediaListOptions?.scoreFormat,
// TODO: Replace with string resources.
stats = listOfNotNull(
query.statistics?.anime?.count?.toString()?.let {
Expand Down Expand Up @@ -247,7 +254,7 @@

fun String?.sanitize() = safeValueOf(this)

fun MediaListStatus?.toTrackingStatus(type: Type): TrackingStatus = when (this) {
fun MediaListStatus?.toTrackingStatus(type: Type) = when (this) {

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function toTrackingStatus is missing documentation.
MediaListStatus.CURRENT -> if (type == Type.ANIME) WATCHING else READING
MediaListStatus.PLANNING -> if (type == Type.ANIME) PLAN_TO_WATCH else PLAN_TO_READ
MediaListStatus.COMPLETED -> COMPLETED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ private const val SHOW_USER_DESCRIPTION = true
private const val USE_PROFILE_COLOR = true
private const val USE_EXPRESSIVE_PROGRESS_INDICATOR = true
private const val PROFILE_COLOR = "blue"
private const val SCORE_FORMAT = "POINT_10_DECIMAL"
private const val IS_DEV_OPTIONS_ENABLED = false
const val DEFAULT_MEDIA_LIST_ORDER = "[]"

Expand Down Expand Up @@ -112,6 +113,8 @@ class PreferencesRepository internal constructor(
// endregion

// region settings

// TODO: Expose and set enum value.
private val themeKey = stringPreferencesKey("theme")
val theme = dataStore.getValue(themeKey, DEFAULT_THEME_KEY)
suspend fun setTheme(theme: String) {
Expand All @@ -130,6 +133,7 @@ class PreferencesRepository internal constructor(
dataStore.setValue(isAmoledKey, isAmoled)
}

// TODO: Expose and set enum value.
private val densityKey = stringPreferencesKey("density")
val density = dataStore.getValue(densityKey, DEFAULT_DENSITY_KEY)
suspend fun setDensity(density: String) {
Expand All @@ -142,6 +146,7 @@ class PreferencesRepository internal constructor(
dataStore.setValue(isNsfwEnabledKey, isNsfwEnabled)
}

// TODO: Expose and set enum value.
private val languageKey = stringPreferencesKey("language")
val language = dataStore.getValue(languageKey, DEFAULT_LANGUAGE_KEY)
suspend fun setLanguage(language: String) {
Expand Down Expand Up @@ -178,6 +183,13 @@ class PreferencesRepository internal constructor(
dataStore.setValue(profileColorKey, profileColor)
}

// TODO: Expose and set enum value.
private val scoreFormatKey = stringPreferencesKey("score_format")
val scoreFormat = dataStore.getValue(scoreFormatKey, SCORE_FORMAT)
suspend fun setScoreFormat(scoreFormat: String?) {
dataStore.setValue(scoreFormatKey, scoreFormat)
}

// region developer options
private val isDevOptionsEnabledKey = booleanPreferencesKey("dev_options_enabled")
val isDevOptionsEnabled = dataStore.getValue(isDevOptionsEnabledKey, IS_DEV_OPTIONS_ENABLED)
Expand Down
1 change: 1 addition & 0 deletions profile/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ tasks.withType<KotlinCompile>().configureEach {
)
}

// TODO: Remove unused deps,
dependencies {
implementation(projects.api.anilist)
implementation(projects.api.preferences)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.imashnake.animite.api.anilist.AnilistUserRepository
import com.imashnake.animite.api.anilist.sanitize.media.Media
import com.imashnake.animite.api.anilist.type.MediaType
import com.imashnake.animite.api.anilist.type.ScoreFormat
import com.imashnake.animite.api.preferences.PreferencesRepository
import com.imashnake.animite.core.resource.Resource
import com.imashnake.animite.core.resource.Resource.Companion.asResource
Expand Down Expand Up @@ -49,6 +50,11 @@

val useProfileColor = preferencesRepository.useProfileColor.filterNotNull()
val profileColor = preferencesRepository.profileColor.filterNotNull()
val scoreFormat = preferencesRepository

Check warning

Code scanning / detekt

Public properties require documentation. Warning

The property scoreFormat is missing documentation.
.scoreFormat
.filterNotNull()
.map { ScoreFormat.safeValueOf(it) }

val useExpressiveProgressIndicator = preferencesRepository.useExpressiveProgressIndicator.filterNotNull()

val viewer = combine(
Expand All @@ -63,6 +69,7 @@
// Don't update with cache
if (useNetwork) {
preferencesRepository.setProfileColor(user.color)
preferencesRepository.setScoreFormat(user.scoreFormat?.name)
preferencesRepository.setAnimeListOrder(user.animeListOrder)
preferencesRepository.setMangaListOrder(user.mangaListOrder)
}
Expand All @@ -78,7 +85,8 @@
flow2 = combine(
preferencesRepository.viewerId.filterNotNull(),
preferencesRepository.language.filterNotNull(),
::Pair
scoreFormat,
::Triple
),
flow3 = preferencesRepository.animeListOrder.filterNotNull().map {
Json.decodeFromString<List<String>>(it)
Expand All @@ -90,7 +98,8 @@
type = MediaType.ANIME,
useNetwork = useNetwork,
language = Media.Language.valueOf(it.second.second),
mediaListOrder = it.third
scoreFormat = it.second.third,
mediaListOrder = it.third,
)
}.asResource().stateIn(
scope = viewModelScope,
Expand All @@ -103,7 +112,8 @@
flow2 = combine(
preferencesRepository.viewerId.filterNotNull(),
preferencesRepository.language.filterNotNull(),
::Pair
scoreFormat,
::Triple
),
flow3 = preferencesRepository.mangaListOrder.filterNotNull().map {
Json.decodeFromString<List<String>>(it)
Expand All @@ -115,6 +125,7 @@
type = MediaType.MANGA,
useNetwork = useNetwork,
language = Media.Language.valueOf(it.second.second),
scoreFormat = it.second.third,
mediaListOrder = it.third
)
}.asResource().stateIn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import kotlinx.collections.immutable.ImmutableList
fun MediaTab(
mediaCollection: User.MediaCollection?,
listVisibility: SnapshotStateMap<Int, Boolean>,
useExpressiveProgressIndicator: Boolean,
updateMediaListsOrder: (List<String>) -> Unit,
onNavigateToMediaItem: (MediaPage) -> Unit,
useExpressiveProgressIndicator: Boolean,
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
) {
Expand Down Expand Up @@ -65,9 +65,9 @@ private fun UserMediaLists(
type: Media.Small.Type,
lists: ImmutableList<User.MediaCollection.NamedTrackingList>,
listVisibility: SnapshotStateMap<Int, Boolean>,
useExpressiveProgressIndicator: Boolean,
updateMediaListsOrder: (List<String>) -> Unit,
onNavigateToMediaItem: (MediaPage) -> Unit,
useExpressiveProgressIndicator: Boolean,
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
) {
Expand Down
Loading
Loading