Skip to content

Commit 1a2a394

Browse files
committed
ToastState
1 parent 5c3c844 commit 1a2a394

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

Examples/Examples.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 22 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/ToastUI/ToastState.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import CasePaths
2+
import SwiftUI
3+
import SwiftUINavigation
4+
5+
public struct ToastState: Identifiable {
6+
public let id = UUID()
7+
8+
let style: ToastView.Style
9+
let icon: Image?
10+
let title: String
11+
let subtitle: String?
12+
13+
public init(
14+
style: ToastView.Style = .regular,
15+
icon: Image? = nil,
16+
title: String,
17+
subtitle: String? = nil
18+
) {
19+
self.style = style
20+
self.icon = icon
21+
self.title = title
22+
self.subtitle = subtitle
23+
}
24+
}
25+
26+
extension ToastState: Equatable {
27+
public static func == (lhs: ToastState, rhs: ToastState) -> Bool {
28+
lhs.style == rhs.style &&
29+
lhs.icon == rhs.icon &&
30+
lhs.title == rhs.title &&
31+
lhs.subtitle == rhs.subtitle
32+
}
33+
}
34+
35+
extension ToastState: Hashable {
36+
public func hash(into hasher: inout Hasher) {
37+
hasher.combine(style)
38+
// hasher.combine(icon)
39+
hasher.combine(title)
40+
hasher.combine(subtitle)
41+
}
42+
}
43+
44+
extension ToastView {
45+
public init(_ state: ToastState) {
46+
self.init(style: state.style, icon: state.icon, title: state.title, subtitle: state.subtitle)
47+
}
48+
}
49+
50+
extension View {
51+
public func toast(
52+
unwrapping value: Binding<ToastState?>,
53+
position: ToastPosition = ToastDefaults.position,
54+
duration: TimeInterval = ToastDefaults.duration,
55+
tapToDismiss: Bool = ToastDefaults.tapToDismiss,
56+
offsetY: CGFloat = ToastDefaults.offsetY,
57+
onDismiss: (() -> Void)? = nil
58+
) -> some View {
59+
toast(
60+
unwrapping: value,
61+
position: position,
62+
duration: duration,
63+
tapToDismiss: tapToDismiss,
64+
offsetY: offsetY,
65+
onDismiss: onDismiss
66+
) { $state in
67+
ToastView(state)
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)