|
| 1 | +// |
| 2 | +// SwiftUIView.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Qi Hon on 2021/6/4. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +@available(iOS 13.0, *) |
| 11 | +extension View { |
| 12 | + public func alert( |
| 13 | + isPresent: Binding<Bool>, |
| 14 | + alertView: SPAlertView, |
| 15 | + duration: TimeInterval = 2.0, |
| 16 | + haptic: SPAlertHaptic = .none |
| 17 | + ) -> some View { |
| 18 | + if isPresent.wrappedValue { |
| 19 | + let alertCompletion = alertView.completion |
| 20 | + let alertDismiss = { |
| 21 | + isPresent.wrappedValue = false |
| 22 | + alertCompletion?() |
| 23 | + } |
| 24 | + alertView.present(duration: duration, haptic: haptic, completion: alertDismiss) |
| 25 | + } |
| 26 | + return self |
| 27 | + } |
| 28 | + |
| 29 | + public func alert(isPresent: Binding<Bool>, |
| 30 | + title: String = "", |
| 31 | + message: String? = nil, |
| 32 | + duration: TimeInterval = 2.0, |
| 33 | + dismissOnTap: Bool = true, |
| 34 | + present: SPAlertIconPreset = .done, |
| 35 | + haptic: SPAlertHaptic = .none, |
| 36 | + layout: SPAlertLayout = .init(), |
| 37 | + completion: (()-> Void)? = nil |
| 38 | + ) -> some View { |
| 39 | + let alertView = SPAlertView(title: title, message: message, preset: present) |
| 40 | + alertView.dismissByTap = dismissOnTap |
| 41 | + alertView.layout = layout |
| 42 | + alertView.completion = completion |
| 43 | + |
| 44 | + return alert(isPresent: isPresent, alertView: alertView, duration: duration, haptic: haptic) |
| 45 | + } |
| 46 | + |
| 47 | + public func alert(isPresent: Binding<Bool>, |
| 48 | + message: String, |
| 49 | + duration: TimeInterval = 2.0, |
| 50 | + dismissOnTap: Bool = true, |
| 51 | + haptic: SPAlertHaptic = .none, |
| 52 | + completion: (()-> Void)? = nil |
| 53 | + ) -> some View { |
| 54 | + let alertView = SPAlertView(message: message) |
| 55 | + alertView.dismissByTap = dismissOnTap |
| 56 | + alertView.completion = completion |
| 57 | + |
| 58 | + return alert(isPresent: isPresent, alertView: alertView, duration: duration, haptic: haptic) |
| 59 | + } |
| 60 | +} |
0 commit comments