Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Block editor: save when editor is backgrounded or un-presented (#1044)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gordonbrander authored Dec 21, 2023
1 parent 3edc461 commit 428478e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions xcode/Subconscious/Shared/UIKit/BlockEditor/BlockEditorModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 428478e

Please sign in to comment.