Basic Algorithm Cleanup (#897)

This commit is contained in:
Ethan Pippin 2023-10-31 22:11:23 -06:00 committed by GitHub
parent a49b0edc68
commit 744029495a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 13 deletions

View File

@ -31,7 +31,7 @@ extension Array {
} }
func oneSatisfies(_ predicate: (Element) throws -> Bool) rethrows -> Bool { func oneSatisfies(_ predicate: (Element) throws -> Bool) rethrows -> Bool {
try first(where: predicate) != nil try contains(where: predicate)
} }
func prepending(_ element: Element) -> [Element] { func prepending(_ element: Element) -> [Element] {

View File

@ -165,36 +165,38 @@ extension BaseItemDto {
var fullChapterInfo: [ChapterInfo.FullInfo] { var fullChapterInfo: [ChapterInfo.FullInfo] {
guard let chapters else { return [] } guard let chapters else { return [] }
let ranges: [Range<Int>] = [] let ranges: [Range<Int>] = chapters
.appending(chapters.map(\.startTimeSeconds)) .map(\.startTimeSeconds)
.appending(runTimeSeconds + 1) .appending(runTimeSeconds + 1)
.adjacentPairs() .adjacentPairs()
.map { $0 ..< $1 } .map { $0 ..< $1 }
return chapters return zip(chapters, ranges)
.enumerated() .enumerated()
.map { index, chapterInfo in .map { i, zip in
let client = Container.userSession.callAsFunction().client
let parameters = Paths.GetItemImageParameters( let parameters = Paths.GetItemImageParameters(
maxWidth: 500, maxWidth: 500,
quality: 90, quality: 90,
imageIndex: index imageIndex: i
) )
let request = Paths.getItemImage( let request = Paths.getItemImage(
itemID: id ?? "", itemID: id ?? "",
imageType: ImageType.chapter.rawValue, imageType: ImageType.chapter.rawValue,
parameters: parameters parameters: parameters
) )
let imageURL = client.fullURL(with: request) let imageURL = Container
.userSession
.callAsFunction()
.client
.fullURL(with: request)
let range = ranges.first(where: { $0.first == chapterInfo.startTimeSeconds }) ?? startTimeSeconds ..< startTimeSeconds + 1 return .init(
chapterInfo: zip.0,
return ChapterInfo.FullInfo(
chapterInfo: chapterInfo,
imageSource: .init(url: imageURL), imageSource: .init(url: imageURL),
secondsRange: range secondsRange: zip.1
) )
} }
} }