From de09f9dd52b5806e2a16bb71f3889dba74d7261b Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Fri, 17 Dec 2021 20:55:52 +0100 Subject: [PATCH] SponsorBlock segments loading improvement --- Model/Player/PlayerModel.swift | 24 ++++++++++++++++++------ Model/SponsorBlock/SponsorBlockAPI.swift | 17 ++++++++++++----- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Model/Player/PlayerModel.swift b/Model/Player/PlayerModel.swift index ae6e9453..6fd8a14c 100644 --- a/Model/Player/PlayerModel.swift +++ b/Model/Player/PlayerModel.swift @@ -128,19 +128,29 @@ final class PlayerModel: ObservableObject { func upgradeToStream(_ stream: Stream) { if !self.stream.isNil, self.stream != stream { - playStream(stream, of: currentVideo!, preservingTime: true) + playStream(stream, of: currentVideo!, preservingTime: true, upgrading: true) } } func playStream( _ stream: Stream, of video: Video, - preservingTime: Bool = false + preservingTime: Bool = false, + upgrading: Bool = false ) { playerError = nil - resetSegments() - sponsorBlock.loadSegments(videoID: video.videoID, categories: Defaults[.sponsorBlockCategories]) - comments.load() + if !upgrading { + resetSegments() + + sponsorBlock.loadSegments( + videoID: video.videoID, + categories: Defaults[.sponsorBlockCategories] + ) { [weak self] in + if Defaults[.showChannelSubscribers] { + self?.loadCurrentItemChannelDetails() + } + } + } if let url = stream.singleAssetURL { logger.info("playing stream with one asset\(stream.kind == .hls ? " (HLS)" : ""): \(url)") @@ -154,7 +164,9 @@ final class PlayerModel: ObservableObject { loadComposition(stream, of: video, preservingTime: preservingTime) } - updateCurrentArtwork() + if !upgrading { + updateCurrentArtwork() + } } private func pauseOnPlayerDismiss() { diff --git a/Model/SponsorBlock/SponsorBlockAPI.swift b/Model/SponsorBlock/SponsorBlockAPI.swift index ef1019af..e26604e8 100644 --- a/Model/SponsorBlock/SponsorBlockAPI.swift +++ b/Model/SponsorBlock/SponsorBlockAPI.swift @@ -7,7 +7,7 @@ import SwiftyJSON final class SponsorBlockAPI: ObservableObject { static let categories = ["sponsor", "selfpromo", "intro", "outro", "interaction", "music_offtopic"] - let logger = Logger(label: "net.yattee.app.sb") + let logger = Logger(label: "stream.yattee.app.sb") @Published var videoID: String? @Published var segments = [Segment]() @@ -27,22 +27,27 @@ final class SponsorBlockAPI: ObservableObject { } } - func loadSegments(videoID: String, categories: Set) { + func loadSegments(videoID: String, categories: Set, completionHandler: @escaping () -> Void = {}) { guard !skipSegmentsURL.isNil, self.videoID != videoID else { + completionHandler() return } self.videoID = videoID - requestSegments(categories: categories) + requestSegments(categories: categories, completionHandler: completionHandler) } - private func requestSegments(categories: Set) { + private func requestSegments(categories: Set, completionHandler: @escaping () -> Void = {}) { guard let url = skipSegmentsURL, !categories.isEmpty else { return } - AF.request(url, parameters: parameters(categories: categories)).responseJSON { response in + AF.request(url, parameters: parameters(categories: categories)).responseJSON { [weak self] response in + guard let self = self else { + return + } + switch response.result { case let .success(value): self.segments = JSON(value).arrayValue.map(SponsorBlockSegment.init).sorted { $0.end < $1.end } @@ -56,6 +61,8 @@ final class SponsorBlockAPI: ObservableObject { self.logger.error("failed to load SponsorBlock segments: \(error.localizedDescription)") } + + completionHandler() } }