diff --git a/ElementX/Sources/Screens/Onboarding/IdentityConfirmationScreen/IdentityConfirmationScreenViewModel.swift b/ElementX/Sources/Screens/Onboarding/IdentityConfirmationScreen/IdentityConfirmationScreenViewModel.swift index 1ab5c57614..a903f6708d 100644 --- a/ElementX/Sources/Screens/Onboarding/IdentityConfirmationScreen/IdentityConfirmationScreenViewModel.swift +++ b/ElementX/Sources/Screens/Onboarding/IdentityConfirmationScreen/IdentityConfirmationScreenViewModel.swift @@ -25,18 +25,13 @@ class IdentityConfirmationScreenViewModel: IdentityConfirmationScreenViewModelTy super.init(initialViewState: IdentityConfirmationScreenViewState(learnMoreURL: appSettings.encryptionURL)) - userSession.sessionSecurityStatePublisher - .receive(on: DispatchQueue.main) - .sink { [weak self] state in - Task { - await self?.updateWithSessionSecurityState(state) - } + Task { [weak self] in + for await state in userSession.sessionSecurityStatePublisher.values { + // We need to call this inside an AsyncSequence otherwise there's a race condition when the method suspends. + await self?.updateWithSessionSecurityState(state) } - .store(in: &cancellables) - - Task { - await updateWithSessionSecurityState(userSession.sessionSecurityStatePublisher.value) } + .store(in: &cancellables) } // MARK: - Public diff --git a/ElementX/Sources/Screens/Onboarding/IdentityConfirmationScreen/View/IdentityConfirmationScreen.swift b/ElementX/Sources/Screens/Onboarding/IdentityConfirmationScreen/View/IdentityConfirmationScreen.swift index a183adcbb1..002dfef31a 100644 --- a/ElementX/Sources/Screens/Onboarding/IdentityConfirmationScreen/View/IdentityConfirmationScreen.swift +++ b/ElementX/Sources/Screens/Onboarding/IdentityConfirmationScreen/View/IdentityConfirmationScreen.swift @@ -104,6 +104,8 @@ struct IdentityConfirmationScreen: View { // MARK: - Previews struct IdentityConfirmationScreen_Previews: PreviewProvider, TestablePreview { + static var viewModel = makeViewModel() + static var previews: some View { NavigationStack { IdentityConfirmationScreen(context: viewModel.context) @@ -113,7 +115,7 @@ struct IdentityConfirmationScreen_Previews: PreviewProvider, TestablePreview { }) } - private static var viewModel: IdentityConfirmationScreenViewModel { + static func makeViewModel() -> IdentityConfirmationScreenViewModel { let clientProxy = ClientProxyMock(.init()) let userSession = UserSessionMock(.init(clientProxy: clientProxy)) userSession.sessionSecurityStatePublisher = CurrentValuePublisher(.init(verificationState: .unverified, recoveryState: .enabled))