mirror of
https://github.com/yattee/yattee.git
synced 2024-12-22 21:43:41 +00:00
move updating the time to PlayerModel
this makes the chapter view much much smoother
This commit is contained in:
parent
0d9c27319d
commit
d361ef01d4
@ -131,7 +131,7 @@ final class PlayerModel: ObservableObject {
|
|||||||
@Default(.rotateToLandscapeOnEnterFullScreen) private var rotateToLandscapeOnEnterFullScreen
|
@Default(.rotateToLandscapeOnEnterFullScreen) private var rotateToLandscapeOnEnterFullScreen
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@Published var currentChapter: Int?
|
@Published var currentChapterIndex: Int?
|
||||||
|
|
||||||
var accounts: AccountsModel { .shared }
|
var accounts: AccountsModel { .shared }
|
||||||
var comments: CommentsModel { .shared }
|
var comments: CommentsModel { .shared }
|
||||||
@ -1114,4 +1114,36 @@ final class PlayerModel: ObservableObject {
|
|||||||
onPlayStream.forEach { $0(stream) }
|
onPlayStream.forEach { $0(stream) }
|
||||||
onPlayStream.removeAll()
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,13 +6,12 @@ import SwiftUI
|
|||||||
#if !os(tvOS)
|
#if !os(tvOS)
|
||||||
struct ChapterView: View {
|
struct ChapterView: View {
|
||||||
var chapter: Chapter
|
var chapter: Chapter
|
||||||
var nextChapterStart: Double?
|
|
||||||
|
|
||||||
var chapterIndex: Int
|
var chapterIndex: Int
|
||||||
@ObservedObject private var player = PlayerModel.shared
|
@ObservedObject private var player = PlayerModel.shared
|
||||||
|
|
||||||
var isCurrentChapter: Bool {
|
var isCurrentChapter: Bool {
|
||||||
player.currentChapter == chapterIndex
|
player.currentChapterIndex == chapterIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
@ -31,7 +30,7 @@ import SwiftUI
|
|||||||
.receive(on: DispatchQueue.main)
|
.receive(on: DispatchQueue.main)
|
||||||
) { notification in
|
) { notification in
|
||||||
if let cmTime = notification.object as? CMTime {
|
if let cmTime = notification.object as? CMTime {
|
||||||
self.handleTimeUpdate(cmTime)
|
player.updateTime(cmTime)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -74,13 +73,6 @@ import SwiftUI
|
|||||||
static var thumbnailHeight: Double {
|
static var thumbnailHeight: Double {
|
||||||
thumbnailWidth / 1.7777
|
thumbnailWidth / 1.7777
|
||||||
}
|
}
|
||||||
|
|
||||||
private func handleTimeUpdate(_ cmTime: CMTime) {
|
|
||||||
let time = CMTimeGetSeconds(cmTime)
|
|
||||||
if time >= chapter.start, nextChapterStart == nil || time < nextChapterStart! {
|
|
||||||
player.currentChapter = chapterIndex
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
@ -144,7 +136,7 @@ struct ChapterView_Preview: PreviewProvider {
|
|||||||
ChapterViewTVOS(chapter: .init(title: "Chapter", start: 30))
|
ChapterViewTVOS(chapter: .init(title: "Chapter", start: 30))
|
||||||
.injectFixtureEnvironmentObjects()
|
.injectFixtureEnvironmentObjects()
|
||||||
#else
|
#else
|
||||||
ChapterView(chapter: .init(title: "Chapter", start: 30), nextChapterStart: nil, chapterIndex: 0)
|
ChapterView(chapter: .init(title: "Chapter", start: 30), chapterIndex: 0)
|
||||||
.injectFixtureEnvironmentObjects()
|
.injectFixtureEnvironmentObjects()
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -70,8 +70,7 @@ struct ChaptersView: View {
|
|||||||
private func chapterViews(for chaptersToShow: ArraySlice<Chapter>, opacity: Double = 1.0, clickable: Bool = true) -> some View {
|
private func chapterViews(for chaptersToShow: ArraySlice<Chapter>, opacity: Double = 1.0, clickable: Bool = true) -> some View {
|
||||||
ForEach(Array(chaptersToShow.indices), id: \.self) { index in
|
ForEach(Array(chaptersToShow.indices), id: \.self) { index in
|
||||||
let chapter = chaptersToShow[index]
|
let chapter = chaptersToShow[index]
|
||||||
let nextChapterStart: Double? = index < chaptersToShow.count - 1 ? chaptersToShow[index + 1].start : nil
|
ChapterView(chapter: chapter, chapterIndex: index)
|
||||||
ChapterView(chapter: chapter, nextChapterStart: nextChapterStart, chapterIndex: index)
|
|
||||||
.opacity(index == 0 ? 1.0 : opacity)
|
.opacity(index == 0 ? 1.0 : opacity)
|
||||||
.allowsHitTesting(clickable)
|
.allowsHitTesting(clickable)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user