-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContentSettingFeature.swift
More file actions
483 lines (454 loc) ยท 19.1 KB
/
ContentSettingFeature.swift
File metadata and controls
483 lines (454 loc) ยท 19.1 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//
// AddLinkFeature.swift
// Feature
//
// Created by ๊น๋ํ on 7/17/24.
import UIKit
import ComposableArchitecture
import DSKit
import Domain
import CoreKit
import DSKit
import Util
@Reducer
public struct ContentSettingFeature {
/// - Dependency
@Dependency(\.dismiss)
private var dismiss
@Dependency(SwiftSoupClient.self)
private var swiftSoup
@Dependency(PasteboardClient.self)
private var pasteboard
@Dependency(ContentClient.self)
private var contentClient
@Dependency(CategoryClient.self)
private var categoryClient
@Dependency(KeyboardClient.self)
private var keyboardClient
/// - State
@ObservableState
public struct State: Equatable {
public init(
contentId: Int? = nil,
urlText: String? = nil,
isShareExtension: Bool = false
) {
self.domain = .init(contentId: contentId, data: urlText)
self.isShareExtension = isShareExtension
}
fileprivate var domain: ContentSetting
var urlText: String {
get { domain.data }
set { domain.data = newValue }
}
var title: String {
get { domain.title }
set { domain.title = newValue }
}
var memo: String {
get { domain.memo }
set { domain.memo = newValue }
}
var content: BaseContentDetail? {
get { domain.content }
}
var pokitList: [BaseCategoryItem]? {
get { domain.categoryListInQuiry.data }
}
var linkTextInputState: PokitInputStyle.State = .default
var titleTextInpuState: PokitInputStyle.State = .default
var memoTextAreaState: PokitInputStyle.State = .default
@Shared(.inMemory("SelectCategory")) var categoryId: Int?
var selectedPokit: BaseCategoryItem?
var linkTitle: String? = nil
var linkImageURL: String? = nil
var linkPopup: PokitLinkPopup.PopupType?
var contentLoading: Bool = false
var saveIsLoading: Bool = false
var link: String?
var showLinkPreview = false
var isShareExtension: Bool
var pokitAddSheetPresented: Bool = false
var isKeyboardVisible: Bool = false
}
/// - Action
public enum Action: FeatureAction, ViewAction {
case view(View)
case inner(InnerAction)
case async(AsyncAction)
case scope(ScopeAction)
case delegate(DelegateAction)
@CasePathable
public enum View: Equatable, BindableAction {
/// - Binding
case binding(BindingAction<State>)
/// - Button Tapped
case ํฌํท์ ํ_๋ฒํผ_๋๋ ์๋
case ํฌํท์ ํ_ํญ๋ชฉ_๋๋ ์๋(pokit: BaseCategoryItem)
case ๋ทฐ๊ฐ_๋ํ๋ฌ์๋
case ์ ์ฅ_๋ฒํผ_๋๋ ์๋
case ํฌํท์ถ๊ฐ_๋ฒํผ_๋๋ ์๋
case ๋งํฌํ์
_๋ฒํผ_๋๋ ์๋
case ๋งํฌ์ง์ฐ๊ธฐ_๋ฒํผ_๋๋ ์๋
case ์ ๋ชฉ์ง์ฐ๊ธฐ_๋ฒํผ_๋๋ ์๋
case ๋ค๋ก๊ฐ๊ธฐ_๋ฒํผ_๋๋ ์๋
}
public enum InnerAction {
case linkPopup(URL?)
case linkPreview
case ๋ฉํ๋ฐ์ดํฐ_์กฐํ_์ํ(url: URL)
case ๋ฉํ๋ฐ์ดํ
จ_์กฐํ_๋ฐ์(title: String?, imageURL: String?)
case URL_์ ํจ์ฑ_ํ์ธ
case ๋งํฌ๋ณต์ฌ_๋ฐ์(String?)
case ์ปจํ
์ธ _์์ธ_์กฐํ_API_๋ฐ์(content: BaseContentDetail)
case ์นดํ
๊ณ ๋ฆฌ_์์ธ_์กฐํ_API_๋ฐ์(category: BaseCategory)
case ์นดํ
๊ณ ๋ฆฌ_๋ชฉ๋ก_์กฐํ_API_๋ฐ์(categoryList: BaseCategoryListInquiry)
case ์ ํํ_ํฌํท_์ธ๋ฉ๋ชจ๋ฆฌ_์ญ์
case ๋งํฌํ์
_ํ์ฑํ(PokitLinkPopup.PopupType)
case error(Error)
case ํค๋ณด๋_๊ฐ์ง_๋ฐ์(Bool)
}
public enum AsyncAction: Equatable {
case ์ปจํ
์ธ _์์ธ_์กฐํ_API(id: Int)
case ์นดํ
๊ณ ๋ฆฌ_์์ธ_์กฐํ_API(id: Int?, sharedId: Int?)
case ์นดํ
๊ณ ๋ฆฌ_๋ชฉ๋ก_์กฐํ_API
case ์ปจํ
์ธ _์์ _API
case ์ปจํ
์ธ _์ถ๊ฐ_API
case ํด๋ฆฝ๋ณด๋_๊ฐ์ง
case ํค๋ณด๋_๊ฐ์ง
}
public enum ScopeAction: Equatable { case ์์ }
public enum DelegateAction: Equatable {
case ์ ์ฅํ๊ธฐ_์๋ฃ(category: BaseCategoryItem)
case ํฌํท์ถ๊ฐํ๊ธฐ
case dismiss
}
}
/// - Initiallizer
public init() {}
/// - Reducer Core
private func core(into state: inout State, action: Action) -> Effect<Action> {
switch action {
/// - View
case .view(let viewAction):
return handleViewAction(viewAction, state: &state)
/// - Inner
case .inner(let innerAction):
return handleInnerAction(innerAction, state: &state)
/// - Async
case .async(let asyncAction):
return handleAsyncAction(asyncAction, state: &state)
/// - Scope
case .scope(let scopeAction):
return handleScopeAction(scopeAction, state: &state)
/// - Delegate
case .delegate(let delegateAction):
return handleDelegateAction(delegateAction, state: &state)
}
}
/// - Reducer body
public var body: some ReducerOf<Self> {
BindingReducer(action: \.view)
Reduce(self.core)
}
}
//MARK: - FeatureAction Effect
private extension ContentSettingFeature {
/// - View Effect
func handleViewAction(_ action: Action.View, state: inout State) -> Effect<Action> {
switch action {
case .binding(\.urlText):
enum CancelID { case urlTextChanged }
return .send(.inner(.URL_์ ํจ์ฑ_ํ์ธ)).debounce(
/// - 1์ด๋ง๋ค `urlText`๋ณํ์ ๋ง์ง๋ง์ ๊ฐ์งํ์ฌ ์ด๋ฒคํธ ๋ฐฉ์ถ
id: CancelID.urlTextChanged,
for: 1,
scheduler: DispatchQueue.main
)
case .binding:
return .none
case .ํฌํท์ ํ_๋ฒํผ_๋๋ ์๋:
return .send(.async(.์นดํ
๊ณ ๋ฆฌ_๋ชฉ๋ก_์กฐํ_API))
case .ํฌํท์ ํ_ํญ๋ชฉ_๋๋ ์๋(pokit: let pokit):
state.selectedPokit = pokit
return .none
case .๋ทฐ๊ฐ_๋ํ๋ฌ์๋:
var mergeEffect: [Effect<Action>] = [
.send(.async(.์นดํ
๊ณ ๋ฆฌ_๋ชฉ๋ก_์กฐํ_API)),
.send(.inner(.URL_์ ํจ์ฑ_ํ์ธ)),
.send(.async(.ํด๋ฆฝ๋ณด๋_๊ฐ์ง)),
.send(.async(.ํค๋ณด๋_๊ฐ์ง))
]
if let id = state.domain.contentId {
mergeEffect.append(.send(.async(.์ปจํ
์ธ _์์ธ_์กฐํ_API(id: id))))
}
return .merge(mergeEffect)
case .์ ์ฅ_๋ฒํผ_๋๋ ์๋:
let isEdit = state.domain.categoryId != nil
if state.domain.title == Constants.์ ๋ชฉ์_์
๋ ฅํด์ฃผ์ธ์_๋ฌธ๊ตฌ {
state.domain.title = state.title
}
state.saveIsLoading = true
return isEdit
? .send(.async(.์ปจํ
์ธ _์์ _API))
: .send(.async(.์ปจํ
์ธ _์ถ๊ฐ_API))
case .ํฌํท์ถ๊ฐ_๋ฒํผ_๋๋ ์๋:
guard state.domain.categoryTotalCount < 30 else {
/// ๐จ Error Case [1]: ํฌํท ๊ฐฏ์๊ฐ 30๊ฐ ์ด์์ผ ๊ฒฝ์ฐ
state.linkPopup = .text(title: Constants.ํฌํท_์ต๋_๊ฐฏ์_๋ฌธ๊ตฌ)
return .none
}
/// ๋ฐํ
์ํธ ๋ด๋ฆฌ๊ณ `ํฌํท์ถ๊ฐํ๊ธฐ` depth ์ถ๊ฐ
state.pokitAddSheetPresented = false
return .send(.delegate(.ํฌํท์ถ๊ฐํ๊ธฐ))
case .๋ค๋ก๊ฐ๊ธฐ_๋ฒํผ_๋๋ ์๋:
state.categoryId = nil
return state.isShareExtension
? .send(.delegate(.dismiss))
: .run { _ in await dismiss() }
case .๋งํฌํ์
_๋ฒํผ_๋๋ ์๋:
guard case .link = state.linkPopup else { return .none }
return .send(.inner(.๋งํฌ๋ณต์ฌ_๋ฐ์(state.link)))
case .๋งํฌ์ง์ฐ๊ธฐ_๋ฒํผ_๋๋ ์๋:
state.domain.data = ""
state.domain.title = ""
return .none
case .์ ๋ชฉ์ง์ฐ๊ธฐ_๋ฒํผ_๋๋ ์๋:
state.domain.title = ""
return .none
}
}
/// - Inner Effect
func handleInnerAction(_ action: Action.InnerAction, state: inout State) -> Effect<Action> {
switch action {
case let .linkPopup(url):
guard let url else { return .none }
state.link = url.absoluteString
state.linkPopup = .link(
title: Constants.๋ณต์ฌํ_๋งํฌ_์ ์ฅํ๊ธฐ_๋ฌธ๊ตฌ,
url: url.absoluteString
)
return .none
case .linkPreview:
state.showLinkPreview = true
return .none
case .๋ฉํ๋ฐ์ดํฐ_์กฐํ_์ํ(url: let url):
return .run { send in
async let title = try? swiftSoup.parseOGTitle(url)
async let imageURL = try? swiftSoup.parseOGImageURL(url)
await send(
.inner(.๋ฉํ๋ฐ์ดํ
จ_์กฐํ_๋ฐ์(title: title, imageURL: imageURL)),
animation: .pokitDissolve
)
}
case let .๋ฉํ๋ฐ์ดํ
จ_์กฐํ_๋ฐ์(title: title, imageURL: imageURL):
let contentTitle = state.title.isEmpty
? Constants.์ ๋ชฉ์_์
๋ ฅํด์ฃผ์ธ์_๋ฌธ๊ตฌ
: state.title
state.linkImageURL = imageURL ?? Constants.๊ธฐ๋ณธ_์ธ๋ค์ผ_์ฃผ์.absoluteString
state.linkTitle = title ?? contentTitle
if let title, state.domain.title.isEmpty {
state.domain.title = title
}
state.domain.thumbNail = imageURL
return .send(.inner(.linkPreview), animation: .pokitDissolve)
case .URL_์ ํจ์ฑ_ํ์ธ:
guard
let url = URL(string: state.domain.data),
!state.domain.data.isEmpty
else {
/// ๐จ Error Case [1]: ์ฌ๋ฐ๋ฅธ ๋งํฌ๊ฐ ์๋ ๋
state.linkPopup = nil
state.linkTitle = nil
state.domain.title = ""
state.linkImageURL = nil
state.domain.thumbNail = nil
return .none
}
return .send(.inner(.๋ฉํ๋ฐ์ดํฐ_์กฐํ_์ํ(url: url)), animation: .pokitDissolve)
case .๋งํฌ๋ณต์ฌ_๋ฐ์(let urlText):
state.linkPopup = nil
state.link = nil
guard let urlText else { return .none }
state.domain.data = urlText
return .send(.inner(.URL_์ ํจ์ฑ_ํ์ธ))
case .์ปจํ
์ธ _์์ธ_์กฐํ_API_๋ฐ์(content: let content):
state.domain.content = content
state.domain.data = content.data
state.domain.contentId = content.id
state.domain.title = content.title
state.domain.categoryId = content.category.categoryId
state.domain.memo = content.memo
state.domain.alertYn = content.alertYn
state.contentLoading = false
let id = content.category.categoryId
return .merge(
.send(.inner(.URL_์ ํจ์ฑ_ํ์ธ)),
.send(.async(.์นดํ
๊ณ ๋ฆฌ_์์ธ_์กฐํ_API(id: id, sharedId: state.categoryId)))
)
case .์นดํ
๊ณ ๋ฆฌ_์์ธ_์กฐํ_API_๋ฐ์(category: let category):
state.selectedPokit = BaseCategoryItem(
id: category.categoryId,
userId: 0,
categoryName: category.categoryName,
categoryImage: category.categoryImage,
contentCount: 0,
createdAt: "",
//TODO: v2 property ์์
openType: .๋น๊ณต๊ฐ,
keywordType: .default,
userCount: 0,
isFavorite: false
)
return .none
case .์นดํ
๊ณ ๋ฆฌ_๋ชฉ๋ก_์กฐํ_API_๋ฐ์(categoryList: let categoryList):
/// - `์นดํ
๊ณ ๋ฆฌ_๋ชฉ๋ก_์กฐํ`์ filter ์ต์
์ `false`๋ก ํด๋์๊ธฐ ๋๋ฌธ์ `๋ฏธ๋ถ๋ฅ` ์นดํ
๊ณ ๋ฆฌ ๋ํ ํญ๋ชฉ์์ ์กฐํ๊ฐ ๊ฐ๋ฅํจ
/// [1]. `๋ฏธ๋ถ๋ฅ`์ ํด๋นํ๋ ์ธ๋ฑ์ค ๋ฒํธ์ ํญ๋ชฉ์ ์ฒดํฌ, ์๋ค๋ฉด ๋ชฉ๋ก๊ฐฑ์ ์ด ๋ถ๊ฐํจ
guard
let unclassifiedItemIdx = categoryList.data?.firstIndex(where: {
$0.categoryName == Constants.๋ฏธ๋ถ๋ฅ
})
else { return .none }
guard
let unclassifiedItem = categoryList.data?.first(where: {
$0.categoryName == Constants.๋ฏธ๋ถ๋ฅ
})
else { return .none }
/// [2]. ์๋ก์ด list๋ณ์๋ฅผ ๋ง๋ค์ด์ฃผ๊ณ ์นดํ
๊ณ ๋ฆฌ ํญ๋ชฉ ์์๋ฅผ ์ฌ๋ฐฐ์น (์ต์ ์ ์ ๋ ฌ ์ ๋ฏธ๋ถ๋ฅ๋ ํญ์ ๋งจ ๋ง์ง๋ง)
var list = categoryList
list.data?.remove(at: unclassifiedItemIdx)
list.data?.insert(unclassifiedItem, at: 0)
/// [3]. ๋๋ฉ์ธ ํญ๋ชฉ ๋ฆฌ์คํธ์ list ํ ๋น
state.domain.categoryListInQuiry = list
/// [4]. ์ต์ด ์ง์
์: `๋ฏธ๋ถ๋ฅ`๋ก ์ค์ ํจ. ํฌํท ์ถ๊ฐํ๊ณ ์๋ค๋ฉด `@Shared`์ ๊ฐ์ด ์๊ธฐ ๋๋ฌธ์ ๊ธฐ์กด ๊ฐ์ ์
๋ฐ์ดํธํจ
if state.selectedPokit == nil {
state.selectedPokit = unclassifiedItem
}
return .none
case .์ ํํ_ํฌํท_์ธ๋ฉ๋ชจ๋ฆฌ_์ญ์ :
state.selectedPokit = nil
return .none
case let .๋งํฌํ์
_ํ์ฑํ(type):
state.linkPopup = type
state.saveIsLoading = false
return .none
case let .error(error):
guard let errorResponse = error as? ErrorResponse else { return .none }
return .send(
.inner(.๋งํฌํ์
_ํ์ฑํ(.error(title: errorResponse.message))),
animation: .pokitSpring
)
case let .ํค๋ณด๋_๊ฐ์ง_๋ฐ์(response):
state.isKeyboardVisible = response
return .none
}
}
/// - Async Effect
func handleAsyncAction(_ action: Action.AsyncAction, state: inout State) -> Effect<Action> {
switch action {
case .์ปจํ
์ธ _์์ธ_์กฐํ_API(id: let id):
state.contentLoading = true
return .run { send in
let content = try await contentClient.์ปจํ
์ธ _์์ธ_์กฐํ("\(id)").toDomain()
await send(.inner(.์ปจํ
์ธ _์์ธ_์กฐํ_API_๋ฐ์(content: content)), animation: .pokitDissolve)
}
case let .์นดํ
๊ณ ๋ฆฌ_์์ธ_์กฐํ_API(id, sharedId):
return .run { send in
if let sharedId {
let category = try await categoryClient.์นดํ
๊ณ ๋ฆฌ_์์ธ_์กฐํ("\(sharedId)").toDomain()
await send(.inner(.์นดํ
๊ณ ๋ฆฌ_์์ธ_์กฐํ_API_๋ฐ์(category: category)))
} else if let id {
let category = try await categoryClient.์นดํ
๊ณ ๋ฆฌ_์์ธ_์กฐํ("\(id)").toDomain()
await send(.inner(.์นดํ
๊ณ ๋ฆฌ_์์ธ_์กฐํ_API_๋ฐ์(category: category)))
}
}
case .์นดํ
๊ณ ๋ฆฌ_๋ชฉ๋ก_์กฐํ_API:
let request = BasePageableRequest(
page: state.domain.pageable.page,
size: 30,
sort: state.domain.pageable.sort
)
let id = state.domain.categoryId
let sharedId = state.categoryId
return .merge(
.send(.async(.์นดํ
๊ณ ๋ฆฌ_์์ธ_์กฐํ_API(id: id, sharedId: sharedId))),
categoryListFetch(request: request)
)
case .์ปจํ
์ธ _์์ _API:
guard
let contentId = state.domain.contentId,
let categoryId = state.selectedPokit?.id,
let category = state.domain.categoryListInQuiry.data?.first(where: {
$0.id == categoryId
})
else { return .none }
let request = ContentBaseRequest(
data: state.domain.data,
title: state.domain.title,
categoryId: categoryId,
memo: state.domain.memo,
alertYn: state.domain.alertYn.rawValue,
thumbNail: state.domain.thumbNail
)
return .run { send in
let _ = try await contentClient.์ปจํ
์ธ _์์ (
"\(contentId)",
request
)
await send(.inner(.์ ํํ_ํฌํท_์ธ๋ฉ๋ชจ๋ฆฌ_์ญ์ ))
await send(.delegate(.์ ์ฅํ๊ธฐ_์๋ฃ(category: category)))
} catch: { error, send in
await send(.inner(.error(error)))
}
case .์ปจํ
์ธ _์ถ๊ฐ_API:
guard
let categoryId = state.selectedPokit?.id,
let category = state.domain.categoryListInQuiry.data?.first(where: {
$0.id == categoryId
})
else { return .none }
let request = ContentBaseRequest(
data: state.domain.data,
title: state.domain.title,
categoryId: categoryId,
memo: state.domain.memo,
alertYn: state.domain.alertYn.rawValue,
thumbNail: state.domain.thumbNail
)
return .run { send in
let content = try await contentClient.์ปจํ
์ธ _์ถ๊ฐ(request)
await send(.inner(.์ ํํ_ํฌํท_์ธ๋ฉ๋ชจ๋ฆฌ_์ญ์ ))
await send(.delegate(.์ ์ฅํ๊ธฐ_์๋ฃ(category: category)))
} catch: { error, send in
await send(.inner(.error(error)))
}
case .ํด๋ฆฝ๋ณด๋_๊ฐ์ง:
return .run { send in
for await _ in self.pasteboard.changes() {
let url = try await pasteboard.probableWebURL()
await send(.inner(.linkPopup(url)), animation: .pokitSpring)
}
}
case .ํค๋ณด๋_๊ฐ์ง:
return .run { send in
for await detect in await keyboardClient.isVisible() {
await send(.inner(.ํค๋ณด๋_๊ฐ์ง_๋ฐ์(detect)), animation: .pokitSpring)
}
}
}
}
/// - Scope Effect
func handleScopeAction(_ action: Action.ScopeAction, state: inout State) -> Effect<Action> {
return .none
}
/// - Delegate Effect
func handleDelegateAction(_ action: Action.DelegateAction, state: inout State) -> Effect<Action> {
return .none
}
func categoryListFetch(request: BasePageableRequest) -> Effect<Action> {
return .run { send in
let categoryList = try await categoryClient.์นดํ
๊ณ ๋ฆฌ_๋ชฉ๋ก_์กฐํ(request, false, true).toDomain()
await send(.inner(.์นดํ
๊ณ ๋ฆฌ_๋ชฉ๋ก_์กฐํ_API_๋ฐ์(categoryList: categoryList)), animation: .pokitDissolve)
}
}
}