Skip to content

Commit

Permalink
fix: 코드리뷰 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
Jihyun247 committed Aug 17, 2024
1 parent 0fb8520 commit 0a1c21d
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
53 changes: 53 additions & 0 deletions Projects/Shared/DesignSystem/Sources/Component/Dialog/Dialog.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,16 @@

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<T: Dialog>: ViewModifier {
@Binding var dialog: T?

func body(content: Content) -> some View {
ZStack(alignment: .center) {
content
.frame(maxWidth: .infinity, maxHeight: .infinity)
.zIndex(1)

if isPresented {
if let dialog {
Global.Color.black.opacity(Global.Opacity._50d)
.ignoresSafeArea()
.zIndex(2)
Expand All @@ -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)
Expand All @@ -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?()
}
)
Expand All @@ -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<Bool>,
firstButton: DialogButtonModel,
secondButton: DialogButtonModel? = nil
public func dialog<T: Dialog>(
dialog: Binding<T?>
) -> some View {
return self.modifier(
DialogViewModifier(
title: title,
subTitle: subTitle,
isPresented: isPresented,
firstButton: firstButton,
secondButton: secondButton
)
)
return self.modifier(DialogViewModifier(dialog: dialog))
}
}

0 comments on commit 0a1c21d

Please sign in to comment.