// // BaseViewModifier.swift // JJUNGTABLE // // Created by Sean Kim on 6/11/24. // import SwiftUI import Combine struct BaseViewModifier: ViewModifier { @ObservedObject private var networkMonitor = NetworkMonitor.shared @EnvironmentObject var viewModel: ViewModel func body(content: Content) -> some View { content // 네트워크 끊기게 되면 여기서 처리 함 .onChange(of: networkMonitor.isConnected) { isConnectd in if !isConnectd { viewModel.alertData = .init(body: """ 네트워크 문제가 발생하였습니다. 확인 후 다시 실행해주세요. """, button: [ButtonType(name: "확인", role: .none , function: {exit(1)})]) viewModel.showAlert.toggle() } } // 알럿은 공통단으로 다음과 같이 구성을 해놓을 거임 .alert(viewModel.alertData.title, isPresented: $viewModel.showAlert, presenting: $viewModel.alertData) { data in let count = data.button.count ForEach(0 ..< count, id: \.self) { index in let btn = data.wrappedValue.button[index] Button(role: btn.role) { printLog(btn) if let function = btn.function { function() } } label: { Text("\(btn.name)") } } } message: { data in Text("\(data.body.wrappedValue)") } } } extension View { func setBaseViewModifier() -> some View { self.modifier(BaseViewModifier()) } }