From 0a1c21dbafd4faba1707b7711000a56f787d075a Mon Sep 17 00:00:00 2001 From: Jihyun247 Date: Sun, 18 Aug 2024 03:37:23 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=BD=94=EB=93=9C=EB=A6=AC=EB=B7=B0=20?= =?UTF-8?q?=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/Component/DialogDetailView.swift | 60 ++++++++-------- .../Sources/Component/Dialog/Dialog.swift | 53 ++++++++++++++ .../Component/Dialog/DialogViewModifier.swift | 71 ++++++------------- 3 files changed, 102 insertions(+), 82 deletions(-) create mode 100644 Projects/Shared/DesignSystem/Sources/Component/Dialog/Dialog.swift diff --git a/Projects/Shared/DesignSystem/Example/Sources/Component/DialogDetailView.swift b/Projects/Shared/DesignSystem/Example/Sources/Component/DialogDetailView.swift index 3165cc6..6136a2f 100644 --- a/Projects/Shared/DesignSystem/Example/Sources/Component/DialogDetailView.swift +++ b/Projects/Shared/DesignSystem/Example/Sources/Component/DialogDetailView.swift @@ -11,64 +11,60 @@ import SwiftUI import DesignSystem struct DialogDetailView: View { - @State var titleSubTitleTwoButtonDialogIsPresented: Bool = false - @State var titleSubTitleOneButtonDialogIsPresented: Bool = false - @State var titleTwoButtonDialogIsPresented: Bool = false - @State var titleOneButtonDialogIsPresented: Bool = false + @State var titleSubTitleTwoButtonDialog: DefaultDialog? + @State var titleSubTitleOneButtonDialog: DefaultDialog? + @State var titleTwoButtonDialog: DefaultDialog? + @State var titleOneButtonDialog: DefaultDialog? var body: some View { VStack(spacing: 10) { Spacer() Button { - titleSubTitleTwoButtonDialogIsPresented = true + titleSubTitleTwoButtonDialog = DefaultDialog( + title: "Dialog Title", + subTitle: "Dialog Subtext를 입력해주세요.\n최대 2줄을 넘지 않도록 해요.", + firstButton: DialogButtonModel(title: "Button1"), + secondButton: DialogButtonModel(title: "Button2", action: { print(" PRINT !!") }) + ) } label: { Text("title SubTitle & Two Button") } Button { - titleSubTitleOneButtonDialogIsPresented = true + titleSubTitleOneButtonDialog = DefaultDialog( + title: "Dialog Title", + subTitle: "Dialog Subtext를 입력해주세요.\n최대 2줄을 넘지 않도록 해요.", + firstButton: DialogButtonModel(title: "Button1") + ) } label: { Text("title SubTitle & One Button") } Button { - titleTwoButtonDialogIsPresented = true + titleTwoButtonDialog = DefaultDialog( + title: "Dialog Title", + firstButton: DialogButtonModel(title: "Button1"), + secondButton: DialogButtonModel(title: "Button2", action: { print(" PRINT !!") }) + ) } label: { Text("title & Two Button") } Button { - titleOneButtonDialogIsPresented = true + titleOneButtonDialog = DefaultDialog( + title: "Dialog Title", + firstButton: DialogButtonModel(title: "Button1") + ) } label: { Text("title & One Button") } Spacer() } - .dialog( - title: "Dialog Title", - subTitle: "Dialog Subtext를 입력해주세요.\n최대 2줄을 넘지 않도록 해요.", - isPresented: $titleSubTitleTwoButtonDialogIsPresented, - firstButton: DialogButtonModel(title: "Button1"), - secondButton: DialogButtonModel(title: "Button2", action: { print(" PRINT !!") }) - ) - .dialog( - title: "Dialog Title", - subTitle: "Dialog Subtext를 입력해주세요.\n최대 2줄을 넘지 않도록 해요.", - isPresented: $titleSubTitleOneButtonDialogIsPresented, - firstButton: DialogButtonModel(title: "Button1") - ) - .dialog( - title: "Dialog Title", - isPresented: $titleTwoButtonDialogIsPresented, - firstButton: DialogButtonModel(title: "Button1"), - secondButton: DialogButtonModel(title: "Button2", action: { print(" PRINT !!") }) - ) - .dialog( - title: "Dialog Title", - isPresented: $titleOneButtonDialogIsPresented, - firstButton: DialogButtonModel(title: "Button1") - ) + .dialog(dialog: $titleSubTitleTwoButtonDialog) + .dialog(dialog: $titleSubTitleOneButtonDialog) + .dialog(dialog: $titleTwoButtonDialog) + .dialog(dialog: $titleOneButtonDialog) } } diff --git a/Projects/Shared/DesignSystem/Sources/Component/Dialog/Dialog.swift b/Projects/Shared/DesignSystem/Sources/Component/Dialog/Dialog.swift new file mode 100644 index 0000000..df160c3 --- /dev/null +++ b/Projects/Shared/DesignSystem/Sources/Component/Dialog/Dialog.swift @@ -0,0 +1,53 @@ +// +// Dialog.swift +// DesignSystem +// +// Created by 김지현 on 8/18/24. +// Copyright © 2024 PomoNyang. All rights reserved. +// + +import SwiftUI + +public struct DialogButtonModel: Equatable { + public static func == (lhs: DialogButtonModel, rhs: DialogButtonModel) -> Bool { + lhs.title == rhs.title + } + + let title: String + let leftIcon: Image? + let rightIcon: Image? + let action: (() -> Void)? // 버튼 액션을 Nil로 두면 자동으로 CancelButton이 됩니다 + + public init(title: String, leftIcon: Image? = nil, rightIcon: Image? = nil, action: (() -> Void)? = nil) { + self.title = title + self.leftIcon = leftIcon + self.rightIcon = rightIcon + self.action = action + } +} + +public protocol Dialog: Equatable { + var title: String { get } + var subTitle: String? { get } + var firstButton: DialogButtonModel { get } + var secondButton: DialogButtonModel? { get } +} + +public struct DefaultDialog: Dialog { + public var title: String + public var subTitle: String? + public var firstButton: DialogButtonModel + public var secondButton: DialogButtonModel? + + public init( + title: String, + subTitle: String? = nil, + firstButton: DialogButtonModel, + secondButton: DialogButtonModel? = nil + ) { + self.title = title + self.subTitle = subTitle + self.firstButton = firstButton + self.secondButton = secondButton + } +} diff --git a/Projects/Shared/DesignSystem/Sources/Component/Dialog/DialogViewModifier.swift b/Projects/Shared/DesignSystem/Sources/Component/Dialog/DialogViewModifier.swift index c81e001..96cb6fc 100644 --- a/Projects/Shared/DesignSystem/Sources/Component/Dialog/DialogViewModifier.swift +++ b/Projects/Shared/DesignSystem/Sources/Component/Dialog/DialogViewModifier.swift @@ -8,26 +8,8 @@ import SwiftUI -public struct DialogButtonModel { - let title: String - let leftIcon: Image? - let rightIcon: Image? - let action: (() -> Void)? // 버튼 액션을 Nil로 두면 자동으로 CancelButton이 됩니다 - - public init(title: String, leftIcon: Image? = nil, rightIcon: Image? = nil, action: (() -> Void)? = nil) { - self.title = title - self.leftIcon = leftIcon - self.rightIcon = rightIcon - self.action = action - } -} - -struct DialogViewModifier: ViewModifier { - let title: String - let subTitle: String? - @Binding var isPresented: Bool - let firstButton: DialogButtonModel - let secondButton: DialogButtonModel? +struct DialogViewModifier: ViewModifier { + @Binding var dialog: T? func body(content: Content) -> some View { ZStack(alignment: .center) { @@ -35,7 +17,7 @@ struct DialogViewModifier: ViewModifier { .frame(maxWidth: .infinity, maxHeight: .infinity) .zIndex(1) - if isPresented { + if let dialog { Global.Color.black.opacity(Global.Opacity._50d) .ignoresSafeArea() .zIndex(2) @@ -45,19 +27,19 @@ struct DialogViewModifier: ViewModifier { VStack(spacing: Alias.Spacing.large) { VStack(spacing: Alias.Spacing.small) { HStack { - Text(title) + Text(dialog.title) .font(Typography.header4) .foregroundStyle(Alias.Color.Text.primary) Spacer() Button { - self.isPresented = false + self.dialog = nil } label: { DesignSystemAsset.Image._24CancelPrimary.swiftUIImage } } .padding(.top, Alias.Spacing.small) - if let subTitle = subTitle { + if let subTitle = dialog.subTitle { HStack { Text(subTitle) .font(Typography.subBodyR) @@ -68,24 +50,24 @@ struct DialogViewModifier: ViewModifier { } // MARK: Buttons - HStack { + HStack(spacing: 12) { Button( - title: LocalizedStringKey(firstButton.title), - leftIcon: firstButton.leftIcon, - rightIcon: firstButton.rightIcon, + title: LocalizedStringKey(dialog.firstButton.title), + leftIcon: dialog.firstButton.leftIcon, + rightIcon: dialog.firstButton.rightIcon, action: { - self.isPresented = false - firstButton.action?() + self.dialog = nil + dialog.firstButton.action?() } ) - .buttonStyle(.box(level: firstButton.action == nil ? .tertiary : .primary, size: .medium, width: .low)) - if let secondButton = secondButton { + .buttonStyle(.box(level: dialog.firstButton.action == nil ? .tertiary : .primary, size: .medium, width: .low)) + if let secondButton = dialog.secondButton { Button( title: LocalizedStringKey(secondButton.title), leftIcon: secondButton.leftIcon, rightIcon: secondButton.rightIcon, action: { - self.isPresented = false + self.dialog = nil secondButton.action?() } ) @@ -94,32 +76,21 @@ struct DialogViewModifier: ViewModifier { } } .padding(.all, Alias.Spacing.xLarge) - .frame(width: 335) .background(Global.Color.white) .cornerRadius(Alias.BorderRadius.medium) + .padding(.horizontal, Alias.Spacing.xLarge) .zIndex(3) } } - .animation(.easeInOut(duration: 0.3), value: !self.isPresented) + .ignoresSafeArea() + .animation(.easeInOut(duration: 0.3), value: self.dialog == nil) } } extension View { - public func dialog( - title: String, - subTitle: String? = nil, - isPresented: Binding, - firstButton: DialogButtonModel, - secondButton: DialogButtonModel? = nil + public func dialog( + dialog: Binding ) -> some View { - return self.modifier( - DialogViewModifier( - title: title, - subTitle: subTitle, - isPresented: isPresented, - firstButton: firstButton, - secondButton: secondButton - ) - ) + return self.modifier(DialogViewModifier(dialog: dialog)) } }