Skip to content

Commit dc25617

Browse files
committed
refactoring
1 parent c2ea4b6 commit dc25617

File tree

3 files changed

+49
-65
lines changed

3 files changed

+49
-65
lines changed

Sources/swiftui-loop-videoplayer/protocol/LoopPlayerViewProtocol.swift

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ import SwiftUI
1515
public protocol LoopPlayerViewProtocol {
1616

1717
#if os(iOS) || os(tvOS)
18-
associatedtype View :UIView
18+
associatedtype View : UIView
1919
#elseif os(macOS)
2020
associatedtype View : NSView
2121
#else
2222
associatedtype View : CustomView
2323
#endif
2424

2525
associatedtype ErrorView
26+
27+
#if os(iOS) || os(tvOS)
28+
associatedtype PlayerView: LoopingPlayerProtocol, UIView
29+
#elseif os(macOS)
30+
associatedtype PlayerView: LoopingPlayerProtocol, NSView
31+
#else
32+
associatedtype PlayerView: LoopingPlayerProtocol, CustomView
33+
#endif
2634

2735
/// Settings for configuring the video player.
2836
var settings: Settings { get }
@@ -58,62 +66,24 @@ public extension LoopPlayerViewProtocol{
5866
activateFullScreenConstraints(for: errorView, in: view)
5967
}
6068
}
61-
62-
/// Adds a subview to a given view and activates full-screen constraints on the subview within the parent view.
63-
/// - Parameters:
64-
/// - view: The parent view to which the subview will be added.
65-
/// - subView: The subview that will be added to the parent view.
66-
@MainActor
67-
func compose(_ view: View, _ subView: View) {
68-
view.addSubview(subView)
69-
activateFullScreenConstraints(for: subView, in: view)
70-
}
71-
}
72-
73-
#if os(iOS) || os(tvOS)
74-
@available(iOS 14, tvOS 14, *)
75-
public extension LoopPlayerViewProtocol where Self: UIViewRepresentable, Context == UIViewRepresentableContext<Self> {
7669

7770
/// Creates a player view for looping video content.
7871
/// - Parameters:
7972
/// - context: The UIViewRepresentable context providing environment data and coordinator.
8073
/// - asset: The AVURLAsset to be used for video playback.
8174
/// - Returns: A PlayerView instance conforming to LoopingPlayerProtocol.
8275
@MainActor
83-
func createPlayerView<PlayerView: LoopingPlayerProtocol>(
84-
context: Context,
85-
asset: AVURLAsset) -> PlayerView {
76+
func makePlayerView(
77+
_ container: View,
78+
asset: AVURLAsset?) -> PlayerView? {
8679

80+
if let asset{
81+
let player = PlayerView(asset: asset, gravity: settings.gravity)
82+
container.addSubview(player)
83+
activateFullScreenConstraints(for: player, in: container)
84+
return player
85+
}
8786

88-
let player = PlayerView(asset: asset, gravity: settings.gravity)
89-
90-
player.delegate = context.coordinator as? PlayerErrorDelegate
91-
return player
92-
}
93-
}
94-
95-
#endif
96-
97-
#if os(macOS)
98-
@available(macOS 11, *)
99-
public extension LoopPlayerViewProtocol where Self: NSViewRepresentable, Context == NSViewRepresentableContext<Self> {
100-
101-
/// Creates a player view for looping video content.
102-
/// - Parameters:
103-
/// - context: The NSViewRepresentable context providing environment data and coordinator.
104-
/// - asset: The AVURLAsset to be used for video playback.
105-
/// - Returns: A PlayerView instance conforming to LoopingPlayerProtocol.
106-
@MainActor
107-
func createPlayerView<PlayerView: LoopingPlayerProtocol>(
108-
context: Context,
109-
asset: AVURLAsset) -> PlayerView {
110-
111-
let player = PlayerView(asset: asset, gravity: settings.gravity)
112-
113-
player.delegate = context.coordinator as? PlayerErrorDelegate
114-
return player
87+
return nil
11588
}
11689
}
117-
#endif
118-
119-

Sources/swiftui-loop-videoplayer/view/loop/ios/LoopPlayerViewIOS.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct LoopPlayerViewIOS: UIViewRepresentable, LoopPlayerViewProtocol {
2323

2424
typealias ErrorView = ErrorMsgViewIOS
2525

26+
typealias PlayerView = LoopingPlayerUIView
2627

2728
/// Settings for the player view
2829
public let settings: Settings
@@ -45,12 +46,13 @@ struct LoopPlayerViewIOS: UIViewRepresentable, LoopPlayerViewProtocol {
4546
/// - Parameter context: The context for the view
4647
/// - Returns: A configured UIView
4748
func makeUIView(context: Context) -> UIView {
48-
let container = UIView()
49-
50-
if let asset{
51-
let player: LoopingPlayerUIView = createPlayerView(context: context, asset: asset)
52-
compose(container, player)
53-
}
49+
let container = UIView()
50+
51+
if let player: PlayerView = makePlayerView(
52+
container,
53+
asset: asset){
54+
player.delegate = context.coordinator
55+
}
5456

5557
makeErrorView(container, error: error)
5658

@@ -67,8 +69,8 @@ struct LoopPlayerViewIOS: UIViewRepresentable, LoopPlayerViewProtocol {
6769
updateView(uiView, error: error)
6870
}
6971

70-
/// Creates the coordinator for handling player errors
71-
/// - Returns: A configured Coordinator
72+
/// Creates a coordinator that handles error-related updates and interactions between the SwiftUI view and its underlying model.
73+
/// - Returns: An instance of PlayerErrorCoordinator that can be used to manage error states and communicate between the view and model.
7274
func makeCoordinator() -> PlayerErrorCoordinator {
7375
PlayerErrorCoordinator($error)
7476
}

Sources/swiftui-loop-videoplayer/view/loop/mac/LoopPlayerViewMacOS.swift

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ struct LoopPlayerViewMacOS: NSViewRepresentable, LoopPlayerViewProtocol {
2222

2323
typealias ErrorView = ErrorMsgViewMacOS
2424

25+
typealias PlayerView = LoopingPlayerNSView
26+
2527
/// Settings for the player view
2628
public let settings: Settings
2729

@@ -39,25 +41,35 @@ struct LoopPlayerViewMacOS: NSViewRepresentable, LoopPlayerViewProtocol {
3941
self._error = State(initialValue: detectError(settings: settings, asset: self.asset))
4042
}
4143

44+
/// Creates the NSView for the representable component. It initializes the view, configures it with a player if available, and adds an error view if necessary.
45+
/// - Parameter context: The context containing environment and state information used during view creation.
46+
/// - Returns: A fully configured NSView containing both the media player and potentially an error message display.
4247
func makeNSView(context: Context) -> NSView {
4348
let container = NSView()
44-
45-
if let asset{
46-
let player: LoopingPlayerNSView = createPlayerView(context: context, asset: asset)
47-
compose(container, player)
49+
50+
if let player: PlayerView = makePlayerView(
51+
container,
52+
asset: asset){
53+
player.delegate = context.coordinator
4854
}
49-
50-
makeErrorView(container, error: error)
51-
52-
return container
55+
56+
makeErrorView(container, error: error)
57+
58+
return container
5359
}
5460

61+
/// Updates the specified NSView during the view's lifecycle in response to state changes.
62+
/// - Parameters:
63+
/// - nsView: The NSView that needs updating.
64+
/// - context: The context containing environment and state information used during the view update.
5565
func updateNSView(_ nsView: NSView, context: Context) {
5666
nsView.subviews.filter { $0 is ErrorView }.forEach { $0.removeFromSuperview() }
5767

5868
updateView(nsView, error: error)
5969
}
6070

71+
/// Creates a coordinator that handles error-related updates and interactions between the SwiftUI view and its underlying model.
72+
/// - Returns: An instance of PlayerErrorCoordinator that can be used to manage error states and communicate between the view and model.
6173
func makeCoordinator() -> PlayerErrorCoordinator {
6274
PlayerErrorCoordinator($error)
6375
}

0 commit comments

Comments
 (0)