Skip to content

Commit

Permalink
Fix subject search position sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Nov 3, 2024
1 parent bea1c14 commit 29a6205
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ value class RoleSet(
operator fun minus(other: RoleSet): RoleSet = RoleSet(delegate - other.delegate.toSet())
operator fun contains(role: Role): Boolean = role in delegate

fun indexOf(role: Role): Int = delegate.indexOf(role)

@Stable
companion object {
@Stable
Expand All @@ -40,10 +42,17 @@ value class RoleSet(
/**
* 过滤 [RelatedPersonInfo] 中的角色, 返回符合条件的 [RelatedPersonInfo]
*/
fun List<RelatedPersonInfo>.filter(roleSet: RoleSet): Sequence<RelatedPersonInfo> {
return asSequence().filter f@{ person ->
fun Sequence<RelatedPersonInfo>.filter(roleSet: RoleSet): Sequence<RelatedPersonInfo> {
return filter f@{ person ->
person.position in roleSet
}
}

fun Sequence<RelatedPersonInfo>.sortedWith(roleSet: RoleSet): Sequence<RelatedPersonInfo> {
return sortedBy { person ->
val index = roleSet.indexOf(person.position)
if (index == -1) Int.MAX_VALUE else index
}
}

typealias Role = PersonPosition
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,16 @@ class SubjectPreviewItemInfo(
)
}
val staff = relatedPersonList?.let {
val persons = relatedPersonList.asSequence()
.filter(roleSet)
.sortedWith(roleSet)
.take(4)
.toList()

if (persons.isEmpty()) return@let null

buildString {
append("制作: ")
val persons = relatedPersonList.filter(roleSet).take(4).toList()
persons.forEachIndexed { index, relatedPersonInfo ->
append(relatedPersonInfo.personInfo.displayName)
if (index != persons.lastIndex) {
Expand All @@ -84,7 +91,7 @@ class SubjectPreviewItemInfo(
}
}
}
val actors = characters?.let {
val actors = characters?.takeIf { it.isNotEmpty() }?.let {
buildString {
append("配音: ")

Expand Down

0 comments on commit 29a6205

Please sign in to comment.