import Foundation // MARK: - API Loading State /// API 加载状态枚举 enum APILoadingState: Equatable { case loading // 正在加载 case error(message: String) // 加载失败,包含错误信息 case success // 加载成功 } // MARK: - API Loading Item /// 单个 API 加载项 struct APILoadingItem: Identifiable, Equatable { let id: UUID let state: APILoadingState let shouldShowError: Bool // 是否显示错误信息 let shouldShowLoading: Bool // 是否显示loading动画 let createdAt: Date init(id: UUID = UUID(), state: APILoadingState, shouldShowError: Bool = true, shouldShowLoading: Bool = true) { self.id = id self.state = state self.shouldShowError = shouldShowError self.shouldShowLoading = shouldShowLoading self.createdAt = Date() } /// 是否应该显示此项目 var shouldDisplay: Bool { switch state { case .loading: return shouldShowLoading case .error: return shouldShowError case .success: return false } } /// 是否是错误状态 var isError: Bool { if case .error = state { return true } return false } /// 获取错误信息 var errorMessage: String? { if case .error(let message) = state { return message } return nil } } // MARK: - API Loading Configuration /// API Loading 配置 struct APILoadingConfiguration { /// Loading 视图大小 static let loadingSize: CGFloat = 88 /// 背景透明度 static let backgroundAlpha: CGFloat = 0.6 /// 圆角大小 static let cornerRadius: CGFloat = 12 /// 错误信息显示时长(秒) static let errorDisplayDuration: TimeInterval = 2.0 /// 动画时长 static let animationDuration: Double = 0.3 }