Skip to content

Commit

Permalink
feat: BBToastService 구현, BBToast의 버튼 액션이 전달되지 않는 문제 수정 (depromeet#646)
Browse files Browse the repository at this point in the history
* feat: BBToastService 구현

* feat: BBToast문서주석 작성

* fix: Toast의 Button위치가 올바르지 않은 문제 수정

* feat: 코드 리뷰 바녕
  • Loading branch information
rlarjsdn3 authored Sep 26, 2024
1 parent 32bdd37 commit c1c593c
Show file tree
Hide file tree
Showing 21 changed files with 446 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ protocol ManagementNavigatorProtocol: BaseNavigator {
func toFamilyNameSetting()

func presentSharingSheet(url: URL?)

func showSuccessToast()
func showErrorToast()

}

final class ManagementNavigator: ManagementNavigatorProtocol {
Expand Down Expand Up @@ -72,18 +70,4 @@ final class ManagementNavigator: ManagementNavigatorProtocol {
navigationController.present(activityVC, animated: true)
}


// MARK: - Show

func showSuccessToast() {
BBToast.default(
image: DesignSystemAsset.link.image,
title: "링크가 복사되었어요"
).show()
}

func showErrorToast() {
BBToast.style(.error).show()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,7 @@ extension MainViewReactor {
}
}

let eventMutation = provider.managementService.event
.flatMap { event -> Observable<Mutation> in
switch event {
case .didTapCopyUrlAction:
self.pushViewController(type: .showToastMessage(DesignSystemAsset.link.image, "링크가 복사되었어요"))
return .empty()
default:
return .empty()
}
}

return Observable<Mutation>.merge(mutation, eventMutation, homeMutation)
return Observable<Mutation>.merge(mutation, homeMutation)
}

func mutate(action: Action) -> Observable<Mutation> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
//

import Core
import DesignSystem
import UIKit

import RxSwift

// TODO: - 코드 리팩토링하기

public class CopyInvitationUrlActivity: UIActivity {
Expand Down Expand Up @@ -48,8 +51,14 @@ public class CopyInvitationUrlActivity: UIActivity {
}

public override func perform() {
let viewConfig = BBToastViewConfiguration(minWidth: 100)
provider.bbToastService.show(
image: DesignSystemAsset.link.image,
title: "링크가 복사되었어요",
viewConfig: viewConfig
)

UIPasteboard.general.string = url.description
provider.managementService.didTapCopUrlAction() // TODO: - 오타 수정
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
// Created by 김건우 on 12/11/23.
//

import UIKit

import Core
import DesignSystem
import Domain
import Differentiator
import UIKit

import ReactorKit
import RxSwift

public final class ManagementReactor: Reactor {

Expand Down Expand Up @@ -101,24 +100,6 @@ public final class ManagementReactor: Reactor {
}


public func transform(mutation: Observable<Mutation>) -> Observable<Mutation> {
let eventMutation = provider.managementService.event
.withUnretained(self)
.flatMap {
switch $0.1 {
case .didTapCopyUrlAction:
$0.0.navigator.showSuccessToast()
return Observable<Mutation>.empty()

@unknown default:
return Observable<Mutation>.empty()
}
}

return Observable<Mutation>.merge(mutation, eventMutation)
}


// MARK: - Mutate

public func mutate(action: Action) -> Observable<Mutation> {
Expand All @@ -135,7 +116,7 @@ public final class ManagementReactor: Reactor {
.concatMap {
guard let url = $0.1?.url else {
Haptic.notification(type: .error)
$0.0.navigator.showErrorToast()
$0.0.provider.bbToastService.show(.error)
return Observable<Mutation>.just(.setHiddenSharingProgressHud(true))
}

Expand Down Expand Up @@ -176,7 +157,7 @@ public final class ManagementReactor: Reactor {
.concatMap {
guard let results = $0.1?.results else {
Haptic.notification(type: .error)
$0.0.navigator.showErrorToast()
$0.0.provider.bbToastService.show(.error)
return Observable.concat(
Observable<Mutation>.just(.setMemberDatasource([])),
Observable<Mutation>.just(.setHiddenTableProgressHud(true)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension BBAlert {
/// 예를 들어, BBAlert 뷰가 나타나거나 사라질 때 페이드 효과를 주기 원한다면 아래와 같이 ``BBAlertConfiguration``을 설정해야 합니다.
///
/// ```swift
/// let config = BBConfiguration(
/// let config = BBAlertConfiguration(
/// enteringAnimation: .fade(alpha: 0),
/// exitingAnimation: .fade(alpha: 0)
/// )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension BBAlert {
/// 아무런 배경 색상을 적용하지 않습니다.
case none

// 특정 색상을 배경 색상으로 적용합니다.
/// 특정 색상을 배경 색상으로 적용합니다.
case color(color: UIColor = defaultImageTint.withAlphaComponent(0.25))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import UIKit

import SnapKit

extension UIControl.State: Hashable {

}

/// BBButton은 `titleLabel`이 BBFontStyle에 맞게 커스텀되어 있는 버튼입니다.
public class BBButton: UIButton {

// MARK: - Views
Expand Down Expand Up @@ -120,3 +118,6 @@ public class BBButton: UIButton {
}

}


extension UIControl.State: Hashable { }
73 changes: 64 additions & 9 deletions 14th-team5-iOS/Core/Sources/Bibbi/BBCommons/BBToast/BBToast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,38 @@ import UIKit

// MARK: - Typealias

public typealias BBToastAction = ((BBToast?) -> Void)?
/// BBToast 버튼의 동작을 정의하는 핸들러입니다.
public typealias BBToastActionHandler = ((BBToast?) -> Void)?

/// BBToast는 BBToast를 화면에 띄우게 도와줍니다.
///
/// **show(after:)** 메서드로 BBToastt를 띄울 수 있으며, **close(animated:, completion:)** 메서드로 BBToast를 사라지게 할 수 있습니다.
///
/// 아래는 BBToast를 띄우는 가장 기본적인 방법을 보여줍니다.
///
/// ```swift
/// let toast = BBTast.text("Hello, BBToast!")
/// toast.show()
/// ```
///
/// BBToast는 델리게이트 패턴을 지원합니다. BBToast의 생명 주기에 맞게 필요한 동작을 구현할 수 있습니다. BBToast는 멀티캐스트 델리게이트 패턴으로 구현되어 있습니다.
///
/// /아래는 BBToast에 델리게이트 패턴을 구현하는 방법을 보여줍니다.
///
/// ```swift
/// // ViewController.swift
/// let toast = BBToast.text("Hello, BBToast!")
/// toast.addDelegate(self)
///
/// extension ViewController: BBToastDelegate { ... }
/// ````
///
/// - Note: 지원하는 델리게이트 메서드에 대한 자세한 정보는 ``BBToastDelegate``를 참조하세요.
///
/// ``BBToastConfiguration``과 ``BBToastViewConfiguration`` 구조체를 활용하여 BBToast의 애니메이션, 배경 색상 및 BBToast 뷰의 크기, 둥글기 반경을 설정할 수 있습니다.
///
/// - Authors: 김소월
///
public class BBToast {

// MARK: - Properties
Expand Down Expand Up @@ -76,7 +106,7 @@ public class BBToast {
/// - Returns: BBToast
public static func `default`(
image: UIImage,
imageTint: UIColor = defaultImageTint,
imageTint: UIColor? = defaultImageTint,
title: String,
titleColor: UIColor? = nil,
titleFontStyle: BBFontStyle? = nil,
Expand Down Expand Up @@ -111,14 +141,15 @@ public class BBToast {
/// - config: Toast 설정값
/// - Returns: BBToast
public static func button(
image: UIImage,
imageTint: UIColor = defaultImageTint,
image: UIImage? = nil,
imageTint: UIColor? = defaultImageTint,
title: String,
titleColor: UIColor? = nil,
titleFontStyle: BBFontStyle? = nil,
buttonTitle: String,
buttonTitleFontStyle: BBFontStyle? = nil,
buttonTint: UIColor? = nil,
action: BBToastActionHandler = nil,
viewConfig: BBToastViewConfiguration = BBToastViewConfiguration(),
config: BBToastConfiguration = BBToastConfiguration()
) -> BBToast {
Expand All @@ -132,6 +163,7 @@ public class BBToast {
buttonTitle: buttonTitle,
buttonTitleFontStlye: buttonTitleFontStyle,
buttonTint: buttonTint,
action: action,
viewConfig: viewConfig
),
viewConfig: viewConfig
Expand All @@ -152,7 +184,7 @@ public class BBToast {
switch style {
case .error:
let viewConfig = BBToastViewConfiguration(
minWidth: 250
minWidth: 100
)
let view = DefaultToastView(
child: IconToastView(
Expand All @@ -166,6 +198,11 @@ public class BBToast {
}
}

/// 직접 커스텀한 뷰로 BBToast를 생성합니다.
/// - Parameters:
/// - view: BBToastStackView 프로토콜을 준수하는 UIView
/// - config: BBToast 설정값
/// - Returns: BBToast
public static func custom(
view: BBToastView,
config: BBToastConfiguration = BBToastConfiguration()
Expand All @@ -176,14 +213,20 @@ public class BBToast {

// MARK: - Show

/// BBToast를 화면에 보이게 합니다.
/// - Parameters:
/// - type: HapticFeedback의 타입
/// - time: 지연 시간
public func show(
haptic type: UINotificationFeedbackGenerator.FeedbackType,
after time: TimeInterval = 0
) {
UINotificationFeedbackGenerator().notificationOccurred(type)
Haptic.notification(type: type)
show(after: time)
}

/// BBToast를 화면에 보이게 합니다.
/// - Parameter delay: 지연 시간
public func show(after delay: TimeInterval = 0) {
if let backgroundView = self.createBackgroundView() {
self.backgroundView = backgroundView
Expand Down Expand Up @@ -228,6 +271,12 @@ public class BBToast {

// MARK: - Close

/// BBToast를 화면에 사라지게 합니다.
///
/// BBToast가 화면에 사라지고 나면 완료 핸들러가 호출됩니다. 완료 핸들러는 델리게이트의 `didCloseToast(_:)` 메서드가 호출되기 전에 실행됩니다.
/// - Parameters:
/// - animated: 애니메이션 유무
/// - completion: 완료 핸들러
public func close(
animated: Bool = true,
completion: (() -> Void)? = nil
Expand Down Expand Up @@ -286,16 +335,22 @@ public class BBToast {

extension BBToast {

/// 델리게이트 패턴을 적용합니다.
/// - Parameter delegate: BBToastDelegate 프로토콜을 준수하는 객체
public func addDelegate(_ delegate: BBToastDelegate) {
Self.multicast.add(delegate)
}

public func addButtonAction(
_ action: BBToastAction = nil
/// 버튼을 추가합니다.
///
/// `BBToast.button` 메서드로 BBToast를 생성하는 경우, 해당 메서드로 버튼의 액션을 정의해주어야 합니다.
/// - Parameter action: 버튼의 액션
public func setAction(
_ action: BBToastActionHandler = nil
) {
if let view = view as? DefaultToastView,
let subview = view.child as? ButtonToastView {
subview.buttonAction = action
subview.action = action
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ import UIKit

extension BBToast {

/// BBToast 뷰가 나타나거나 사라질 때 수행되는 애니메이션을 정의한 열거형입니다.
///
/// BBToast 뷰가 나타날 때(enteringAnimation)는 뷰의 초기 상태를 전달해야 합니다. BBToast 뷰가 사라질 때(exitingAnimation)은 뷰의 최종 상태를 전달해야 합니다.
///
/// 예를 들어, BBToast 뷰가 나타나거나 사라질 때 페이드 효과를 주기 원한다면 아래와 같이 ``BBToastConfiguration``을 설정해야 합니다.
///
/// ```swift
/// let config = BBToastConfiguration(
/// enteringAnimation: .fade(alpha: 0),
/// exitingAnimation: .fade(alpha: 0)
/// )
/// ```
///
/// - Authors: 김소월
public enum Animation {
case slide(x: CGFloat, y: CGFloat)
case fade(alpha: CGFloat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ import UIKit

extension BBToast {

/// Alert 뒤의 배경 색상을 설정합니다.
///
/// - Authors: 김소월
public enum Background {

/// 아무런 배경 색상을 적용하지 않습니다.
case none

/// 특정 색상을 배경 색상으로 적용합니다.
case color(color: UIColor = defaultImageTint.withAlphaComponent(0.25))
}

Expand Down
Loading

0 comments on commit c1c593c

Please sign in to comment.