From 205056f63609d177e89b6982a78e8990485f1d94 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 22 Apr 2023 22:18:42 -0400 Subject: [PATCH] Fix draft being deleted too early causing empty UI during dismiss compose animation --- .../ComposeUI/Sources/ComposeUI/API/PostService.swift | 3 --- .../ComposeUI/Controllers/ComposeController.swift | 9 ++++++--- Tusker/Scenes/ComposeSceneDelegate.swift | 3 ++- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Packages/ComposeUI/Sources/ComposeUI/API/PostService.swift b/Packages/ComposeUI/Sources/ComposeUI/API/PostService.swift index 46025789..e89f9216 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/API/PostService.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/API/PostService.swift @@ -57,9 +57,6 @@ class PostService: ObservableObject { do { let (_, _) = try await mastodonController.run(request) currentStep += 1 - - DraftsPersistentContainer.shared.viewContext.delete(self.draft) - DraftsPersistentContainer.shared.save() } catch let error as Client.Error { throw Error.posting(error) } diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift index 5bb9af5e..d230f570 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift @@ -45,6 +45,7 @@ public final class ComposeController: ViewController { @Published var isShowingDraftsList = false @Published var poster: PostService? @Published var postError: (any Error)? + @Published public private(set) var didPostSuccessfully = false var isPosting: Bool { poster != nil @@ -172,13 +173,15 @@ public final class ComposeController: ViewController { do { try await poster.post() + didPostSuccessfully = true + // wait .25 seconds so the user can see the progress bar has completed try? await Task.sleep(nanoseconds: 250_000_000) - config.dismiss(.post) - // don't unset the poster, so the ui remains disabled while dismissing + config.dismiss(.post) + } catch let error as PostService.Error { self.postError = error self.poster = nil @@ -202,7 +205,7 @@ public final class ComposeController: ViewController { } func onDisappear() { - if !draft.hasContent { + if !draft.hasContent || didPostSuccessfully { DraftsPersistentContainer.shared.viewContext.delete(draft) } DraftsPersistentContainer.shared.save() diff --git a/Tusker/Scenes/ComposeSceneDelegate.swift b/Tusker/Scenes/ComposeSceneDelegate.swift index 47ee01e4..517e7da7 100644 --- a/Tusker/Scenes/ComposeSceneDelegate.swift +++ b/Tusker/Scenes/ComposeSceneDelegate.swift @@ -81,7 +81,8 @@ class ComposeSceneDelegate: UIResponder, UIWindowSceneDelegate, TuskerSceneDeleg if let window = window, let nav = window.rootViewController as? UINavigationController, - let compose = nav.topViewController as? ComposeHostingController { + let compose = nav.topViewController as? ComposeHostingController, + !compose.controller.didPostSuccessfully { scene.userActivity = UserActivityManager.editDraftActivity(id: compose.controller.draft.id, accountID: scene.session.mastodonController!.accountInfo!.id) } }