Replace the ServiceLocator with direct dependency injection (#5548)

Removes the ServiceLocator singleton and instead directly injects the dependencies it was holding for more control over instantiation of the various component subtrees and better visibility over how they're being used.
This commit is contained in:
Michael
2026-05-14 01:45:49 -05:00
committed by GitHub
parent f70182d09a
commit ff5dcb040d
108 changed files with 971 additions and 729 deletions
@@ -162,10 +162,11 @@ private extension AppAppearance {
struct AdvancedSettingsScreen_Previews: PreviewProvider, TestablePreview {
static let viewModel = {
AppSettings.resetAllSettings()
return AdvancedSettingsScreenViewModel(advancedSettings: AppSettings(),
analytics: ServiceLocator.shared.analytics,
let appSettings = AppSettings()
return AdvancedSettingsScreenViewModel(advancedSettings: appSettings,
analytics: .mock(settings: appSettings),
clientProxy: ClientProxyMock(.init()),
userIndicatorController: UserIndicatorControllerMock())
userIndicatorController: UserIndicatorControllerMock.default)
}()
static var previews: some View {
@@ -40,8 +40,9 @@ struct AnalyticsSettingsScreen: View {
struct AnalyticsSettingsScreen_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
let appSettings = AppSettings()
let analytics = AnalyticsService.mock(settings: appSettings)
let viewModel = AnalyticsSettingsScreenViewModel(appSettings: appSettings,
analytics: ServiceLocator.shared.analytics)
analytics: analytics)
AnalyticsSettingsScreen(context: viewModel.context)
}
}
@@ -244,10 +244,11 @@ private extension Set<TraceLogPack> {
// MARK: - Previews
struct DeveloperOptionsScreen_Previews: PreviewProvider {
static let viewModel = DeveloperOptionsScreenViewModel(developerOptions: ServiceLocator.shared.settings,
elementCallBaseURL: ServiceLocator.shared.settings.elementCallBaseURL,
static let viewModel = DeveloperOptionsScreenViewModel(developerOptions: AppSettings(),
elementCallBaseURL: AppSettings().elementCallBaseURL,
appHooks: AppHooks(),
clientProxy: ClientProxyMock(.init()))
static var previews: some View {
ElementNavigationStack {
DeveloperOptionsScreen(context: viewModel.context)
@@ -277,7 +277,7 @@ struct SettingsScreen_Previews: PreviewProvider, TestablePreview {
let userSession = UserSessionMock(.init(clientProxy: ClientProxyMock(.init(userID: "@userid:example.com",
deviceID: "AAAAAAAAAAA"))))
return SettingsScreenViewModel(userSession: userSession,
appSettings: ServiceLocator.shared.settings,
appSettings: AppSettings(),
isBugReportServiceEnabled: isBugReportServiceEnabled,
isInSecondaryWindow: false)
}
@@ -141,7 +141,7 @@ struct UserDetailsEditScreen_Previews: PreviewProvider, TestablePreview {
UserDetailsEditScreenViewModel(userSession: UserSessionMock(.init(clientProxy: ClientProxyMock(.init(userID: "@stefan:matrix.org",
canChangeAvatar: canChangeProfile,
canChangeDisplayName: canChangeProfile)))),
mediaUploadingPreprocessor: .init(appSettings: ServiceLocator.shared.settings),
mediaUploadingPreprocessor: .init(appSettings: AppSettings()),
userIndicatorController: UserIndicatorControllerMock.default)
}
}