diff --git a/README.md b/README.md index 44f39ab..67105d2 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This package uses SwiftUI and requires a target of iOS 15.0. This means that the ## How to Use -Wrap whatever view you want the Toast notifaction to appear in in a ZStack and add Toast as the last view. +Use the toast view by adding the view which presents the toast, see example below: ```swift import SwiftUI @@ -16,12 +16,12 @@ import CoffeeToast struct ContentView: View { @State private var toastIsShown = false + var body: some View { - ZStack { + Toast("TOAST NOTIFICATION", isShown: $toastIsShown) { Button("Toggle Toast") { - toastIsShown.toggle() - } - Toast(isShown: $toastIsShown, color: .red, text: "TOAST NOTIFICATION", duration: 2.0) + toastIsShown = true + } } } } @@ -29,7 +29,8 @@ struct ContentView: View { ## v1 Configuration options -Background Color -Text -Duration -Foreground Color (AKA Text Color) +- Background Color +- Text +- Duration +- Foreground Color (AKA Text Color) +- Content view diff --git a/Sources/CoffeeToast/CoffeeToast.swift b/Sources/CoffeeToast/CoffeeToast.swift index 5bee015..44bdf22 100644 --- a/Sources/CoffeeToast/CoffeeToast.swift +++ b/Sources/CoffeeToast/CoffeeToast.swift @@ -1,52 +1,62 @@ import SwiftUI -public struct CoffeeToast { - public var text = "Hello, World!" - - public init() {} -} - /// Toast notification /// - Parameter isShown: The boolean variable that triggers if the notification is shown /// - Parameter color: Background color of the notification /// - Parameter text: Text the notification is supposed to show /// - Parameter duration: How long is the notification supposed to show before dismissing /// - Parameter foregroundColor: The color of the text. This is optional and defaults to `Color.white` -public struct Toast: View { +/// - Parameter content: The view which will present the toast +public struct Toast: View { @Binding var isShown: Bool - var color: Color var text: String var duration: Double var foregroundColor: Color + var backgroundColor: Color + + private let content: Content - public init(isShown: Binding, - color: Color, - text: String, - duration: Double, - foregroundColor: Color = .white) { + public init(_ text: String, + duration: Double = 2.0, + foregroundColor: Color = .white, + backgroundColor: Color = .red, + isShown: Binding, + @ViewBuilder content: () -> Content) { self._isShown = isShown - self.color = color self.text = text self.duration = duration self.foregroundColor = foregroundColor + self.backgroundColor = backgroundColor + self.content = content() } public var body: some View { - VStack { - Spacer() - Text("Hello") - .padding() - .background(color) - .foregroundColor(foregroundColor) - .cornerRadius(10.0) - .offset(x: 0, y: isShown ? -10 : 100) - .animation(.default, value: isShown) - .onChange(of: isShown) { newValue in - DispatchQueue.main.asyncAfter(deadline: .now() + duration) { - self.isShown = false + ZStack { + content + VStack { + Spacer() + Text(text) + .padding() + .background(backgroundColor) + .foregroundColor(foregroundColor) + .cornerRadius(10.0) + .offset(x: 0, y: isShown ? -10 : 100) + .animation(.default, value: isShown) + .onChange(of: isShown) { newValue in + DispatchQueue.main.asyncAfter(deadline: .now() + duration) { + self.isShown = false + } } - } + } + } + } +} + +struct ContentView_Previews: PreviewProvider { + static var previews: some View { + Toast("TOAST NOTIFICATION", isShown: .constant(true)) { + Text("This is my awesome content") } } } diff --git a/Tests/CoffeeToastTests/CoffeeToastTests.swift b/Tests/CoffeeToastTests/CoffeeToastTests.swift index 459554a..fe053f3 100644 --- a/Tests/CoffeeToastTests/CoffeeToastTests.swift +++ b/Tests/CoffeeToastTests/CoffeeToastTests.swift @@ -5,15 +5,11 @@ import Combine @testable import CoffeeToast final class CoffeeToastTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(CoffeeToast().text, "Hello, World!") - } func testToast() throws { - let sampleToast = Toast(isShown: Binding.constant(true), color: .red, text: "Error", duration: 2.0) + let sampleToast = Toast("Error", isShown: .constant(true)) { + Text("This is my awesome content") + } XCTAssertEqual(sampleToast.text, "Error") } }