From 09e9ef542cfc6ef6cf9fa74de1bdde1af0db0a30 Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Sun, 24 Mar 2024 16:08:09 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Refactor=20toast=20animations=20and?= =?UTF-8?q?=20transitions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Set toast view z-index based on toast id - Add transition animation for toast removal - Simplify toast removal logic --- DesignSystemApp/ContentView.swift | 2 +- .../Components/Toast/View/ToastGroup.swift | 1 + .../Components/Toast/View/ToastView.swift | 20 +++---------------- 3 files changed, 5 insertions(+), 18 deletions(-) diff --git a/DesignSystemApp/ContentView.swift b/DesignSystemApp/ContentView.swift index 8764a1c..0b939af 100644 --- a/DesignSystemApp/ContentView.swift +++ b/DesignSystemApp/ContentView.swift @@ -12,7 +12,7 @@ struct ContentView: View { var body: some View { VStack { Button("Toast!") { - Toast.shared.present(title: "Hello", symbol: "globe") + Toast.shared.present(title: "Airpods Pro", symbol: "airpodspro", isUserInteractionEnabled: true) } } .padding() diff --git a/Shared/Sources/DesignSystem/Components/Toast/View/ToastGroup.swift b/Shared/Sources/DesignSystem/Components/Toast/View/ToastGroup.swift index d0edc55..eaf7355 100644 --- a/Shared/Sources/DesignSystem/Components/Toast/View/ToastGroup.swift +++ b/Shared/Sources/DesignSystem/Components/Toast/View/ToastGroup.swift @@ -20,6 +20,7 @@ struct ToastGroup: View { ToastView(size: size, item: toast) .scaleEffect(scale(toast)) .offset(y: offsetY(toast)) + .zIndex(Double(model.toasts.firstIndex(where: { $0.id == toast.id }) ?? 0)) } } .padding(.bottom, safeArea.top == .zero ? 15 : 10) diff --git a/Shared/Sources/DesignSystem/Components/Toast/View/ToastView.swift b/Shared/Sources/DesignSystem/Components/Toast/View/ToastView.swift index 84db7d6..6c6daa0 100644 --- a/Shared/Sources/DesignSystem/Components/Toast/View/ToastView.swift +++ b/Shared/Sources/DesignSystem/Components/Toast/View/ToastView.swift @@ -55,32 +55,18 @@ struct ToastView: View { } } ) - .offset(y: animateIn ? 0 : 150) - .offset(y: !animateOut ? 0 : 150) .task { - guard !animateIn else { return } - withAnimation(.snappy) { - animateIn = true - } - try? await Task.sleep(for: .seconds(item.duration.rawValue)) - removeToast() } // Limiting Size .frame(maxWidth: size.width * 0.7) + .transition(.offset(y: 150)) } private func removeToast() { - guard !animateOut else { return } - withAnimation(.snappy, completionCriteria: .logicallyComplete) { - animateOut = true - } completion: { - removeToastItem() + withAnimation(.snappy ) { + Toast.shared.toasts.removeAll(where: { $0.id == item.id }) } } - - private func removeToastItem() { - Toast.shared.toasts.removeAll(where: { $0.id == item.id }) - } }