From 18cbbd3c9047e08c3abd32e44c056786665f2b66 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Wed, 21 Dec 2022 18:13:41 +0100 Subject: [PATCH] Fix crashes --- Model/CommentsModel.swift | 2 +- Model/Player/Backends/MPVClient.swift | 1 + Model/Player/PlayerModel.swift | 2 +- Model/Player/PlayerQueue.swift | 6 +++--- Model/Player/PlayerStreams.swift | 3 ++- Shared/OpenURLHandler.swift | 5 +++-- Shared/Videos/WatchView.swift | 4 +++- Shared/Views/ShareButton.swift | 20 ++++++++++++-------- 8 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Model/CommentsModel.swift b/Model/CommentsModel.swift index bcd77bf8..fdf904d9 100644 --- a/Model/CommentsModel.swift +++ b/Model/CommentsModel.swift @@ -42,7 +42,7 @@ final class CommentsModel: ObservableObject { firstPage = page.isNil || page!.isEmpty - player.playerAPI(video).comments(video.videoID, page: page)? + player.playerAPI(video)?.comments(video.videoID, page: page)? .load() .onSuccess { [weak self] response in if let page: CommentsPage = response.typedContent() { diff --git a/Model/Player/Backends/MPVClient.swift b/Model/Player/Backends/MPVClient.swift index 1e57dfe9..8f06b83d 100644 --- a/Model/Player/Backends/MPVClient.swift +++ b/Model/Player/Backends/MPVClient.swift @@ -438,6 +438,7 @@ final class MPVClient: ObservableObject { } private func setString(_ name: String, _ value: String) { + guard mpv != nil else { return } mpv_set_property_string(mpv, name, value) } diff --git a/Model/Player/PlayerModel.swift b/Model/Player/PlayerModel.swift index a6606a7d..2c5ea8a1 100644 --- a/Model/Player/PlayerModel.swift +++ b/Model/Player/PlayerModel.swift @@ -724,7 +724,7 @@ final class PlayerModel: ObservableObject { DispatchQueue.main.asyncAfter(deadline: .now() + 3) { [weak self] in guard let self else { return } - self.playerAPI(item.video).loadDetails(item, completionHandler: { newItem in + self.playerAPI(item.video)?.loadDetails(item, completionHandler: { newItem in guard newItem.videoID == self.autoplayItem?.videoID else { return } self.autoplayItem = newItem self.updateRemoteCommandCenter() diff --git a/Model/Player/PlayerQueue.swift b/Model/Player/PlayerQueue.swift index 99bc28a6..7aca5818 100644 --- a/Model/Player/PlayerQueue.swift +++ b/Model/Player/PlayerQueue.swift @@ -100,7 +100,7 @@ extension PlayerModel { InstancesModel.shared.forPlayer ?? accounts.current?.instance ?? InstancesModel.shared.all.first } - func playerAPI(_ video: Video) -> VideosAPI! { + func playerAPI(_ video: Video) -> VideosAPI? { guard let url = video.instanceURL else { return accounts.api } switch video.app { case .local: @@ -181,7 +181,7 @@ extension PlayerModel { let playTime = currentItem.shouldRestartPlaying ? CMTime.zero : time guard let video = newItem.video else { return } - playerAPI(video).loadDetails(currentItem, failureHandler: { self.videoLoadFailureHandler($0, video: self.currentItem.video) }) { newItem in + playerAPI(video)?.loadDetails(currentItem, failureHandler: { self.videoLoadFailureHandler($0, video: video) }) { newItem in self.playItem(newItem, at: playTime) } } @@ -228,7 +228,7 @@ extension PlayerModel { } if loadDetails { - playerAPI(item.video).loadDetails(item, failureHandler: { self.videoLoadFailureHandler($0, video: video) }) { [weak self] newItem in + playerAPI(item.video)?.loadDetails(item, failureHandler: { self.videoLoadFailureHandler($0, video: video) }) { [weak self] newItem in guard let self else { return } videoDetailsLoadHandler(newItem.video, newItem) diff --git a/Model/Player/PlayerStreams.swift b/Model/Player/PlayerStreams.swift index 5e50e204..4504b499 100644 --- a/Model/Player/PlayerStreams.swift +++ b/Model/Player/PlayerStreams.swift @@ -20,8 +20,9 @@ extension PlayerModel { guard let playerInstance else { return } + guard let api = playerAPI(video) else { return } logger.info("loading streams from \(playerInstance.description)") - fetchStreams(playerAPI(video).video(video.videoID), instance: playerInstance, video: video, onCompletion: onCompletion) + fetchStreams(api.video(video.videoID), instance: playerInstance, video: video, onCompletion: onCompletion) } private func fetchStreams( diff --git a/Shared/OpenURLHandler.swift b/Shared/OpenURLHandler.swift index 333a194d..0994770c 100644 --- a/Shared/OpenURLHandler.swift +++ b/Shared/OpenURLHandler.swift @@ -99,10 +99,11 @@ struct OpenURLHandler { Windows.main.open() #endif - player.videoBeingOpened = Video(app: accounts.current.app!, videoID: id) + let video = Video(app: accounts.current.app!, videoID: id) + player.videoBeingOpened = video player - .playerAPI(player.videoBeingOpened!) + .playerAPI(video)? .video(id) .load() .onSuccess { response in diff --git a/Shared/Videos/WatchView.swift b/Shared/Videos/WatchView.swift index 64e253d8..18ffe690 100644 --- a/Shared/Videos/WatchView.swift +++ b/Shared/Videos/WatchView.swift @@ -40,7 +40,9 @@ struct WatchView: View { if finished, let watch { PlayerModel.shared.removeWatch(watch) } else { - Watch.markAsWatched(videoID: watch?.videoID ?? videoID, account: AccountsModel.shared.current, duration: watch?.videoDuration ?? duration, context: backgroundContext) + if let account = AccountsModel.shared.current { + Watch.markAsWatched(videoID: watch?.videoID ?? videoID, account: account, duration: watch?.videoDuration ?? duration, context: backgroundContext) + } } FeedModel.shared.calculateUnwatchedFeed() diff --git a/Shared/Views/ShareButton.swift b/Shared/Views/ShareButton.swift index 76306212..0a046b60 100644 --- a/Shared/Views/ShareButton.swift +++ b/Shared/Views/ShareButton.swift @@ -47,7 +47,7 @@ struct ShareButton: View { private var instanceActions: some View { Group { Button(labelForShareURL(accounts.app.name)) { - if let url = player.playerAPI(contentItem.video).shareURL(contentItem) { + if let url = player.playerAPI(contentItem.video)?.shareURL(contentItem) { shareAction(url) } else { navigation.presentAlert( @@ -59,12 +59,16 @@ struct ShareButton: View { if contentItemIsPlayerCurrentVideo { Button(labelForShareURL(accounts.app.name, withTime: true)) { - shareAction( - player.playerAPI(player.currentVideo!).shareURL( - contentItem, - time: player.backend.currentTime - )! - ) + if let video = player.videoForDisplay, + let api = player.playerAPI(video) + { + shareAction( + api.shareURL( + contentItem, + time: player.backend.currentTime + )! + ) + } } } } @@ -93,7 +97,7 @@ struct ShareButton: View { } private var contentItemIsPlayerCurrentVideo: Bool { - contentItem.contentType == .video && contentItem.video?.videoID == player.currentVideo?.videoID + contentItem.contentType == .video && contentItem.video?.videoID == player.videoForDisplay?.videoID } @ViewBuilder private var remoteURLAction: some View {