Skip to content

Commit d88c119

Browse files
committed
code format and comments
1 parent f3d1b88 commit d88c119

File tree

4 files changed

+53
-20
lines changed

4 files changed

+53
-20
lines changed

Sources/swiftui-loop-videoplayer/LoopPlayerView.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,40 +121,55 @@ fileprivate func errorTpliOS(_ error: VPErrors, _ color: Color, _ fontSize: CGFl
121121
// MARK: - Representable for macOS
122122

123123
#if os(macOS)
124+
/// A view representable for macOS to display a looping video player.
124125
@available(macOS 11.0, *)
125126
struct LoopPlayerViewRepresentableMacOS: NSViewRepresentable {
126127

128+
/// The settings for the video player.
127129
let settings: Settings
128130

131+
/// Creates the NSView instance for the video player.
132+
/// - Parameter context: The context in which the view is created.
133+
/// - Returns: An NSView that displays the video player, or an error view if there is an issue.
129134
func makeNSView(context: Context) -> NSView {
130135
let name = settings.name
131136
let ext = settings.ext
132137
let gravity = settings.gravity
133138
let color = settings.errorColor
134139
let fontSize = settings.errorFontSize
135140

141+
// Check if the settings are unique
136142
guard settings.areUnique else {
137143
return errorTplmacOS(.settingsNotUnique, color, fontSize)
138144
}
139145

146+
// Create the looping player view or return an error view if the file is not found
140147
guard let view = LoopingPlayerNSView(name, width: ext, gravity: gravity) else {
141148
return errorTplmacOS(.fileNotFound(name), color, fontSize)
142149
}
143150
return view
144151
}
145152

153+
/// Updates the NSView instance.
154+
/// - Parameters:
155+
/// - nsView: The NSView instance to update.
156+
/// - context: The context in which the update occurs.
146157
func updateNSView(_ nsView: NSView, context: Context) {
158+
// Update the view if necessary
147159
}
148160
}
149161

150162
// MARK: - Helpers for macOS
151163

164+
/// A custom NSTextView for displaying error messages on macOS.
152165
fileprivate class ErrorMsgTextViewMacOS: NSTextView {
153166

167+
/// Overrides the intrinsic content size to allow flexible width and height.
154168
override var intrinsicContentSize: NSSize {
155169
return NSSize(width: NSView.noIntrinsicMetric, height: NSView.noIntrinsicMetric)
156170
}
157171

172+
/// Called when the view is added to a superview. Sets up the constraints for the view.
158173
override func viewDidMoveToSuperview() {
159174
super.viewDidMoveToSuperview()
160175
guard let superview = superview else { return }
@@ -169,6 +184,7 @@ fileprivate class ErrorMsgTextViewMacOS: NSTextView {
169184
])
170185
}
171186

187+
/// Adjusts the layout to center the text vertically within the view.
172188
override func layout() {
173189
super.layout()
174190

@@ -184,6 +200,12 @@ fileprivate class ErrorMsgTextViewMacOS: NSTextView {
184200
}
185201
}
186202

203+
/// Creates a custom error view for macOS displaying an error message.
204+
/// - Parameters:
205+
/// - error: The error object containing the error description.
206+
/// - color: The color to be used for the error text.
207+
/// - fontSize: The font size to be used for the error text.
208+
/// - Returns: An `NSView` containing the error message text view centered with padding.
187209
fileprivate func errorTplmacOS(_ error: VPErrors, _ color: Color, _ fontSize: CGFloat) -> NSView {
188210
let textView = ErrorMsgTextViewMacOS()
189211
textView.isEditable = false

Sources/swiftui-loop-videoplayer/enum/Setting.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
//
77

88
import Foundation
9-
import AVKit
109
import SwiftUI
10+
#if canImport(AVKit)
11+
import AVKit
12+
#endif
13+
1114

1215
/// Settings for loop video player
1316
@available(iOS 14.0, macOS 11.0, tvOS 14.0, *)
Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
//
2-
// File.swift
3-
//
2+
// VPErrors.swift
3+
//
44
//
55
// Created by Igor on 09.07.2023.
66
//
77

88
import Foundation
99

10-
enum VPErrors : Error, CustomStringConvertible{
10+
/// An enumeration of possible errors that can occur in the video player.
11+
enum VPErrors: Error, CustomStringConvertible {
1112

13+
/// Error case for when a file is not found.
14+
/// - Parameter name: The name of the file that was not found.
1215
case fileNotFound(String)
1316

17+
/// Error case for when settings are not unique.
1418
case settingsNotUnique
1519

16-
var description: String{
17-
switch(self){
18-
case .fileNotFound(let name) : return "File not found \(name)"
19-
case .settingsNotUnique : return "Settings are not unique"
20+
/// A description of the error, suitable for display.
21+
var description: String {
22+
switch self {
23+
/// Returns a description indicating that the specified file was not found.
24+
/// - Parameter name: The name of the file that was not found.
25+
case .fileNotFound(let name): return "File not found: \(name)"
2026

27+
/// Returns a description indicating that the settings are not unique.
28+
case .settingsNotUnique: return "Settings are not unique"
2129
}
2230
}
2331
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
// Created by Igor on 10.02.2023.
66
//
77

8+
#if canImport(AVKit)
89
import AVKit
10+
#endif
911

10-
11-
#if canImport(UIKit) || os(tvOS)
12+
#if canImport(UIKit)
1213
import UIKit
1314

1415
@available(iOS 14.0, tvOS 14.0, *)
@@ -30,7 +31,7 @@ class LoopingPlayerUIView: UIView {
3031
/// - gravity: A structure that defines how a layer displays a player’s visual content within the layer’s bounds
3132
init?(_ name: String, width ext: String, gravity: AVLayerVideoGravity) {
3233

33-
/// Load the resource
34+
// Load the resource
3435
guard let fileUrl = Bundle.main.url(forResource: name, withExtension: ext) else{
3536
return nil
3637
}
@@ -39,7 +40,7 @@ class LoopingPlayerUIView: UIView {
3940

4041
let item = AVPlayerItem(asset: asset)
4142

42-
/// Setup the player
43+
// Setup the player
4344
let player = AVQueuePlayer()
4445
player.isMuted = true
4546
playerLayer.player = player
@@ -48,10 +49,10 @@ class LoopingPlayerUIView: UIView {
4849

4950
super.init(frame: CGRect.zero)
5051

51-
/// Create a new player looper with the queue player and template item
52+
// Create a new player looper with the queue player and template item
5253
playerLooper = AVPlayerLooper(player: player, templateItem: item)
5354

54-
/// Start the movie
55+
// Start the movie
5556
player.play()
5657

5758
layer.addSublayer(playerLayer)
@@ -63,7 +64,6 @@ class LoopingPlayerUIView: UIView {
6364
playerLayer.frame = bounds
6465
}
6566
}
66-
6767
#elseif canImport(Cocoa)
6868
import Cocoa
6969

@@ -86,7 +86,7 @@ class LoopingPlayerNSView: NSView {
8686
/// - gravity: A structure that defines how a layer displays a player’s visual content within the layer’s bounds
8787
init?(_ name: String, width ext: String, gravity: AVLayerVideoGravity) {
8888

89-
/// Load the resource
89+
// Load the resource
9090
guard let fileUrl = Bundle.main.url(forResource: name, withExtension: ext) else{
9191
return nil
9292
}
@@ -95,7 +95,7 @@ class LoopingPlayerNSView: NSView {
9595

9696
let item = AVPlayerItem(asset: asset)
9797

98-
/// Setup the player
98+
// Setup the player
9999
let player = AVQueuePlayer()
100100
player.isMuted = true
101101
playerLayer.player = player
@@ -104,18 +104,18 @@ class LoopingPlayerNSView: NSView {
104104

105105
super.init(frame: .zero)
106106

107-
/// Create a new player looper with the queue player and template item
107+
// Create a new player looper with the queue player and template item
108108
playerLooper = AVPlayerLooper(player: player, templateItem: item)
109109

110-
/// Start the movie
110+
// Start the movie
111111
player.play()
112112

113113
layer = CALayer()
114114
layer?.addSublayer(playerLayer)
115115
wantsLayer = true
116116
}
117117

118-
/// Called automatically when the view's bounds change
118+
// Called automatically when the view's bounds change
119119
override func layout() {
120120
super.layout()
121121
playerLayer.frame = bounds

0 commit comments

Comments
 (0)