JJ_iOS/JJUNGTABLE/Common/Modifier/BaseViewModifier.swift
2024-06-26 12:38:42 +09:00

56 lines
2.0 KiB
Swift

//
// 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())
}
}