Skip to content

Commit bd292e1

Browse files
committed
refactoring
1 parent 11b6dd3 commit bd292e1

File tree

5 files changed

+32
-25
lines changed

5 files changed

+32
-25
lines changed

Sources/swiftui-loop-videoplayer/protocol/player/AbstractPlayer.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import CoreImage
1414
@MainActor @preconcurrency
1515
public protocol AbstractPlayer: AnyObject {
1616

17-
var currentSettings : VideoSettings? { get set }
17+
/// An optional property that stores the current video settings.
18+
///
19+
/// This property holds an instance of `VideoSettings` or nil if no settings have been configured yet.
20+
/// It is a computed property with both getter and setter to retrieve and update the video settings respectively.
21+
var currentSettings: VideoSettings? { get set }
1822

1923
/// The delegate to be notified about errors encountered by the player.
2024
var delegate: PlayerDelegateProtocol? { get set }
@@ -153,8 +157,10 @@ extension AbstractPlayer{
153157
}
154158

155159
guard currentItem?.status == .readyToPlay else{
160+
/// The case when the video if finished and we are trying to seek back
156161
if let currentAsset, let currentSettings{
157-
update(asset: currentAsset, settings: currentSettings.GetWithNotLoopNotAutoplay){ [weak self] status in
162+
let settings = currentSettings.GetSettingsWithNotAutoPlay
163+
update(asset: currentAsset, settings: settings){ [weak self] status in
158164
if status == .readyToPlay{
159165
self?.seek(to: time)
160166
}else {

Sources/swiftui-loop-videoplayer/utils/VideoSettings.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,26 @@ public struct VideoSettings: Equatable{
103103
public extension VideoSettings {
104104

105105
/// Returns a new instance of VideoSettings with loop set to false and notAutoPlay set to true, keeping other settings unchanged.
106-
var GetWithNotLoopNotAutoplay: VideoSettings {
107-
VideoSettings(name: self.name, ext: self.ext, loop: false, mute: self.mute, notAutoPlay: true, timePublishing: self.timePublishing, gravity: self.gravity, errorColor: self.errorColor, errorFontSize: self.errorFontSize, errorWidgetOff: self.errorWidgetOff, unique: self.unique)
106+
var GetSettingsWithNotAutoPlay : VideoSettings {
107+
VideoSettings(name: self.name, ext: self.ext, loop: self.loop, mute: self.mute, notAutoPlay: true, timePublishing: self.timePublishing, gravity: self.gravity, errorColor: self.errorColor, errorFontSize: self.errorFontSize, errorWidgetOff: self.errorWidgetOff, unique: self.unique)
108+
}
109+
110+
/// Checks if the asset has changed based on the provided settings and current asset.
111+
/// - Parameters:
112+
/// - asset: The current asset being played.
113+
/// - Returns: A new `AVURLAsset` if the asset has changed, or `nil` if the asset remains the same.
114+
func getAssetIfDifferent(than asset: AVURLAsset?) -> AVURLAsset?{
115+
let newAsset = assetFor(self)
116+
117+
if asset == nil {
118+
return newAsset
119+
}
120+
121+
if let newUrl = newAsset?.url, let oldUrl = asset?.url, newUrl != oldUrl{
122+
return newAsset
123+
}
124+
125+
return nil
108126
}
109127
}
110128

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ extension LoopPlayerMultiPlatform: UIViewRepresentable{
115115
@MainActor func updateUIView(_ uiView: UIView, context: Context) {
116116
let player = uiView.findFirstSubview(ofType: PlayerView.self)
117117
if let player {
118-
if let asset = getAssetIfChanged(for: settings, and: player.currentAsset) {
118+
if let asset = settings.getAssetIfDifferent(than: player.currentAsset) {
119119
player.update(asset: asset, settings: settings)
120120
}
121121

@@ -163,7 +163,7 @@ extension LoopPlayerMultiPlatform: NSViewRepresentable{
163163
@MainActor func updateNSView(_ nsView: NSView, context: Context) {
164164
let player = nsView.findFirstSubview(ofType: PlayerView.self)
165165
if let player {
166-
if let asset = getAssetIfChanged(for: settings, and: player.currentAsset){
166+
if let asset = settings.getAssetIfDifferent(than: player.currentAsset){
167167
player.update(asset: asset, settings: settings)
168168
}
169169
// Check if command changed before applying it
@@ -183,22 +183,3 @@ extension LoopPlayerMultiPlatform: NSViewRepresentable{
183183
}
184184
}
185185
#endif
186-
187-
/// Checks if the asset has changed based on the provided settings and current asset.
188-
/// - Parameters:
189-
/// - settings: The current video settings, containing the asset's name and extension.
190-
/// - asset: The current asset being played.
191-
/// - Returns: A new `AVURLAsset` if the asset has changed, or `nil` if the asset remains the same.
192-
fileprivate func getAssetIfChanged(for settings: VideoSettings, and asset: AVURLAsset?) -> AVURLAsset?{
193-
let newAsset = assetFor(settings)
194-
195-
if asset == nil {
196-
return newAsset
197-
}
198-
199-
if let newUrl = newAsset?.url, let oldUrl = asset?.url, newUrl != oldUrl{
200-
return newAsset
201-
}
202-
203-
return nil
204-
}

Sources/swiftui-loop-videoplayer/view/player/ios/LoopingPlayerUIView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import UIKit
1818
@MainActor @preconcurrency
1919
class LoopingPlayerUIView: UIView, LoopingPlayerProtocol {
2020

21+
/// This property holds an instance of `VideoSettings`
2122
internal var currentSettings : VideoSettings?
2223

2324
/// `filters` is an array that stores CIFilter objects used to apply different image processing effects

Sources/swiftui-loop-videoplayer/view/player/mac/LoopingPlayerNSView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import AppKit
2020
@MainActor @preconcurrency
2121
class LoopingPlayerNSView: NSView, LoopingPlayerProtocol {
2222

23+
/// This property holds an instance of `VideoSettings`
2324
internal var currentSettings : VideoSettings?
2425

2526
/// `filters` is an array that stores CIFilter objects used to apply different image processing effects

0 commit comments

Comments
 (0)