Skip to content

add SwiftUI modifier #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,36 @@ alertView.present(duration: 1.5, haptic: .success, completion: nil)

You can remove duration and completion, its have default values.

### SwiftUI

Use like system alert:

* only show message tips

```swift
Button("Show alert") {
showAlert = true
}.alert(isPresent: $showAlert, message: "this is message only")
```

* show message, title, image and other configuration

```swift
Button("Show alert") {
showAlert = true
}.alert(isPresent: $showAlert,
title: "Alert title",
message: "Alert message",
duration: 2.0,
dismissOnTap: false,
present: .custom(UIImage(systemName: "heart")!),
haptic: .success,
layout: .init(),
completion: {
print("Alert is destory")
})
```

## Other Projects

#### [SPPermissions](https://github.com/ivanvorobei/SPPermissions)
Expand Down
60 changes: 60 additions & 0 deletions Sources/SPAlert/SwiftUI/SwiftUIAlert.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// SwiftUIView.swift
//
//
// Created by Qi Hon on 2021/6/4.
//

import SwiftUI

@available(iOS 13.0, *)
extension View {
public func alert(
isPresent: Binding<Bool>,
alertView: SPAlertView,
duration: TimeInterval = 2.0,
haptic: SPAlertHaptic = .none
) -> some View {
if isPresent.wrappedValue {
let alertCompletion = alertView.completion
let alertDismiss = {
isPresent.wrappedValue = false
alertCompletion?()
}
alertView.present(duration: duration, haptic: haptic, completion: alertDismiss)
}
return self
}

public func alert(isPresent: Binding<Bool>,
title: String = "",
message: String? = nil,
duration: TimeInterval = 2.0,
dismissOnTap: Bool = true,
present: SPAlertIconPreset = .done,
haptic: SPAlertHaptic = .none,
layout: SPAlertLayout = .init(),
completion: (()-> Void)? = nil
) -> some View {
let alertView = SPAlertView(title: title, message: message, preset: present)
alertView.dismissByTap = dismissOnTap
alertView.layout = layout
alertView.completion = completion

return alert(isPresent: isPresent, alertView: alertView, duration: duration, haptic: haptic)
}

public func alert(isPresent: Binding<Bool>,
message: String,
duration: TimeInterval = 2.0,
dismissOnTap: Bool = true,
haptic: SPAlertHaptic = .none,
completion: (()-> Void)? = nil
) -> some View {
let alertView = SPAlertView(message: message)
alertView.dismissByTap = dismissOnTap
alertView.completion = completion

return alert(isPresent: isPresent, alertView: alertView, duration: duration, haptic: haptic)
}
}