From 3a1f3b2caaae454813c682c2d2d061fc53522d37 Mon Sep 17 00:00:00 2001 From: Miltiadis Vasilakis Date: Fri, 26 Apr 2024 11:33:07 +0300 Subject: [PATCH] Properly close Note Editor when switching current open item --- .../Views/NoteEditorViewController.swift | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/Zotero/Scenes/General/Views/NoteEditorViewController.swift b/Zotero/Scenes/General/Views/NoteEditorViewController.swift index ad7d4d163..3f38795b3 100644 --- a/Zotero/Scenes/General/Views/NoteEditorViewController.swift +++ b/Zotero/Scenes/General/Views/NoteEditorViewController.swift @@ -93,9 +93,10 @@ final class NoteEditorViewController: UIViewController { case .done: let done = UIBarButtonItem(title: L10n.done, style: .done, target: nil, action: nil) done.rx.tap - .subscribe(with: self, onNext: { `self`, _ in + .subscribe(onNext: { [weak self] _ in + guard let self else { return } forceSaveIfNeeded() - self.navigationController?.presentingViewController?.dismiss(animated: true, completion: nil) + navigationController?.presentingViewController?.dismiss(animated: true, completion: nil) }) .disposed(by: disposeBag) item = done @@ -110,8 +111,12 @@ final class NoteEditorViewController: UIViewController { self?.coordinatorDelegate }, completion: { [weak self] changedCurrentItem, openItemsChanged in - guard let self, !changedCurrentItem && openItemsChanged else { return } - openItemsController.setOpenItemsUserActivity(from: self, libraryId: viewModel.state.library.identifier) + guard let self else { return } + if changedCurrentItem { + forceSaveIfNeeded() + } else if openItemsChanged { + openItemsController.setOpenItemsUserActivity(from: self, libraryId: viewModel.state.library.identifier) + } } ) let openItemsMenu = UIMenu(title: L10n.Accessibility.Pdf.openItems, options: [.displayInline], children: [deferredOpenItemsMenuElement]) @@ -128,12 +133,6 @@ final class NoteEditorViewController: UIViewController { guard let item = navigationItem.rightBarButtonItems?.first(where: { button in RightBarButtonItem(rawValue: button.tag) == .restoreOpenItems }) else { return } item.image = .openItemsImage(count: count) } - - func forceSaveIfNeeded() { - guard debounceDisposeBag != nil else { return } - debounceDisposeBag = nil - viewModel.process(action: .save) - } } func setupWebView() { @@ -215,6 +214,12 @@ final class NoteEditorViewController: UIViewController { self?.viewModel.process(action: .setTags(tags)) }) } + + private func forceSaveIfNeeded() { + guard debounceDisposeBag != nil else { return } + debounceDisposeBag = nil + viewModel.process(action: .save) + } } extension NoteEditorViewController: WKNavigationDelegate {