Skip to content

Commit a1986da

Browse files
committed
update
1 parent d55155e commit a1986da

File tree

8 files changed

+137
-80
lines changed

8 files changed

+137
-80
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ This package uses declarative approach to declare parameters for the video compo
2121
Ext("mp4")
2222
Gravity(.resizeAspectFill)
2323
ErrorGroup{
24-
EText("Not found")
24+
EColor(.accentColor)
2525
EFontSize(27)
2626
}
2727
}
@@ -38,7 +38,7 @@ This package uses declarative approach to declare parameters for the video compo
3838
}
3939
}
4040
```
41-
If you add any setting twice or more the first one only will be applied. You can group error settings in group **ErrorGroup** or just pass all settings as a linear list of settings. You don't need to follow some specific order for settings, just pass in arbitrary oder settings you are interested in. The only required setting is **FileName**.
41+
You can group error settings in group **ErrorGroup** or just pass all settings as a linear list of settings. You don't need to follow some specific order for settings, just pass in an arbitrary oder you are interested in. The only required setting is **FileName**.
4242

4343
### Settings
4444

@@ -47,7 +47,7 @@ If you add any setting twice or more the first one only will be applied. You can
4747
|**FileName("swipe")**| Name of the video to play| - |
4848
|**Ext("mp4")**| Video extension | "mp4" |
4949
|**Gravity(.resizeAspectFill)**| A structure that defines how a layer displays a player’s visual content within the layer’s bounds | .resizeAspect |
50-
|**EText("Not found")**| Error message text| "Resource is not found" |
50+
|**EColor(.accentColor)**| Error message text color| .red |
5151
|**EFontSize(27)**| Size of the error text | 17.0 |
5252
## SwiftUI example for the package
5353
[ SwiftUI loop video player example](https://github.com/The-Igor/swiftui-loop-videoplayer-example)

Sources/swiftui-loop-videoplayer/LoopPlayerView.swift

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ public struct LoopPlayerView: UIViewRepresentable {
2929
fileName: String,
3030
ext: String = "mp4",
3131
gravity: AVLayerVideoGravity = .resizeAspect,
32-
eText : String = "Resource is not found",
32+
eColor : Color = .accentColor,
3333
eFontSize : CGFloat = 17.0
34-
) {
35-
self.settings = Settings{
36-
FileName(fileName)
37-
Ext(ext)
38-
Gravity(gravity)
39-
ErrorGroup{
40-
EText(eText)
41-
EFontSize(eFontSize)
42-
}
34+
) {
35+
settings = Settings{
36+
FileName(fileName)
37+
Ext(ext)
38+
Gravity(gravity)
39+
ErrorGroup{
40+
EColor(eColor)
41+
EFontSize(eFontSize)
4342
}
4443
}
44+
}
4545

4646
/// Player initializer in a declarative way
4747
/// - Parameter settings: Set of settings
@@ -58,26 +58,22 @@ public struct LoopPlayerView: UIViewRepresentable {
5858
/// - Parameter context: Contains details about the current state of the system
5959
/// - Returns: View
6060
public func makeUIView(context: Context) -> UIView {
61+
6162
let name = settings.name
6263
let ext = settings.ext
6364
let gravity = settings.gravity
65+
let color = settings.errorColor
66+
let fontSize = settings.errorFontSize
67+
68+
guard settings.areUnique else{
69+
return errorTpl(.settingsNotUnique, color, fontSize)
70+
}
71+
6472
guard let view = LoopingPlayerUIView(name, width: ext, gravity: gravity) else{
65-
return errorTpl()
73+
return errorTpl(.fileNotFound(name), color, fontSize)
6674
}
6775
return view
6876
}
69-
70-
// MARK: - Private
71-
72-
/// - Returns: Error view
73-
private func errorTpl() -> ErrorMsgTextView{
74-
let textView = ErrorMsgTextView()
75-
textView.backgroundColor = .clear
76-
textView.text = settings.errorText
77-
textView.textAlignment = .center
78-
textView.font = UIFont.systemFont(ofSize: settings.errorFontSize)
79-
return textView
80-
}
8177
}
8278

8379

@@ -94,3 +90,14 @@ fileprivate class ErrorMsgTextView: UITextView {
9490
}
9591
}
9692
}
93+
94+
/// - Returns: Error view
95+
fileprivate func errorTpl(_ error : VPErrors,_ color : Color, _ fontSize: CGFloat) -> ErrorMsgTextView{
96+
let textView = ErrorMsgTextView()
97+
textView.backgroundColor = .clear
98+
textView.text = error.description
99+
textView.textAlignment = .center
100+
textView.font = UIFont.systemFont(ofSize: fontSize)
101+
textView.textColor = UIColor(color)
102+
return textView
103+
}

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

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import Foundation
99
import AVKit
10+
import SwiftUI
1011

1112
/// Settings for loop video player
1213
@available(iOS 14.0, *)
@@ -27,6 +28,9 @@ public enum Setting: Equatable{
2728
/// Size of the error text
2829
case errorFontSize(CGFloat)
2930

31+
/// Color of the error text
32+
case errorColor(Color)
33+
3034
/// Case name
3135
var caseName: String {
3236
Mirror(reflecting: self).children.first?.label ?? "\(self)"
@@ -42,28 +46,3 @@ public enum Setting: Equatable{
4246
return firstChild.value
4347
}
4448
}
45-
46-
@available(iOS 14.0, *)
47-
extension Array where Element == Setting{
48-
49-
/// Find first setting by case name
50-
/// - Parameter name: Case name
51-
/// - Returns: Setting
52-
private func first(_ name : String) -> Setting?{
53-
self.first(where: { $0.caseName == name })
54-
}
55-
56-
57-
/// Fetch associated value
58-
/// - Parameters:
59-
/// - name: Case name
60-
/// - defaulted: Default value
61-
/// - Returns: Associated value
62-
func fetch<T>(by name : String, defaulted : T) -> T{
63-
guard let value = first(name)?.associatedValue as? T else {
64-
return defaulted
65-
}
66-
67-
return value
68-
}
69-
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Igor on 09.07.2023.
6+
//
7+
8+
import Foundation
9+
10+
enum VPErrors : Error, CustomStringConvertible{
11+
12+
case fileNotFound(String)
13+
14+
case settingsNotUnique
15+
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+
21+
}
22+
}
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Array+.swift
3+
//
4+
//
5+
// Created by Igor on 09.07.2023.
6+
//
7+
8+
import Foundation
9+
10+
@available(iOS 14.0, *)
11+
extension Array where Element == Setting{
12+
13+
/// Find first setting by case name
14+
/// - Parameter name: Case name
15+
/// - Returns: Setting
16+
private func first(_ name : String) -> Setting?{
17+
self.first(where: { $0.caseName == name })
18+
}
19+
20+
21+
/// Fetch associated value
22+
/// - Parameters:
23+
/// - name: Case name
24+
/// - defaulted: Default value
25+
/// - Returns: Associated value
26+
func fetch<T>(by name : String, defaulted : T) -> T{
27+
guard let value = first(name)?.associatedValue as? T else {
28+
return defaulted
29+
}
30+
31+
return value
32+
}
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// EText.swift
3+
//
4+
//
5+
// Created by Igor on 07.07.2023.
6+
//
7+
8+
import SwiftUI
9+
10+
@available(iOS 14.0, *)
11+
public struct EColor: SettingsConvertible{
12+
13+
/// Error color
14+
private let value : Color
15+
16+
// MARK: - Life circle
17+
18+
/// - Parameter value: Error color
19+
public init(_ value: Color) { self.value = value }
20+
21+
/// Fetch settings
22+
@_spi(Private)
23+
public func asSettings() -> [Setting] {
24+
[.errorColor(value)]
25+
}
26+
}

Sources/swiftui-loop-videoplayer/settings/errors/EText.swift

Lines changed: 0 additions & 25 deletions
This file was deleted.

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by Igor on 07.07.2023.
66
//
77

8-
import Foundation
8+
import SwiftUI
99
import AVKit
1010

1111
@available(iOS 14.0, *)
@@ -20,27 +20,41 @@ public struct Settings{
2020
/// A structure that defines how a layer displays a player’s visual content within the layer’s bounds
2121
public let gravity: AVLayerVideoGravity
2222

23-
/// Error message text
24-
public let errorText : String
23+
/// Error message text color
24+
public let errorColor : Color
2525

2626
/// Size of the error text Default : 17.0
2727
public let errorFontSize : CGFloat
28+
29+
/// Is settings are unique
30+
private let unique : Bool
2831

32+
public var areUnique : Bool {
33+
unique
34+
}
2935

3036
// MARK: - Life circle
3137

3238
public init(@SettingsBuilder builder: () -> [Setting]){
3339
let settings = builder()
3440

41+
unique = check(settings)
42+
3543
name = settings.fetch(by : "name", defaulted: "")
3644

3745
ext = settings.fetch(by : "ext", defaulted: "mp4")
3846

3947
gravity = settings.fetch(by : "gravity", defaulted: .resizeAspect)
4048

41-
errorText = settings.fetch(by : "errorText", defaulted: "Resource is not found")
49+
errorColor = settings.fetch(by : "errorColor", defaulted: .red)
4250

4351
errorFontSize = settings.fetch(by : "errorFontSize", defaulted: 17)
4452
}
4553
}
4654

55+
fileprivate func check(_ settings : [Setting]) -> Bool{
56+
let cases : [String] = settings.map{ $0.caseName }
57+
let set = Set(cases)
58+
return cases.count == set.count
59+
}
60+

0 commit comments

Comments
 (0)