Skip to content

Commit

Permalink
add indicator to show user if the stream is live or on-demand
Browse files Browse the repository at this point in the history
  • Loading branch information
fishcakeday committed Sep 26, 2023
1 parent dc863f8 commit 6d8ea7b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
24 changes: 24 additions & 0 deletions damus/Views/Video/DamusVideoPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions damus/Views/Video/DamusVideoPlayerViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ 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

private var cancellables = Set<AnyCancellable>()

private var videoSizeObserver: NSKeyValueObservation?
private var videoLiveObserver: NSKeyValueObservation?

private var is_scrolled_into_view = false {
didSet {
Expand Down Expand Up @@ -81,6 +83,7 @@ final class DamusVideoPlayerViewModel: ObservableObject {
.store(in: &cancellables)

setupVideoSizeObserver()
setupLiveObserver()
}

private func setupVideoSizeObserver() {
Expand All @@ -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
Expand Down Expand Up @@ -125,5 +140,6 @@ final class DamusVideoPlayerViewModel: ObservableObject {

deinit {
videoSizeObserver?.invalidate()
videoLiveObserver?.invalidate()
}
}

0 comments on commit 6d8ea7b

Please sign in to comment.