-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainTabFeature.swift
More file actions
321 lines (301 loc) ยท 11.4 KB
/
MainTabFeature.swift
File metadata and controls
321 lines (301 loc) ยท 11.4 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
//
// MainTabFeature.swift
// App
//
// Created by ๊น๋ฏผํธ on 7/11/24.
import SwiftUI
import ComposableArchitecture
import FeaturePokit
import FeatureRecommend
import FeatureContentDetail
import Domain
import DSKit
import Util
import CoreKit
@Reducer
public struct MainTabFeature {
/// - Dependency
@Dependency(PasteboardClient.self)
private var pasteBoard
@Dependency(CategoryClient.self)
private var categoryClient
@Dependency(UserDefaultsClient.self)
private var userDefaults
@Dependency(\.amplitude.track)
private var amplitudeTrack
/// - State
@ObservableState
public struct State: Equatable {
var selectedTab: MainTab = .pokit
var isBottomSheetPresented: Bool = false
var linkPopup: PokitLinkPopup.PopupType?
var isErrorSheetPresented: Bool = false
var link: String?
var error: BaseError?
var path: StackState<MainTabPath.State> = .init()
var pokit: PokitRootFeature.State
var recommend: RecommendFeature.State = .init()
@Presents var contentDetail: ContentDetailFeature.State?
@Shared(.inMemory("SelectCategory")) var categoryId: Int?
@Shared(.inMemory("PushTapped")) var isPushTapped: Bool = false
var categoryOfSavedContent: BaseCategoryItem?
public init() {
self.pokit = .init()
}
}
/// - Action
public enum Action: FeatureAction, BindableAction, ViewAction {
case binding(BindingAction<State>)
case pushAlertTapped(Bool)
case view(View)
case inner(InnerAction)
case async(AsyncAction)
case scope(ScopeAction)
case delegate(DelegateAction)
/// Todo: scope๋ก ์ด๋
case path(StackAction<MainTabPath.State, MainTabPath.Action>)
case pokit(PokitRootFeature.Action)
case recommend(RecommendFeature.Action)
case contentDetail(PresentationAction<ContentDetailFeature.Action>)
@CasePathable
public enum View: Equatable {
case addButtonTapped
case addSheetTypeSelected(TabAddSheetType)
case ๋งํฌํ์
_๋ฒํผ_๋๋ ์๋
case onAppear
case onOpenURL(url: URL)
case ๊ฒฝ๊ณ _ํ์ธ๋ฒํผ_ํด๋ฆญ
case ๊ฒ์_๋ฒํผ_๋๋ ์๋
case ์๋ฆผ_๋ฒํผ_๋๋ ์๋
}
public enum InnerAction: Equatable {
case ๋งํฌ์ถ๊ฐ๋ฐ์์ ์ด๋(contentId: Int)
case linkCopySuccess(URL?)
case ๊ณต์ ํฌํท_์ด๋(sharedCategory: CategorySharing.SharedCategory)
case ๊ฒฝ๊ณ _๋์(BaseError)
case errorSheetPresented(Bool)
case ๋งํฌํ์
_ํ์ฑํ(PokitLinkPopup.PopupType)
case ์นดํ
๊ณ ๋ฆฌ์์ธ_์ด๋(category: BaseCategoryItem)
}
public enum AsyncAction: Equatable {
case ๊ณต์ ๋ฐ์_์นดํ
๊ณ ๋ฆฌ_์กฐํ(categoryId: Int)
}
public enum ScopeAction: Equatable { case doNothing }
public enum DelegateAction: Equatable {
case ๋งํฌ์ถ๊ฐํ๊ธฐ
case ํฌํท์ถ๊ฐํ๊ธฐ
case ๋ก๊ทธ์์
case ํ์ํํด
case ์๋ฆผํจ์ด๋
}
}
/// initiallizer
public init() {}
/// - Reducer Core
private func core(into state: inout State, action: Action) -> Effect<Action> {
switch action {
case .binding(\.linkPopup):
guard state.linkPopup == nil else { return .none }
state.categoryOfSavedContent = nil
return .none
case .binding(\.selectedTab):
switch state.selectedTab {
case .pokit:
amplitudeTrack(.view_home_pokit(entryPoint: "pokit"))
case .recommend:
amplitudeTrack(.view_home_recommend(entryPoint: "recommend"))
}
return .none
case .binding:
return .none
case let .pushAlertTapped(isTapped):
if isTapped {
return .send(.delegate(.์๋ฆผํจ์ด๋))
} else {
return .none
}
/// - 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)
case .path:
return .none
case .pokit:
return .none
case .recommend:
return .none
case .contentDetail:
return .none
}
}
/// - Reducer body
public var body: some ReducerOf<Self> {
Scope(state: \.pokit, action: \.pokit) { PokitRootFeature() }
Scope(state: \.recommend, action: \.recommend) {
RecommendFeature()
}
BindingReducer()
navigationReducer
Reduce(self.core)
.ifLet(\.$contentDetail, action: \.contentDetail) {
ContentDetailFeature()
}
}
}
//MARK: - FeatureAction Effect
private extension MainTabFeature {
/// - View Effect
func handleViewAction(_ action: Action.View, state: inout State) -> Effect<Action> {
switch action {
case .addButtonTapped:
state.isBottomSheetPresented.toggle()
return .none
case .addSheetTypeSelected(let type):
state.isBottomSheetPresented = false
switch type {
case .๋งํฌ์ถ๊ฐ: return .send(.delegate(.๋งํฌ์ถ๊ฐํ๊ธฐ))
case .ํฌํท์ถ๊ฐ: return .send(.delegate(.ํฌํท์ถ๊ฐํ๊ธฐ))
}
case .๋งํฌํ์
_๋ฒํผ_๋๋ ์๋:
return linkPopupButtonTapped(state: &state)
case .onAppear:
if state.isPushTapped {
return .send(.pushAlertTapped(true))
}
return .merge(
.run { send in
for await _ in self.pasteBoard.changes() {
let url = try await pasteBoard.probableWebURL()
await send(.inner(.linkCopySuccess(url)), animation: .pokitSpring)
}
},
.publisher {
state.$isPushTapped.publisher
.map(Action.pushAlertTapped)
}
)
case .onOpenURL(url: let url):
guard
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
else { return .none }
let queryItems = components.queryItems ?? []
guard
let categoryIdString = queryItems.first(where: { $0.name == "categoryId" })?.value,
let categoryId = Int(categoryIdString)
else { return .none }
switch state.selectedTab {
case .pokit:
amplitudeTrack(.view_home_pokit(entryPoint: "deeplink"))
case .recommend:
amplitudeTrack(.view_home_recommend(entryPoint: "deeplink"))
}
return .send(.async(.๊ณต์ ๋ฐ์_์นดํ
๊ณ ๋ฆฌ_์กฐํ(categoryId: categoryId)))
case .๊ฒฝ๊ณ _ํ์ธ๋ฒํผ_ํด๋ฆญ:
state.error = nil
return .run { send in await send(.inner(.errorSheetPresented(false))) }
case .๊ฒ์_๋ฒํผ_๋๋ ์๋:
switch state.selectedTab {
case .pokit: return .none
case .recommend:
return RecommendFeature()
.reduce(
into: &state.recommend,
action: .view(.๊ฒ์_๋ฒํผ_๋๋ ์๋)
)
.map(Action.recommend)
}
case .์๋ฆผ_๋ฒํผ_๋๋ ์๋:
switch state.selectedTab {
case .pokit: return .none
case .recommend:
return RecommendFeature()
.reduce(
into: &state.recommend,
action: .view(.์๋ฆผ_๋ฒํผ_๋๋ ์๋)
)
.map(Action.recommend)
}
}
}
/// - Inner Effect
func handleInnerAction(_ action: Action.InnerAction, state: inout State) -> Effect<Action> {
switch action {
case let .linkCopySuccess(url):
guard let url else { return .none }
state.linkPopup = .link(
title: Constants.๋ณต์ฌํ_๋งํฌ_์ ์ฅํ๊ธฐ_๋ฌธ๊ตฌ,
url: url.absoluteString
)
state.link = url.absoluteString
return .none
case let .๊ฒฝ๊ณ _๋์(error):
state.error = error
return .run { send in await send(.inner(.errorSheetPresented(true))) }
case let .errorSheetPresented(isPresented):
state.isErrorSheetPresented = isPresented
return .none
case let .๋งํฌํ์
_ํ์ฑํ(type):
state.linkPopup = type
return .none
case let .์นดํ
๊ณ ๋ฆฌ์์ธ_์ด๋(category):
if category.categoryName == Constants.๋ฏธ๋ถ๋ฅ {
state.selectedTab = .pokit
state.path.removeAll()
return .send(.pokit(.delegate(.๋ฏธ๋ถ๋ฅ_์นดํ
๊ณ ๋ฆฌ_ํ์ฑํ)))
}
state.path.append(.์นดํ
๊ณ ๋ฆฌ์์ธ(.init(category: category)))
return .none
default: return .none
}
}
/// - Async Effect
func handleAsyncAction(_ action: Action.AsyncAction, state: inout State) -> Effect<Action> {
switch action {
case let .๊ณต์ ๋ฐ์_์นดํ
๊ณ ๋ฆฌ_์กฐํ(categoryId: categoryId):
return .run { send in
do {
let request = BasePageableRequest(page: 0, size: 10, sort: ["createdAt", "desc"])
let sharedCategory = try await categoryClient.๊ณต์ ๋ฐ์_์นดํ
๊ณ ๋ฆฌ_์กฐํ("\(categoryId)", request).toDomain()
await send(.inner(.๊ณต์ ํฌํท_์ด๋(sharedCategory: sharedCategory)), animation: .smooth)
} catch {
guard let errorResponse = error as? ErrorResponse else { return }
let errorDomain = BaseError(response: errorResponse)
await send(.inner(.๊ฒฝ๊ณ _๋์(errorDomain)))
}
}
}
}
/// - 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 linkPopupButtonTapped(state: inout State) -> Effect<Action> {
switch state.linkPopup {
case .link:
state.linkPopup = nil
return .send(.delegate(.๋งํฌ์ถ๊ฐํ๊ธฐ))
case .success:
state.linkPopup = nil
guard let category = state.categoryOfSavedContent else { return .none }
state.categoryOfSavedContent = nil
return .send(.inner(.์นดํ
๊ณ ๋ฆฌ์์ธ_์ด๋(category: category)))
case .error, .text, .warning, .report, .none:
return .none
}
}
}