Skip to content

Commit b5928e3

Browse files
authored
[DVMod] Initial Build of DVMod System. (#12)
2 parents 74f80a5 + 3c3e11d commit b5928e3

File tree

7 files changed

+1373
-37
lines changed

7 files changed

+1373
-37
lines changed
Lines changed: 157 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,202 @@
1-
import Foundation
21
import SwiftUI
32

43
/// [DE] A configuration class working for `DefinedView` protocol system.
4+
///
5+
/// Fields of this struct should be able to access from outside (for development purpose),
6+
/// but should NOT be able to modify from outside!
7+
/// So we make them all `private(set)` for modifier and `public` for accessor.
58
public struct DefinedViewConfiguration {
69
/// A boolean representing if the `width` and `height` are activated for defining the internal frame size.
710
///
8-
/// - Important: This variable should not be changed!
9-
var inactive: Bool
11+
/// - Important: This variable should NOT be changed!
12+
private(set) public var inactive: Bool
1013

14+
/// The frame width of the view.
1115
///
12-
var width: CGFloat
16+
/// When it is `.infinity`, we will automatically push left and right boundaries to the limit.
17+
/// You can manually set the width by using `.infinity`,
18+
/// or you can use `.full` in setting the frame to avoid buggy.
19+
private(set) public var width: CGFloat
1320

21+
/// The frame height of the view.
1422
///
15-
var height: CGFloat
23+
/// When it is `.infinity`, we will automatically push top and bottom boundaries to the limit.
24+
/// You can manually set the width by using `.infinity`,
25+
/// or you can use `.full` in setting the frame to avoid buggy.
26+
private(set) public var height: CGFloat
27+
28+
/// Width constrain flag. Is this view constrained by the **width**?
29+
///
30+
/// True for constrain. False for not.
31+
///
32+
/// In other words, if it is not constrained, then we will not render the frame by the width value.
33+
private(set) public var isWidthConstrained: Bool
1634

35+
/// Height constrain flag. Is this view constrained by the **height**?
1736
///
18-
var isWidthConstrained: Bool
37+
/// True for constrain. False for not.
38+
///
39+
/// In other words, if it is not constrained, then we will not render the frame by the height value.
40+
private(set) public var isHeightConstrained: Bool
1941

42+
/// Width setup flag. Is this view constrained by a post-setting **width**?
43+
///
44+
/// True for manually setting. False for default setting.
2045
///
21-
var isHeightConstrained: Bool
46+
/// In other words, check if we need to apply another frame set on this view.
47+
/// If it has already been set once, we do NOT need to, then try a wrap.
48+
private(set) public var isWidthSet: Bool
2249

50+
/// Height setup flag. Is this view constrained by a post-setting **height**?
2351
///
24-
public init() {
25-
self.inactive = false
52+
/// True for manually setting. False for default setting.
53+
///
54+
/// In other words, check if we need to apply another frame set on this view.
55+
/// If it has already been set once, we do NOT need to, then try a wrap.
56+
private(set) public var isHeightSet: Bool
57+
58+
/// The frame alignment of the view.
59+
///
60+
/// It treats as the same as the default `alignment`,
61+
/// but we are actually able to manually setup the usage of this value in a view component.
62+
///
63+
/// - Note: The developer can manually use this value instead of using it in the default way.
64+
private(set) public var alignment: Alignment
65+
66+
/// Alignment setup flag. Is this view constrained by a post-setting **alignment**?
67+
///
68+
/// True for manually setting. False for default setting.
69+
private(set) public var isAlignmentSet: Bool
70+
71+
/// [DE Inactive] Create a ViewConfiguration that does NOT apply onto the view.
72+
///
73+
/// - Note: In this case, the root view should NOT use these values anymore!
74+
public init(inactive: Bool) {
75+
self.inactive = inactive
2676
self.width = 0
2777
self.isWidthConstrained = false
78+
self.isWidthSet = false
2879
self.height = 0
2980
self.isHeightConstrained = false
81+
self.isHeightSet = false
82+
self.alignment = .center
83+
self.isAlignmentSet = false
3084
}
3185

86+
/// [DE] Create a ViewConfiguration with by default no frame constrained.
3287
///
33-
public init(inactive: Bool) {
34-
self.inactive = inactive
88+
/// - Parameters:
89+
/// - alignment: The default alignment of the frame. (Optional, default as `.center`)
90+
/// - isAlignmentSet: Will we treat it as a manual set? (So the alignment will not be overrided later)
91+
public init(
92+
alignment: Alignment = .center,
93+
isAlignmentSet: Bool = false
94+
) {
95+
self.inactive = false
3596
self.width = 0
3697
self.isWidthConstrained = false
98+
self.isWidthSet = false
99+
self.height = 0
100+
self.isHeightConstrained = false
101+
self.isHeightSet = false
102+
self.alignment = alignment
103+
self.isAlignmentSet = isAlignmentSet
104+
}
105+
106+
/// [DE] Create a ViewConfiguration with by default width frame constrained.
107+
///
108+
/// - Parameters:
109+
/// - width: The width of the view frame.
110+
/// - isWidthSet: Will we treat this width as a manual set? (So the width will not be overrided later)
111+
/// - alignment: The default alignment of the frame.
112+
/// - isAlignmentSet: Will we treat this alignment as a manual set? (So the alignment will not be overrided later)
113+
public init(
114+
width: CGFloat,
115+
isWidthSet: Bool = false,
116+
alignment: Alignment = .center,
117+
isAlignmentSet: Bool = false
118+
) {
119+
self.inactive = false
120+
self.width = width
121+
self.isWidthConstrained = true
122+
self.isWidthSet = isWidthSet
37123
self.height = 0
38124
self.isHeightConstrained = false
125+
self.isHeightSet = false
126+
self.alignment = alignment
127+
self.isAlignmentSet = isAlignmentSet
128+
}
129+
130+
/// [DE] Create a ViewConfiguration with by default height frame constrained.
131+
///
132+
/// - Parameters:
133+
/// - height: The height of the view frame.
134+
/// - isHeightSet: Will we treat this height as a manual set? (So the height will not be overrided later)
135+
/// - alignment: The default alignment of the frame.
136+
/// - isAlignmentSet: Will we treat this alignment as a manual set? (So the alignment will not be overrided later)
137+
public init(
138+
height: CGFloat,
139+
isHeightSet: Bool = false,
140+
alignment: Alignment = .center,
141+
isAlignmentSet: Bool = false
142+
) {
143+
self.inactive = false
144+
self.width = 0
145+
self.isWidthConstrained = false
146+
self.isWidthSet = false
147+
self.height = height
148+
self.isHeightConstrained = true
149+
self.isHeightSet = isHeightSet
150+
self.alignment = alignment
151+
self.isAlignmentSet = isAlignmentSet
39152
}
40153

154+
/// [DE] Create a ViewConfiguration with by default full frame constrained.
41155
///
42-
public init(width: CGFloat, height: CGFloat) {
156+
/// - Parameters:
157+
/// - width: The width of the view frame.
158+
/// - isWidthSet: Will we treat this width as a manual set? (So the width will not be overrided later)
159+
/// - height: The height of the view frame.
160+
/// - isHeightSet: Will we treat this height as a manual set? (So the height will not be overrided later)
161+
/// - alignment: The default alignment of the frame.
162+
/// - isAlignmentSet: Will we treat this alignment as a manual set? (So the alignment will not be overrided later)
163+
public init(
164+
width: CGFloat,
165+
isWidthSet: Bool = false,
166+
height: CGFloat,
167+
isHeightSet: Bool = false,
168+
alignment: Alignment = .center,
169+
isAlignmentSet: Bool = false
170+
) {
43171
self.inactive = false
44172
self.width = width
45173
self.isWidthConstrained = true
174+
self.isWidthSet = isWidthSet
46175
self.height = height
47176
self.isHeightConstrained = true
177+
self.isHeightSet = isHeightSet
178+
self.alignment = alignment
179+
self.isAlignmentSet = isAlignmentSet
48180
}
49181

50182
/// [Internal]
51-
init(oldConfiguration: DefinedViewConfiguration,
52-
width: CGFloat? = nil,
53-
height: CGFloat? = nil) {
183+
internal init(
184+
oldConfiguration: DefinedViewConfiguration,
185+
width: CGFloat? = nil,
186+
isWidthSet: Bool? = nil,
187+
height: CGFloat? = nil,
188+
isHeightSet: Bool? = nil,
189+
alignment: Alignment? = nil,
190+
isAlignmentSet: Bool? = nil
191+
) {
54192
self.inactive = oldConfiguration.inactive // this value may NOT be changed during copying.
55193
self.width = width ?? oldConfiguration.width
56194
self.isWidthConstrained = oldConfiguration.isWidthConstrained || width != nil
195+
self.isWidthSet = isWidthSet ?? oldConfiguration.isWidthSet
57196
self.height = height ?? oldConfiguration.height
58197
self.isHeightConstrained = oldConfiguration.isHeightConstrained || height != nil
198+
self.isHeightSet = isHeightSet ?? oldConfiguration.isHeightSet
199+
self.alignment = alignment ?? oldConfiguration.alignment
200+
self.isAlignmentSet = isAlignmentSet ?? (oldConfiguration.isAlignmentSet || alignment != nil)
59201
}
60202
}
Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Foundation
21
import SwiftUI
32

43
// MARK: - Protocol
@@ -7,13 +6,15 @@ import SwiftUI
76
///
87
/// You can also use it if you want to create your own `DefinedView` component.
98
///
10-
/// - TODO: Fix `status` pointer problem.
9+
/// - TODO: Finish the development of Configurations.
1110
public protocol DefinedView : View {
1211
/// Configurate all things related to view frame.
1312
///
1413
/// If you do not want to constrain the view with a bridge frame, you can disable it:
1514
///
16-
/// var viewConfiguration = DefinedViewConfiguration(inactive: true)
15+
/// ``` swift
16+
/// var viewConfiguration = DefinedViewConfiguration(inactive: true)
17+
/// ```
1718
///
1819
/// otherwise, you can set the default frame size by using basic initializer
1920
/// `init(default:loading:active:done:error:disabled:)`.
@@ -25,7 +26,9 @@ public protocol DefinedView : View {
2526
///
2627
/// If you do not want to constrain the view with background views, you can disable it:
2728
///
28-
/// var backgroundConfiguration = DefinedViewBackgroundConfiguration(inactive: true)
29+
/// ``` swift
30+
/// var backgroundConfiguration = DefinedViewBackgroundConfiguration(inactive: true)
31+
/// ```
2932
///
3033
/// otherwise, you can set the default background views by using basic initializer
3134
/// `init(default:loading:active:done:error:disabled:)`.
@@ -36,8 +39,9 @@ public protocol DefinedView : View {
3639
/// Configurate all things related to border setup.
3740
///
3841
/// If you do not want to constrain the view with a border, you can disable it:
39-
///
40-
/// var borderConfiguration = DefinedViewBorderConfiguration(inactive: true)
42+
/// ``` swift
43+
/// var borderConfiguration = DefinedViewBorderConfiguration(inactive: true)
44+
/// ```
4145
///
4246
/// otherwise, you can set the border setup by using basic initializer
4347
/// `init(default:loading:active:done:error:disabled:)`.
@@ -48,20 +52,18 @@ public protocol DefinedView : View {
4852
/// Configurate all things related to overlay view.
4953
///
5054
/// If you do not want to constrain the view with an overlay, you can disable it:
51-
///
52-
/// var overlayConfiguration = DefinedViewOverlayConfiguration(inactive: true)
55+
/// ``` swift
56+
/// var overlayConfiguration = DefinedViewOverlayConfiguration(inactive: true)
57+
/// ```
5358
///
5459
/// otherwise, you can set the default overlay views by using basic initializer
5560
/// `init(default:loading:active:done:error:disabled:)`.
5661
///
5762
/// - Note: This is only a configuration for the root view, not for the hierarchy level.
5863
var overlayConfiguration: DefinedViewOverlayConfiguration { get set }
5964

60-
///
65+
/// The status of an UI component.
6166
var status: GeneralStatus { get set }
62-
63-
// TODO: It seems like having some problem on using `$` prefix on Swift 5.5
64-
// var $status: Binding<GeneralStatus> { get set }
6567
}
6668

6769
// MARK: - Extensions for fields
@@ -72,8 +74,9 @@ extension DefinedView {
7274
get {
7375
DefinedViewConfiguration(inactive: true)
7476
}
75-
set(value) {
76-
self.viewConfiguration = value
77+
set {
78+
// WARNING: should not be assigned without defining it locally!
79+
DefinedWarning.send(from: "DefinedViewProtocol", "You should NOT do things with viewConfiguration if you did not define it locally. Nothing is going to be changed if you proceed.")
7780
}
7881
}
7982

@@ -82,8 +85,9 @@ extension DefinedView {
8285
get {
8386
DefinedViewBackgroundConfiguration(inactive: true)
8487
}
85-
set(value) {
86-
self.backgroundConfiguration = value
88+
set {
89+
// WARNING: should not be assigned without defining it locally!
90+
DefinedWarning.send(from: "DefinedViewProtocol", "You should NOT do things with backgroundConfiguration if you did not define it locally. Nothing is going to be changed if you proceed.")
8791
}
8892
}
8993

@@ -92,8 +96,9 @@ extension DefinedView {
9296
get {
9397
DefinedViewBorderConfiguration(inactive: true)
9498
}
95-
set(value) {
96-
self.borderConfiguration = value
99+
set {
100+
// WARNING: should not be assigned without defining it locally!
101+
DefinedWarning.send(from: "DefinedViewProtocol", "You should NOT do things with borderConfiguration if you did not define it locally. Nothing is going to be changed if you proceed.")
97102
}
98103
}
99104

@@ -102,8 +107,9 @@ extension DefinedView {
102107
get {
103108
DefinedViewOverlayConfiguration(inactive: true)
104109
}
105-
set(value) {
106-
self.overlayConfiguration = value
110+
set {
111+
// WARNING: should not be assigned without defining it locally!
112+
DefinedWarning.send(from: "DefinedViewProtocol", "You should NOT do things with overlayConfiguration if you did not define it locally. Nothing is going to be changed if you proceed.")
107113
}
108114
}
109115

@@ -112,8 +118,9 @@ extension DefinedView {
112118
get {
113119
return .default
114120
}
115-
set(value) {
116-
self.status = value
121+
set {
122+
// WARNING: should not be assigned without defining it locally!
123+
DefinedWarning.send(from: "DefinedViewProtocol", "You should NOT do things with status if you did not define it locally. Nothing is going to be changed if you proceed.")
117124
}
118125
}
119126
}

0 commit comments

Comments
 (0)