Skip to content

Commit

Permalink
Add position for insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
YuantongL committed Mar 10, 2021
1 parent 20d20a7 commit 9f2c184
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
Binary file not shown.
6 changes: 6 additions & 0 deletions Source/Toast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public struct AnyToast: Toast {
}
}

extension AnyToast: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.type.id == rhs.type.id
}
}

extension View {
public func hasToasts(_ toasts: [AnyToast]) -> some View {
BottomToastView(initialToasts: toasts) {
Expand Down
19 changes: 17 additions & 2 deletions Source/ToastManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import SwiftUI
/// Manages toasts appear/hiden on the screen
public class BottomToastManager: ObservableObject {

public enum InsertPosition {
case top
case bottom
}

/// Stores all hidden toasts
@Published
private(set) var hiddenToastTypes: Set<AnyToastType> = []
Expand All @@ -24,9 +29,19 @@ public class BottomToastManager: ObservableObject {
}

/// Insert a new toast to the top of the toast stack (new appears from the bottom of the view)
public func insert(_ toast: AnyToast) {
public func insert(_ toast: AnyToast, position: InsertPosition = .bottom) {
guard !toasts.contains(where: {
$0 == toast
}) else {
return
}
withAnimation(.easeInOut(duration: 0.2)) {
toasts.append(toast)
switch position {
case .top:
toasts.insert(toast, at: 0)
case .bottom:
toasts.append(toast)
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions Source/ToastType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ public struct AnyToastType: ToastType {
}

extension AnyToastType: Hashable {}

extension AnyToastType: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.id == rhs.id
}
}
2 changes: 1 addition & 1 deletion SwiftUIToastManager.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Pod::Spec.new do |spec|
spec.ios.deployment_target = '14.0'
spec.name = 'SwiftUIToastManager'
spec.version = '1.1'
spec.version = '1.2'
spec.swift_versions = '5.0'
spec.license = { :type => 'MIT' }
spec.homepage = 'https://github.com/YuantongL/SwiftUIToastManager'
Expand Down

0 comments on commit 9f2c184

Please sign in to comment.