|
| 1 | +// The MIT License (MIT) |
| 2 | +// Copyright © 2020 Ivan Vorobei ( [email protected]) |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in all |
| 12 | +// copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +// SOFTWARE. |
| 21 | + |
| 22 | +// Thanks @HonQii for PR! |
| 23 | + |
| 24 | +import SwiftUI |
| 25 | + |
| 26 | +@available(iOS 13.0, *) |
| 27 | +extension View { |
| 28 | + |
| 29 | + public func spAlert( |
| 30 | + isPresent: Binding<Bool>, |
| 31 | + alertView: SPAlertView, |
| 32 | + duration: TimeInterval = 2.0, |
| 33 | + haptic: SPAlertHaptic = .none |
| 34 | + ) -> some View { |
| 35 | + if isPresent.wrappedValue { |
| 36 | + let alertCompletion = alertView.completion |
| 37 | + let alertDismiss = { |
| 38 | + isPresent.wrappedValue = false |
| 39 | + alertCompletion?() |
| 40 | + } |
| 41 | + alertView.present(duration: duration, haptic: haptic, completion: alertDismiss) |
| 42 | + } |
| 43 | + return self |
| 44 | + } |
| 45 | + |
| 46 | + public func spAlert(isPresent: Binding<Bool>, |
| 47 | + title: String = "", |
| 48 | + message: String? = nil, |
| 49 | + duration: TimeInterval = 2.0, |
| 50 | + dismissOnTap: Bool = true, |
| 51 | + preset: SPAlertIconPreset = .done, |
| 52 | + haptic: SPAlertHaptic = .none, |
| 53 | + layout: SPAlertLayout? = nil, |
| 54 | + completion: (()-> Void)? = nil |
| 55 | + ) -> some View { |
| 56 | + let alertView = SPAlertView(title: title, message: message, preset: preset) |
| 57 | + alertView.dismissByTap = dismissOnTap |
| 58 | + alertView.layout = layout ?? SPAlertLayout(for: preset) |
| 59 | + alertView.completion = completion |
| 60 | + |
| 61 | + return spAlert(isPresent: isPresent, alertView: alertView, duration: duration, haptic: haptic) |
| 62 | + } |
| 63 | + |
| 64 | + public func spAlert(isPresent: Binding<Bool>, |
| 65 | + message: String, |
| 66 | + duration: TimeInterval = 2.0, |
| 67 | + dismissOnTap: Bool = true, |
| 68 | + haptic: SPAlertHaptic = .none, |
| 69 | + completion: (()-> Void)? = nil |
| 70 | + ) -> some View { |
| 71 | + |
| 72 | + let alertView = SPAlertView(message: message) |
| 73 | + alertView.dismissByTap = dismissOnTap |
| 74 | + alertView.completion = completion |
| 75 | + |
| 76 | + return spAlert(isPresent: isPresent, alertView: alertView, duration: duration, haptic: haptic) |
| 77 | + } |
| 78 | +} |
0 commit comments