diff --git a/Extensions/Double+Format.swift b/Extensions/Double+Format.swift new file mode 100644 index 00000000..488823d8 --- /dev/null +++ b/Extensions/Double+Format.swift @@ -0,0 +1,17 @@ +import Foundation + +extension Double { + func formattedAsPlaybackTime() -> String? { + guard !isZero else { + return nil + } + + let formatter = DateComponentsFormatter() + + formatter.unitsStyle = .positional + formatter.allowedUnits = self >= (60 * 60) ? [.hour, .minute, .second] : [.minute, .second] + formatter.zeroFormattingBehavior = [.pad] + + return formatter.string(from: self) + } +} diff --git a/Model/Player/PlayerModel.swift b/Model/Player/PlayerModel.swift index 19d532b4..64b53ea4 100644 --- a/Model/Player/PlayerModel.swift +++ b/Model/Player/PlayerModel.swift @@ -28,7 +28,6 @@ final class PlayerModel: ObservableObject { @Published var queue = [PlayerQueueItem]() @Published var currentItem: PlayerQueueItem! @Published var live = false - @Published var time: CMTime? @Published var history = [PlayerQueueItem]() @@ -58,6 +57,18 @@ final class PlayerModel: ObservableObject { player.timeControlStatus == .playing } + var time: CMTime? { + currentItem?.playbackTime + } + + var playerItemDuration: CMTime? { + player.currentItem?.asset.duration + } + + var videoDuration: TimeInterval? { + currentItem?.duration ?? currentVideo?.length + } + func togglePlay() { isPlaying ? pause() : play() } @@ -78,8 +89,8 @@ final class PlayerModel: ObservableObject { player.pause() } - func playVideo(_ video: Video) { - savedTime = nil + func playVideo(_ video: Video, time: CMTime? = nil) { + savedTime = time shouldResumePlaying = true loadAvailableStreams(video) { streams in @@ -88,33 +99,32 @@ final class PlayerModel: ObservableObject { } self.streamSelection = stream - self.playStream(stream, of: video, forcePlay: true) + self.playStream(stream, of: video, preservingTime: !time.isNil) } } func upgradeToStream(_ stream: Stream) { if !self.stream.isNil, self.stream != stream { - playStream(stream, of: currentItem.video, preservingTime: true) + playStream(stream, of: currentVideo!, preservingTime: true) } } private func playStream( _ stream: Stream, of video: Video, - forcePlay: Bool = false, preservingTime: Bool = false ) { if let url = stream.singleAssetURL { logger.info("playing stream with one asset\(stream.kind == .hls ? " (HLS)" : ""): \(url)") - insertPlayerItem(stream, for: video, forcePlay: forcePlay, preservingTime: preservingTime) + insertPlayerItem(stream, for: video, preservingTime: preservingTime) } else { logger.info("playing stream with many assets:") logger.info("composition audio asset: \(stream.audioAsset.url)") logger.info("composition video asset: \(stream.videoAsset.url)") Task { - await self.loadComposition(stream, of: video, forcePlay: forcePlay, preservingTime: preservingTime) + await self.loadComposition(stream, of: video, preservingTime: preservingTime) } } } @@ -122,51 +132,58 @@ final class PlayerModel: ObservableObject { private func insertPlayerItem( _ stream: Stream, for video: Video, - forcePlay: Bool = false, preservingTime: Bool = false ) { let playerItem = playerItem(stream) - guard playerItem != nil else { return } attachMetadata(to: playerItem!, video: video, for: stream) + DispatchQueue.main.async { self.stream = stream self.composition = AVMutableComposition() } - shouldResumePlaying = forcePlay || isPlaying + let replaceItemAndSeek = { + self.player.replaceCurrentItem(with: playerItem) + self.seekToSavedTime { finished in + guard finished else { + return + } + self.savedTime = nil + DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { + self.play() + } + } + } + + let startPlaying = { + DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { + self.play() + } + } if preservingTime { - saveTime { - self.player.replaceCurrentItem(with: playerItem) - - self.seekToSavedTime { finished in - guard finished else { - return - } - DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { - forcePlay || self.shouldResumePlaying ? self.play() : self.pause() - self.shouldResumePlaying = false - } + if savedTime.isNil { + saveTime { + replaceItemAndSeek() + startPlaying() } + } else { + replaceItemAndSeek() + startPlaying() } } else { player.replaceCurrentItem(with: playerItem) - - DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { - forcePlay || self.shouldResumePlaying ? self.play() : self.pause() - self.shouldResumePlaying = false - } + startPlaying() } } private func loadComposition( _ stream: Stream, of video: Video, - forcePlay: Bool = false, preservingTime: Bool = false ) async { await loadCompositionAsset(stream.audioAsset, type: .audio, of: video) @@ -177,7 +194,7 @@ final class PlayerModel: ObservableObject { return } - insertPlayerItem(stream, for: video, forcePlay: forcePlay, preservingTime: preservingTime) + insertPlayerItem(stream, for: video, preservingTime: preservingTime) } private func loadCompositionAsset(_ asset: AVURLAsset, type: AVMediaType, of video: Video) async { @@ -274,6 +291,7 @@ final class PlayerModel: ObservableObject { @objc func itemDidPlayToEndTime() { if queue.isEmpty { + addCurrentItemToHistory() resetQueue() #if os(tvOS) avPlayerViewController!.dismiss(animated: true) { @@ -318,7 +336,8 @@ final class PlayerModel: ObservableObject { timeObserver = player.addPeriodicTimeObserver(forInterval: interval, queue: .main) { _ in self.currentRate = self.player.rate self.live = self.currentVideo?.live ?? false - self.time = self.player.currentTime() + self.currentItem?.playbackTime = self.player.currentTime() + self.currentItem?.videoDuration = self.player.currentItem?.asset.duration.seconds } } } diff --git a/Model/Player/PlayerQueue.swift b/Model/Player/PlayerQueue.swift index f30b6211..0a549e9a 100644 --- a/Model/Player/PlayerQueue.swift +++ b/Model/Player/PlayerQueue.swift @@ -37,12 +37,15 @@ extension PlayerModel { func playItem(_ item: PlayerQueueItem, video: Video? = nil) { currentItem = item + if currentItem.playbackTime.isNil { + currentItem.playbackTime = .zero + } if video != nil { currentItem.video = video! } - playVideo(currentItem.video) + playVideo(currentVideo!, time: item.playbackTime) } func advanceToNextItem() { @@ -54,6 +57,8 @@ extension PlayerModel { } func advanceToItem(_ newItem: PlayerQueueItem) { + addCurrentItemToHistory() + let item = remove(newItem)! loadDetails(newItem.video) { video in self.playItem(item, video: video) @@ -86,10 +91,11 @@ extension PlayerModel { @discardableResult func enqueueVideo( _ video: Video, play: Bool = false, + atTime: CMTime? = nil, prepending: Bool = false, videoDetailsLoadHandler: @escaping (Video, PlayerQueueItem) -> Void = { _, _ in } ) -> PlayerQueueItem? { - let item = PlayerQueueItem(video) + let item = PlayerQueueItem(video, playbackTime: atTime) queue.insert(item, at: prepending ? 0 : queue.endIndex) @@ -117,13 +123,23 @@ extension PlayerModel { } func addCurrentItemToHistory() { - if let item = currentItem, !history.contains(where: { $0.video.videoID == item.video.videoID }) { + if let item = currentItem { + if let index = history.firstIndex(where: { $0.video.videoID == item.video.videoID }) { + history.remove(at: index) + } + history.insert(item, at: 0) } } func playHistory(_ item: PlayerQueueItem) { - let newItem = enqueueVideo(item.video, prepending: true) + var time = item.playbackTime + + if item.shouldRestartPlaying { + time = .zero + } + + let newItem = enqueueVideo(item.video, atTime: time, prepending: true) advanceToItem(newItem!) diff --git a/Model/Player/PlayerQueueItem.swift b/Model/Player/PlayerQueueItem.swift index c067ad7f..806ddd1d 100644 --- a/Model/Player/PlayerQueueItem.swift +++ b/Model/Player/PlayerQueueItem.swift @@ -4,10 +4,28 @@ import Foundation struct PlayerQueueItem: Hashable, Identifiable { var id = UUID() var video: Video + var playbackTime: CMTime? + var videoDuration: TimeInterval? - init(_ video: Video) { + init(_ video: Video, playbackTime: CMTime? = nil, videoDuration: TimeInterval? = nil) { self.video = video + self.playbackTime = playbackTime + self.videoDuration = videoDuration } - var playerItems = [AVPlayerItem]() + var duration: TimeInterval { + videoDuration ?? video.length + } + + var shouldRestartPlaying: Bool { + guard let seconds = playbackTime?.seconds else { + return false + } + + return duration - seconds <= 10 + } + + func hash(into hasher: inout Hasher) { + hasher.combine(id) + } } diff --git a/Model/Player/PlayerStreams.swift b/Model/Player/PlayerStreams.swift index 3af74314..7cfb862b 100644 --- a/Model/Player/PlayerStreams.swift +++ b/Model/Player/PlayerStreams.swift @@ -112,6 +112,10 @@ extension PlayerModel { } func streamsSorter(_ lhs: Stream, _ rhs: Stream) -> Bool { - lhs.kind == rhs.kind ? (lhs.resolution.height > rhs.resolution.height) : (lhs.kind < rhs.kind) + if lhs.resolution.isNil || rhs.resolution.isNil { + return lhs.kind < rhs.kind + } + + return lhs.kind == rhs.kind ? (lhs.resolution.height > rhs.resolution.height) : (lhs.kind < rhs.kind) } } diff --git a/Model/Video.swift b/Model/Video.swift index aeb34b6f..8e5d69c6 100644 --- a/Model/Video.swift +++ b/Model/Video.swift @@ -72,20 +72,6 @@ struct Video: Identifiable, Equatable, Hashable { self.streams = streams } - var playTime: String? { - guard !length.isZero else { - return nil - } - - let formatter = DateComponentsFormatter() - - formatter.unitsStyle = .positional - formatter.allowedUnits = length >= (60 * 60) ? [.hour, .minute, .second] : [.minute, .second] - formatter.zeroFormattingBehavior = [.pad] - - return formatter.string(from: length) - } - var publishedDate: String? { (published.isEmpty || published == "0 seconds ago") ? nil : published } diff --git a/Pearvidious.xcodeproj/project.pbxproj b/Pearvidious.xcodeproj/project.pbxproj index af50a253..1491f8ef 100644 --- a/Pearvidious.xcodeproj/project.pbxproj +++ b/Pearvidious.xcodeproj/project.pbxproj @@ -265,6 +265,9 @@ 37BE0BDC26A2367F0092E2DB /* Player.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37BE0BDB26A2367F0092E2DB /* Player.swift */; }; 37C194C726F6A9C8005D3B96 /* RecentsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C194C626F6A9C8005D3B96 /* RecentsModel.swift */; }; 37C194C826F6A9C8005D3B96 /* RecentsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C194C626F6A9C8005D3B96 /* RecentsModel.swift */; }; + 37C3A241272359900087A57A /* Double+Format.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C3A240272359900087A57A /* Double+Format.swift */; }; + 37C3A242272359900087A57A /* Double+Format.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C3A240272359900087A57A /* Double+Format.swift */; }; + 37C3A243272359900087A57A /* Double+Format.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C3A240272359900087A57A /* Double+Format.swift */; }; 37C7A1D5267BFD9D0010EAD6 /* SponsorBlockSegment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C7A1D4267BFD9D0010EAD6 /* SponsorBlockSegment.swift */; }; 37C7A1D6267BFD9D0010EAD6 /* SponsorBlockSegment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C7A1D4267BFD9D0010EAD6 /* SponsorBlockSegment.swift */; }; 37C7A1D7267BFD9D0010EAD6 /* SponsorBlockSegment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C7A1D4267BFD9D0010EAD6 /* SponsorBlockSegment.swift */; }; @@ -473,6 +476,7 @@ 37BE0BD926A214630092E2DB /* PlayerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerViewController.swift; sourceTree = ""; }; 37BE0BDB26A2367F0092E2DB /* Player.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Player.swift; sourceTree = ""; }; 37C194C626F6A9C8005D3B96 /* RecentsModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecentsModel.swift; sourceTree = ""; }; + 37C3A240272359900087A57A /* Double+Format.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Double+Format.swift"; sourceTree = ""; }; 37C7A1D4267BFD9D0010EAD6 /* SponsorBlockSegment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SponsorBlockSegment.swift; sourceTree = ""; }; 37CC3F44270CE30600608308 /* PlayerQueueItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerQueueItem.swift; sourceTree = ""; }; 37CC3F4B270CFE1700608308 /* PlayerQueueView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerQueueView.swift; sourceTree = ""; }; @@ -799,6 +803,7 @@ children = ( 379775922689365600DD52A8 /* Array+Next.swift */, 376578842685429C00D4EA09 /* CaseIterable+Next.swift */, + 37C3A240272359900087A57A /* Double+Format.swift */, 37BA794E26DC3E0E002A0235 /* Int+Format.swift */, 377A20A82693C9A2002842B8 /* TypedContentAccessors.swift */, 3743CA51270F284F00E4D32B /* View+Borders.swift */, @@ -1446,6 +1451,7 @@ 37484C1D26FC83A400287258 /* InstancesSettingsView.swift in Sources */, 37BD07BB2698AB60003EBB87 /* AppSidebarNavigation.swift in Sources */, 37D4B0E42671614900C925CA /* PearvidiousApp.swift in Sources */, + 37C3A241272359900087A57A /* Double+Format.swift in Sources */, 37FB285E272225E800A57617 /* ContentItemView.swift in Sources */, 3797758B2689345500DD52A8 /* Store.swift in Sources */, 37732FF02703A26300F04329 /* ValidationStatusView.swift in Sources */, @@ -1502,6 +1508,7 @@ 37BD07BC2698AB60003EBB87 /* AppSidebarNavigation.swift in Sources */, 3748186F26A769D60084E870 /* DetailBadge.swift in Sources */, 372915E72687E3B900F5A35B /* Defaults.swift in Sources */, + 37C3A242272359900087A57A /* Double+Format.swift in Sources */, 37DD87C8271C9CFE0027CBF9 /* PlayerStreams.swift in Sources */, 376578922685490700D4EA09 /* PlaylistsView.swift in Sources */, 37484C2626FC83E000287258 /* InstanceFormView.swift in Sources */, @@ -1602,6 +1609,7 @@ 37DD87C9271C9CFE0027CBF9 /* PlayerStreams.swift in Sources */, 375168D82700FDB9008F96A6 /* Debounce.swift in Sources */, 37BA794126DB8F97002A0235 /* ChannelVideosView.swift in Sources */, + 37C3A243272359900087A57A /* Double+Format.swift in Sources */, 37AAF29226740715007FC770 /* Channel.swift in Sources */, 37EAD86D267B9C5600D9E01B /* SponsorBlockAPI.swift in Sources */, 37732FF22703A26300F04329 /* ValidationStatusView.swift in Sources */, diff --git a/Shared/Player/PlayerQueueRow.swift b/Shared/Player/PlayerQueueRow.swift index 7e3fdbd1..40183f21 100644 --- a/Shared/Player/PlayerQueueRow.swift +++ b/Shared/Player/PlayerQueueRow.swift @@ -25,7 +25,7 @@ struct PlayerQueueRow: View { } } } label: { - VideoBanner(video: item.video) + VideoBanner(video: item.video, playbackTime: item.playbackTime, videoDuration: item.videoDuration) } .buttonStyle(.plain) } diff --git a/Shared/Player/VideoDetails.swift b/Shared/Player/VideoDetails.swift index 6f1166a5..0ddf6b42 100644 --- a/Shared/Player/VideoDetails.swift +++ b/Shared/Player/VideoDetails.swift @@ -211,7 +211,7 @@ struct VideoDetails: View { var publishedDateSection: some View { Group { - if let video = player.currentItem.video { + if let video = player.currentVideo { HStack(spacing: 4) { if let published = video.publishedDate { Text(published) @@ -235,7 +235,7 @@ struct VideoDetails: View { var countsSection: some View { Group { - if let video = player.currentItem.video { + if let video = player.currentVideo { HStack { Spacer() diff --git a/Shared/Videos/VideoBanner.swift b/Shared/Videos/VideoBanner.swift index 0deb777c..29b7f13d 100644 --- a/Shared/Videos/VideoBanner.swift +++ b/Shared/Videos/VideoBanner.swift @@ -1,14 +1,30 @@ +import CoreMedia import Foundation import SDWebImageSwiftUI import SwiftUI struct VideoBanner: View { let video: Video + var playbackTime: CMTime? + var videoDuration: TimeInterval? + + init(video: Video, playbackTime: CMTime? = nil, videoDuration: TimeInterval? = nil) { + self.video = video + self.playbackTime = playbackTime + self.videoDuration = videoDuration + } var body: some View { - HStack(alignment: .center, spacing: 12) { - smallThumbnail + HStack(alignment: .top, spacing: 12) { + VStack(spacing: thumbnailStackSpacing) { + smallThumbnail + if !playbackTime.isNil { + ProgressView(value: progressViewValue, total: progressViewTotal) + .progressViewStyle(.linear) + .frame(maxWidth: thumbnailWidth) + } + } VStack(alignment: .leading, spacing: 4) { Text(video.title) .truncationMode(.middle) @@ -22,20 +38,29 @@ struct VideoBanner: View { Spacer() - if let time = video.playTime { + if let time = (videoDuration ?? video.length).formattedAsPlaybackTime() { Text(time) .fontWeight(.light) } } .foregroundColor(.secondary) } + .padding(.vertical, playbackTime.isNil ? 0 : 5) } .contentShape(Rectangle()) .buttonStyle(.plain) .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 100, alignment: .center) } - var smallThumbnail: some View { + private var thumbnailStackSpacing: Double { + #if os(tvOS) + 8 + #else + 3 + #endif + } + + private var smallThumbnail: some View { WebImage(url: video.thumbnailURL(quality: .medium)) .resizable() .placeholder { @@ -43,19 +68,35 @@ struct VideoBanner: View { } .indicator(.activity) #if os(tvOS) - .frame(width: 177, height: 100) + .frame(width: thumbnailWidth, height: 100) .mask(RoundedRectangle(cornerRadius: 12)) #else - .frame(width: 88, height: 50) + .frame(width: thumbnailWidth, height: 50) .mask(RoundedRectangle(cornerRadius: 6)) #endif } + + private var thumbnailWidth: Double { + #if os(tvOS) + 177 + #else + 88 + #endif + } + + private var progressViewValue: Double { + [playbackTime?.seconds, videoDuration].compactMap { $0 }.min() ?? 0 + } + + private var progressViewTotal: Double { + videoDuration ?? video.length + } } struct VideoBanner_Previews: PreviewProvider { static var previews: some View { VStack(spacing: 20) { - VideoBanner(video: Video.fixture) + VideoBanner(video: Video.fixture, playbackTime: CMTime(seconds: 400, preferredTimescale: 10000)) VideoBanner(video: Video.fixtureUpcomingWithoutPublishedOrViews) } .frame(maxWidth: 900) diff --git a/Shared/Videos/VideoCell.swift b/Shared/Videos/VideoCell.swift index f4d25ed7..fe47a929 100644 --- a/Shared/Videos/VideoCell.swift +++ b/Shared/Videos/VideoCell.swift @@ -104,13 +104,13 @@ struct VideoCell: View { .frame(minHeight: 180) #if os(tvOS) - if video.playTime != nil || video.live || video.upcoming { + if let time = video.length.formattedAsPlaybackTime() || video.live || video.upcoming { Spacer() VStack(alignment: .center) { Spacer() - if let time = video.playTime { + if let time = video.length.formattedAsPlaybackTime() { HStack(spacing: 4) { Image(systemName: "clock") Text(time) @@ -204,7 +204,7 @@ struct VideoCell: View { HStack(alignment: .top) { Spacer() - if let time = video.playTime { + if let time = video.length.formattedAsPlaybackTime() { DetailBadge(text: time, style: .prominent) } } diff --git a/Shared/Views/PlayerControlsView.swift b/Shared/Views/PlayerControlsView.swift index 20859382..ed8b6e7f 100644 --- a/Shared/Views/PlayerControlsView.swift +++ b/Shared/Views/PlayerControlsView.swift @@ -56,32 +56,46 @@ struct PlayerControlsView: View { .keyboardShortcut("o") #endif - Group { - if model.isPlaying { - Button(action: { - model.pause() - }) { - Label("Pause", systemImage: "pause.fill") + ZStack(alignment: .bottom) { + HStack { + Group { + if model.isPlaying { + Button(action: { + model.pause() + }) { + Label("Pause", systemImage: "pause.fill") + } + } else { + Button(action: { + model.play() + }) { + Label("Play", systemImage: "play.fill") + } + .disabled(model.player.currentItem.isNil) + } } - } else { - Button(action: { - model.play() - }) { - Label("Play", systemImage: "play.fill") - } - .disabled(model.player.currentItem.isNil) - } - } - .frame(minWidth: 30) - .scaleEffect(1.7) - #if !os(tvOS) - .keyboardShortcut("p") - #endif + .frame(minWidth: 30) + .scaleEffect(1.7) + #if !os(tvOS) + .keyboardShortcut("p") + #endif - Button(action: { model.advanceToNextItem() }) { - Label("Next", systemImage: "forward.fill") + Button(action: { model.advanceToNextItem() }) { + Label("Next", systemImage: "forward.fill") + } + .disabled(model.queue.isEmpty) + } + + ProgressView(value: progressViewValue, total: progressViewTotal) + .progressViewStyle(.linear) + #if os(iOS) + .offset(x: 0, y: 15) + .frame(maxWidth: 60) + #else + .offset(x: 0, y: 20) + .frame(maxWidth: 70) + #endif } - .disabled(model.queue.isEmpty) } .buttonStyle(.plain) .labelStyle(.iconOnly) @@ -97,6 +111,14 @@ struct PlayerControlsView: View { }) #endif } + + private var progressViewValue: Double { + [model.time?.seconds, model.videoDuration].compactMap { $0 }.min() ?? 0 + } + + private var progressViewTotal: Double { + model.playerItemDuration?.seconds ?? model.currentVideo?.length ?? progressViewValue + } } struct PlayerControlsView_Previews: PreviewProvider { diff --git a/tvOS/NowPlayingView.swift b/tvOS/NowPlayingView.swift index 7f15c375..1acc88f4 100644 --- a/tvOS/NowPlayingView.swift +++ b/tvOS/NowPlayingView.swift @@ -29,7 +29,6 @@ struct NowPlayingView: View { } } .onPlayPauseCommand(perform: player.togglePlay) - .padding(.bottom, 20) } @@ -72,7 +71,7 @@ struct NowPlayingView: View { player.playHistory(item) player.presentPlayer() } label: { - VideoBanner(video: item.video) + VideoBanner(video: item.video, playbackTime: item.playbackTime, videoDuration: item.videoDuration) } .contextMenu { Button("Delete", role: .destructive) {