Skip to content
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ AlertToast(displayMode: DisplayMode = .alert,
- **Alert:** pop at the center of the screen.
- **HUD:** drop from the top of the screen.
- **Banner:** pop/slide from the bottom of the screen.
- **Custom:** present any view of your own at the center of the screen (see [Custom view](#custom-view)).

#### Available alert types:
- **Regular:** text only (title and subtitle).
Expand Down Expand Up @@ -238,6 +239,21 @@ AlertToast(type: .image(String, Color), title: Optional(String), subTitle: Optio
AlertToast(type: .loading, title: Optional(String), subTitle: Optional(String))
```

#### Custom view:
Present any view you like using the `customView` initializer. It is shown centered like `.alert` and respects `duration` / `tapToDismiss`.

```swift
.toast(isPresenting: $showToast) {
AlertToast {
HStack {
ProgressView()
Text("Uploading…")
.font(.headline)
}
}
}
```

You can add many `.toast` modifiers on a single view.

## 📖 Article
Expand Down
41 changes: 36 additions & 5 deletions Sources/AlertToast/AlertToast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ public struct AlertToast: View{

///Banner from the bottom of the view
case banner(_ transition: BannerAnimation)

///Present a fully custom view at the center of the screen
case custom
}

/// Determine what the alert will display
Expand Down Expand Up @@ -217,7 +220,10 @@ public struct AlertToast: View{

///Customize your alert appearance
public var style: AlertStyle? = nil


///The custom content shown when `displayMode` is `.custom`
public var customView: (() -> AnyView)? = nil

///Full init
public init(displayMode: DisplayMode = .alert,
type: AlertType,
Expand All @@ -241,7 +247,16 @@ public struct AlertToast: View{
self.type = type
self.title = title
}


///Init for a fully custom view, presented centered like `.alert`
public init<Content: View>(@ViewBuilder customView: @escaping () -> Content,
style: AlertStyle? = nil){
self.displayMode = .custom
self.type = .regular
self.style = style
self.customView = { AnyView(customView()) }
}

///Banner from the bottom of the view
public var banner: some View{
VStack{
Expand Down Expand Up @@ -410,7 +425,21 @@ public struct AlertToast: View{
.alertBackground(style?.backgroundColor ?? nil, useGlassEffect: style?.useGlassEffect ?? true)
.cornerRadius(10)
}


///Fully custom content provided via the `customView` initializer
public var custom: some View{
Group{
if let customView = customView {
customView()
} else {
EmptyView()
}
}
.padding()
.alertBackground(style?.backgroundColor ?? nil, useGlassEffect: style?.useGlassEffect ?? true)
.cornerRadius(10)
}

///Body init determine by `displayMode`
public var body: some View{
switch displayMode{
Expand All @@ -420,6 +449,8 @@ public struct AlertToast: View{
hud
case .banner:
banner
case .custom:
custom
}
}
}
Expand Down Expand Up @@ -472,7 +503,7 @@ public struct AlertToastModifier: ViewModifier{
if isPresenting{

switch alert().displayMode{
case .alert:
case .alert, .custom:
alert()
.onTapGesture {
onTap?()
Expand Down Expand Up @@ -582,7 +613,7 @@ public struct AlertToastModifier: ViewModifier{
onAppearAction()
}
})
case .alert:
case .alert, .custom:
content
.overlay(ZStack{
main()
Expand Down
10 changes: 10 additions & 0 deletions Tests/AlertToastTests/AlertToastTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import XCTest
import SwiftUI
@testable import AlertToast

final class AlertToastTests: XCTestCase {
Expand All @@ -10,7 +11,16 @@ final class AlertToastTests: XCTestCase {
XCTAssertEqual(toast.subTitle, "Subtitle")
}

func testCustomViewInit() {
let toast = AlertToast {
Text("Custom")
}
XCTAssertEqual(toast.displayMode, .custom)
XCTAssertNotNil(toast.customView)
}

static var allTests = [
("testInit", testInit),
("testCustomViewInit", testCustomViewInit),
]
}