From 6d8ea7bf52842689e726eaeaa32475daba5a8a68 Mon Sep 17 00:00:00 2001 From: Fishcake Date: Tue, 26 Sep 2023 15:43:02 +0900 Subject: [PATCH] add indicator to show user if the stream is live or on-demand --- damus/Views/Video/DamusVideoPlayer.swift | 24 +++++++++++++++++++ .../Video/DamusVideoPlayerViewModel.swift | 16 +++++++++++++ 2 files changed, 40 insertions(+) diff --git a/damus/Views/Video/DamusVideoPlayer.swift b/damus/Views/Video/DamusVideoPlayer.swift index bbca2a3758..9b08a61a85 100644 --- a/damus/Views/Video/DamusVideoPlayer.swift +++ b/damus/Views/Video/DamusVideoPlayer.swift @@ -43,6 +43,9 @@ struct DamusVideoPlayer: View { if model.has_audio { mute_button } + if model.is_live { + live_indicator + } } .onChange(of: centerY) { _ in update_is_visible(centerY: centerY) @@ -93,6 +96,27 @@ struct DamusVideoPlayer: View { } } } + + private var live_indicator: some View { + ZStack { + VStack { + HStack { + Capsule() + .fill(Color.black.opacity(0.5)) + .frame(width: 80, height: 30) + .overlay( + Text("LIVE") + .bold() + .foregroundColor(.red) + .padding(.horizontal) + ) + .padding([.top, .leading]) + Spacer() + } + Spacer() + } + } + } } struct DamusVideoPlayer_Previews: PreviewProvider { static var previews: some View { diff --git a/damus/Views/Video/DamusVideoPlayerViewModel.swift b/damus/Views/Video/DamusVideoPlayerViewModel.swift index 584b071250..803c3bac0a 100644 --- a/damus/Views/Video/DamusVideoPlayerViewModel.swift +++ b/damus/Views/Video/DamusVideoPlayerViewModel.swift @@ -31,6 +31,7 @@ final class DamusVideoPlayerViewModel: ObservableObject { let id = UUID() @Published var has_audio = false + @Published var is_live = false @Binding var video_size: CGSize? @Published var is_muted = true @Published var is_loading = true @@ -38,6 +39,7 @@ final class DamusVideoPlayerViewModel: ObservableObject { private var cancellables = Set() private var videoSizeObserver: NSKeyValueObservation? + private var videoLiveObserver: NSKeyValueObservation? private var is_scrolled_into_view = false { didSet { @@ -81,6 +83,7 @@ final class DamusVideoPlayerViewModel: ObservableObject { .store(in: &cancellables) setupVideoSizeObserver() + setupLiveObserver() } private func setupVideoSizeObserver() { @@ -94,6 +97,18 @@ final class DamusVideoPlayerViewModel: ObservableObject { }) } + private func setupLiveObserver() { + videoLiveObserver = player.currentItem?.observe(\.duration, options: [.new], changeHandler: { [weak self] (playerItem, change) in + guard let self = self else { return } + if let newDuration = change.newValue, newDuration != .zero { + DispatchQueue.main.async { + print("DEBUG:: newDuration: \(newDuration)") + self.is_live = newDuration == .indefinite + } + } + }) + } + private func load() async { if let meta = controller.metadata(for: url) { has_audio = meta.has_audio @@ -125,5 +140,6 @@ final class DamusVideoPlayerViewModel: ObservableObject { deinit { videoSizeObserver?.invalidate() + videoLiveObserver?.invalidate() } }