|
| 1 | +// |
| 2 | +// DialogViewModifier.swift |
| 3 | +// DesignSystem |
| 4 | +// |
| 5 | +// Created by 김지현 on 8/17/24. |
| 6 | +// Copyright © 2024 PomoNyang. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import SwiftUI |
| 10 | + |
| 11 | +public struct DialogButtonModel { |
| 12 | + let title: String |
| 13 | + let leftIcon: Image? |
| 14 | + let rightIcon: Image? |
| 15 | + let action: (() -> Void)? // 버튼 액션을 Nil로 두면 자동으로 CancelButton이 됩니다 |
| 16 | + |
| 17 | + public init(title: String, leftIcon: Image? = nil, rightIcon: Image? = nil, action: (() -> Void)? = nil) { |
| 18 | + self.title = title |
| 19 | + self.leftIcon = leftIcon |
| 20 | + self.rightIcon = rightIcon |
| 21 | + self.action = action |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +struct DialogViewModifier: ViewModifier { |
| 26 | + let title: String |
| 27 | + let subTitle: String? |
| 28 | + @Binding var isPresented: Bool |
| 29 | + let firstButton: DialogButtonModel |
| 30 | + let secondButton: DialogButtonModel? |
| 31 | + |
| 32 | + func body(content: Content) -> some View { |
| 33 | + ZStack(alignment: .center) { |
| 34 | + content |
| 35 | + .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 36 | + .zIndex(1) |
| 37 | + |
| 38 | + if isPresented { |
| 39 | + Global.Color.black.opacity(Global.Opacity._50d) |
| 40 | + .ignoresSafeArea() |
| 41 | + .zIndex(2) |
| 42 | + |
| 43 | + |
| 44 | + // MARK: Title & SubTitle |
| 45 | + VStack(spacing: Alias.Spacing.large) { |
| 46 | + VStack(spacing: Alias.Spacing.small) { |
| 47 | + HStack { |
| 48 | + Text(title) |
| 49 | + .font(Typography.header4) |
| 50 | + .foregroundStyle(Alias.Color.Text.primary) |
| 51 | + Spacer() |
| 52 | + Button { |
| 53 | + self.isPresented = false |
| 54 | + } label: { |
| 55 | + DesignSystemAsset.Image._24CancelPrimary.swiftUIImage |
| 56 | + } |
| 57 | + } |
| 58 | + .padding(.top, Alias.Spacing.small) |
| 59 | + |
| 60 | + if let subTitle = subTitle { |
| 61 | + HStack { |
| 62 | + Text(subTitle) |
| 63 | + .font(Typography.subBodyR) |
| 64 | + .foregroundStyle(Alias.Color.Text.secondary) |
| 65 | + Spacer() |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // MARK: Buttons |
| 71 | + HStack { |
| 72 | + Button( |
| 73 | + title: LocalizedStringKey(firstButton.title), |
| 74 | + leftIcon: firstButton.leftIcon, |
| 75 | + rightIcon: firstButton.rightIcon, |
| 76 | + action: { |
| 77 | + self.isPresented = false |
| 78 | + firstButton.action?() |
| 79 | + } |
| 80 | + ) |
| 81 | + .buttonStyle(.box(level: firstButton.action == nil ? .tertiary : .primary, size: .medium, width: .low)) |
| 82 | + if let secondButton = secondButton { |
| 83 | + Button( |
| 84 | + title: LocalizedStringKey(secondButton.title), |
| 85 | + leftIcon: secondButton.leftIcon, |
| 86 | + rightIcon: secondButton.rightIcon, |
| 87 | + action: { |
| 88 | + self.isPresented = false |
| 89 | + secondButton.action?() |
| 90 | + } |
| 91 | + ) |
| 92 | + .buttonStyle(.box(level: secondButton.action == nil ? .tertiary : .primary, size: .medium, width: .low)) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + .padding(.all, Alias.Spacing.xLarge) |
| 97 | + .frame(width: 335) |
| 98 | + .background(Global.Color.white) |
| 99 | + .cornerRadius(Alias.BorderRadius.medium) |
| 100 | + .zIndex(3) |
| 101 | + } |
| 102 | + } |
| 103 | + .animation(.easeInOut(duration: 0.3), value: !self.isPresented) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +extension View { |
| 108 | + public func dialog( |
| 109 | + title: String, |
| 110 | + subTitle: String? = nil, |
| 111 | + isPresented: Binding<Bool>, |
| 112 | + firstButton: DialogButtonModel, |
| 113 | + secondButton: DialogButtonModel? = nil |
| 114 | + ) -> some View { |
| 115 | + return self.modifier( |
| 116 | + DialogViewModifier( |
| 117 | + title: title, |
| 118 | + subTitle: subTitle, |
| 119 | + isPresented: isPresented, |
| 120 | + firstButton: firstButton, |
| 121 | + secondButton: secondButton |
| 122 | + ) |
| 123 | + ) |
| 124 | + } |
| 125 | +} |
0 commit comments