@@ -11,20 +11,48 @@ import OpenGraphShims
11
11
12
12
// MARK: - ViewAlias
13
13
14
+ /// A protocol for creating view aliases that can be used to reference source views indirectly.
15
+ ///
16
+ /// ViewAlias allows the creation of placeholder views that will be replaced with actual
17
+ /// source views at render time. This enables more flexible view composition patterns
18
+ /// and reuse of view hierarchies across different parts of an application.
19
+ ///
20
+ /// Conforming types must provide an empty initializer and will inherit the view
21
+ /// implementation from the source view when rendered.
14
22
protocol ViewAlias : PrimitiveView {
15
23
init ( )
16
24
}
17
25
18
26
// MARK: - View + ViewAlias Extension
19
27
20
28
extension View {
29
+ /// Defines a view alias that will be replaced with the provided source view.
30
+ ///
31
+ /// Use this method to associate a view alias with a source view. When the
32
+ /// alias is used elsewhere in the view hierarchy, it will be replaced with
33
+ /// the source view provided here.
34
+ ///
35
+ /// - Parameters:
36
+ /// - alias: The view alias type to associate with the source view.
37
+ /// - source: A closure that returns the source view to be used when rendering the alias.
38
+ /// - Returns: A view that establishes the connection between the alias and source view.
21
39
func viewAlias< Alias, Source> (
22
40
_ alias: Alias . Type ,
23
41
_ source: ( ) -> Source
24
42
) -> some View where Alias: ViewAlias , Source: View {
25
43
modifier ( StaticSourceWriter < Alias , Source > ( source: source ( ) ) )
26
44
}
27
45
46
+ /// Conditionally defines a view alias based on a predicate.
47
+ ///
48
+ /// Use this method to associate a view alias with a source view only when
49
+ /// a specific condition is met based on the ViewInputPredicate.
50
+ ///
51
+ /// - Parameters:
52
+ /// - predicate: The predicate type used to determine if the alias should be applied.
53
+ /// - alias: The view alias type to associate with the source view.
54
+ /// - source: A closure that returns the source view to be used when rendering the alias.
55
+ /// - Returns: A view that conditionally establishes the connection between the alias and source view.
28
56
func viewAlias< Predicate, Alias, Source> (
29
57
if predicate: Predicate . Type ,
30
58
_ alias: Alias . Type ,
@@ -33,6 +61,16 @@ extension View {
33
61
modifier ( StaticSourceWriter < Alias , Source > ( source: source ( ) ) . requiring ( predicate) )
34
62
}
35
63
64
+ /// Defines an optional view alias that will be replaced with the provided source view when it exists.
65
+ ///
66
+ /// Use this method to associate a view alias with an optional source view. When the
67
+ /// alias is used elsewhere in the view hierarchy, it will be replaced with the source
68
+ /// view if it's non-nil, or not rendered if the source is nil.
69
+ ///
70
+ /// - Parameters:
71
+ /// - alias: The view alias type to associate with the optional source view.
72
+ /// - source: A closure that returns the optional source view to be used when rendering the alias.
73
+ /// - Returns: A view that establishes the conditional connection between the alias and source view.
36
74
func optionalViewAlias< Alias, Source> (
37
75
_ alias: Alias . Type ,
38
76
_ source: ( ) -> Source ?
@@ -64,6 +102,14 @@ private protocol AnySourceFormula {
64
102
65
103
// MARK: - OptionalViewAlias
66
104
105
+ /// A property wrapper that provides conditional access to a view alias based on source availability.
106
+ ///
107
+ /// `OptionalViewAlias` checks whether a source view has been defined for the specified
108
+ /// view alias type. If a source exists, the `wrappedValue` provides an instance of the
109
+ /// alias; otherwise, it returns `nil`.
110
+ ///
111
+ /// This is useful for conditionally including views in a hierarchy when their source views
112
+ /// may or may not be defined.
67
113
struct OptionalViewAlias < Alias> : DynamicProperty where Alias: ViewAlias {
68
114
static func _makeProperty< Value> ( in buffer: inout _DynamicPropertyBuffer , container: _GraphValue < Value > , fieldOffset: Int , inputs: inout _GraphInputs ) {
69
115
if let source = inputs [ SourceInput< Alias> . self ] . top {
@@ -80,8 +126,10 @@ struct OptionalViewAlias<Alias>: DynamicProperty where Alias: ViewAlias {
80
126
}
81
127
}
82
128
129
+ /// Indicates whether a source view has been defined for this alias.
83
130
var sourceExists : Bool
84
131
132
+ /// Returns an instance of the alias if a source view exists, otherwise `nil`.
85
133
var wrappedValue : Alias ? {
86
134
sourceExists ? Alias ( ) : nil
87
135
}
0 commit comments