DarthRumata/Toaster
SwiftUI Toast library
Features
- Predefined toast styles (error, success, warning)
- Customizable toast views
- Flexible presentation options
Installation
To use Toaster in your SwiftUI project, simply add the Toaster package as a dependency in your Package.swift file:
dependencies: [
.package(url: "https://github.com/DarthRumata/Toaster.git", from: "0.4.0")
]Usage
To present a toast message, you can use the `toastView` modifier on your SwiftUI views. Toaster provides multiple variants of the toastView modifier depending on your use case.
Predefined Toast
You can show a predefined toast using the toastView(toast:) modifier. It takes a binding to a Toast object as input.
struct ContentView: View {
@State var error: Error?
var body: some View {
let errorBinding = Binding<Toast?>(get: {
if error != nil {
return Toast(message: "An error occurred", style: .error)
}
return nil
}, set: { newValue in
if newValue == nil {
error = nil
}
})
VStack {
// Your view content
Button("Show Error Toast") {
error = MyCustomError()
}
}
.toastView(toast: errorBinding)
}
}Custom Toast
If you want to present a custom toast view, you can use the `toastView(isPresented:view:)` modifier. It takes a binding to a boolean value indicating the toast presentation state and a closure that returns the custom toast view.
struct ContentView: View {
@State var isToastPresented = false
var body: some View {
VStack {
// Your view content
Button("Show Custom Toast") {
isToastPresented = true
}
}
.toastView(isPresented: $isToastPresented) {
CustomToastView()
}
}
}Scheduler
To control the presentation of toast messages manually, you can use the ToastScheduler class. You can create an instance of the scheduler and pass it to the `toastView(scheduler:)` modifier.
class ViewModel: ObservableObject {
private(set) var toastScheduler = ToastScheduler()
}
struct ContentView: View {
@StateObject var viewModel = ViewModel()
var body: some View {
VStack {
// Your view content
Button("Schedule Toast") {
viewModel.toastScheduler.present(CustomToastView(), dismissDelay: 3)
}
}
.toastView(scheduler: viewModel.toastScheduler)
}
}Package Metadata
Repository: DarthRumata/Toaster
Stars: 2
Forks: 0
Open issues: 0
Default branch: main
Primary language: swift
License: MIT
README: README.md