Files
suixinkan_ios_new/suixinkan/App/State/ToastCenter.swift
汉秋 49e997ddba 修复合作获客扫码绑定崩溃与 Toast 层级,并接入分成比例修改。
将扫码页提升到列表层弹出并补齐 AppDelegate.window,避免扫码时 UIKit 取主窗口崩溃;Toast 改用独立 UIWindow 显示在 sheet 之上;合作获客员支持短信验证修改分成比例,同时优化冷启动根视图创建时机并补充相关测试。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-03 20:37:24 +08:00

191 lines
5.7 KiB
Swift
Raw Permalink 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
import UIKit
@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 宿 sheet / fullScreenCover present
@MainActor
private final class ToastWindowPresenter {
static let shared = ToastWindowPresenter()
private var toastWindow: UIWindow?
/// UIWindow Toast
func show(message: String) {
guard let windowScene = resolveWindowScene() else { return }
let window: UIWindow
if let existing = toastWindow, existing.windowScene === windowScene {
window = existing
} else {
let newWindow = PassthroughWindow(windowScene: windowScene)
newWindow.windowLevel = .alert + 1
newWindow.backgroundColor = .clear
toastWindow = newWindow
window = newWindow
}
let hosting = UIHostingController(
rootView: ToastBannerHostView(message: message)
)
hosting.view.backgroundColor = .clear
window.rootViewController = hosting
window.isHidden = false
}
/// Toast
func hide() {
toastWindow?.isHidden = true
toastWindow?.rootViewController = nil
}
/// window scene Toast
private func resolveWindowScene() -> UIWindowScene? {
let scenes = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }
return scenes.first { $0.activationState == .foregroundActive } ?? scenes.first
}
}
/// Toast
private final class PassthroughWindow: UIWindow {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
guard let view = super.hitTest(point, with: event) else { return nil }
return view === rootViewController?.view ? nil : view
}
}
/// Toast
private struct ToastBannerHostView: View {
let message: String
var body: some View {
ZStack {
ToastBannerContent(message: message)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.allowsHitTesting(false)
}
}
/// Toast
private struct ToastBannerContent: View {
let message: String
var body: some View {
HStack(spacing: 10) {
Image(systemName: "exclamationmark.triangle.fill")
.font(.system(size: 16, weight: .semibold))
Text(message)
.font(.system(size: 16, weight: .medium))
.lineLimit(3)
.multilineTextAlignment(.leading)
.fixedSize(horizontal: false, vertical: true)
}
.foregroundStyle(.white)
.padding(.horizontal, 20)
.padding(.vertical, 14)
.background(Color.black.opacity(0.78), in: RoundedRectangle(cornerRadius: 12))
.padding(.horizontal, 40)
.accessibilityIdentifier("global.toast")
}
}
/// Toast ToastCenter
private struct GlobalToastOverlay: ViewModifier {
@EnvironmentObject private var toastCenter: ToastCenter
func body(content: Content) -> some View {
content
.onAppear {
syncToastWindow(with: toastCenter.message)
}
.onChange(of: toastCenter.message) { message in
syncToastWindow(with: message)
}
}
/// Toast
private func syncToastWindow(with message: String?) {
if let message {
ToastWindowPresenter.shared.show(message: message)
} else {
ToastWindowPresenter.shared.hide()
}
}
}
#if DEBUG
/// Toast
struct ToastSnapshot: Equatable {
let message: String?
}
#endif
extension View {
/// Toast
func globalToastOverlay() -> some View {
modifier(GlobalToastOverlay())
}
}