|
| 1 | +// |
| 2 | +// TToastModifier.swift |
| 3 | +// DesignSystem |
| 4 | +// |
| 5 | +// Created by ๋ฐ๋ฏผ์ on 2/3/25. |
| 6 | +// Copyright ยฉ 2025 yapp25thTeamTnT. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import SwiftUI |
| 10 | + |
| 11 | +/// ํ ์คํธ ๋ฉ์์ง๋ฅผ ํ๋ฉด์ ํ์ํ๋ ViewModifier |
| 12 | +/// - `isPresented` ๋ฐ์ธ๋ฉ์ ํตํด ํ ์คํธ ๋ฉ์์ง์ ํ์ ์ฌ๋ถ๋ฅผ ์ ์ด |
| 13 | +/// - ์ ๋๋ฉ์ด์
์ ์ ์ฉํ์ฌ ์์ฐ์ค๋ฝ๊ฒ ๋ํ๋ฌ๋ค๊ฐ ์ฌ๋ผ์ง๋ ํจ๊ณผ |
| 14 | +public struct TToastViewModifier<InnerContent: View>: ViewModifier { |
| 15 | + /// ํ ์คํธ์ ํ์๋ ๋ด๋ถ ์ฝํ
์ธ ํด๋ก์ |
| 16 | + private let innerContent: () -> InnerContent |
| 17 | + /// ํ ์คํธ ํ์ ์ฌ๋ถ |
| 18 | + @Binding private var isPresented: Bool |
| 19 | + /// ํ ์คํธ๊ฐ ํ๋ฉด์ ๋ณด์ด๋ ์ํ์ธ์ง ์ฌ๋ถ (์ ๋๋ฉ์ด์
์ฉ) |
| 20 | + @State private var isVisible: Bool = false |
| 21 | + |
| 22 | + /// TToastViewModifier ์ด๊ธฐํ ๋ฉ์๋ |
| 23 | + /// - Parameters: |
| 24 | + /// - isPresented: ํ ์คํธ ํ์ ์ฌ๋ถ๋ฅผ ์ ์ดํ๋ Binding |
| 25 | + /// - newContent: ํ ์คํธ์ ํ์๋ ๋ด๋ถ ์ฝํ
์ธ ํด๋ก์ |
| 26 | + public init( |
| 27 | + isPresented: Binding<Bool>, |
| 28 | + newContent: @escaping () -> InnerContent |
| 29 | + ) { |
| 30 | + self._isPresented = isPresented |
| 31 | + self.innerContent = newContent |
| 32 | + } |
| 33 | + |
| 34 | + public func body(content: Content) -> some View { |
| 35 | + ZStack { |
| 36 | + // ๊ธฐ์กด ๋ทฐ |
| 37 | + content |
| 38 | + .onTapGesture { |
| 39 | + isPresented = false |
| 40 | + } |
| 41 | + |
| 42 | + if isPresented { |
| 43 | + // ํ ์คํธ ๋ทฐ |
| 44 | + self.innerContent() |
| 45 | + .padding(.horizontal, 20) |
| 46 | + .padding(.bottom, 24) |
| 47 | + .opacity(isVisible ? 1 : 0) |
| 48 | + .transition(.move(edge: .bottom).combined(with: .opacity)) |
| 49 | + .onAppear { |
| 50 | + showToast() |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + .animation(.easeInOut, value: isPresented) |
| 55 | + } |
| 56 | + |
| 57 | + /// ํ ์คํธ ๋ฉ์์ง๋ฅผ ํ์ํ๊ณ ์๋์ผ๋ก ์ฌ๋ผ์ง๋๋ก ์ฒ๋ฆฌํ๋ ํจ์ |
| 58 | + private func showToast() { |
| 59 | + // ํ์ด๋์ธ |
| 60 | + withAnimation(.easeInOut(duration: 0.3)) { |
| 61 | + isVisible = true |
| 62 | + } |
| 63 | + // 2์ด๊ฐ ์ ์ง - ์๋์ผ๋ก ์ฌ๋ผ์ง |
| 64 | + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { |
| 65 | + // ํ์ด๋ ์์ |
| 66 | + withAnimation(.easeInOut(duration: 0.3)) { |
| 67 | + isVisible = false |
| 68 | + } |
| 69 | + // present ํด์ |
| 70 | + DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { |
| 71 | + isPresented = false |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +public extension View { |
| 78 | + /// ํ ์คํธ ๋ฉ์์ง๋ฅผ ํ๋ฉด์ ํ์ํ๋ ViewModifier |
| 79 | + /// |
| 80 | + /// - `isPresented`: ํ ์คํธ์ ํ์ ์ฌ๋ถ๋ฅผ ์ ์ดํ๋ Binding. |
| 81 | + /// - `message`: ํ ์คํธ์ ํ์ํ ๋ฉ์์ง. |
| 82 | + /// - `leftView`: ํ ์คํธ ์ข์ธก์ ์ถ๊ฐํ ์์ด์ฝ์ด๋ ๋ทฐ. |
| 83 | + func tToast<LeftView: View>( |
| 84 | + isPresented: Binding<Bool>, |
| 85 | + message: String, |
| 86 | + leftView: @escaping () -> LeftView |
| 87 | + ) -> some View { |
| 88 | + self.modifier( |
| 89 | + TToastViewModifier( |
| 90 | + isPresented: isPresented, |
| 91 | + newContent: { |
| 92 | + TToastView(message: message, leftView: leftView) |
| 93 | + } |
| 94 | + ) |
| 95 | + ) |
| 96 | + } |
| 97 | +} |
0 commit comments