forked from RevenueCat/purchases-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextComponentView.swift
199 lines (182 loc) · 6.46 KB
/
TextComponentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// TextComponentView.swift
//
// Created by Josh Holtz on 6/11/24.
import Foundation
import RevenueCat
import SwiftUI
#if PAYWALL_COMPONENTS
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
struct TextComponentView: View {
@Environment(\.componentViewState)
private var componentViewState
@Environment(\.screenCondition)
private var screenCondition
private let viewModel: TextComponentViewModel
internal init(viewModel: TextComponentViewModel) {
self.viewModel = viewModel
}
var body: some View {
viewModel.styles(
state: self.componentViewState,
condition: self.screenCondition
) { style in
Group {
if style.visible {
Text(style.text)
.font(style.textStyle)
.fontWeight(style.fontWeight)
.fixedSize(horizontal: false, vertical: true)
.multilineTextAlignment(style.horizontalAlignment)
.foregroundStyle(style.color)
.padding(style.padding)
.background(style.backgroundColor)
.padding(style.margin)
} else {
EmptyView()
}
}
}
}
}
#if DEBUG
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
struct TextComponentView_Previews: PreviewProvider {
static var previews: some View {
// Default
TextComponentView(
// swiftlint:disable:next force_try
viewModel: try! .init(
localizedStrings: [
"id_1": .string("Hello, world")
],
component: .init(
text: "id_1",
color: .init(light: "#000000")
)
)
)
.previewLayout(.sizeThatFits)
.previewDisplayName("Default")
// Customizations
TextComponentView(
// swiftlint:disable:next force_try
viewModel: try! .init(
localizedStrings: [
"id_1": .string("Hello, world")
],
component: .init(
text: "id_1",
fontFamily: nil,
fontWeight: .black,
color: .init(light: "#ff0000"),
backgroundColor: .init(light: "#dedede"),
padding: .init(top: 10,
bottom: 10,
leading: 20,
trailing: 20),
margin: .init(top: 20,
bottom: 20,
leading: 10,
trailing: 10),
textStyle: .footnote,
horizontalAlignment: .leading
)
)
)
.previewLayout(.sizeThatFits)
.previewDisplayName("Customizations")
// State - Selected
TextComponentView(
// swiftlint:disable:next force_try
viewModel: try! .init(
localizedStrings: [
"id_1": .string("Hello, world")
],
component: .init(
text: "id_1",
color: .init(light: "#000000"),
overrides: .init(
states: .init(
selected: .init(
fontWeight: .black,
color: .init(light: "#ff0000"),
backgroundColor: .init(light: "#0000ff"),
padding: .init(top: 10,
bottom: 10,
leading: 10,
trailing: 10),
margin: .init(top: 10,
bottom: 10,
leading: 10,
trailing: 10),
textStyle: .title
)
)
)
)
)
)
.environment(\.componentViewState, .selected)
.previewLayout(.sizeThatFits)
.previewDisplayName("State - Selected")
// Condition - Medium
TextComponentView(
// swiftlint:disable:next force_try
viewModel: try! .init(
localizedStrings: [
"id_1": .string("THIS TEXT SHOULDN'T SHOW"),
"id_2": .string("Showing medium condition")
],
component: .init(
text: "id_1",
color: .init(light: "#000000"),
overrides: .init(
conditions: .init(
medium: .init(
text: "id_2"
)
)
)
)
)
)
.environment(\.screenCondition, .medium)
.previewLayout(.sizeThatFits)
.previewDisplayName("Condition - Medium")
// Condition - Has medium but not medium
TextComponentView(
// swiftlint:disable:next force_try
viewModel: try! .init(
localizedStrings: [
"id_1": .string("Showing compact condition"),
"id_2": .string("SHOULDN'T SHOW MEDIUM")
],
component: .init(
text: "id_1",
color: .init(light: "#000000"),
overrides: .init(
conditions: .init(
medium: .init(
text: "id_2"
)
)
)
)
)
)
.environment(\.screenCondition, .compact)
.previewLayout(.sizeThatFits)
.previewDisplayName("Condition - Has medium but not medium")
}
}
#endif
#endif