Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public struct ToastOverlayView: View {
.padding(.top, 12)
.transition(.move(edge: .top).combined(with: .opacity))
.onTapGesture {
if let action = toast.action {
action.handler()
}
toastCenter.dismiss(id: toast.id)
}
}
Expand Down
30 changes: 29 additions & 1 deletion Packages/Env/Sources/Env/ToastCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ import SwiftUI
@Observable
public final class ToastCenter {
public struct Toast: Identifiable, Equatable {
public struct Action: Identifiable, Equatable {
public let id: UUID
public let handler: () -> Void

public init(id: UUID = UUID(), handler: @escaping () -> Void) {
self.id = id
self.handler = handler
}

public static func == (lhs: Action, rhs: Action) -> Bool {
lhs.id == rhs.id
}
}

public enum Kind: Equatable {
case message
case progress
Expand All @@ -16,6 +30,7 @@ public final class ToastCenter {
public var tint: Color
public var kind: Kind
public var progress: Double?
public var action: Action?

public init(
id: UUID = UUID(),
Expand All @@ -24,7 +39,8 @@ public final class ToastCenter {
systemImage: String? = nil,
tint: Color = .accentColor,
kind: Kind = .message,
progress: Double? = nil
progress: Double? = nil,
action: Action? = nil
) {
self.id = id
self.title = title
Expand All @@ -33,6 +49,18 @@ public final class ToastCenter {
self.tint = tint
self.kind = kind
self.progress = progress
self.action = action
}

public static func == (lhs: Toast, rhs: Toast) -> Bool {
lhs.id == rhs.id &&
lhs.title == rhs.title &&
lhs.message == rhs.message &&
lhs.systemImage == rhs.systemImage &&
lhs.tint == rhs.tint &&
lhs.kind == rhs.kind &&
lhs.progress == rhs.progress &&
lhs.action?.id == rhs.action?.id
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extension StatusEditor {
@Environment(\.modelContext) private var context
@Environment(UserPreferences.self) private var preferences
@Environment(Theme.self) private var theme
@Environment(RouterPath.self) private var routerPath
@Environment(ToastCenter.self) private var toastCenter

#if targetEnvironment(macCatalyst)
Expand Down Expand Up @@ -149,13 +150,16 @@ extension StatusEditor {
mainStore.postingToastID = nil
}

if status != nil {
if let status {
let successToast = ToastCenter.Toast(
id: toastID,
title: String(localized: "toast.posting.success.title"),
systemImage: "checkmark.circle.fill",
tint: theme.tintColor,
kind: .message
kind: .message,
action: .init { [routerPath] in
routerPath.navigate(to: .statusDetailWithStatus(status: status))
}
)
toastCenter.update(id: toastID, toast: successToast, autoDismissAfter: .seconds(3))
} else {
Expand Down
Loading