Skip to content

Commit 8151308

Browse files
committed
refactoring
1 parent 9175823 commit 8151308

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

Sources/swiftui-loop-videoplayer/view/loop/helpers/PlayerErrorCoordinator.swift

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,44 @@
88
import SwiftUI
99

1010
@MainActor
11-
internal class PlayerErrorCoordinator: NSObject, PlayerErrorDelegate {
11+
internal class PlayerCoordinator: NSObject, PlayerErrorDelegate {
1212

13+
/// Stores the last command applied to the player.
14+
private var lastCommand: PlaybackCommand?
15+
16+
/// A binding to an optional `VPErrors` instance, used to report errors back to the parent view.
1317
@Binding private var error: VPErrors?
1418

19+
/// Initializes a new instance of `PlayerCoordinator`.
20+
/// - Parameter error: A binding to an optional `VPErrors` instance to manage error reporting.
1521
init(_ error: Binding<VPErrors?>) {
1622
self._error = error
1723
}
1824

25+
/// Deinitializes the coordinator and prints a debug message if in DEBUG mode.
1926
deinit {
2027
#if DEBUG
2128
print("deinit Coordinator")
2229
#endif
2330
}
2431

25-
/// Handles receiving an error and updates the error state in the parent view
26-
/// - Parameter error: The error received
32+
/// Handles receiving an error and updates the error state in the parent view.
33+
/// This method is called when an error is encountered during playback or other operations.
34+
/// - Parameter error: The error received.
2735
func didReceiveError(_ error: VPErrors) {
28-
self.error = error
36+
self.error = error
37+
}
38+
39+
/// Sets the last command applied to the player.
40+
/// This method updates the stored `lastCommand` to the provided value.
41+
/// - Parameter command: The `PlaybackCommand` that was last applied to the player.
42+
func setLastCommand(_ command: PlaybackCommand) {
43+
self.lastCommand = command
44+
}
45+
46+
/// Retrieves the last command applied to the player.
47+
/// - Returns: The `PlaybackCommand` that was last applied to the player.
48+
var getLastCommand : PlaybackCommand? {
49+
return lastCommand
2950
}
3051
}

Sources/swiftui-loop-videoplayer/view/loop/main/LoopPlayerMultiPlatform.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ struct LoopPlayerMultiPlatform: LoopPlayerViewProtocol {
6868

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

@@ -101,8 +101,12 @@ extension LoopPlayerMultiPlatform: UIViewRepresentable{
101101
if let player {
102102
if let asset = getAssetIfChanged(for: settings, and: player.currentAsset) {
103103
player.update(asset: asset)
104-
} else {
104+
}
105+
106+
// Check if command changed before applying it
107+
if context.coordinator.getLastCommand != command {
105108
player.setCommand(command)
109+
context.coordinator.setLastCommand(command) // Update the last command in the coordinator
106110
}
107111
}
108112

@@ -139,9 +143,13 @@ extension LoopPlayerMultiPlatform: NSViewRepresentable{
139143
if let player {
140144
if let asset = getAssetIfChanged(for: settings, and: player.currentAsset){
141145
player.update(asset: asset)
142-
}else{
146+
}
147+
// Check if command changed before applying it
148+
if context.coordinator.getLastCommand != command {
143149
player.setCommand(command)
150+
context.coordinator.setLastCommand(command) // Update the last command in the coordinator
144151
}
152+
145153
}
146154

147155
updateView(nsView, error: error)

0 commit comments

Comments
 (0)