-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPokitLinkPopup.swift
More file actions
249 lines (220 loc) Β· 6.58 KB
/
PokitLinkPopup.swift
File metadata and controls
249 lines (220 loc) Β· 6.58 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
//
// PokitLinkPopUp.swift
// DSKit
//
// Created by κΉλν on 7/22/24.
//
import SwiftUI
import Combine
public struct PokitLinkPopup: View {
@Binding
private var type: PokitLinkPopup.PopupType?
@State
private var second: Int = 0
private let action: (() -> Void)?
private let until: Int
private let timer = Timer.publish(
every: 1,
on: .main,
in: .common
).autoconnect()
public init(type: Binding<PokitLinkPopup.PopupType?>) {
self._type = type
switch type.wrappedValue {
case let .link(_, _, until),
let .text(_, until),
let .success(_, until),
let .error(_, until),
let .warning(_, until),
let .report(_, until):
self.until = until
default:
self.until = 2
}
self.action = nil
}
private init(
type: Binding<PokitLinkPopup.PopupType?>,
until: Int,
action: (() -> Void)?
) {
self._type = type
self.until = until
self.action = action
}
public var body: some View {
ZStack {
background
popup
.padding(.vertical, 12)
.padding(.horizontal, 20)
}
.frame(width: 335, height: 60)
.transition(.move(edge: .bottom).combined(with: .opacity))
.onReceive(timer) { _ in
guard second < until else {
closedPopup()
return
}
second += 1
}
.onAppear(perform: feedback)
}
private var popup: some View {
HStack(spacing: 0) {
Button {
action?()
} label: {
VStack(alignment: .leading, spacing: 0) {
Text(title)
.lineLimit(2)
.pokitFont(titleFont)
.multilineTextAlignment(.leading)
.foregroundStyle(textColor)
if case let .link(_, url, _) = type {
Text(url)
.lineLimit(1)
.pokitFont(.detail2)
.foregroundStyle(.pokit(.text(.inverseWh)))
}
}
if case .success = type {
Image(.icon(.check))
.resizable()
.frame(width: 24, height: 24)
.foregroundStyle(.pokit(.icon(.inverseWh)))
}
Spacer()
}
closeButton
}
}
private var background: some View {
RoundedRectangle(cornerRadius: 9999, style: .continuous)
.fill(backgroundColor)
}
private var closeButton: some View {
Button(action: closedPopup) {
Group {
if case .report = type {
Image(.icon(.check))
.resizable()
} else {
Image(.icon(.x))
.resizable()
}
}
.frame(width: 24, height: 24)
.foregroundStyle(iconColor)
}
}
private func closedPopup() {
withAnimation(.pokitSpring) {
type = nil
second = 0
}
}
private func feedback() {
switch type {
case .link, .text, .warning:
UINotificationFeedbackGenerator()
.notificationOccurred(.warning)
case .success, .report:
UINotificationFeedbackGenerator()
.notificationOccurred(.success)
case .error:
UINotificationFeedbackGenerator()
.notificationOccurred(.error)
case .none: break
}
}
private var backgroundColor: Color {
switch type {
case .link, .text:
return .pokit(.bg(.tertiary))
case .success:
return .pokit(.bg(.success))
case .error, .report:
return .pokit(.bg(.error))
case .warning:
return .pokit(.bg(.warning))
case .none: return .clear
}
}
private var iconColor: Color {
switch type {
case .warning:
return .pokit(.icon(.primary))
default:
return .pokit(.icon(.inverseWh))
}
}
private var textColor: Color {
switch type {
case .warning:
return .pokit(.text(.primary))
default:
return .pokit(.text(.inverseWh))
}
}
private var titleFont: PokitFont {
switch type {
case .link: return .b2(.b)
default: return .b3(.b)
}
}
private var title: String {
switch type {
case let .link(title, _, _),
let .text(title, _),
let .success(title, _),
let .error(title, _),
let .warning(title, _),
let .report(title, _):
return title
default: return ""
}
}
public func onAction(_ action: @escaping () -> Void) -> Self {
PokitLinkPopup(
type: self.$type,
until: self.until,
action: action
)
}
}
public extension PokitLinkPopup {
enum PopupType: Equatable {
case link(title: String, url: String, until: Int = 2)
case text(title: String, until: Int = 2)
case success(title: String, until: Int = 2)
case error(title: String, until: Int = 2)
case warning(title: String, until: Int = 2)
case report(title: String, until: Int = 2)
}
}
#Preview {
VStack {
PokitLinkPopup(
type: .constant(.link(
title: "볡μ¬ν λ§ν¬ μ μ₯νκΈ°",
url: "https://www.youtube.com/watch?v=xSTwqKUyM8k"
))
)
PokitLinkPopup(
type: .constant(.text(title: "μ΅λ 30κ°μ ν¬ν·μ μμ±ν μ μμ΅λλ€.\nν¬ν·μ μμ ν λ€μ μΆκ°ν΄μ£ΌμΈμ."))
)
PokitLinkPopup(
type: .constant(.success(title: "λ§ν¬μ μ₯ μλ£"))
)
PokitLinkPopup(
type: .constant(.error(title: "λ§ν¬μ μ₯ μ€ν¨"))
)
PokitLinkPopup(
type: .constant(.warning(title: "μ μ₯κ³΅κ° λΆμ‘±"))
)
PokitLinkPopup(
type: .constant(.report(title: "μ κ³ κ° μλ£λμμ΅λλ€"))
)
}
}