forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReferralProgramView.swift
More file actions
209 lines (188 loc) · 5.93 KB
/
Copy pathReferralProgramView.swift
File metadata and controls
209 lines (188 loc) · 5.93 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
import AppKit
import OmiTheme
import SwiftUI
@MainActor
final class ReferralViewModel: ObservableObject {
enum State: Equatable {
case idle
case loading
case loaded(String)
case failed
}
@Published private(set) var state: State = .idle
private let loadLink: () async throws -> ReferralLinkResponse
init() {
self.loadLink = {
try await APIClient.shared.getReferralLink()
}
}
init(loadLink: @escaping () async throws -> ReferralLinkResponse) {
self.loadLink = loadLink
}
func load() async {
guard state == .idle || state == .failed else { return }
state = .loading
do {
state = .loaded(try await loadLink().referralURL)
} catch {
state = .failed
}
}
}
struct ReferralProgramView: View {
@StateObject private var viewModel: ReferralViewModel
@State private var copied = false
private let showsIntroduction: Bool
init(showsIntroduction: Bool = true) {
self.showsIntroduction = showsIntroduction
_viewModel = StateObject(wrappedValue: ReferralViewModel())
}
init(viewModel: ReferralViewModel, showsIntroduction: Bool = true) {
self.showsIntroduction = showsIntroduction
_viewModel = StateObject(wrappedValue: viewModel)
}
var body: some View {
VStack(spacing: OmiSpacing.xl) {
if showsIntroduction {
Image(systemName: "gift")
.scaledFont(size: 28, weight: .semibold)
.foregroundColor(Ink.primary)
.frame(width: 52, height: 52)
.background(Circle().fill(Ink.wash))
.accessibilityHidden(true)
VStack(spacing: OmiSpacing.sm) {
Text("Give a friend one free month of Operator")
.scaledFont(size: OmiType.title, weight: .semibold)
.foregroundColor(Ink.primary)
.multilineTextAlignment(.center)
Text("Share your unique link. When a friend joins Omi, they'll get one month of Operator free.")
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.secondary)
.multilineTextAlignment(.center)
.fixedSize(horizontal: false, vertical: true)
}
}
referralControl
}
.frame(maxWidth: .infinity)
.task { await viewModel.load() }
}
@ViewBuilder
private var referralControl: some View {
switch viewModel.state {
case .idle, .loading:
ProgressView("Getting your link…")
.controlSize(.small)
.foregroundColor(Ink.secondary)
.frame(minHeight: 44)
case .loaded(let link):
HStack(spacing: OmiSpacing.sm) {
Text(link)
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.secondary)
.lineLimit(1)
.truncationMode(.middle)
.textSelection(.enabled)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.horizontal, OmiSpacing.md)
.frame(height: 38)
.settingsGlassWell(radius: SettingsGlassMetrics.controlRadius)
.accessibilityIdentifier("referral-link")
Button {
copy(link)
} label: {
Label(copied ? "Copied" : "Copy link", systemImage: copied ? "checkmark" : "doc.on.doc")
}
.buttonStyle(OmiButtonStyle(.primary, size: .compact))
.accessibilityIdentifier("copy-referral-link")
}
case .failed:
VStack(spacing: OmiSpacing.sm) {
Text("We couldn't get your referral link.")
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.secondary)
Button("Try again") {
Task { await viewModel.load() }
}
.buttonStyle(OmiButtonStyle(.secondary, size: .compact))
.accessibilityIdentifier("retry-referral-link")
}
.frame(minHeight: 44)
}
}
private func copy(_ link: String) {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(link, forType: .string)
copied = true
Task {
try? await Task.sleep(for: .seconds(2))
copied = false
}
}
}
struct ReferralSheetView: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
VStack(alignment: .trailing, spacing: 0) {
Button {
dismiss()
} label: {
Image(systemName: "xmark")
.scaledFont(size: OmiType.caption, weight: .semibold)
.foregroundColor(Ink.secondary)
.frame(width: 28, height: 28)
.background(Circle().fill(Ink.wash))
}
.buttonStyle(.plain)
.accessibilityLabel("Close")
ReferralProgramView()
.padding(.horizontal, OmiSpacing.xxl)
.padding(.bottom, OmiSpacing.xxl)
}
.padding(OmiSpacing.lg)
.frame(width: 540, height: 340)
.background(Ink.surface)
}
}
struct ReferralTopBarButton: View {
let action: () -> Void
@State private var isHovering = false
var body: some View {
Button(action: action) {
HStack(spacing: OmiSpacing.xs) {
Image(systemName: "gift")
.scaledFont(size: OmiType.caption, weight: .semibold)
.frame(width: TopNavigationPillMetrics.iconWidth)
Text("Refer")
.scaledFont(size: OmiType.caption, weight: .semibold)
.lineLimit(1)
.fixedSize()
}
.foregroundStyle(Ink.primary)
.padding(.horizontal, TopNavigationPillMetrics.horizontalPadding)
.frame(height: TopNavigationPillMetrics.height)
.background {
Capsule(style: .continuous)
.fill(isHovering ? Ink.wash : Color.clear)
}
.overlay {
Capsule(style: .continuous).strokeBorder(Ink.hairline, lineWidth: 1)
}
.contentShape(Capsule(style: .continuous))
.fixedSize()
}
.buttonStyle(.plain)
.onHover { isHovering = $0 }
.help("Refer a friend")
.accessibilityLabel("Refer a friend")
.accessibilityIdentifier("top-navigation-refer")
}
}
extension SettingsContentView {
var referralSection: some View {
settingsCard(settingId: "referral.link") {
ReferralProgramView()
.padding(.vertical, OmiSpacing.xl)
}
}
}