highlight current chapter when clicked on it

This commit is contained in:
Toni Förster 2023-11-29 00:31:53 +01:00
parent 7de702ad23
commit 982dca1846
No known key found for this signature in database
GPG Key ID: 292F3E5086C83FC7

View File

@ -22,6 +22,10 @@ import SwiftUI
var body: some View { var body: some View {
Button(action: { Button(action: {
player.backend.seek(to: chapter.start, seekType: .userInteracted) player.backend.seek(to: chapter.start, seekType: .userInteracted)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { // Introducing a delay to give the player a chance to skip to the chapter
PlayerTimeModel.shared.currentTime = CMTime(seconds: chapter.start, preferredTimescale: 1)
handleTimeUpdate(PlayerTimeModel.shared.currentTime)
}
}) { }) {
Group { Group {
verticalChapter verticalChapter
@ -30,13 +34,7 @@ import SwiftUI
} }
.buttonStyle(.plain) .buttonStyle(.plain)
.onReceive(PlayerTimeModel.shared.$currentTime) { cmTime in .onReceive(PlayerTimeModel.shared.$currentTime) { cmTime in
let time = CMTimeGetSeconds(cmTime) self.handleTimeUpdate(cmTime)
if time >= self.chapter.start, self.nextChapterStart == nil || time < self.nextChapterStart! {
player.currentChapterIndex = self.chapterIndex
if !player.playedChapters.contains(self.chapterIndex) {
player.playedChapters.append(self.chapterIndex)
}
}
} }
} }
@ -50,7 +48,7 @@ import SwiftUI
.lineLimit(3) .lineLimit(3)
.multilineTextAlignment(.leading) .multilineTextAlignment(.leading)
.font(.headline) .font(.headline)
.foregroundColor(isCurrentChapter ? .appRed : .primary) .foregroundColor(isCurrentChapter ? Color("AppRedColor") : .primary)
Text(chapter.start.formattedAsPlaybackTime(allowZero: true) ?? "") Text(chapter.start.formattedAsPlaybackTime(allowZero: true) ?? "")
.font(.system(.subheadline).monospacedDigit()) .font(.system(.subheadline).monospacedDigit())
.foregroundColor(.secondary) .foregroundColor(.secondary)
@ -78,6 +76,16 @@ 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.currentChapterIndex = chapterIndex
if !player.playedChapters.contains(chapterIndex) {
player.playedChapters.append(chapterIndex)
}
}
}
} }
#else #else