Skip to content

Commit 788b071

Browse files
committed
refactor: Home, Today의 각 State를 NavigationRouter를 개선하여 정리
1 parent 9b9c623 commit 788b071

8 files changed

Lines changed: 68 additions & 105 deletions

File tree

DevLog/UI/Common/MainView.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ struct MainView: View {
1111
@Environment(\.diContainer) var container: DIContainer
1212
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
1313
@State var viewModel: MainViewModel
14-
@State private var homeNavigationState = HomeNavigationState()
15-
@State private var todayNavigationState = TodayNavigationState()
14+
@State private var homeNavigationRouter = NavigationRouter<HomeRoute>()
15+
@State private var todayNavigationRouter = NavigationRouter<TodayRoute>()
1616
@State private var todoIdToPresent: TodoIdItem?
1717
@Binding var selectedTab: MainTab
1818

@@ -106,7 +106,7 @@ struct MainView: View {
106106
} detail: {
107107
homeRegularDetailView
108108
}
109-
.environment(homeNavigationState)
109+
.environment(homeNavigationRouter)
110110
case .today:
111111
NavigationSplitView {
112112
mainSidebar
@@ -115,7 +115,7 @@ struct MainView: View {
115115
} detail: {
116116
todayRegularDetailView
117117
}
118-
.environment(todayNavigationState)
118+
.environment(todayNavigationRouter)
119119
case .notification:
120120
let viewModel = makePushNotificationListViewModel()
121121
NavigationSplitView {
@@ -205,7 +205,7 @@ struct MainView: View {
205205
private var homeView: some View {
206206
Group {
207207
if isCompactLayout {
208-
NavigationStack(path: $homeNavigationState.path) {
208+
NavigationStack(path: $homeNavigationRouter.path) {
209209
homeContentView
210210
.navigationDestination(for: HomeRoute.self) { homeRoute in
211211
homeDestinationView(homeRoute)
@@ -215,7 +215,7 @@ struct MainView: View {
215215
homeContentView
216216
}
217217
}
218-
.environment(homeNavigationState)
218+
.environment(homeNavigationRouter)
219219
}
220220

221221
private var homeContentView: some View {
@@ -241,16 +241,16 @@ struct MainView: View {
241241

242242
private var homeDetailPath: Binding<[HomeRoute]> {
243243
Binding(
244-
get: { homeNavigationState.detailPath },
245-
set: { homeNavigationState.detailPath = $0 }
244+
get: { homeNavigationRouter.detailPath },
245+
set: { homeNavigationRouter.detailPath = $0 }
246246
)
247247
}
248248

249249
@ViewBuilder
250250
private var homeRegularDetailView: some View {
251251
NavigationStack(path: homeDetailPath) {
252252
Group {
253-
if let homeRoute = homeNavigationState.root {
253+
if let homeRoute = homeNavigationRouter.root {
254254
homeDestinationView(homeRoute)
255255
} else {
256256
ContentUnavailableView(
@@ -316,7 +316,7 @@ struct MainView: View {
316316
private var todayView: some View {
317317
Group {
318318
if isCompactLayout {
319-
NavigationStack(path: $todayNavigationState.path) {
319+
NavigationStack(path: $todayNavigationRouter.path) {
320320
todayContentView
321321
.navigationDestination(for: TodayRoute.self) { todayRoute in
322322
todayDestinationView(todayRoute)
@@ -326,7 +326,7 @@ struct MainView: View {
326326
todayContentView
327327
}
328328
}
329-
.environment(todayNavigationState)
329+
.environment(todayNavigationRouter)
330330
}
331331

332332
private var todayContentView: some View {
@@ -348,16 +348,16 @@ struct MainView: View {
348348

349349
private var todayDetailPath: Binding<[TodayRoute]> {
350350
Binding(
351-
get: { todayNavigationState.detailPath },
352-
set: { todayNavigationState.detailPath = $0 }
351+
get: { todayNavigationRouter.detailPath },
352+
set: { todayNavigationRouter.detailPath = $0 }
353353
)
354354
}
355355

356356
@ViewBuilder
357357
private var todayRegularDetailView: some View {
358358
NavigationStack(path: todayDetailPath) {
359359
Group {
360-
if let todayRoute = todayNavigationState.root {
360+
if let todayRoute = todayNavigationRouter.root {
361361
todayDestinationView(todayRoute)
362362
} else {
363363
ContentUnavailableView(

DevLog/UI/Common/NavigationRouter.swift

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,31 @@
88
import SwiftUI
99

1010
@Observable
11-
final class NavigationRouter {
12-
var path = NavigationPath()
11+
final class NavigationRouter<Route: Hashable> {
12+
var path: [Route] = []
1313

14-
func push(_ element: any Hashable) {
15-
Task { @MainActor in
16-
path.append(element)
14+
var root: Route? {
15+
path.first
16+
}
17+
18+
var detailPath: [Route] {
19+
get {
20+
Array(path.dropFirst())
1721
}
22+
set {
23+
if let route = root {
24+
path = [route] + newValue
25+
} else {
26+
path = newValue
27+
}
28+
}
29+
}
30+
31+
func show(_ route: Route) {
32+
path = [route]
33+
}
34+
35+
func push(_ route: Route) {
36+
path.append(route)
1837
}
1938
}

DevLog/UI/Home/HomeView.swift

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import SwiftUI
99

1010
struct HomeView: View {
1111
@Environment(\.diContainer) var container: any DIContainer
12-
@Environment(HomeNavigationState.self) var homeNavigationState
12+
@Environment(NavigationRouter<HomeRoute>.self) private var router
1313
@State var viewModel: HomeViewModel
1414
let isCompactLayout: Bool
1515
@ScaledMetric(relativeTo: .largeTitle) private var labelWidth = CGFloat(34)
@@ -261,7 +261,7 @@ struct HomeView: View {
261261
}
262262
} else {
263263
Button {
264-
homeNavigationState.show(.category(item))
264+
router.show(.category(item))
265265
} label: {
266266
labelImage(
267267
text: item.localizedName,
@@ -281,7 +281,7 @@ struct HomeView: View {
281281
}
282282
} else {
283283
Button {
284-
homeNavigationState.show(.todo(TodoIdItem(id: item.id)))
284+
router.show(.todo(TodoIdItem(id: item.id)))
285285
} label: {
286286
RecentTodoRow(todo: item)
287287
.frame(maxWidth: .infinity, alignment: .leading)
@@ -299,7 +299,7 @@ struct HomeView: View {
299299
}
300300
} else {
301301
Button {
302-
homeNavigationState.show(.webPage(item))
302+
router.show(.webPage(item))
303303
} label: {
304304
WebItemRow(item: item, showsChevron: false)
305305
.frame(maxWidth: .infinity, alignment: .leading)
@@ -399,36 +399,6 @@ struct HomeView: View {
399399

400400
}
401401

402-
@Observable
403-
final class HomeNavigationState {
404-
var path: [HomeRoute] = []
405-
406-
var root: HomeRoute? {
407-
path.first
408-
}
409-
410-
var detailPath: [HomeRoute] {
411-
get {
412-
Array(path.dropFirst())
413-
}
414-
set {
415-
if let homeRoute = root {
416-
path = [homeRoute] + newValue
417-
} else {
418-
path = newValue
419-
}
420-
}
421-
}
422-
423-
func show(_ homeRoute: HomeRoute) {
424-
path = [homeRoute]
425-
}
426-
427-
func push(_ homeRoute: HomeRoute) {
428-
path.append(homeRoute)
429-
}
430-
}
431-
432402
enum HomeRoute: Hashable {
433403
case category(TodoCategoryItem)
434404
case todo(TodoIdItem)

DevLog/UI/Home/TodoListView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import SwiftUI
99

1010
struct TodoListView: View {
11-
@Environment(HomeNavigationState.self) private var homeNavigationState
11+
@Environment(NavigationRouter<HomeRoute>.self) private var router
1212
@Environment(\.diContainer) var container: DIContainer
1313
@Environment(\.colorScheme) private var colorScheme
1414
@ScaledMetric(relativeTo: .body) private var headerHeight = 41
@@ -409,6 +409,6 @@ struct TodoListView: View {
409409
}
410410

411411
private func selectTodo(_ todoId: String) {
412-
homeNavigationState.push(.todo(TodoIdItem(id: todoId)))
412+
router.push(.todo(TodoIdItem(id: todoId)))
413413
}
414414
}

DevLog/UI/Profile/ProfileView.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import SwiftUI
99

1010
struct ProfileView: View {
1111
@State var viewModel: ProfileViewModel
12-
@State private var router = NavigationRouter()
12+
@State private var router = NavigationRouter<ProfileRoute>()
1313
@Environment(\.diContainer) private var container
1414
@FocusState private var focused: Bool
1515

@@ -88,14 +88,14 @@ struct ProfileView: View {
8888
ToolbarItem(placement: .topBarTrailing) {
8989
HStack(spacing: 0) {
9090
Button {
91-
router.push(Path.settings)
91+
router.push(.settings)
9292
} label: {
9393
Image(systemName: "gearshape")
9494
}
9595
}
9696
}
9797
}
98-
.navigationDestination(for: Path.self) { path in
98+
.navigationDestination(for: ProfileRoute.self) { path in
9999
switch path {
100100
case .settings:
101101
SettingView(viewModel: SettingViewModel(
@@ -116,6 +116,8 @@ struct ProfileView: View {
116116
todoId: todoId,
117117
showEditButton: false
118118
))
119+
case .theme, .pushNotification, .account:
120+
EmptyView()
119121
}
120122
}
121123
.onAppear { viewModel.send(.onAppear) }
@@ -350,7 +352,7 @@ struct ProfileView: View {
350352
ForEach(activities) { activity in
351353
Button {
352354
if !activity.isDeleted {
353-
router.push(Path.activity(activity.todoId))
355+
router.push(.activity(activity.todoId))
354356
}
355357
} label: {
356358
let item = TodoCategoryItem(from: activity.category)
@@ -395,8 +397,12 @@ struct ProfileView: View {
395397
.padding(.top, 4)
396398
}
397399

398-
private enum Path: Hashable {
399-
case settings
400-
case activity(String)
401-
}
400+
}
401+
402+
enum ProfileRoute: Hashable {
403+
case settings
404+
case activity(String)
405+
case theme
406+
case pushNotification
407+
case account
402408
}

DevLog/UI/Search/SearchView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import SwiftUI
1010
struct SearchView: View {
1111
@Environment(\.dismiss) private var dismiss
1212
@Environment(\.diContainer) private var container: DIContainer
13-
@State private var router = NavigationRouter()
13+
@State private var router = NavigationRouter<Path>()
1414
@State var viewModel: SearchViewModel
1515

1616
var body: some View {

DevLog/UI/Setting/SettingView.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import SwiftUI
99

1010
struct SettingView: View {
1111
@Environment(\.diContainer) var container: DIContainer
12+
@Environment(NavigationRouter<ProfileRoute>.self) private var router
1213
@State var viewModel: SettingViewModel
13-
@Environment(NavigationRouter.self) var router
1414

1515
var body: some View {
1616
let connected = viewModel.state.isNetworkConnected
1717
Form {
1818
Section {
1919
Button {
20-
router.push(Path.theme)
20+
router.push(.theme)
2121
} label: {
2222
HStack {
2323
Text(String(localized: "settings_theme"))
@@ -29,7 +29,7 @@ struct SettingView: View {
2929
}
3030

3131
Button {
32-
router.push(Path.pushNotification)
32+
router.push(.pushNotification)
3333
} label: {
3434
Text(String(localized: "settings_notifications"))
3535
.foregroundStyle(connected ? Color.primary : Color.secondary)
@@ -83,7 +83,7 @@ struct SettingView: View {
8383

8484
Section {
8585
Button {
86-
router.push(Path.account)
86+
router.push(.account)
8787
} label: {
8888
Text(String(localized: "settings_account"))
8989
}
@@ -110,7 +110,7 @@ struct SettingView: View {
110110
}
111111
.navigationTitle(String(localized: "nav_settings"))
112112
.navigationBarTitleDisplayMode(.inline)
113-
.navigationDestination(for: Path.self) { path in
113+
.navigationDestination(for: ProfileRoute.self) { path in
114114
switch path {
115115
case .theme:
116116
ThemeView(
@@ -134,6 +134,8 @@ struct SettingView: View {
134134
unlinkProviderUseCase: container.resolve(UnlinkAuthProviderUseCase.self)
135135
)
136136
)
137+
case .settings, .activity:
138+
EmptyView()
137139
}
138140
}
139141
.alert(
@@ -156,10 +158,6 @@ struct SettingView: View {
156158
}
157159
}
158160

159-
private enum Path: Hashable {
160-
case theme, pushNotification, account
161-
}
162-
163161
@ViewBuilder
164162
private var alertButtons: some View {
165163
switch viewModel.state.alertType {

0 commit comments

Comments
 (0)