Skip to content

Commit

Permalink
Merge pull request #16 from sanzaru/15-toast-message-issue
Browse files Browse the repository at this point in the history
Fixed issue with missing publishing of changed state variables into t…
  • Loading branch information
sanzaru authored Mar 26, 2022
2 parents 8c6c585 + 688c7d7 commit 1bc531a
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions Sources/SimpleToast/SimpleToast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@

import SwiftUI


struct SimpleToast<SimpleToastContent: View>: ViewModifier {
@Binding var showToast: Bool

let options: SimpleToastOptions
let onDismiss: (() -> Void)?

let content: () -> SimpleToastContent

@State private var timer: Timer? = nil
@State private var offset: CGSize = .zero

private let toastContent: SimpleToastContent

/// Dimiss the toast on drag
/// TODO: Needs better implementation.
private var dragGesture: some Gesture {
Expand All @@ -37,6 +36,18 @@ struct SimpleToast<SimpleToastContent: View>: ViewModifier {
offset = .zero
}
}

init(
showToast: Binding<Bool>,
options: SimpleToastOptions,
onDismiss: (() -> Void)? = nil,
@ViewBuilder content: @escaping () -> SimpleToastContent
) {
self._showToast = showToast
self.options = options
self.onDismiss = onDismiss
self.toastContent = content()
}

func body(content: Content) -> some View {
// Main view content
Expand All @@ -59,28 +70,28 @@ struct SimpleToast<SimpleToastContent: View>: ViewModifier {
Group {
switch options.modifierType {
case .slide:
self.content()
toastContent
.modifier(SimpleToastSlide(showToast: $showToast, options: options))
.gesture(dragGesture)
.onTapGesture(perform: dismiss)
.offset(offset)

case .scale:
self.content()
toastContent
.modifier(SimpleToastScale(showToast: $showToast, options: options))
.gesture(dragGesture)
.onTapGesture(perform: dismiss)
.offset(offset)

case .skew:
self.content()
toastContent
.modifier(SimpleToastSkew(showToast: $showToast, options: options))
//.gesture(dragGesture)
.onTapGesture(perform: dismiss)
.offset(offset)

default:
self.content()
toastContent
.modifier(SimpleToastFade(showToast: $showToast, options: options))
.gesture(dragGesture)
.onTapGesture(perform: dismiss)
Expand Down Expand Up @@ -117,7 +128,6 @@ struct SimpleToast<SimpleToastContent: View>: ViewModifier {
}
}


// MARK: - View extensions

public extension View {
Expand Down

0 comments on commit 1bc531a

Please sign in to comment.