Skip to content
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

Add Content View #3

Merged
merged 3 commits into from
Feb 3, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,29 @@ 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
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
}
}
}
}
```

## v1 Configuration options

Background Color
Text
Duration
Foreground Color (AKA Text Color)
- Background Color
- Text
- Duration
- Foreground Color (AKA Text Color)
- Content view
64 changes: 37 additions & 27 deletions Sources/CoffeeToast/CoffeeToast.swift
Original file line number Diff line number Diff line change
@@ -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<Content: View>: 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<Bool>,
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<Bool>,
@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")
}
}
}
10 changes: 3 additions & 7 deletions Tests/CoffeeToastTests/CoffeeToastTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}