mirror of
https://github.com/yattee/yattee.git
synced 2025-08-09 20:24:06 +00:00
move updating the time to PlayerModel
this makes the chapter view much much smoother
This commit is contained in:
@@ -131,7 +131,7 @@ final class PlayerModel: ObservableObject {
|
||||
@Default(.rotateToLandscapeOnEnterFullScreen) private var rotateToLandscapeOnEnterFullScreen
|
||||
#endif
|
||||
|
||||
@Published var currentChapter: Int?
|
||||
@Published var currentChapterIndex: Int?
|
||||
|
||||
var accounts: AccountsModel { .shared }
|
||||
var comments: CommentsModel { .shared }
|
||||
@@ -1114,4 +1114,36 @@ final class PlayerModel: ObservableObject {
|
||||
onPlayStream.forEach { $0(stream) }
|
||||
onPlayStream.removeAll()
|
||||
}
|
||||
|
||||
func updateTime(_ cmTime: CMTime) {
|
||||
let time = CMTimeGetSeconds(cmTime)
|
||||
let newChapterIndex = chapterForTime(time)
|
||||
if currentChapterIndex != newChapterIndex {
|
||||
DispatchQueue.main.async {
|
||||
self.currentChapterIndex = newChapterIndex
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func chapterForTime(_ time: Double) -> Int? {
|
||||
guard let chapters = self.videoForDisplay?.chapters else {
|
||||
return nil
|
||||
}
|
||||
|
||||
for (index, chapter) in chapters.enumerated() {
|
||||
let nextChapterStartTime = index < (chapters.count - 1) ? chapters[index + 1].start : nil
|
||||
|
||||
if let nextChapterStart = nextChapterStartTime {
|
||||
if time >= chapter.start, time < nextChapterStart {
|
||||
return index
|
||||
}
|
||||
} else {
|
||||
if time >= chapter.start {
|
||||
return index
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user