Skip to content

Commit

Permalink
stripped some more of the code to make it more dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
josephjoeljo committed Jun 8, 2024
1 parent b0cb489 commit 5e8363e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 91 deletions.
46 changes: 0 additions & 46 deletions Sources/SwiftToast/ToastView.swift

This file was deleted.

53 changes: 8 additions & 45 deletions Sources/SwiftToast/ToastViewModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ import Foundation
public struct ToastViewModifier: ViewModifier {

@Binding var isPresented: Bool
var position: DisplayPosition
@Binding var position: DisplayPosition
@Binding var toastContent: () -> any View

public func body(content: Content) -> some View {
switch position {
case .top:
return content
.frame(maxWidth: .infinity, maxHeight: .infinity)
.overlay(alignment: .top) {
ToastViewer()
AnyView(toastContent())
.onChange(of: isPresented) { newValue in
guard newValue == true else {
return
}
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
withAnimation(.easeInOut) {
isPresented = false
}
Expand All @@ -35,12 +36,12 @@ public struct ToastViewModifier: ViewModifier {
return content
.frame(maxWidth: .infinity, maxHeight: .infinity)
.overlay(alignment: .bottom) {
ToastViewer()
AnyView(toastContent())
.onChange(of: isPresented) { newValue in
guard newValue == true else {
return
}
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
withAnimation(.easeInOut) {
isPresented = false
}
Expand All @@ -49,49 +50,11 @@ public struct ToastViewModifier: ViewModifier {
}
}
}

@ViewBuilder func ToastViewer() -> some View {
if isPresented {
switch position {
case .top:
withAnimation {
ToastView {
HStack {
Image(systemName: "exclamationmark.triangle")
.foregroundColor(.white)
Text("Message")
.foregroundColor(.white)
}
.lineLimit(2)
.fixedSize(horizontal: false, vertical: true)
}
.offset(y: 15)
.transition(.offset(y: -1000))
}
case .bottom:
withAnimation {
ToastView {
HStack {
Image(systemName: "exclamationmark.triangle")
.foregroundColor(.white)
Text("Message")
.foregroundColor(.white)
}
.lineLimit(2)
.fixedSize(horizontal: false, vertical: true)
}
.offset(y: -20)
.transition(.offset(y: 1000))
}
}
}
}

}

extension View {

public func toast(isPresented: Binding<Bool>, position: DisplayPosition) -> some View {
self.modifier(ToastViewModifier(isPresented: isPresented, position: position))
public func toast(isPresented: Binding<Bool>, position: Binding<DisplayPosition>, content: Binding<() -> any View>) -> some View {
self.modifier(ToastViewModifier(isPresented: isPresented, position: position, toastContent: content))
}
}

0 comments on commit 5e8363e

Please sign in to comment.