@@ -12,7 +12,8 @@ extension View {
1212 isPresented: Binding < Bool > ,
1313 duration: TimeInterval = 2 ,
1414 message: String ,
15- action: ( ( ) -> Void ) ? = nil
15+ action: ( ( ) -> Void ) ? = nil ,
16+ onDismiss: ( ( ) -> Void ) ? = nil
1617 ) -> some View {
1718 self
1819 . frame ( maxWidth: . infinity, maxHeight: . infinity)
@@ -21,9 +22,9 @@ extension View {
2122 isPresented: isPresented,
2223 duration: duration,
2324 message: message,
24- action: action
25+ action: action,
26+ onDismiss: onDismiss
2527 )
26- . foregroundStyle ( action == nil ? Color ( . label) : . blue)
2728 . padding ( . horizontal, 12 )
2829 }
2930 }
@@ -34,30 +35,41 @@ private struct ToastOverlayView: View {
3435 let duration : TimeInterval
3536 let message : String
3637 let action : ( ( ) -> Void ) ?
38+ let onDismiss : ( ( ) -> Void ) ?
3739
3840 @State private var yOffset : CGFloat = 0
3941 @State private var opacityValue : Double = 0
4042 @State private var dismissWorkItem : DispatchWorkItem ?
43+ @State private var isTapped : Bool = false
4144
4245 var body : some View {
4346 if isPresented {
44- ToastCardView ( message)
45- . offset ( y: yOffset)
46- . opacity ( opacityValue)
47- . onAppear {
48- presentAnimated ( )
49- scheduleDismiss ( )
50- }
51- . onDisappear {
52- dismissWorkItem? . cancel ( )
53- dismissWorkItem = nil
54- isPresented = false
55- }
56- . onTapGesture {
57- dismissAnimated ( )
58- action ? ( )
47+ ToastCardView (
48+ message,
49+ textColor: action == nil ? . primary : . blue
50+ )
51+ . offset ( y: yOffset)
52+ . opacity ( opacityValue)
53+ . onAppear {
54+ presentAnimated ( )
55+ scheduleDismiss ( )
56+ }
57+ . onDisappear {
58+ dismissWorkItem? . cancel ( )
59+ dismissWorkItem = nil
60+ isPresented = false
61+
62+ // 토스트를 탭하지 않고 자동으로 사라진 경우에만 onDismiss 호출
63+ if !isTapped {
64+ onDismiss ? ( )
5965 }
60- . transition ( . identity)
66+ }
67+ . onTapGesture {
68+ isTapped = true
69+ dismissAnimated ( )
70+ action ? ( )
71+ }
72+ . transition ( . identity)
6173 }
6274 }
6375
@@ -96,14 +108,20 @@ private struct ToastOverlayView: View {
96108
97109private struct ToastCardView : View {
98110 let message : String
111+ let textColor : Color
99112
100- init ( _ message: String ) {
113+ init (
114+ _ message: String ,
115+ textColor: Color = . primary
116+ ) {
101117 self . message = message
118+ self . textColor = textColor
102119 }
103120
104121 var body : some View {
105122 Text ( message)
106123 . font ( . caption)
124+ . foregroundStyle ( textColor)
107125 . multilineTextAlignment ( . center)
108126 . lineLimit ( 3 )
109127 . padding ( . vertical, 12 )
0 commit comments