From 428478ee5b8430183a285e4f0f2d44c04c2dda25 Mon Sep 17 00:00:00 2001 From: Gordon Brander Date: Wed, 20 Dec 2023 20:56:12 -0500 Subject: [PATCH] Block editor: save when editor is backgrounded or un-presented (#1044) Fixes #1041 ## Testing Setup: - Go to Settings > Developer > Block Editor, and turn on block editor. Scenario 1: - Create a new note or edit an existing note - Type some text - Swipe back What should happen: note should be saved. Scenario 2: - Create a new note or edit an existing note - Type some text - Background the app What should happen: note should be saved. --- .../Components/Detail/MemoEditorDetail.swift | 9 ++++++++- .../UIKit/BlockEditor/BlockEditorModel.swift | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/xcode/Subconscious/Shared/Components/Detail/MemoEditorDetail.swift b/xcode/Subconscious/Shared/Components/Detail/MemoEditorDetail.swift index 4c8c524b..cfa94710 100644 --- a/xcode/Subconscious/Shared/Components/Detail/MemoEditorDetail.swift +++ b/xcode/Subconscious/Shared/Components/Detail/MemoEditorDetail.swift @@ -106,7 +106,8 @@ struct MemoEditorDetailView: View { // See https://developer.apple.com/documentation/swiftui/scenephase // 2022-02-08 Gordon Brander .onChange(of: self.scenePhase) { phase in - store.send(MemoEditorDetailAction.scenePhaseChange(phase)) + store.send(.scenePhaseChange(phase)) + blockEditorStore.send(.scenePhaseChange(phase)) } // Save when back button pressed. // Note that .onDisappear is too late, because by the time the save @@ -117,6 +118,7 @@ struct MemoEditorDetailView: View { .onChange(of: self.isPresented) { isPresented in if !isPresented { store.send(.autosave) + blockEditorStore.send(.autosave) } } /// Catch link taps and handle them here @@ -311,6 +313,11 @@ extension MemoEditorDetailNotification { extension MemoEditorDetailNotification { static func from(_ action: BlockEditor.Action) -> Self? { switch action { + case let .succeedSave(entry): + return .succeedSaveEntry( + address: entry.address, + modified: entry.contents.modified + ) case let .requestFindLinkDetail(link): return .requestFindLinkDetail(link) default: diff --git a/xcode/Subconscious/Shared/UIKit/BlockEditor/BlockEditorModel.swift b/xcode/Subconscious/Shared/UIKit/BlockEditor/BlockEditorModel.swift index 3ce486f3..86f7883f 100644 --- a/xcode/Subconscious/Shared/UIKit/BlockEditor/BlockEditorModel.swift +++ b/xcode/Subconscious/Shared/UIKit/BlockEditor/BlockEditorModel.swift @@ -79,6 +79,8 @@ extension BlockEditor { extension BlockEditor { // MARK: Actions enum Action { + /// Handle app backgrounding, etec + case scenePhaseChange(ScenePhase) /// View is ready for updates. /// Sent during viewDidLoad after performing first view update for /// initial state and subscribing to changes. @@ -242,6 +244,11 @@ extension BlockEditor.Model: ModelProtocol { environment: Environment ) -> Update { switch action { + case .scenePhaseChange: + return scenePhaseChange( + state: state, + environment: environment + ) case .ready: return ready( state: state, @@ -509,6 +516,17 @@ extension BlockEditor.Model: ModelProtocol { ) } } + + static func scenePhaseChange( + state: Self, + environment: Environment + ) -> Update { + return update( + state: state, + action: .autosave, + environment: environment + ) + } static func ready( state: Self,