How can I make progress view that can apply on every view?

Viewed 30

Can I make progress view that can apply on every view due to condition in SWIFTUI, like some extension that put progress view over current view?

I have tried with view modifier like I handle API errors and alerts:

class ProgressViewHandling: ObservableObject {
    
    @Published var isShowed: Bool?
    
    func showProgress() {
        self.isShowed = true
    }
    
    func hideProgress() {
        self.isShowed = false
    }
}
struct HandleProgressView: ViewModifier {
    
    @StateObject var progressViewHandling = ProgressViewHandling()
    
    func body(content: Content) -> some View {
        content
            .environmentObject(progressViewHandling)
            .background(
                ZStack(alignment: .center) {
                    if let isShowed = $progressViewHandling.isShowed.wrappedValue {
                        if isShowed {
                            ProgressView()
                                .progressViewStyle(CircularProgressViewStyle())
                        }
                    }
                }
            )
    }
}
func withProgressViewHandling() -> some View {
        modifier(HandleProgressView())
    }
0 Answers
Related