Skip to content

Commit 35d38aa

Browse files
committed
feat: toast 가 뜬 이후 뷰가 전환된 후 다시 원복될 때 duration이 남아 있으면 뜨도록 개선
1 parent f0b5382 commit 35d38aa

1 file changed

Lines changed: 40 additions & 14 deletions

File tree

DevLog/UI/Common/Componeent/Toast.swift

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ private struct ToastOverlayView<Label: View>: View {
4141
@State private var opacityValue: Double = 0
4242
@State private var dismissWorkItem: DispatchWorkItem?
4343
@State private var isTapped: Bool = false
44+
@State private var isScheduled: Bool = false
4445

4546
var body: some View {
4647
if isPresented {
@@ -50,19 +51,18 @@ private struct ToastOverlayView<Label: View>: View {
5051
)
5152
.offset(y: yOffset)
5253
.opacity(opacityValue)
54+
.onChange(of: isPresented) { newValue in
55+
if newValue {
56+
resetForNewPresentation()
57+
presentAnimated()
58+
scheduleDismissIfNeeded()
59+
} else {
60+
cleanupPresentation()
61+
}
62+
}
5363
.onAppear {
5464
presentAnimated()
55-
scheduleDismiss()
56-
}
57-
.onDisappear {
58-
dismissWorkItem?.cancel()
59-
dismissWorkItem = nil
60-
isPresented = false
61-
62-
// 토스트를 탭하지 않고 자동으로 사라진 경우에만 onDismiss 호출
63-
if !isTapped {
64-
onDismiss?()
65-
}
65+
scheduleDismissIfNeeded()
6666
}
6767
.onTapGesture {
6868
isTapped = true
@@ -74,16 +74,36 @@ private struct ToastOverlayView<Label: View>: View {
7474
}
7575

7676
private func presentAnimated() {
77-
dismissWorkItem?.cancel()
78-
dismissWorkItem = nil
77+
guard opacityValue == 0 else { return }
7978

8079
withAnimation(.spring(response: 0.5, dampingFraction: 1, blendDuration: 0.0)) {
8180
yOffset = -100
8281
opacityValue = 1
8382
}
8483
}
8584

86-
private func scheduleDismiss() {
85+
private func resetForNewPresentation() {
86+
dismissWorkItem?.cancel()
87+
dismissWorkItem = nil
88+
isScheduled = false
89+
isTapped = false
90+
yOffset = 0
91+
opacityValue = 0
92+
}
93+
94+
private func cleanupPresentation() {
95+
dismissWorkItem?.cancel()
96+
dismissWorkItem = nil
97+
isScheduled = false
98+
isTapped = false
99+
yOffset = 0
100+
opacityValue = 0
101+
}
102+
103+
private func scheduleDismissIfNeeded() {
104+
guard !isScheduled else { return }
105+
isScheduled = true
106+
87107
let workItem = DispatchWorkItem {
88108
dismissAnimated()
89109
}
@@ -102,6 +122,12 @@ private struct ToastOverlayView<Label: View>: View {
102122

103123
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
104124
isPresented = false
125+
isScheduled = false
126+
127+
if !isTapped {
128+
onDismiss?()
129+
}
130+
isTapped = false
105131
}
106132
}
107133
}

0 commit comments

Comments
 (0)