AcaMate_iOS/AcaMate/1. View/10. Common/NavigationView.swift

105 lines
2.9 KiB
Swift

//
// NavigationView.swift
// AcaMate
//
// Created by Sean Kim on 12/12/24.
//
import SwiftUI
import Combine
///
struct NavigationView: View {
@EnvironmentObject var appVM: AppViewModel
@State private var history: [PathName] = [.Intro]
var body: some View {
VStack(spacing: 0) {
ZStack {
switch appVM.naviState.path {
case .NONE:
EmptyView()
case .Intro:
IntroView()
case .Login :
LoginView()
case .SelectAcademy(let bids):
SelectAcademyView(bids: bids)
case .Main:
MainView()
case .ChatRoom(let id):
ChattingRoomView(roomID: id)
}
}
}
.onChange(of: appVM.naviState) { old, new in
switch new.act {
case .NONE:
break
case .ADD:
addHistory(path: new.path)
case .POP:
popHistory()
case .RESET:
resetHistory(path: new.path)
case .MOVE:
moveHistory(path: new.path)
}
printLog("\(old.path) => \(new.path)")
showHistory()
}
.onTapGesture {
endTextEditing()
}
.fullDrawView(.Normal.normal)
.setAlert()
.setNetwork()
.loadingView(isLoading: $appVM.isLoading)
}
///
private func addHistory(path: PathName) {
history.append(path)
}
///
private func popHistory() {
history.removeLast()
appVM.naviState.set(act: .NONE, path: history.last ?? .NONE)
// naviState.set(act: .NONE, path: history.last ?? .NONE)
}
///
private func resetHistory(path: PathName) {
history.removeAll()
addHistory(path: path)
}
/// ,
private func moveHistory(path: PathName) {
if path == .NONE {
appVM.naviState.set(act: .RESET, path: history.first ?? .Main)
// naviState.set(act: .RESET, path: history.first ?? .Main)
}
if history.contains(path) {
let remove = history.count - history.firstIndex(of: path)! - 1
history.removeLast(remove)
if remove > 0 {
appVM.naviState.set(act: .NONE, path: path)
// naviState.set(act: .NONE, path: path)
return
}
}
appVM.naviState.set(act: .RESET, path: path)
// naviState.set(act: .RESET, path: path)
}
private func showHistory() {
printLog(history)
}
}