Skip to content

Commit 0e31b4c

Browse files
committed
Change video file dynamically
1 parent d88c119 commit 0e31b4c

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

Sources/swiftui-loop-videoplayer/LoopPlayerView.swift

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public struct LoopPlayerView: View {
1717
/// Set of settings for video the player
1818
public let settings: Settings
1919

20+
private var videoId : String{
21+
[settings.name, settings.ext].joined(separator: ".")
22+
}
23+
2024
// MARK: - Life cycle
2125

2226
/// Player initializer
@@ -55,10 +59,12 @@ public struct LoopPlayerView: View {
5559
public var body: some View {
5660
#if os(iOS) || os(tvOS)
5761
LoopPlayerViewRepresentableIOS(settings: settings)
58-
.frame(maxWidth: .infinity, maxHeight: .infinity)
62+
.frame(maxWidth: .infinity, maxHeight: .infinity)
63+
.id(videoId)
5964
#elseif os(macOS)
6065
LoopPlayerViewRepresentableMacOS(settings: settings)
6166
.frame(maxWidth: .infinity, maxHeight: .infinity)
67+
.id(videoId)
6268
#endif
6369
}
6470
}
@@ -67,11 +73,40 @@ public struct LoopPlayerView: View {
6773

6874
#if os(iOS) || os(tvOS)
6975
@available(iOS 14.0, tvOS 14.0, *)
76+
@MainActor
7077
struct LoopPlayerViewRepresentableIOS: UIViewRepresentable {
7178

7279
let settings: Settings
7380

81+
init(settings: Settings) {
82+
self.settings = settings
83+
}
84+
7485
func makeUIView(context: Context) -> UIView {
86+
createView()
87+
}
88+
89+
func updateUIView(_ uiView: UIView, context: Context) {
90+
// Remove all subviews and add the new one
91+
uiView.subviews.forEach { $0.removeFromSuperview() }
92+
let newView = createView()
93+
uiView.addSubview(newView)
94+
95+
// Ensure the new view fits the bounds of the container view
96+
newView.translatesAutoresizingMaskIntoConstraints = false
97+
NSLayoutConstraint.activate([
98+
newView.leadingAnchor.constraint(equalTo: uiView.leadingAnchor),
99+
newView.trailingAnchor.constraint(equalTo: uiView.trailingAnchor),
100+
newView.topAnchor.constraint(equalTo: uiView.topAnchor),
101+
newView.bottomAnchor.constraint(equalTo: uiView.bottomAnchor)
102+
])
103+
104+
// Force layout update
105+
uiView.setNeedsLayout()
106+
uiView.layoutIfNeeded()
107+
}
108+
109+
private func createView() -> UIView {
75110
let name = settings.name
76111
let ext = settings.ext
77112
let gravity = settings.gravity
@@ -87,9 +122,6 @@ struct LoopPlayerViewRepresentableIOS: UIViewRepresentable {
87122
}
88123
return view
89124
}
90-
91-
func updateUIView(_ uiView: UIView, context: Context) {
92-
}
93125
}
94126

95127
// MARK: - Helpers for iOS and tvOS

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import SwiftUI
99
import AVKit
1010

1111
@available(iOS 14.0, macOS 11.0, tvOS 14.0, *)
12-
public struct Settings{
12+
public struct Settings: Equatable{
1313

1414
// MARK: - Public properties
1515

0 commit comments

Comments
 (0)