Skip to content

a lightweight, multiplatform, easy-to-use package that for swiftUI errors handling.

License

Notifications You must be signed in to change notification settings

chocoford/SwiftyAlert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swifty Alert

GitHub License X (formerly Twitter) Follow

SwiftyAlert is a lightweight, multiplatform, easy-to-use swiftUI package that for errors handling. Integrated with SwiftUI Environment.

Platforms

  • macOS 12.0+
  • iOS 15.0+
  • macCatalyst 15.0+
  • tvOS 15.0+
  • watchOS 8.0+
  • visionOS 1.0+

Installation

In an app project or framework, in Xcode:

  • Select the menu: File → Swift Packages → Add Package Dependency...

  • Enter this URL: https://github.com/chocoford/SwiftyAlert


Or in a Swift Package, add it to the Package.swift dependencies:

let package = Package(
    ...
    dependencies: [
        .package(url: "https://github.com/chocoford/SwiftyAlert.git", from: "1.0.0"),
    ],
    targets: [
        .target(
            ...
            dependencies: [
                ...
                "SwiftyAlert"
            ],
            ...
        )
    ]
)

Usage

Import package

import SwiftyAlert

Inject environments somewhere first

for example, you can inject environments directly in YourApp.swift

@main
struct YourApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .swiftyAlert() // <-- inject environments here.
        }
    }
}

Use with @Environment

struct ContentView: View {
  @Environment(\.alert) private var alert // <-- use with @Environment
  
  var body: some View {
	Button {
          alert(title: "Alert") {
              Button {
                  ...
              } label: {
                  Text("Alert action")
              }
          } message: {}
          
          // or just use with error
          do {...} catch {
             alert(error)
          }
          
        } label: {
          Text("Alert")
        }
    }
}

Use with AlertToast

Also if your project has imported AlertToast, you can use alertToast in the same way!

import AlertToast
import SwiftyAlert

struct ContentView: View {
  @Environment(\.alertToast) private var alertToast

  var body: some View {
    Button {
      do {...} catch {
        alertToast(error) 
      }
    } label: {
      Text("Toggle alertToast")
    }
  }
}

Use with AlertToast and .sheet or .fullscreenCover

You can additionally apply .swiftyAlert(...) to make alertToast displayed in sheet or fullscreenCover.

...
.sheet(isPresented: $showSheet) {
    SheetView()
        .swiftyAlert() // <-- inject for sheet view to display alertToast
}

Trouble shooting

  • Undefined symbols:... Just Reset Package Caches and build again. The problem will be gone.

Acknowledgment

elai950 - AlertToast: Create Apple-like alerts & toasts using SwiftUI

About

a lightweight, multiplatform, easy-to-use package that for swiftUI errors handling.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages