-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContentCardFeature.swift
More file actions
197 lines (174 loc) ยท 6.69 KB
/
ContentCardFeature.swift
File metadata and controls
197 lines (174 loc) ยท 6.69 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
//
// LinkCardFeature.swift
// Feature
//
// Created by ๊น๋ํ on 11/17/24.
import SwiftUI
import ComposableArchitecture
import Domain
import CoreKit
import DSKit
import Util
@Reducer
public struct ContentCardFeature {
/// - Dependency
@Dependency(SwiftSoupClient.self)
private var swiftSoupClient
@Dependency(\.openURL)
private var openURL
@Dependency(ContentClient.self)
private var contentClient
/// - State
@ObservableState
public struct State: Equatable, Identifiable {
public let id = UUID()
public var content: BaseContentItem
public init(content: BaseContentItem) {
self.content = content
}
}
/// - 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 {
case ์ปจํ
์ธ _ํญ๋ชฉ_๋๋ ์๋
case ์ปจํ
์ธ _ํญ๋ชฉ_์ผ๋ฐฅ_๋ฒํผ_๋๋ ์๋
case ์ฆ๊ฒจ์ฐพ๊ธฐ_๋ฒํผ_๋๋ ์๋
case ๋ฉํ๋ฐ์ดํฐ_์กฐํ
}
@CasePathable
public enum InnerAction: Equatable {
case ๋ฉํ๋ฐ์ดํฐ_์กฐํ_์ํ_๋ฐ์(String)
case ์ฆ๊ฒจ์ฐพ๊ธฐ_API_๋ฐ์(Bool)
}
@CasePathable
public enum AsyncAction: Equatable {
case ๋ฉํ๋ฐ์ดํฐ_์กฐํ_์ํ
case ์ฆ๊ฒจ์ฐพ๊ธฐ_API
case ์ฆ๊ฒจ์ฐพ๊ธฐ_์ทจ์_API
case ์ธ๋ค์ผ_์์ _API
}
public enum ScopeAction: Equatable { case doNothing }
public enum DelegateAction: Equatable {
case ์ปจํ
์ธ _ํญ๋ชฉ_์ผ๋ฐฅ_๋ฒํผ_๋๋ ์๋(content: BaseContentItem)
}
}
/// - 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> {
Reduce(self.core)
}
}
//MARK: - FeatureAction Effect
private extension ContentCardFeature {
/// - View Effect
func handleViewAction(_ action: Action.View, state: inout State) -> Effect<Action> {
switch action {
case .์ปจํ
์ธ _ํญ๋ชฉ_๋๋ ์๋:
guard let url = URL(string: state.content.data) else {
return .none
}
return .run { _ in await openURL(url) }
case .์ปจํ
์ธ _ํญ๋ชฉ_์ผ๋ฐฅ_๋ฒํผ_๋๋ ์๋:
return .send(.delegate(.์ปจํ
์ธ _ํญ๋ชฉ_์ผ๋ฐฅ_๋ฒํผ_๋๋ ์๋(content: state.content)))
case .๋ฉํ๋ฐ์ดํฐ_์กฐํ:
return shared(.async(.๋ฉํ๋ฐ์ดํฐ_์กฐํ_์ํ), state: &state)
case .์ฆ๊ฒจ์ฐพ๊ธฐ_๋ฒํผ_๋๋ ์๋:
guard let isFavorite = state.content.isFavorite else {
return .none
}
UIImpactFeedbackGenerator(style: .light)
.impactOccurred()
return isFavorite
? shared(.async(.์ฆ๊ฒจ์ฐพ๊ธฐ_์ทจ์_API), state: &state)
: shared(.async(.์ฆ๊ฒจ์ฐพ๊ธฐ_API), state: &state)
}
}
/// - Inner Effect
func handleInnerAction(_ action: Action.InnerAction, state: inout State) -> Effect<Action> {
switch action {
case let .๋ฉํ๋ฐ์ดํฐ_์กฐํ_์ํ_๋ฐ์(imageURL):
state.content.thumbNail = imageURL
return shared(.async(.์ธ๋ค์ผ_์์ _API), state: &state)
case .์ฆ๊ฒจ์ฐพ๊ธฐ_API_๋ฐ์(let favorite):
state.content.isFavorite = favorite
return .none
}
}
/// - Async Effect
func handleAsyncAction(_ action: Action.AsyncAction, state: inout State) -> Effect<Action> {
switch action {
case .๋ฉํ๋ฐ์ดํฐ_์กฐํ_์ํ:
guard let url = URL(string: state.content.data) else {
return .none
}
return .run { send in
let imageURL = try? await swiftSoupClient.parseOGImageURL(url)
await send(.inner(.๋ฉํ๋ฐ์ดํฐ_์กฐํ_์ํ_๋ฐ์(imageURL ?? Constants.๊ธฐ๋ณธ_์ธ๋ค์ผ_์ฃผ์.absoluteString)))
}
case .์ฆ๊ฒจ์ฐพ๊ธฐ_API:
return .run { [id = state.content.id] send in
let _ = try await contentClient.์ฆ๊ฒจ์ฐพ๊ธฐ("\(id)")
await send(.inner(.์ฆ๊ฒจ์ฐพ๊ธฐ_API_๋ฐ์(true)), animation: .pokitDissolve)
}
case .์ฆ๊ฒจ์ฐพ๊ธฐ_์ทจ์_API:
return .run { [id = state.content.id] send in
try await contentClient.์ฆ๊ฒจ์ฐพ๊ธฐ_์ทจ์("\(id)")
await send(.inner(.์ฆ๊ฒจ์ฐพ๊ธฐ_API_๋ฐ์(false)), animation: .pokitDissolve)
}
case .์ธ๋ค์ผ_์์ _API:
return .run { [content = state.content] _ in
let request = ThumbnailRequest(thumbnail: content.thumbNail)
try await contentClient.์ธ๋ค์ผ_์์ ("\(content.id)", request)
}
}
}
/// - 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 shared(_ action: Action, state: inout State) -> Effect<Action> {
switch action {
case .view(let viewAction):
return handleViewAction(viewAction, state: &state)
case .inner(let innerAction):
return handleInnerAction(innerAction, state: &state)
case .async(let asyncAction):
return handleAsyncAction(asyncAction, state: &state)
case .scope(let scopeAction):
return handleScopeAction(scopeAction, state: &state)
case .delegate(let delegateAction):
return handleDelegateAction(delegateAction, state: &state)
}
}
}