@@ -139,15 +139,41 @@ private extension JetpackWindowManager {
139139 !hasFailedExportAttempts,
140140 let schemeUrl = URL ( string: " \( AppScheme . wordpressMigrationV1. rawValue) \( WordPressExportRoute ( ) . path. removingPrefix ( " / " ) ) " )
141141 else {
142- if rootViewController == nil {
143- showSignInUI ( )
142+ performSafeRootNavigation { [ weak self ] in
143+ self ? . showSignInUI ( )
144144 }
145145 return
146146 }
147147
148148 /// WordPress is a compatible version for migrations, but needs to be loaded to prepare the data
149- if rootViewController == nil {
150- showLoadWordPressUI ( schemeUrl: schemeUrl)
149+ performSafeRootNavigation { [ weak self] in
150+ self ? . showLoadWordPressUI ( schemeUrl: schemeUrl)
151+ }
152+ }
153+
154+ /// This method takes care of preventing screens being abruptly replaced.
155+ ///
156+ /// Since the import method is called whenever the app is foregrounded, we want to make sure that
157+ /// any root view controller replacements only happen where it is "allowed":
158+ ///
159+ /// 1. When there's no root view controller yet, or
160+ /// 2. When the Load WordPress screen is shown.
161+ ///
162+ /// Note: We should remove this method when the migration phase is concluded and we no longer need
163+ /// to perfom the migration.
164+ ///
165+ /// - Parameter navigationClosure: The closure containing logic that eventually calls the `show` method.
166+ func performSafeRootNavigation( with navigationClosure: @escaping ( ) -> Void ) {
167+ switch rootViewController {
168+ case . none:
169+ // we can perform the navigation directly when there's no root view controller yet.
170+ navigationClosure ( )
171+ case . some( let viewController) where viewController is MigrationLoadWordPressViewController :
172+ // allow the Load WordPress view to be replaced in case the migration process fails.
173+ viewController. dismiss ( animated: true , completion: navigationClosure)
174+ default :
175+ // do nothing when another root view controller is already displayed.
176+ break
151177 }
152178 }
153179}
0 commit comments