Skip to content

Commit

Permalink
feat: toast can have action, can be removed
Browse files Browse the repository at this point in the history
  • Loading branch information
fleuryj committed Dec 6, 2024
1 parent 75e0bb2 commit 3716cd1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
13 changes: 11 additions & 2 deletions SBBDesignSystemMobileSwiftUI/Views/SBBToast/ToastView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ public struct ToastView: View {
}

public var body: some View {
viewModel.label
.sbbFont(.small_light)
HStack(alignment: .center, spacing: 16) {
viewModel.label
.sbbFont(.small_light)

if let actionLabel = viewModel.actionLabel, let action = viewModel.onClickAction {
Button(action: action) {
actionLabel
.sbbFont(.small_bold)
}
}
}
.foregroundColor(Color.sbbColor(.white))
.multilineTextAlignment(.center)
.padding(.horizontal, 20)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ import SwiftUI
*/
public class SBBToast: Identifiable, ObservableObject {

public let id = UUID()
public let id: UUID
let label: Text
let easeInOutAnimationDuration: Double
let presentedDuration: Double
let actionLabel: Text?
let onClickAction: (() -> Void)?

@Published var isPresented = false

Expand All @@ -62,10 +64,13 @@ public class SBBToast: Identifiable, ObservableObject {
- easeInOutAnimationDuration: The duration of the ease-in and ease-out animation performed upon presenting/hiding a toast.
- presentedDuration: The duration during which the toast is presented on screen
*/
public init(label: Text, easeInOutAnimationDuration: Double = 1, presentedDuration: Double = 2) {
public init(label: Text, easeInOutAnimationDuration: Double = 1, presentedDuration: Double = 2, id: UUID = UUID(), actionLabel: Text? = nil, onClickAction: (() -> Void)? = nil) {
self.label = label
self.easeInOutAnimationDuration = easeInOutAnimationDuration
self.presentedDuration = presentedDuration
self.actionLabel = actionLabel
self.onClickAction = onClickAction
self.id = id
}

// initializer for SwiftUI Previews
Expand All @@ -74,5 +79,8 @@ public class SBBToast: Identifiable, ObservableObject {
self.easeInOutAnimationDuration = 1
self.presentedDuration = 2
self.isPresented = isPresented
self.actionLabel = nil
self.onClickAction = nil
self.id = UUID()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,22 @@ public class SBBToastService: ObservableObject {
}

/**
Instantaneously removes an already pressented SBBToast View. SBBToast Views are removed automatically after the defined presentation duration, this method allows to remove them sooner.
Instantaneously removes an already presented SBBToast View. SBBToast Views are removed automatically after the defined presentation duration, this method allows to remove them sooner.

- Parameters:
- toast: The SBBToast View to be removed instantaneously.
*/
func removeToast(_ toast: SBBToast) {
public func removeToast(_ toast: SBBToast) {
toasts.removeAll(where: { $0.id == toast.id })
}

/**
Instantaneously removes an already presented SBBToast View. SBBToast Views are removed automatically after the defined presentation duration, this method allows to remove them sooner.

- Parameters:
- where: The condition for which to remove the SBBToast.
*/
public func removeToast(where condition: (SBBToast) -> Bool) {
toasts.removeAll(where: condition)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ struct ToastDemo: View {
Text("Show Toast")
}
.buttonStyle(SBBPrimaryButtonStyle())

Button(action: {
let uuid = UUID()
counter += 1
toastService.showToast(SBBToast(label: Text("Hello Toast \(counter)\nThis Toast has multiple lines, in fact it might be longer than 2 IC2000 compositions linked together."), id: uuid, actionLabel: Text("Remove it"), onClickAction: {
toastService.removeToast(where: { $0.id == uuid })
}))
}) {
Text("Show Multiline Toast with Action")
}
.buttonStyle(SBBSecondaryButtonStyle())
Button(action: {
let uuid = UUID()
counter += 1
toastService.showToast(SBBToast(label: Text("Hello Toast \(counter)"), id: uuid, actionLabel: Text("Remove it"), onClickAction: {
toastService.removeToast(where: { $0.id == uuid })
}))
}) {
Text("Show Toast with Action")
}
.buttonStyle(SBBPrimaryButtonStyle())
}
.sbbScreenPadding()
}
Expand All @@ -36,7 +57,6 @@ struct ToastDemo: View {
.sbbStyle()
.sbbToastContainer()
.colorScheme(colorScheme)

}
}

Expand Down

0 comments on commit 3716cd1

Please sign in to comment.