Skip to content

Commit

Permalink
Improve note editor activity title update
Browse files Browse the repository at this point in the history
  • Loading branch information
mvasilak committed May 8, 2024
1 parent 2101643 commit 9a0d97b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
1 change: 0 additions & 1 deletion Zotero/Scenes/Detail/DetailCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,6 @@ extension DetailCoordinator: DetailNoteEditorCoordinatorDelegate {
// If indeed a new note is created inform open items controller about it.
if isCreated, let self, let openItemsController = controllers.userControllers?.openItemsController {
openItemsController.open(.note(libraryId: library.identifier, key: note.key), for: sessionIdentifier)
openItemsController.setOpenItemsUserActivity(from: navigationController, libraryId: library.identifier, title: note.title)
}

case .failure:
Expand Down
9 changes: 9 additions & 0 deletions Zotero/Scenes/General/Models/NoteEditorState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct NoteEditorState: ViewModelState {
static let tags = Changes(rawValue: 1 << 0)
static let save = Changes(rawValue: 1 << 1)
static let openItems = Changes(rawValue: 1 << 2)
static let displayTitle = Changes(rawValue: 1 << 3)
}

struct TitleData {
Expand All @@ -55,6 +56,7 @@ struct NoteEditorState: ViewModelState {
var tags: [Tag]
var changes: Changes
var openItemsCount: Int
var displayTitle: String?

init(kind: Kind, library: Library, title: TitleData?, text: String, tags: [Tag], openItemsCount: Int) {
self.kind = kind
Expand All @@ -64,6 +66,13 @@ struct NoteEditorState: ViewModelState {
self.title = title
changes = []
self.openItemsCount = openItemsCount
displayTitle = generateDisplayTitle()
}

func generateDisplayTitle() -> String? {
let parts = [title?.title, NotePreviewGenerator.preview(from: text)].compactMap({ $0 })
guard !parts.isEmpty else { return nil }
return parts.joined(separator: " / ")
}

mutating func cleanup() {
Expand Down
11 changes: 11 additions & 0 deletions Zotero/Scenes/General/ViewModels/NoteEditorActionHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ struct NoteEditorActionHandler: ViewModelActionHandler, BackgroundDbProcessingAc
state.kind = .edit(key: note.key)
}
saveCallback(note.key, .success((note: note, isCreated: true)))
updateDisplayTitleIfNeeded()

case .failure(let error):
DDLogError("NoteEditorActionHandler: can't create item note: \(error)")
Expand All @@ -120,9 +121,19 @@ struct NoteEditorActionHandler: ViewModelActionHandler, BackgroundDbProcessingAc
saveCallback(key, .failure(error))
} else {
saveCallback(key, .success((note: note, isCreated: false)))
updateDisplayTitleIfNeeded()
}
}
}

func updateDisplayTitleIfNeeded() {
let displayTitle = viewModel.state.generateDisplayTitle()
guard displayTitle != viewModel.state.displayTitle else { return }
update(viewModel: viewModel) { state in
state.displayTitle = displayTitle
state.changes = [.displayTitle]
}
}
}
}
}
7 changes: 5 additions & 2 deletions Zotero/Scenes/General/Views/NoteEditorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ final class NoteEditorViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

openItemsController.setOpenItemsUserActivity(from: self, libraryId: viewModel.state.library.identifier, title: viewModel.state.title?.title)
openItemsController.setOpenItemsUserActivity(from: self, libraryId: viewModel.state.library.identifier, title: viewModel.state.displayTitle)

if let data = viewModel.state.title {
navigationItem.titleView = NoteEditorTitleView(type: data.type, title: data.title)
Expand Down Expand Up @@ -115,7 +115,7 @@ final class NoteEditorViewController: UIViewController {
if changedCurrentItem {
forceSaveIfNeeded()
} else if openItemsChanged {
openItemsController.setOpenItemsUserActivity(from: self, libraryId: viewModel.state.library.identifier, title: viewModel.state.title?.title)
openItemsController.setOpenItemsUserActivity(from: self, libraryId: viewModel.state.library.identifier, title: viewModel.state.displayTitle)
}
}
)
Expand Down Expand Up @@ -164,6 +164,9 @@ final class NoteEditorViewController: UIViewController {
if state.changes.contains(.openItems) {
setupNavbarItems(for: state)
}
if state.changes.contains(.displayTitle) {
openItemsController.setOpenItemsUserActivity(from: self, libraryId: state.library.identifier, title: state.displayTitle)
}

func debounceSave() {
debounceDisposeBag = nil
Expand Down

0 comments on commit 9a0d97b

Please sign in to comment.