Skip to content

Commit

Permalink
Improve DetailCoordinator presented item replacement for multiple items
Browse files Browse the repository at this point in the history
  • Loading branch information
mvasilak committed Feb 22, 2024
1 parent af94d78 commit 1edc31b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Zotero/Controllers/Architecture/Coordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ enum SourceView {
protocol Coordinator: AnyObject {
var parentCoordinator: Coordinator? { get }
var childCoordinators: [Coordinator] { get set }
var navigationController: UINavigationController? { get }
var navigationController: UINavigationController? { get set }

func start(animated: Bool)
func childDidFinish(_ child: Coordinator)
Expand Down
24 changes: 17 additions & 7 deletions Zotero/Scenes/Detail/DetailCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ final class DetailCoordinator: Coordinator {
navigationController.present(controller, animated: true, completion: nil)
}

func createPDFController(key: String, parentKey: String?, library: Library, url: URL, page: Int? = nil, preselectedAnnotationKey: String? = nil) -> NavigationViewController {
let navigationController = NavigationViewController()
func createPDFController(key: String, parentKey: String?, library: Library, url: URL, page: Int? = nil, preselectedAnnotationKey: String? = nil) -> DetailNavigationViewController {
let navigationController = DetailNavigationViewController()
navigationController.modalPresentationStyle = .fullScreen

let coordinator = PDFCoordinator(
key: key,
parentKey: parentKey,
Expand All @@ -304,6 +304,7 @@ final class DetailCoordinator: Coordinator {
navigationController: navigationController,
controllers: controllers
)
navigationController.coordinator = coordinator
coordinator.parentCoordinator = self
childCoordinators.append(coordinator)
coordinator.start(animated: false)
Expand All @@ -315,10 +316,14 @@ final class DetailCoordinator: Coordinator {
guard let navigationController else { return }
controllers.userControllers?.openItemsController.open(.pdf(libraryId: library.identifier, key: key), for: sessionIdentifier)

let viewControllerProvider: () -> UIViewController = {
let viewControllerProvider: () -> DetailNavigationViewController = {
self.createPDFController(key: key, parentKey: parentKey, library: library, url: url)
}
if let presentedViewController = navigationController.presentedViewController {
if let presentedDetailNavigationController = presentedViewController as? DetailNavigationViewController {
presentedDetailNavigationController.replaceContents(with: viewControllerProvider(), animated: false)
return
}
guard let window = presentedViewController.view.window else { return }
show(viewControllerProvider: viewControllerProvider, by: navigationController, in: window, animated: false)
return
Expand Down Expand Up @@ -471,8 +476,8 @@ extension DetailCoordinator: DetailItemsCoordinatorDelegate {
tags: [Tag],
title: NoteEditorState.TitleData?,
saveCallback: @escaping NoteEditorSaveCallback
) -> NavigationViewController {
let navigationController = NavigationViewController()
) -> DetailNavigationViewController {
let navigationController = DetailNavigationViewController()
navigationController.modalPresentationStyle = .fullScreen
navigationController.isModalInPresentation = true

Expand All @@ -487,6 +492,7 @@ extension DetailCoordinator: DetailItemsCoordinatorDelegate {
sessionIdentifier: sessionIdentifier,
controllers: controllers
)
navigationController.coordinator = coordinator
coordinator.parentCoordinator = self
childCoordinators.append(coordinator)
coordinator.start(animated: false)
Expand Down Expand Up @@ -947,10 +953,14 @@ extension DetailCoordinator: DetailNoteEditorCoordinatorDelegate {
controllers.userControllers?.openItemsController.open(.note(libraryId: library.identifier, key: key), for: sessionIdentifier)
}

let viewControllerProvider: () -> UIViewController = {
let viewControllerProvider: () -> DetailNavigationViewController = {
self.createNoteController(library: library, kind: kind, text: text, tags: tags, title: title, saveCallback: amendedSaveCallback)
}
if let presentedViewController = navigationController.presentedViewController {
if let presentedDetailNavigationController = presentedViewController as? DetailNavigationViewController {
presentedDetailNavigationController.replaceContents(with: viewControllerProvider(), animated: false)
return
}
guard let window = presentedViewController.view.window else { return }
show(viewControllerProvider: viewControllerProvider, by: navigationController, in: window, animated: false)
return
Expand Down
26 changes: 24 additions & 2 deletions Zotero/Scenes/General/Views/NavigationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,32 @@ class NavigationViewController: UINavigationController {
var statusBarVisible: Bool = true

override var prefersStatusBarHidden: Bool {
return !self.statusBarVisible
return !statusBarVisible
}

deinit {
self.dismissHandler?()
dismissHandler?()
}
}

class DetailNavigationViewController: NavigationViewController {
weak var coordinator: Coordinator?
public func replaceContents(with replacement: DetailNavigationViewController, animated: Bool) {
// Set replacement properties to self.
// Swap coordinators and dismiss handlers, so that the original coordinator is properly deinitialized, along with the original view controllers.
// Swap also the navigation controller property of the two coordinators.
// Store original
let originalCoordinator = coordinator
let originalDismissHandler = dismissHandler
// Swap replacement to original
coordinator = replacement.coordinator
coordinator?.navigationController = self
dismissHandler = replacement.dismissHandler
statusBarVisible = replacement.statusBarVisible
setViewControllers(replacement.viewControllers, animated: animated)
// Swap original to replacement
replacement.coordinator = originalCoordinator
replacement.coordinator?.navigationController = replacement
replacement.dismissHandler = originalDismissHandler
}
}

0 comments on commit 1edc31b

Please sign in to comment.