@@ -197,15 +197,41 @@ private extension JetpackWindowManager {
197197 !hasFailedExportAttempts,
198198 let schemeUrl = URL ( string: " \( AppScheme . wordpressMigrationV1. rawValue) \( WordPressExportRoute ( ) . path. removingPrefix ( " / " ) ) " )
199199 else {
200- if rootViewController == nil {
201- showSignInUI ( )
200+ performSafeRootNavigation { [ weak self ] in
201+ self ? . showSignInUI ( )
202202 }
203203 return
204204 }
205205
206206 /// WordPress is a compatible version for migrations, but needs to be loaded to prepare the data
207- if rootViewController == nil {
208- showLoadWordPressUI ( schemeUrl: schemeUrl)
207+ performSafeRootNavigation { [ weak self] in
208+ self ? . showLoadWordPressUI ( schemeUrl: schemeUrl)
209+ }
210+ }
211+
212+ /// This method takes care of preventing screens being abruptly replaced.
213+ ///
214+ /// Since the import method is called whenever the app is foregrounded, we want to make sure that
215+ /// any root view controller replacements only happen where it is "allowed":
216+ ///
217+ /// 1. When there's no root view controller yet, or
218+ /// 2. When the Load WordPress screen is shown.
219+ ///
220+ /// Note: We should remove this method when the migration phase is concluded and we no longer need
221+ /// to perfom the migration.
222+ ///
223+ /// - Parameter navigationClosure: The closure containing logic that eventually calls the `show` method.
224+ func performSafeRootNavigation( with navigationClosure: @escaping ( ) -> Void ) {
225+ switch rootViewController {
226+ case . none:
227+ // we can perform the navigation directly when there's no root view controller yet.
228+ navigationClosure ( )
229+ case . some( let viewController) where viewController is MigrationLoadWordPressViewController :
230+ // allow the Load WordPress view to be replaced in case the migration process fails.
231+ viewController. dismiss ( animated: true , completion: navigationClosure)
232+ default :
233+ // do nothing when another root view controller is already displayed.
234+ break
209235 }
210236 }
211237}
0 commit comments