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
@@ -13,6 +13,10 @@ import Testing
struct UserProfileScreenViewModelTests {
@Test
func initialState() async throws {
let appSettings = AppSettings()
let analytics = AnalyticsService.mock(settings: appSettings)
let userIndicatorController = UserIndicatorControllerMock.default
let profile = UserProfileProxy(userID: "@alice:matrix.org", displayName: "Alice", avatarURL: .mockMXCAvatar)
let clientProxy = ClientProxyMock(.init())
clientProxy.profileForReturnValue = .success(profile)
@@ -20,9 +24,9 @@ struct UserProfileScreenViewModelTests {
let viewModel = UserProfileScreenViewModel(userID: profile.userID,
isPresentedModally: false,
userSession: UserSessionMock(.init(clientProxy: clientProxy)),
userIndicatorController: ServiceLocator.shared.userIndicatorController,
analytics: ServiceLocator.shared.analytics,
appSettings: ServiceLocator.shared.settings)
userIndicatorController: userIndicatorController,
analytics: analytics,
appSettings: appSettings)
let context = viewModel.context
let waitForMemberToLoad = deferFulfillment(context.observe(\.viewState.userProfile)) { $0 != nil }
@@ -35,6 +39,10 @@ struct UserProfileScreenViewModelTests {
@Test
func initialStateAccountOwner() async throws {
let appSettings = AppSettings()
let analytics = AnalyticsService.mock(settings: appSettings)
let userIndicatorController = UserIndicatorControllerMock.default
let profile = UserProfileProxy(userID: RoomMemberProxyMock.mockMe.userID, displayName: "Me", avatarURL: .mockMXCAvatar)
let clientProxy = ClientProxyMock(.init())
clientProxy.profileForReturnValue = .success(profile)
@@ -42,9 +50,9 @@ struct UserProfileScreenViewModelTests {
let viewModel = UserProfileScreenViewModel(userID: profile.userID,
isPresentedModally: false,
userSession: UserSessionMock(.init(clientProxy: clientProxy)),
userIndicatorController: ServiceLocator.shared.userIndicatorController,
analytics: ServiceLocator.shared.analytics,
appSettings: ServiceLocator.shared.settings)
userIndicatorController: userIndicatorController,
analytics: analytics,
appSettings: appSettings)
let context = viewModel.context
let waitForMemberToLoad = deferFulfillment(context.observe(\.viewState.userProfile)) { $0 != nil }
@@ -57,6 +65,10 @@ struct UserProfileScreenViewModelTests {
@Test
func startingDmWithUnknownUserFetchesIdentity() async throws {
let appSettings = AppSettings()
let analytics = AnalyticsService.mock(settings: appSettings)
let userIndicatorController = UserIndicatorControllerMock.default
let profile = UserProfileProxy.mockAlice
let clientProxy = ClientProxyMock(.init())
@@ -66,9 +78,9 @@ struct UserProfileScreenViewModelTests {
let viewModel = UserProfileScreenViewModel(userID: profile.userID,
isPresentedModally: false,
userSession: UserSessionMock(.init(clientProxy: clientProxy)),
userIndicatorController: ServiceLocator.shared.userIndicatorController,
analytics: ServiceLocator.shared.analytics,
appSettings: ServiceLocator.shared.settings)
userIndicatorController: userIndicatorController,
analytics: analytics,
appSettings: appSettings)
let context = viewModel.context