-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBaseWidget.swift
More file actions
264 lines (228 loc) · 8.63 KB
/
Copy pathBaseWidget.swift
File metadata and controls
264 lines (228 loc) · 8.63 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import SwiftUI
// MARK: - Widget Protocol
/// Protocol that all widgets should conform to for consistent behavior
protocol WidgetProtocol {
associatedtype OptionsType: Codable & Equatable
associatedtype ViewModelType: ObservableObject
var options: OptionsType { get set }
var isEditing: Bool { get set }
var onEditingEnd: (() -> Void)? { get set }
init(options: OptionsType, isEditing: Bool, onEditingEnd: (() -> Void)?)
init(viewModel: ViewModelType, options: OptionsType, isEditing: Bool, onEditingEnd: (() -> Void)?)
}
// MARK: - Common Widget States
/// Common states that widgets can be in
enum WidgetState {
case loading
case loaded
case error(String)
case empty
}
// MARK: - Widget Content Builder
/// Helper for building common widget content patterns
enum WidgetContentBuilder {
/// Creates a standard loading view
static func loadingView() -> some View {
ProgressView()
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
/// Creates a standard error view
static func errorView(_ message: String) -> some View {
CaptionBText(message, textColor: .textSecondary)
.frame(maxWidth: .infinity, alignment: .leading)
}
/// Creates a standard source attribution row
static func sourceRow(source: String) -> some View {
HStack(spacing: 0) {
HStack {
CaptionBText(t("widgets__widget__source"))
.lineLimit(1)
}
.frame(maxWidth: .infinity, alignment: .leading)
HStack {
CaptionBText(source)
.lineLimit(1)
}
.frame(maxWidth: .infinity, alignment: .trailing)
}
.padding(.top, 16)
}
}
/// BaseWidget component that forms the foundation for all widget types in the app
struct BaseWidget<Content: View>: View {
// MARK: - Properties
/// Widget type identifier
let type: WidgetType
/// Content to display within the widget
let content: Content
/// Flag indicating if the widget is in editing mode
var isEditing: Bool = false
/// When false, the widget content has no gray background (e.g. suggestions).
var hasBackground: Bool = true
/// Callback to signal when editing should end
var onEditingEnd: (() -> Void)?
/// State for showing the delete confirmation dialog
@State private var showDeleteDialog = false
@EnvironmentObject private var navigation: NavigationViewModel
@EnvironmentObject private var widgets: WidgetsViewModel
@EnvironmentObject private var currency: CurrencyViewModel
/// Widget metadata computed from type
private var metadata: WidgetMetadata {
let fiatSymbol = currency.symbol
return WidgetMetadata(type: type, fiatSymbol: fiatSymbol)
}
// MARK: - Initialization
/// Initialize a new widget with required and optional parameters
/// - Parameters:
/// - type: Widget type identifier
/// - isEditing: Flag indicating if the widget is in editing mode
/// - onEditingEnd: Callback to signal when editing should end
/// - content: Content view builder for the widget
init(
type: WidgetType,
isEditing: Bool = false,
hasBackground: Bool = true,
onEditingEnd: (() -> Void)? = nil,
@ViewBuilder content: () -> Content
) {
self.type = type
self.isEditing = isEditing
self.hasBackground = hasBackground
self.onEditingEnd = onEditingEnd
self.content = content()
}
private func onEdit() {
navigation.navigate(.widgetDetail(type))
onEditingEnd?()
}
private func onDelete() {
showDeleteDialog = true
}
var body: some View {
VStack(spacing: 0) {
if isEditing {
HStack {
HStack(spacing: 16) {
Image(metadata.icon)
.resizable()
.frame(width: 32, height: 32)
BodyMSBText(truncate(metadata.name, 18))
.lineLimit(1)
}
Spacer()
// Action buttons when in edit mode
if isEditing {
HStack(spacing: 8) {
// Delete button
Button {
onDelete()
} label: {
Image("trash")
.resizable()
.foregroundColor(.textPrimary)
.frame(width: 24, height: 24)
}
.frame(width: 32, height: 32)
.contentShape(Rectangle())
.accessibilityIdentifier("\(metadata.name)_WidgetActionDelete")
// Edit button
Button {
onEdit()
} label: {
Image("gear-six")
.resizable()
.foregroundColor(.textPrimary)
.frame(width: 24, height: 24)
}
.frame(width: 32, height: 32)
.contentShape(Rectangle())
.accessibilityIdentifier("\(metadata.name)_WidgetActionEdit")
Image("burger")
.resizable()
.foregroundColor(.textPrimary)
.frame(width: 24, height: 24)
.frame(width: 32, height: 32)
.contentShape(Rectangle())
.overlay {
Color.clear
.frame(width: 44, height: 44)
.contentShape(Rectangle())
.trackDragHandle()
}
.accessibilityIdentifier("\(metadata.name)_WidgetActionReorder")
}
}
}
}
// Widget content (only shown when not editing)
if !isEditing {
content
}
}
.contentShape(Rectangle())
.accessibilityElement(children: .contain)
.accessibilityIdentifierIfPresent(isEditing ? nil : "\(type.rawValue.capitalized)Widget")
.frame(maxWidth: .infinity)
.padding((hasBackground || isEditing) ? 16 : 0)
.background((hasBackground || isEditing) ? Color.gray6 : Color.clear)
.cornerRadius(hasBackground || isEditing ? 16 : 0)
.alert(
t("widgets__delete__title"),
isPresented: $showDeleteDialog,
actions: {
Button(t("common__cancel"), role: .cancel) {
showDeleteDialog = false
}
Button(t("common__delete_yes"), role: .destructive) {
widgets.deleteWidget(type)
showDeleteDialog = false
}
},
message: {
Text(t("widgets__delete__description", variables: ["name": metadata.name]))
}
)
}
/// Truncate a string to a maximum length
private func truncate(_ text: String, _ maxLength: Int) -> String {
if text.count <= maxLength {
return text
}
let index = text.index(text.startIndex, offsetBy: maxLength - 3)
return String(text[..<index]) + "..."
}
}
// Preview for the BaseWidget
#Preview {
VStack {
BaseWidget(
type: .facts,
isEditing: false,
onEditingEnd: {
print("Editing ended")
}
) {
Text("Widget Content Goes Here")
.frame(height: 100)
.frame(maxWidth: .infinity)
}
BaseWidget(
type: .news,
isEditing: true,
onEditingEnd: {
print("Editing ended")
}
) {
Text("Widget Content Goes Here")
.frame(height: 100)
.frame(maxWidth: .infinity)
}
}
.padding()
.background(Color.black)
.environmentObject(WidgetsViewModel())
.environmentObject(NavigationViewModel())
.environmentObject(CurrencyViewModel())
.environmentObject(SettingsViewModel.shared)
.preferredColorScheme(.dark)
}