Add Lottie-based global loading overlay across key flows.

Introduce GlobalLoadingCenter with reference counting, wire it through RootView and major auth/order/profile screens, and add the loading animation asset plus unit tests.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-06-23 11:25:56 +08:00
parent 1d1eb46ed8
commit 7fd964fe19
17 changed files with 514 additions and 139 deletions

View File

@ -0,0 +1,113 @@
//
// GlobalLoadingCenterTests.swift
// suixinkanTests
//
// Created by Codex on 2026/6/23.
//
import XCTest
@testable import suixinkan
@MainActor
/// Loading 使
final class GlobalLoadingCenterTests: XCTestCase {
/// show/hide
func testShowHideUsesReferenceCount() {
let center = GlobalLoadingCenter()
center.show(message: "加载账号")
center.show(message: "加载权限")
XCTAssertEqual(center.snapshotForTests, GlobalLoadingSnapshot(isVisible: true, message: "加载权限", activeCount: 2))
center.hide()
XCTAssertEqual(center.snapshotForTests, GlobalLoadingSnapshot(isVisible: true, message: "加载权限", activeCount: 1))
center.hide()
XCTAssertEqual(center.snapshotForTests, GlobalLoadingSnapshot(isVisible: false, message: "", activeCount: 0))
}
/// hide show
func testHideNeverDropsReferenceCountBelowZero() {
let center = GlobalLoadingCenter()
center.hide()
XCTAssertEqual(center.snapshotForTests, GlobalLoadingSnapshot(isVisible: false, message: "", activeCount: 0))
}
///
func testUpdateMessageOnlyChangesVisibleMessage() {
let center = GlobalLoadingCenter()
center.updateMessage("不会展示")
XCTAssertEqual(center.snapshotForTests, GlobalLoadingSnapshot(isVisible: false, message: "", activeCount: 0))
center.show(message: "加载中")
center.updateMessage("即将完成")
XCTAssertEqual(center.snapshotForTests, GlobalLoadingSnapshot(isVisible: true, message: "即将完成", activeCount: 1))
}
/// withLoading Loading
func testWithLoadingHidesAfterSuccess() async throws {
let center = GlobalLoadingCenter()
let value = try await center.withLoading(message: "提交中") {
XCTAssertEqual(center.snapshotForTests, GlobalLoadingSnapshot(isVisible: true, message: "提交中", activeCount: 1))
return 42
}
XCTAssertEqual(value, 42)
XCTAssertEqual(center.snapshotForTests, GlobalLoadingSnapshot(isVisible: false, message: "", activeCount: 0))
}
/// withLoading Loading
func testWithLoadingHidesAfterFailure() async {
let center = GlobalLoadingCenter()
do {
_ = try await center.withLoading(message: "提交中") {
throw TestFailure.expected
} as Int
XCTFail("withLoading 应该向外抛出业务错误")
} catch {
XCTAssertEqual(error as? TestFailure, .expected)
}
XCTAssertEqual(center.snapshotForTests, GlobalLoadingSnapshot(isVisible: false, message: "", activeCount: 0))
}
/// withOptionalLoading
func testWithOptionalLoadingDisabledDoesNotChangeState() async throws {
let center = GlobalLoadingCenter()
let value = try await center.withOptionalLoading(false, message: "不展示") {
XCTAssertEqual(center.snapshotForTests, GlobalLoadingSnapshot(isVisible: false, message: "", activeCount: 0))
return "done"
}
XCTAssertEqual(value, "done")
XCTAssertEqual(center.snapshotForTests, GlobalLoadingSnapshot(isVisible: false, message: "", activeCount: 0))
}
/// 使 Loading
func testBusinessCodeOnlyDependsOnCommandCenter() {
let center = GlobalLoadingCenter()
issueLoadingCommand(center)
XCTAssertEqual(center.snapshotForTests, GlobalLoadingSnapshot(isVisible: true, message: "加载中", activeCount: 1))
center.hide()
}
///
private func issueLoadingCommand(_ center: GlobalLoadingCenter) {
center.show(message: "加载中")
}
///
private enum TestFailure: Error, Equatable {
case expected
}
}