diff --git a/Example/VLCUIExample/Shared/ContentView.swift b/Example/VLCUIExample/Shared/ContentView.swift index 93e2057..47d61b4 100644 --- a/Example/VLCUIExample/Shared/ContentView.swift +++ b/Example/VLCUIExample/Shared/ContentView.swift @@ -9,7 +9,15 @@ struct ContentView: View { var body: some View { ZStack(alignment: .bottom) { VLCVideoPlayer(configuration: viewModel.configuration) - .delegate(viewModel) + .eventSubject(viewModel.eventSubject) + .onTicksUpdated { ticks, playbackInformation in + viewModel.ticks = ticks + viewModel.totalTicks = playbackInformation.length + viewModel.position = playbackInformation.position + } + .onStateUpdated { state, _ in + viewModel.playerState = state + } OverlayView(viewModel: viewModel) .padding() diff --git a/Example/VLCUIExample/Shared/ContentViewModel.swift b/Example/VLCUIExample/Shared/ContentViewModel.swift index 643968c..327f111 100644 --- a/Example/VLCUIExample/Shared/ContentViewModel.swift +++ b/Example/VLCUIExample/Shared/ContentViewModel.swift @@ -2,7 +2,7 @@ import Combine import Foundation import VLCUI -class ContentViewModel: ObservableObject, VLCVideoPlayerDelegate { +class ContentViewModel: ObservableObject { @Published var ticks: Int32 = 0 @@ -33,19 +33,4 @@ class ContentViewModel: ObservableObject, VLCVideoPlayerDelegate { func setCustomPosition(_ position: Float) { self.position = position } - - func vlcVideoPlayer(didUpdateTicks ticks: Int32, with playbackInformation: VLCVideoPlayer.PlaybackInformation) { - self.ticks = ticks - self.position = playbackInformation.position - - self.totalTicks = playbackInformation.length - } - - func vlcVideoPlayer(didUpdateState state: VLCVideoPlayer.State, with playbackInformation: VLCVideoPlayer.PlaybackInformation) { - self.playerState = state - - if state == .error { - print("An error has occurred") - } - } } diff --git a/Example/VLCUIExample/VLCUIExample.xcodeproj/project.pbxproj b/Example/VLCUIExample/VLCUIExample.xcodeproj/project.pbxproj index abe6b1d..d924c75 100644 --- a/Example/VLCUIExample/VLCUIExample.xcodeproj/project.pbxproj +++ b/Example/VLCUIExample/VLCUIExample.xcodeproj/project.pbxproj @@ -618,7 +618,7 @@ CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; DEVELOPMENT_ASSET_PATHS = ""; - DEVELOPMENT_TEAM = TY84JMYEFE; + DEVELOPMENT_TEAM = ""; ENABLE_BITCODE = NO; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; @@ -651,7 +651,7 @@ CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; DEVELOPMENT_ASSET_PATHS = ""; - DEVELOPMENT_TEAM = TY84JMYEFE; + DEVELOPMENT_TEAM = ""; ENABLE_BITCODE = NO; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; diff --git a/Sources/VLCUI/UIVLCVideoPlayerView.swift b/Sources/VLCUI/UIVLCVideoPlayerView.swift index eb39886..59fefc0 100644 --- a/Sources/VLCUI/UIVLCVideoPlayerView.swift +++ b/Sources/VLCUI/UIVLCVideoPlayerView.swift @@ -23,7 +23,9 @@ public class UIVLCVideoPlayerView: _PlatformView { private lazy var videoContentView = makeVideoContentView() private var startConfiguration: VLCVideoPlayer.Configuration - private let delegate: VLCVideoPlayerDelegate + private let eventSubject: CurrentValueSubject + private let onTicksUpdated: (Int32, VLCVideoPlayer.PlaybackInformation) -> Void + private let onStateUpdated: (VLCVideoPlayer.State, VLCVideoPlayer.PlaybackInformation) -> Void private let logger: VLCVideoPlayerLogger private var currentMediaPlayer: VLCMediaPlayer? @@ -41,11 +43,15 @@ public class UIVLCVideoPlayerView: _PlatformView { init( configuration: VLCVideoPlayer.Configuration, - delegate: VLCVideoPlayerDelegate, + eventSubject: CurrentValueSubject, + onTicksUpdated: @escaping (Int32, VLCVideoPlayer.PlaybackInformation) -> Void, + onStateUpdated: @escaping (VLCVideoPlayer.State, VLCVideoPlayer.PlaybackInformation) -> Void, logger: VLCVideoPlayerLogger ) { self.startConfiguration = configuration - self.delegate = delegate + self.eventSubject = eventSubject + self.onTicksUpdated = onTicksUpdated + self.onStateUpdated = onStateUpdated self.logger = logger super.init(frame: .zero) @@ -125,7 +131,7 @@ public class UIVLCVideoPlayerView: _PlatformView { public extension UIVLCVideoPlayerView { func setupEventSubjectListener() { - delegate.eventSubject.sink { event in + eventSubject.sink { event in guard let event = event, let currentMediaPlayer = self.currentMediaPlayer, let media = currentMediaPlayer.media else { return } @@ -232,16 +238,13 @@ extension UIVLCVideoPlayerView: VLCMediaPlayerDelegate { let currentTicks = player.time.intValue let playbackInformation = constructPlaybackInformation(player: player, media: player.media!) - delegate.vlcVideoPlayer( - didUpdateTicks: currentTicks, - with: playbackInformation - ) + onTicksUpdated(currentTicks, playbackInformation) // Set playing state if lastPlayerState != .playing, abs(currentTicks - lastPlayerTicks) >= 200 { - delegate.vlcVideoPlayer(didUpdateState: .playing, with: playbackInformation) + onStateUpdated(.playing, playbackInformation) lastPlayerState = .playing lastPlayerTicks = currentTicks @@ -269,7 +272,7 @@ extension UIVLCVideoPlayerView: VLCMediaPlayerDelegate { let wrappedState = VLCVideoPlayer.State(rawValue: player.state.rawValue) ?? .error let playbackInformation = constructPlaybackInformation(player: player, media: player.media!) - delegate.vlcVideoPlayer(didUpdateState: wrappedState, with: playbackInformation) + onStateUpdated(wrappedState, playbackInformation) lastPlayerState = player.state } diff --git a/Sources/VLCUI/VLCVideoPlayer/VLCVideoPlayer.swift b/Sources/VLCUI/VLCVideoPlayer/VLCVideoPlayer.swift index 56b96b1..8c8da1c 100644 --- a/Sources/VLCUI/VLCVideoPlayer/VLCVideoPlayer.swift +++ b/Sources/VLCUI/VLCVideoPlayer/VLCVideoPlayer.swift @@ -1,12 +1,13 @@ +import Combine import Foundation import SwiftUI public struct VLCVideoPlayer: _PlatformRepresentable { - // MARK: Implementation - private var configuration: VLCVideoPlayer.Configuration - private var delegate: VLCVideoPlayerDelegate + private var eventSubject: CurrentValueSubject + private var onTicksUpdated: (Int32, VLCVideoPlayer.PlaybackInformation) -> Void + private var onStateUpdated: (VLCVideoPlayer.State, VLCVideoPlayer.PlaybackInformation) -> Void private var logger: VLCVideoPlayerLogger #if os(macOS) @@ -26,7 +27,9 @@ public struct VLCVideoPlayer: _PlatformRepresentable { private func makeVideoPlayerView() -> UIVLCVideoPlayerView { UIVLCVideoPlayerView( configuration: configuration, - delegate: delegate, + eventSubject: eventSubject, + onTicksUpdated: onTicksUpdated, + onStateUpdated: onStateUpdated, logger: logger ) } @@ -36,7 +39,9 @@ public extension VLCVideoPlayer { init(configuration: VLCVideoPlayer.Configuration) { self.configuration = configuration - self.delegate = DefaultVideoPlayerDelegate() + self.eventSubject = .init(nil) + self.onTicksUpdated = { _, _ in } + self.onStateUpdated = { _, _ in } self.logger = DefaultVideoPlayerLogger() } @@ -48,9 +53,24 @@ public extension VLCVideoPlayer { self.init(configuration: configure()) } - func delegate(_ delegate: VLCVideoPlayerDelegate) -> Self { + /// Sets the event subject for subscribing to player command events + func eventSubject(_ eventSubject: CurrentValueSubject) -> Self { + var copy = self + copy.eventSubject = eventSubject + return copy + } + + /// Sets the action that fires when the media ticks have been updated + func onTicksUpdated(_ action: @escaping (Int32, VLCVideoPlayer.PlaybackInformation) -> Void) -> Self { + var copy = self + copy.onTicksUpdated = action + return copy + } + + /// Sets the action that fires when the media state has been updated + func onStateUpdated(_ action: @escaping (VLCVideoPlayer.State, VLCVideoPlayer.PlaybackInformation) -> Void) -> Self { var copy = self - copy.delegate = delegate + copy.onStateUpdated = action return copy } diff --git a/Sources/VLCUI/VLCVideoPlayerDelegate.swift b/Sources/VLCUI/VLCVideoPlayerDelegate.swift deleted file mode 100644 index 95cb08f..0000000 --- a/Sources/VLCUI/VLCVideoPlayerDelegate.swift +++ /dev/null @@ -1,23 +0,0 @@ -import Combine -import Foundation - -public protocol VLCVideoPlayerDelegate { - - /// The subject to send events to the underlying VLCVideoplayer - var eventSubject: CurrentValueSubject { get set } - - /// Called when the ticks of the player change - func vlcVideoPlayer(didUpdateTicks ticks: Int32, with playbackInformation: VLCVideoPlayer.PlaybackInformation) - - /// Called when the VLCVideoPlayer state has been updated - func vlcVideoPlayer(didUpdateState state: VLCVideoPlayer.State, with playbackInformation: VLCVideoPlayer.PlaybackInformation) -} - -public extension VLCVideoPlayerDelegate { - func vlcVideoPlayer(didUpdateTicks ticks: Int32, with playbackInformation: VLCVideoPlayer.PlaybackInformation) {} - func vlcVideoPlayer(didUpdateState state: VLCVideoPlayer.State, with playbackInformation: VLCVideoPlayer.PlaybackInformation) {} -} - -class DefaultVideoPlayerDelegate: VLCVideoPlayerDelegate { - var eventSubject: CurrentValueSubject = .init(nil) -}