-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRootView.swift
More file actions
122 lines (116 loc) · 4.18 KB
/
Copy pathRootView.swift
File metadata and controls
122 lines (116 loc) · 4.18 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
//
// RootView.swift
// DevLogPresentation
//
// Created by opfic on 5/2/25.
//
import SwiftUI
import Combine
import DevLogCore
import DevLogDomain
public struct RootView: View {
@Environment(\.diContainer) var container: DIContainer
@State var viewModel: RootViewModel
@State private var selectedRoute: Route?
@State private var selectedMainTab: MainTab?
private let widgetURLTab: (URL) -> MainTab?
private let pushNotificationTodoIdPublisher: AnyPublisher<String, Never>
private let clearPushNotificationRoute: () -> Void
public init(
sessionUseCase: ObserveAuthSessionUseCase,
networkConnectivityUseCase: ObserveNetworkConnectivityUseCase,
systemThemeUseCase: ObserveSystemThemeUseCase,
widgetURLTab: @escaping (URL) -> MainTab?,
pushNotificationTodoIdPublisher: AnyPublisher<String, Never>,
clearPushNotificationRoute: @escaping () -> Void
) {
self._viewModel = State(initialValue: RootViewModel(
sessionUseCase: sessionUseCase,
networkConnectivityUseCase: networkConnectivityUseCase,
systemThemeUseCase: systemThemeUseCase
))
self.widgetURLTab = widgetURLTab
self.pushNotificationTodoIdPublisher = pushNotificationTodoIdPublisher
self.clearPushNotificationRoute = clearPushNotificationRoute
}
public var body: some View {
ZStack {
Color(UIColor.systemGroupedBackground).ignoresSafeArea()
if let signIn = viewModel.state.signIn {
if signIn {
MainView(
container: container,
selectedTab: $selectedMainTab
)
} else {
LoginView(viewModel: LoginViewModel(
signInUseCase: container.resolve(SignInUseCase.self))
)
}
}
}
.preferredColorScheme(viewModel.state.theme.colorScheme)
.onAppear { viewModel.send(.onAppear) }
.onChange(of: viewModel.state.signIn) { _, value in
guard let value else { return }
if value {
selectedMainTab = .home
} else {
selectedMainTab = nil
}
}
.onOpenURL { url in
guard let mainTab = widgetURLTab(url) else { return }
switch viewModel.state.signIn {
case .some(false):
break
case .some(true):
selectedMainTab = mainTab
case .none:
break
}
}
.alert(viewModel.state.alertTitle, isPresented: Binding(
get: { viewModel.state.showAlert },
set: { viewModel.send(.setAlert($0)) }
)) {
Button(String(localized: "common_close"), role: .cancel) { }
} message: {
Text(viewModel.state.alertMessage)
}
.sheet(item: $selectedRoute) { route in
switch route {
case .todoDetail(let todoId):
NavigationStack {
TodoDetailView(viewModel: TodoDetailViewModel(
fetchTodoUseCase: container.resolve(FetchTodoByIdUseCase.self),
fetchReferenceItemsUseCase: container.resolve(FetchReferenceItemsUseCase.self),
upsertUseCase: container.resolve(UpsertTodoUseCase.self),
todoId: todoId,
showEditButton: false
))
.toolbar {
ToolbarLeadingButton {
selectedRoute = nil
}
}
}
.background(Color(.secondarySystemBackground))
.presentationDragIndicator(.visible)
}
}
.onReceive(pushNotificationTodoIdPublisher) { todoId in
selectedRoute = .todoDetail(todoId)
clearPushNotificationRoute()
}
}
}
private enum Route: Equatable, Identifiable {
case todoDetail(String)
var id: String {
switch self {
case .todoDetail(let todoId):
return "todo:\(todoId)"
}
}
}