Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce dismiss policy for Toast view #2716

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions podcasts/Common SwiftUI/Toast/Toast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class Toast {
private var window: UIWindow? = nil

/// Display the toast message with the given title and actions
static func show<Style: ToastTheme>(_ title: String, actions: [Action]? = nil, dismissAfter: TimeInterval = 5.0, theme: Style = .defaultTheme, aboveMiniPlayer: Bool = false) {
static func show<Style: ToastTheme>(_ title: String, actions: [Action]? = nil, dismissAfter: ToastViewDismissPolicy = .interval(5.0), theme: Style = .defaultTheme, aboveMiniPlayer: Bool = false) {
// Hide any active toasts
shared.toastDismissed()

guard let scene = SceneHelper.connectedScene() else { return }

let viewModel = ToastViewModel(coordinator: shared, title: title, actions: actions, dismissTime: dismissAfter, aboveMiniPlayer: aboveMiniPlayer)
let viewModel = ToastViewModel(coordinator: shared, title: title, actions: actions, dismissPolicy: dismissAfter, aboveMiniPlayer: aboveMiniPlayer)
let view = ToastView(viewModel: viewModel, style: theme)
let controller = ThemedHostingController(rootView: view)

Expand Down
2 changes: 1 addition & 1 deletion podcasts/Common SwiftUI/Toast/ToastView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ struct ToastView_Previews: PreviewProvider {
ToastView(viewModel: .init(coordinator: PreviewCoordinator(), title: "Hello World", actions: [
.init(title: "Tap Me", action: {
print("Tapped")
})], dismissTime: .infinity), style: .defaultTheme)
})], dismissPolicy: .never), style: .defaultTheme)
}

private class PreviewCoordinator: ToastDelegate {
Expand Down
22 changes: 16 additions & 6 deletions podcasts/Common SwiftUI/Toast/ToastViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ protocol ToastDelegate: AnyObject {
func toastDismissed()
}

enum ToastViewDismissPolicy {
case never
case interval(TimeInterval)
}

class ToastViewModel: ObservableObject {
weak var coordinator: ToastDelegate?

Expand All @@ -13,7 +18,7 @@ class ToastViewModel: ObservableObject {

let title: String
let actions: [Toast.Action]
let dismissTime: TimeInterval
let dismissPolicy: ToastViewDismissPolicy
let aboveMiniPlayer: Bool

deinit {
Expand All @@ -24,11 +29,11 @@ class ToastViewModel: ObservableObject {
/// When this is true the view should animate out and call `didDismiss`
@Published var didAutoDismiss = false

init(coordinator: ToastDelegate, title: String, actions: [Toast.Action]?, dismissTime: TimeInterval, aboveMiniPlayer: Bool = false) {
init(coordinator: ToastDelegate, title: String, actions: [Toast.Action]?, dismissPolicy: ToastViewDismissPolicy, aboveMiniPlayer: Bool = false) {
self.coordinator = coordinator
self.title = title
self.actions = actions ?? []
self.dismissTime = dismissTime
self.dismissPolicy = dismissPolicy
self.aboveMiniPlayer = aboveMiniPlayer
}

Expand All @@ -48,9 +53,14 @@ class ToastViewModel: ObservableObject {

func didAppear() {
// Start the auto dismiss timer
autoDismissTimer = Timer.scheduledTimer(withTimeInterval: dismissTime, repeats: false, block: { [weak self] _ in
self?.handleAutoDismiss()
})
switch dismissPolicy {
case .never:
return
case .interval(let dismissTime):
autoDismissTimer = Timer.scheduledTimer(withTimeInterval: dismissTime, repeats: false, block: { [weak self] _ in
self?.handleAutoDismiss()
})
}
}

// If the toast is already being dismiss, then cancel the timer
Expand Down