diff --git a/Shared/Player/Video Details/ChapterView.swift b/Shared/Player/Video Details/ChapterView.swift index 34babd1b..4e24c966 100644 --- a/Shared/Player/Video Details/ChapterView.swift +++ b/Shared/Player/Video Details/ChapterView.swift @@ -12,7 +12,10 @@ import SwiftUI var showThumbnail: Bool var isCurrentChapter: Bool { - player.currentChapterIndex == chapterIndex + if let currentChapterIndex = player.currentChapterIndex { + return currentChapterIndex == chapterIndex + } + return false } var body: some View { diff --git a/Shared/Player/Video Details/ChaptersView.swift b/Shared/Player/Video Details/ChaptersView.swift index aae1031b..f43a4503 100644 --- a/Shared/Player/Video Details/ChaptersView.swift +++ b/Shared/Player/Video Details/ChaptersView.swift @@ -27,7 +27,22 @@ struct ChaptersView: View { .listStyle(.plain) #else ScrollView(.horizontal) { - LazyHStack(spacing: 20) { chapterViews(for: chapters[...]) }.padding(.horizontal, 15) + ScrollViewReader { scrollViewProxy in + LazyHStack(spacing: 20) { + chapterViews(for: chapters[...], scrollViewProxy: scrollViewProxy) + } + .padding(.horizontal, 15) + .onAppear { + if let currentChapterIndex = player.currentChapterIndex { + scrollViewProxy.scrollTo(currentChapterIndex, anchor: .center) + } + } + .onChange(of: player.currentChapterIndex) { currentChapterIndex in + if let index = currentChapterIndex { + scrollViewProxy.scrollTo(index, anchor: .center) + } + } + } } #endif } else if expand { @@ -65,10 +80,11 @@ struct ChaptersView: View { } #if !os(tvOS) - private func chapterViews(for chaptersToShow: ArraySlice, opacity: Double = 1.0, clickable: Bool = true) -> some View { + private func chapterViews(for chaptersToShow: ArraySlice, opacity: Double = 1.0, clickable: Bool = true, scrollViewProxy: ScrollViewProxy? = nil) -> some View { ForEach(Array(chaptersToShow.indices), id: \.self) { index in let chapter = chaptersToShow[index] ChapterView(chapter: chapter, chapterIndex: index, showThumbnail: showThumbnails) + .id(index) .opacity(index == 0 ? 1.0 : opacity) .allowsHitTesting(clickable) }