Skip to content

Commit

Permalink
remove delegate
Browse files Browse the repository at this point in the history
in favor of SwiftUI style for state changes
  • Loading branch information
LePips committed Sep 24, 2022
1 parent 6ac8795 commit 8a2ebe2
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 59 deletions.
10 changes: 9 additions & 1 deletion Example/VLCUIExample/Shared/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
17 changes: 1 addition & 16 deletions Example/VLCUIExample/Shared/ContentViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Combine
import Foundation
import VLCUI

class ContentViewModel: ObservableObject, VLCVideoPlayerDelegate {
class ContentViewModel: ObservableObject {

@Published
var ticks: Int32 = 0
Expand Down Expand Up @@ -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")
}
}
}
4 changes: 2 additions & 2 deletions Example/VLCUIExample/VLCUIExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
23 changes: 13 additions & 10 deletions Sources/VLCUI/UIVLCVideoPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<VLCVideoPlayer.Event?, Never>
private let onTicksUpdated: (Int32, VLCVideoPlayer.PlaybackInformation) -> Void
private let onStateUpdated: (VLCVideoPlayer.State, VLCVideoPlayer.PlaybackInformation) -> Void
private let logger: VLCVideoPlayerLogger
private var currentMediaPlayer: VLCMediaPlayer?

Expand All @@ -41,11 +43,15 @@ public class UIVLCVideoPlayerView: _PlatformView {

init(
configuration: VLCVideoPlayer.Configuration,
delegate: VLCVideoPlayerDelegate,
eventSubject: CurrentValueSubject<VLCVideoPlayer.Event?, Never>,
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)

Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}

Expand Down
34 changes: 27 additions & 7 deletions Sources/VLCUI/VLCVideoPlayer/VLCVideoPlayer.swift
Original file line number Diff line number Diff line change
@@ -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<VLCVideoPlayer.Event?, Never>
private var onTicksUpdated: (Int32, VLCVideoPlayer.PlaybackInformation) -> Void
private var onStateUpdated: (VLCVideoPlayer.State, VLCVideoPlayer.PlaybackInformation) -> Void
private var logger: VLCVideoPlayerLogger

#if os(macOS)
Expand All @@ -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
)
}
Expand All @@ -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()
}

Expand All @@ -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<VLCVideoPlayer.Event?, Never>) -> 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
}

Expand Down
23 changes: 0 additions & 23 deletions Sources/VLCUI/VLCVideoPlayerDelegate.swift

This file was deleted.

0 comments on commit 8a2ebe2

Please sign in to comment.