Extract dependency wiring and session lifecycle from the container and RootViewController, split NetworkBundle by domain, and add module-level feature services while keeping flat accessors for compatibility. Co-authored-by: Cursor <cursoragent@cursor.com>
72 lines
2.1 KiB
Swift
72 lines
2.1 KiB
Swift
//
|
||
// AppServices.swift
|
||
// suixinkan
|
||
//
|
||
// Created by Codex on 2026/6/26.
|
||
//
|
||
|
||
import Foundation
|
||
|
||
@MainActor
|
||
/// 全局依赖容器,集中提供 ViewModel 与 ViewController 所需的 API 和上下文。
|
||
final class AppServices {
|
||
static let shared = AppServices()
|
||
|
||
let session: SessionBundle
|
||
let context: AppContextBundle
|
||
let network: NetworkBundle
|
||
let ui: UIFeedbackBundle
|
||
let runtime: AppRuntimeBundle
|
||
let appNavigator: AppNavigator
|
||
|
||
/// 生产环境默认初始化。
|
||
private convenience init() {
|
||
self.init(assembly: AppServicesAssembly())
|
||
}
|
||
|
||
/// 测试专用初始化,允许注入 mock `APIClient` 与存储替身。
|
||
convenience init(
|
||
apiClient: APIClient,
|
||
tokenStore: SessionTokenStore = SessionTokenStore(),
|
||
snapshotStore: AccountSnapshotStore = AccountSnapshotStore(),
|
||
preferencesStore: AppPreferencesStore = AppPreferencesStore()
|
||
) {
|
||
self.init(
|
||
assembly: AppServicesAssembly(
|
||
apiClient: apiClient,
|
||
tokenStore: tokenStore,
|
||
snapshotStore: snapshotStore,
|
||
preferencesStore: preferencesStore
|
||
)
|
||
)
|
||
}
|
||
|
||
/// 使用装配器完成容器初始化并建立跨 bundle 绑定。
|
||
private init(assembly: AppServicesAssembly) {
|
||
let dependencies = assembly.makeDependencies()
|
||
session = dependencies.session
|
||
context = dependencies.context
|
||
network = dependencies.network
|
||
ui = dependencies.ui
|
||
runtime = dependencies.runtime
|
||
appNavigator = dependencies.appNavigator
|
||
bindAuthTokenProvider()
|
||
}
|
||
|
||
/// 绑定推送与 UIKit 导航。
|
||
func configurePushNotifications() {
|
||
PushNotificationManager.shared.configure(
|
||
api: pushAPI,
|
||
session: appSession,
|
||
navigator: appNavigator
|
||
)
|
||
}
|
||
|
||
/// 将 `APIClient` 与当前 `AppSession` token 关联。
|
||
private func bindAuthTokenProvider() {
|
||
apiClient.bindAuthTokenProvider { [unowned self] in
|
||
appSession.token
|
||
}
|
||
}
|
||
}
|