@@ -17,6 +17,10 @@ public struct LoopPlayerView: View {
17
17
/// Set of settings for video the player
18
18
public let settings : Settings
19
19
20
+ private var videoId : String {
21
+ [ settings. name, settings. ext] . joined ( separator: " . " )
22
+ }
23
+
20
24
// MARK: - Life cycle
21
25
22
26
/// Player initializer
@@ -55,10 +59,12 @@ public struct LoopPlayerView: View {
55
59
public var body : some View {
56
60
#if os(iOS) || os(tvOS)
57
61
LoopPlayerViewRepresentableIOS ( settings: settings)
58
- . frame ( maxWidth: . infinity, maxHeight: . infinity)
62
+ . frame ( maxWidth: . infinity, maxHeight: . infinity)
63
+ . id ( videoId)
59
64
#elseif os(macOS)
60
65
LoopPlayerViewRepresentableMacOS ( settings: settings)
61
66
. frame ( maxWidth: . infinity, maxHeight: . infinity)
67
+ . id ( videoId)
62
68
#endif
63
69
}
64
70
}
@@ -67,11 +73,40 @@ public struct LoopPlayerView: View {
67
73
68
74
#if os(iOS) || os(tvOS)
69
75
@available ( iOS 14 . 0 , tvOS 14 . 0 , * )
76
+ @MainActor
70
77
struct LoopPlayerViewRepresentableIOS : UIViewRepresentable {
71
78
72
79
let settings : Settings
73
80
81
+ init ( settings: Settings ) {
82
+ self . settings = settings
83
+ }
84
+
74
85
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 {
75
110
let name = settings. name
76
111
let ext = settings. ext
77
112
let gravity = settings. gravity
@@ -87,9 +122,6 @@ struct LoopPlayerViewRepresentableIOS: UIViewRepresentable {
87
122
}
88
123
return view
89
124
}
90
-
91
- func updateUIView( _ uiView: UIView , context: Context ) {
92
- }
93
125
}
94
126
95
127
// MARK: - Helpers for iOS and tvOS
0 commit comments