From 9a0d97b3e0b23600cfe7bd8b07f71e1da3bebd34 Mon Sep 17 00:00:00 2001 From: Miltiadis Vasilakis Date: Wed, 8 May 2024 11:05:54 +0300 Subject: [PATCH] Improve note editor activity title update --- Zotero/Scenes/Detail/DetailCoordinator.swift | 1 - Zotero/Scenes/General/Models/NoteEditorState.swift | 9 +++++++++ .../General/ViewModels/NoteEditorActionHandler.swift | 11 +++++++++++ .../General/Views/NoteEditorViewController.swift | 7 +++++-- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Zotero/Scenes/Detail/DetailCoordinator.swift b/Zotero/Scenes/Detail/DetailCoordinator.swift index c459e655e..a56c20964 100644 --- a/Zotero/Scenes/Detail/DetailCoordinator.swift +++ b/Zotero/Scenes/Detail/DetailCoordinator.swift @@ -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: diff --git a/Zotero/Scenes/General/Models/NoteEditorState.swift b/Zotero/Scenes/General/Models/NoteEditorState.swift index b4351c523..3bbb275f7 100644 --- a/Zotero/Scenes/General/Models/NoteEditorState.swift +++ b/Zotero/Scenes/General/Models/NoteEditorState.swift @@ -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 { @@ -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 @@ -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() { diff --git a/Zotero/Scenes/General/ViewModels/NoteEditorActionHandler.swift b/Zotero/Scenes/General/ViewModels/NoteEditorActionHandler.swift index 592941d52..a02b885e1 100644 --- a/Zotero/Scenes/General/ViewModels/NoteEditorActionHandler.swift +++ b/Zotero/Scenes/General/ViewModels/NoteEditorActionHandler.swift @@ -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)") @@ -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] + } + } } } } diff --git a/Zotero/Scenes/General/Views/NoteEditorViewController.swift b/Zotero/Scenes/General/Views/NoteEditorViewController.swift index 5da6ad7a4..faea483cf 100644 --- a/Zotero/Scenes/General/Views/NoteEditorViewController.swift +++ b/Zotero/Scenes/General/Views/NoteEditorViewController.swift @@ -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) @@ -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) } } ) @@ -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