mirror of
https://github.com/yattee/yattee.git
synced 2025-01-06 01:20:31 +05:30
SponsorBlock segments loading improvement
This commit is contained in:
parent
4fab7c2c16
commit
de09f9dd52
@ -128,19 +128,29 @@ final class PlayerModel: ObservableObject {
|
|||||||
|
|
||||||
func upgradeToStream(_ stream: Stream) {
|
func upgradeToStream(_ stream: Stream) {
|
||||||
if !self.stream.isNil, self.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(
|
func playStream(
|
||||||
_ stream: Stream,
|
_ stream: Stream,
|
||||||
of video: Video,
|
of video: Video,
|
||||||
preservingTime: Bool = false
|
preservingTime: Bool = false,
|
||||||
|
upgrading: Bool = false
|
||||||
) {
|
) {
|
||||||
playerError = nil
|
playerError = nil
|
||||||
resetSegments()
|
if !upgrading {
|
||||||
sponsorBlock.loadSegments(videoID: video.videoID, categories: Defaults[.sponsorBlockCategories])
|
resetSegments()
|
||||||
comments.load()
|
|
||||||
|
sponsorBlock.loadSegments(
|
||||||
|
videoID: video.videoID,
|
||||||
|
categories: Defaults[.sponsorBlockCategories]
|
||||||
|
) { [weak self] in
|
||||||
|
if Defaults[.showChannelSubscribers] {
|
||||||
|
self?.loadCurrentItemChannelDetails()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let url = stream.singleAssetURL {
|
if let url = stream.singleAssetURL {
|
||||||
logger.info("playing stream with one asset\(stream.kind == .hls ? " (HLS)" : ""): \(url)")
|
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)
|
loadComposition(stream, of: video, preservingTime: preservingTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
updateCurrentArtwork()
|
if !upgrading {
|
||||||
|
updateCurrentArtwork()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func pauseOnPlayerDismiss() {
|
private func pauseOnPlayerDismiss() {
|
||||||
|
@ -7,7 +7,7 @@ import SwiftyJSON
|
|||||||
final class SponsorBlockAPI: ObservableObject {
|
final class SponsorBlockAPI: ObservableObject {
|
||||||
static let categories = ["sponsor", "selfpromo", "intro", "outro", "interaction", "music_offtopic"]
|
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 videoID: String?
|
||||||
@Published var segments = [Segment]()
|
@Published var segments = [Segment]()
|
||||||
@ -27,22 +27,27 @@ final class SponsorBlockAPI: ObservableObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadSegments(videoID: String, categories: Set<String>) {
|
func loadSegments(videoID: String, categories: Set<String>, completionHandler: @escaping () -> Void = {}) {
|
||||||
guard !skipSegmentsURL.isNil, self.videoID != videoID else {
|
guard !skipSegmentsURL.isNil, self.videoID != videoID else {
|
||||||
|
completionHandler()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
self.videoID = videoID
|
self.videoID = videoID
|
||||||
|
|
||||||
requestSegments(categories: categories)
|
requestSegments(categories: categories, completionHandler: completionHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func requestSegments(categories: Set<String>) {
|
private func requestSegments(categories: Set<String>, completionHandler: @escaping () -> Void = {}) {
|
||||||
guard let url = skipSegmentsURL, !categories.isEmpty else {
|
guard let url = skipSegmentsURL, !categories.isEmpty else {
|
||||||
return
|
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 {
|
switch response.result {
|
||||||
case let .success(value):
|
case let .success(value):
|
||||||
self.segments = JSON(value).arrayValue.map(SponsorBlockSegment.init).sorted { $0.end < $1.end }
|
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)")
|
self.logger.error("failed to load SponsorBlock segments: \(error.localizedDescription)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
completionHandler()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user