Skip to content

Commit fe45f0e

Browse files
committed
Fix an issue with infinite spinner whem loading dependencies
1 parent 79060f6 commit fe45f0e

File tree

1 file changed

+21
-125
lines changed

1 file changed

+21
-125
lines changed

WordPress/Classes/ViewRelated/NewGutenberg/NewGutenbergViewController.swift

Lines changed: 21 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,6 @@ import WebKit
1010
import CocoaLumberjackSwift
1111

1212
class NewGutenbergViewController: UIViewController, PostEditor, PublishingEditor {
13-
14-
enum EditorLoadingState {
15-
/// We haven't done anything with the editor yet
16-
///
17-
/// Valid states to transition to:
18-
/// - .loadingDependencies
19-
case uninitialized
20-
21-
/// We're loading the editor's dependencies
22-
///
23-
/// Valid states to transition to:
24-
/// - .loadingCancelled
25-
/// - .dependencyError
26-
/// - .dependenciesReady
27-
case loadingDependencies(_ task: Task<Void, Error>)
28-
29-
/// We cancelled loading the editor's dependencies
30-
///
31-
/// Valid states to transition to:
32-
/// - .loadingDependencies
33-
case loadingCancelled
34-
35-
/// An error occured while fetching dependencies
36-
///
37-
/// Valid states to transition to:
38-
/// - .loadingDependencies
39-
case dependencyError(Error)
40-
41-
/// All dependencies have been loaded, so we're ready to start the editor
42-
///
43-
/// Valid states to transition to:
44-
/// - .ready
45-
case dependenciesReady(EditorDependencies)
46-
47-
/// The editor is fully loaded and we've passed all required configuration and data to it
48-
///
49-
/// There are no valid transition states from `.started`
50-
case started
51-
}
52-
5313
struct EditorDependencies {
5414
let settings: String?
5515
let didLoadCookies: Bool
@@ -125,9 +85,7 @@ class NewGutenbergViewController: UIViewController, PostEditor, PublishingEditor
12585
private var suggestionViewBottomConstraint: NSLayoutConstraint?
12686
private var currentSuggestionsController: GutenbergSuggestionsViewController?
12787

128-
private var editorState: EditorLoadingState = .uninitialized
129-
private var dependencyLoadingError: Error?
130-
private var editorLoadingTask: Task<Void, Error>?
88+
private var editorLoadingTask: Task<Void, Never>?
13189

13290
// TODO: remove (none of these APIs are needed for the new editor)
13391
func prepopulateMediaItems(_ media: [Media]) {}
@@ -212,8 +170,6 @@ class NewGutenbergViewController: UIViewController, PostEditor, PublishingEditor
212170
configureNavigationBar()
213171
refreshInterface()
214172

215-
startLoadingDependencies()
216-
217173
SiteSuggestionService.shared.prefetchSuggestionsIfNeeded(for: post.blog) {
218174
// Do nothing
219175
}
@@ -225,55 +181,17 @@ class NewGutenbergViewController: UIViewController, PostEditor, PublishingEditor
225181
// DDLogError("Error syncing JETPACK: \(String(describing: error))")
226182
// })
227183

228-
onViewDidLoad()
229-
}
230-
231-
override func viewWillAppear(_ animated: Bool) {
232-
super.viewWillAppear(animated)
233-
234-
if case .loadingDependencies = self.editorState {
235-
self.showActivityIndicator()
184+
editorLoadingTask = Task { @MainActor in
185+
await loadEditor()
236186
}
237187

238-
if case .loadingCancelled = self.editorState {
239-
startLoadingDependencies()
240-
}
241-
}
242-
243-
override func viewDidAppear(_ animated: Bool) {
244-
super.viewDidAppear(animated)
245-
246-
if case .loadingCancelled = self.editorState {
247-
preconditionFailure("Dependency loading should not be cancelled")
248-
}
249-
250-
self.editorLoadingTask = Task {
251-
do {
252-
while case .loadingDependencies = self.editorState {
253-
try await Task.sleep(nanoseconds: 1000)
254-
}
255-
256-
switch self.editorState {
257-
case .uninitialized: preconditionFailure("Dependencies must be initialized")
258-
case .loadingDependencies: preconditionFailure("Dependencies should not still be loading")
259-
case .loadingCancelled: preconditionFailure("Dependency loading should not be cancelled")
260-
case .dependencyError(let error): self.showEditorError(error)
261-
case .dependenciesReady(let dependencies): try await self.startEditor(settings: dependencies.settings)
262-
case .started: preconditionFailure("The editor should not already be started")
263-
}
264-
} catch {
265-
self.showEditorError(error)
266-
}
267-
}
188+
onViewDidLoad()
268189
}
269190

270191
override func viewWillDisappear(_ animated: Bool) {
271192
super.viewWillDisappear(animated)
272193

273194
if isBeingDismissedDirectlyOrByAncestor() {
274-
if case .loadingDependencies(let task) = editorState {
275-
task.cancel()
276-
}
277195
editorLoadingTask?.cancel()
278196
}
279197
}
@@ -359,49 +277,27 @@ class NewGutenbergViewController: UIViewController, PostEditor, PublishingEditor
359277
}
360278
}
361279

362-
func startLoadingDependencies() {
363-
switch self.editorState {
364-
case .uninitialized:
365-
break // This is fine – we're loading for the first time
366-
case .loadingDependencies:
367-
preconditionFailure("`startLoadingDependencies` should not be called while in the `.loadingDependencies` state")
368-
case .loadingCancelled:
369-
break // This is fine – we're loading after quickly switching posts
370-
case .dependencyError:
371-
break // We're retrying after an error
372-
case .dependenciesReady:
373-
preconditionFailure("`startLoadingDependencies` should not be called while in the `.dependenciesReady` state")
374-
case .started:
375-
preconditionFailure("`startLoadingDependencies` should not be called while in the `.started` state")
376-
}
377-
378-
self.editorState = .loadingDependencies(Task {
379-
do {
380-
let dependencies = try await fetchEditorDependencies()
381-
self.editorState = .dependenciesReady(dependencies)
382-
} catch {
383-
self.editorState = .dependencyError(error)
384-
}
385-
})
386-
}
387-
388280
@MainActor
389-
func startEditor(settings: String?) async throws {
390-
guard case .dependenciesReady = self.editorState else {
391-
preconditionFailure("`startEditor` should only be called when the editor is in the `.dependenciesReady` state.")
392-
}
281+
private func loadEditor() async {
282+
showActivityIndicator()
393283

394-
let updatedConfiguration = self.editorViewController.configuration.toBuilder()
395-
.apply(settings) { $0.setEditorSettings($1) }
396-
.setTitle(post.postTitle ?? "")
397-
.setContent(post.content ?? "")
398-
.build()
284+
do {
285+
let dependencies = try await fetchEditorDependencies()
399286

400-
self.editorViewController.updateConfiguration(updatedConfiguration)
401-
self.editorViewController.startEditorSetup()
287+
let configuration = editorViewController.configuration.toBuilder()
288+
.apply(dependencies.settings) { $0.setEditorSettings($1) }
289+
.setTitle(post.postTitle ?? "")
290+
.setContent(post.content ?? "")
291+
.build()
402292

403-
// Handles refreshing controls with state context after options screen is dismissed
404-
editorContentWasUpdated()
293+
editorViewController.updateConfiguration(configuration)
294+
editorViewController.startEditorSetup()
295+
296+
// Handles refreshing controls with state context after options screen is dismissed
297+
editorContentWasUpdated()
298+
} catch {
299+
// TODO: handle errors
300+
}
405301
}
406302

407303
// MARK: - Keyboard Observers

0 commit comments

Comments
 (0)