Skip to content

Commit

Permalink
style: Improve DialogPresenter code
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Jul 27, 2024
1 parent e7823ff commit 22d0a88
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 57 deletions.
2 changes: 0 additions & 2 deletions Source/DDS/Component/Modal/Dialog/DialogPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,10 @@ public struct DodamDialogPresenter<C: View>: ModalViewProtocol {
.secondaryButton("취소") {
//
}
.show()
}
Button("Show 2") {
provider.present("제목을 입력해주세요")
.message("본문을 입력해주세요")
.show()
}
}
}
Expand Down
89 changes: 34 additions & 55 deletions Source/DDS/Component/Modal/Dialog/DialogProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,6 @@ import Foundation

public final class DialogProvider: ObservableObject, ModalProvider {

public struct Builder {
let title: String
let message: String?
let secondaryButton: DialogButton?
let primaryButton: DialogButton?
let provider: DialogProvider

public init(
title: String,
provider: DialogProvider
) {
self.title = title
self.message = nil
self.secondaryButton = nil
self.primaryButton = nil
self.provider = provider
}

private init(
title: String,
message: String?,
secondaryButton: DialogButton?,
primaryButton: DialogButton?,
provider: DialogProvider
) {
self.title = title
self.message = message
self.secondaryButton = secondaryButton
self.primaryButton = primaryButton
self.provider = provider
}

public func message(_ message: String?) -> Self {
.init(title: title, message: message, secondaryButton: secondaryButton, primaryButton: primaryButton, provider: provider)
}

public func secondaryButton(_ title: String, action: @escaping () -> Void) -> Self {
.init(title: self.title, message: message, secondaryButton: .init(title, action: action), primaryButton: primaryButton, provider: provider)
}

public func primaryButton(_ title: String, action: @escaping () -> Void) -> Self {
.init(title: self.title, message: message, secondaryButton: secondaryButton, primaryButton: .init(title, action: action), provider: provider)
}

public func show() {
provider.title = title
provider.message = message
provider.secondaryButton = secondaryButton
provider.primaryButton = primaryButton
provider.isPresent = true
}
}

@Published public var isPresent = false
@Published var title: String = ""
@Published var message: String?
Expand All @@ -63,9 +10,41 @@ public final class DialogProvider: ObservableObject, ModalProvider {

public init() {}

@discardableResult
public func present(
_ title: String
) -> Builder {
.init(title: title, provider: self)
) -> Self {
self.title = title
self.message = nil
self.secondaryButton = nil
self.primaryButton = nil
self.isPresent = true
return self
}

@discardableResult
public func message(
_ message: String
) -> Self {
self.message = message
return self
}

@discardableResult
public func secondaryButton(
_ title: String,
action: @escaping () -> Void
) -> Self {
self.secondaryButton = .init(title, action: action)
return self
}

@discardableResult
public func primaryButton(
_ title: String,
action: @escaping () -> Void
) -> Self {
self.primaryButton = .init(title, action: action)
return self
}
}

0 comments on commit 22d0a88

Please sign in to comment.