Files
suixinkan_ios_new/suixinkan/App/State/ToastCenter.swift
汉秋 703078352c 从 iOS 17 Observation 迁移至 iOS 16 兼容的 Combine 架构
将最低部署版本降至 iOS 16,以 ObservableObject 替换 @Observable,新增导航与 UI 兼容层,并补充登录冒烟 UI 测试。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-26 10:16:35 +08:00

116 lines
3.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// ToastCenter.swift
// suixinkan
//
// Created by Codex on 2026/6/18.
//
import Combine
import SwiftUI
@MainActor
/// Toast
final class ToastCenter: ObservableObject {
@Published fileprivate var message: String?
private let autoDismissNanoseconds: UInt64
@Published private var dismissTask: Task<Void, Never>?
@Published private var displayToken: UInt64 = 0
/// Toast 2.2
init(autoDismissNanoseconds: UInt64 = 2_200_000_000) {
self.autoDismissNanoseconds = autoDismissNanoseconds
}
/// Toast
func show(_ message: String) {
displayToken &+= 1
self.message = message
scheduleAutoDismiss(token: displayToken)
}
/// Toast
func dismiss() {
displayToken &+= 1
dismissTask?.cancel()
dismissTask = nil
message = nil
}
/// Toast Toast
private func scheduleAutoDismiss(token: UInt64) {
dismissTask?.cancel()
let delay = autoDismissNanoseconds
dismissTask = Task { [weak self] in
do {
try await Task.sleep(nanoseconds: delay)
} catch {
return
}
await MainActor.run {
guard let self, self.displayToken == token else { return }
self.message = nil
self.dismissTask = nil
}
}
}
#if DEBUG
/// Toast
var snapshotForTests: ToastSnapshot {
ToastSnapshot(message: message)
}
#endif
}
/// Toast Toast
private struct GlobalToastOverlay: ViewModifier {
@EnvironmentObject private var toastCenter: ToastCenter
func body(content: Content) -> some View {
content
.overlay(alignment: .top) {
GeometryReader { proxy in
if let message = toastCenter.message {
toastBanner(message, topInset: proxy.safeAreaInsets.top)
.transition(.move(edge: .top).combined(with: .opacity))
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
}
}
.allowsHitTesting(false)
}
.animation(.easeInOut(duration: 0.18), value: toastCenter.message)
}
/// Toast
private func toastBanner(_ message: String, topInset: CGFloat) -> some View {
HStack {
Text(message)
.font(.system(size: 14, weight: .medium))
.lineLimit(2)
.multilineTextAlignment(.center)
.fixedSize(horizontal: false, vertical: true)
.frame(maxWidth: .infinity, alignment: .center)
}
.foregroundStyle(.white)
.padding(.horizontal, AppMetrics.Spacing.pageHorizontal)
.padding(.top, topInset + AppMetrics.Spacing.small)
.padding(.bottom, AppMetrics.Spacing.small)
.frame(maxWidth: .infinity, alignment: .center)
.background(AppDesign.primary.ignoresSafeArea(edges: .top))
}
}
#if DEBUG
/// Toast
struct ToastSnapshot: Equatable {
let message: String?
}
#endif
extension View {
/// Toast
func globalToastOverlay() -> some View {
modifier(GlobalToastOverlay())
}
}