From 95d3170d31297482fe186011191d34e5db9d18fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20F=C3=B6rster?= Date: Mon, 26 Aug 2024 00:46:35 +0200 Subject: [PATCH] fix regression and improve curentChapter handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit with #745 I left som testing changes in the PR that resulted in currentChapter index not being updated. This is fixed now. Also, the ScrollViewReader waiter 0.5s before jumping to the current Chapter. So it is always drawn correctly. Signed-off-by: Toni Förster --- Model/Player/Backends/MPVBackend.swift | 2 ++ .../Player/Video Details/ChaptersView.swift | 25 +++++++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Model/Player/Backends/MPVBackend.swift b/Model/Player/Backends/MPVBackend.swift index a79f95eb..991e01df 100644 --- a/Model/Player/Backends/MPVBackend.swift +++ b/Model/Player/Backends/MPVBackend.swift @@ -464,6 +464,8 @@ final class MPVBackend: PlayerBackend { timeObserverThrottle.execute { self.model.updateWatch(time: self.currentTime) } + + self.model.updateTime(self.currentTime!) } private func stopClientUpdates() { diff --git a/Shared/Player/Video Details/ChaptersView.swift b/Shared/Player/Video Details/ChaptersView.swift index 470eb902..7123dcbb 100644 --- a/Shared/Player/Video Details/ChaptersView.swift +++ b/Shared/Player/Video Details/ChaptersView.swift @@ -29,18 +29,14 @@ struct ChaptersView: View { ScrollView(.horizontal) { ScrollViewReader { scrollViewProxy in LazyHStack(spacing: 20) { - chapterViews(for: chapters[...], scrollViewProxy: scrollViewProxy) + chapterViews(for: chapters[...]) } .padding(.horizontal, 15) .onAppear { - if let currentChapterIndex = player.currentChapterIndex { - scrollViewProxy.scrollTo(currentChapterIndex, anchor: .center) - } + scrollToCurrentChapter(scrollViewProxy) } - .onChange(of: player.currentChapterIndex) { currentChapterIndex in - if let index = currentChapterIndex { - scrollViewProxy.scrollTo(index, anchor: .center) - } + .onChange(of: player.currentChapterIndex) { _ in + scrollToCurrentChapter(scrollViewProxy) } } } @@ -53,7 +49,8 @@ struct ChaptersView: View { } } #else - Section { chapterViews(for: chapters[...]) }.padding(.horizontal) + Section { chapterViews(for: chapters[...]) } + .padding(.horizontal) #endif } else { #if os(iOS) @@ -80,7 +77,7 @@ struct ChaptersView: View { } #if !os(tvOS) - private func chapterViews(for chaptersToShow: ArraySlice, opacity: Double = 1.0, clickable: Bool = true, scrollViewProxy _: ScrollViewProxy? = nil) -> some View { + private func chapterViews(for chaptersToShow: ArraySlice, opacity: Double = 1.0, clickable: Bool = true) -> some View { ForEach(Array(chaptersToShow.indices), id: \.self) { index in let chapter = chaptersToShow[index] ChapterView(chapter: chapter, chapterIndex: index, showThumbnail: showThumbnails) @@ -89,6 +86,14 @@ struct ChaptersView: View { .allowsHitTesting(clickable) } } + + private func scrollToCurrentChapter(_ scrollViewProxy: ScrollViewProxy) { + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { // Slight delay to ensure the view is fully rendered + if let currentChapterIndex = player.currentChapterIndex { + scrollViewProxy.scrollTo(currentChapterIndex, anchor: .center) + } + } + } #endif }