-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPokitLinkCard.swift
More file actions
222 lines (195 loc) ยท 6.11 KB
/
PokitLinkCard.swift
File metadata and controls
222 lines (195 loc) ยท 6.11 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
//
// PokitLinkCard.swift
// DSKit
//
// Created by ๊น๋ํ on 7/10/24.
//
import SwiftUI
import Util
import NukeUI
public struct PokitLinkCard<Item: PokitLinkCardItem>: View {
private let link: Item
private let state: PokitLinkCard.State
private let type: PokitLinkCard.CardType
private let action: (() -> Void)?
private let kebabAction: (() -> Void)?
private let fetchMetaData: (() -> Void)?
private let favoriteAction: (() -> Void)?
private let selectAction: (() -> Void)?
public init(
link: Item,
state: PokitLinkCard.State,
type: PokitLinkCard.CardType = .accept,
action: (() -> Void)? = nil,
kebabAction: (() -> Void)? = nil,
fetchMetaData: (() -> Void)? = nil,
favoriteAction: (() -> Void)? = nil,
selectAction: (() -> Void)? = nil
) {
self.link = link
self.state = state
self.type = type
self.action = action
self.kebabAction = kebabAction
self.fetchMetaData = fetchMetaData
self.favoriteAction = favoriteAction
self.selectAction = selectAction
}
public var body: some View {
VStack(spacing: 20) {
Group {
if let action {
Button(action: { action() }) {
buttonLabel
}
} else {
buttonLabel
}
}
.padding(.top, state == .top ? 0 : 20)
if case .top = state {
divider
}
if case .middle = state {
divider
}
}
}
@MainActor
private var buttonLabel: some View {
HStack(spacing: 12) {
if let url = URL(string: link.thumbNail) {
thumbleNail(url: url)
} else {
placeholder
}
VStack(spacing: 8) {
HStack {
title
Spacer(minLength: kebabAction != nil ? 24 : 0)
}
HStack {
subTitle
Spacer()
}
HStack {
badges()
Spacer()
}
}
.overlay(alignment: .topTrailing) {
if let kebabAction {
kebabButton(kebabAction)
}
}
}
.overlay(alignment: .bottomLeading) {
if case .linkList = type,
let isFavorite = link.isFavorite {
PokitBookmark(
state: isFavorite ? .active : .default,
action: { favoriteAction?() }
)
.padding(4)
}
}
.overlay(alignment: .topLeading) {
if case .unCatgorized(let isSelected) = type {
PokitCheckBox(
baseState: .default,
selectedState: .filled,
isSelected: .constant(isSelected),
shape: .rectangle,
action: { selectAction?() }
)
.padding(4)
}
}
}
private var title: some View {
Text(link.title)
.pokitFont(.b3(.b))
.foregroundStyle(.pokit(.text(.primary)))
.multilineTextAlignment(.leading)
.lineLimit(2)
}
private var subTitle: some View {
Text("\(link.createdAt) โข \(link.domain)")
.pokitFont(.detail2)
.foregroundStyle(.pokit(.text(.tertiary)))
.multilineTextAlignment(.leading)
}
@ViewBuilder
private func badges() -> some View {
let isUnCategorized = link.categoryName == Constants.๋ฏธ๋ถ๋ฅ
HStack(spacing: 6) {
PokitBadge(
state: isUnCategorized
? .unCategorized
: .default(link.categoryName)
)
if let isRead = link.isRead, !isRead {
PokitBadge(state: .unRead)
}
if let memo = link.memo, !memo.isEmpty {
PokitBadge(state: .memo)
}
}
}
@ViewBuilder
private func kebabButton(_ kebabAction: @escaping () -> Void) -> some View {
Button(action: kebabAction) {
Image(.icon(.kebab))
.resizable()
.frame(width: 24, height: 24)
.foregroundStyle(.pokit(.icon(.primary)))
}
}
@MainActor
private func thumbleNail(url: URL) -> some View {
LazyImage(url: url) { phase in
Group {
if let image = phase.image {
image
.resizable()
.aspectRatio(contentMode: .fill)
} else if phase.error != nil {
placeholder
.onAppear { fetchMetaData?() }
} else {
placeholder
}
}
.animation(.pokitDissolve, value: phase.image)
.frame(width: 124, height: 94)
.clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous))
}
}
private var divider: some View {
Rectangle()
.fill(.pokit(.color(.grayScale(._100))))
.frame(height: 1)
}
private var placeholder: some View {
ZStack {
Color.pokit(.bg(.disable))
PokitSpinner()
.foregroundStyle(.pokit(.icon(.brand)))
.frame(width: 48, height: 48)
}
.frame(width: 124, height: 94)
.clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous))
}
}
extension PokitLinkCard {
public enum State {
case top
case middle
case bottom
}
public enum CardType {
case linkList
case unCatgorized(isSelected: Bool)
case accept
}
}