Skip to content

Commit

Permalink
iOS 17 state restoration crash fix (#767)
Browse files Browse the repository at this point in the history
  • Loading branch information
michalrentka authored Sep 20, 2023
1 parent 27853fa commit d878e2a
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 432 deletions.
12 changes: 4 additions & 8 deletions Zotero.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@
B305CEBB29E6E67600B9E2B4 /* AssignItemsToTagDbRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B305CEBA29E6E67600B9E2B4 /* AssignItemsToTagDbRequest.swift */; };
B307A2732704A87D005986B3 /* IdleTimerController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B307A2722704A87D005986B3 /* IdleTimerController.swift */; };
B30A44A629B8799600332B4E /* MasterContainerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B30A44A529B8799600332B4E /* MasterContainerViewController.swift */; };
B30A44A829B882EB00332B4E /* MasterCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = B30A44A729B882EB00332B4E /* MasterCoordinator.swift */; };
B30A44AE29B88E7200332B4E /* TagFilterViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B30A44AD29B88E7200332B4E /* TagFilterViewController.swift */; };
B30A8C672582690900EC56FB /* HighlightAnnotation.swift in Sources */ = {isa = PBXBuildFile; fileRef = B30A8C662582690900EC56FB /* HighlightAnnotation.swift */; };
B30B405F2490CAFC00FAAF6D /* ItemCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = B30B405E2490CAFC00FAAF6D /* ItemCell.xib */; };
Expand Down Expand Up @@ -641,7 +640,7 @@
B3593F56241A61C700760E20 /* CollectionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3593F11241A61C700760E20 /* CollectionCell.swift */; };
B3593F59241A61C700760E20 /* CollectionsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3593F14241A61C700760E20 /* CollectionsViewController.swift */; };
B3593F62241A62DD00760E20 /* DetailCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3593F61241A62DD00760E20 /* DetailCoordinator.swift */; };
B3593F64241A62E400760E20 /* MasterTopCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3593F63241A62E400760E20 /* MasterTopCoordinator.swift */; };
B3593F64241A62E400760E20 /* MasterCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3593F63241A62E400760E20 /* MasterCoordinator.swift */; };
B3593F77241A76E600760E20 /* CollectionEditActionHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3593F6F241A76E600760E20 /* CollectionEditActionHandler.swift */; };
B3593F78241A76E600760E20 /* CollectionEditError.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3593F71241A76E600760E20 /* CollectionEditError.swift */; };
B3593F79241A76E600760E20 /* CollectionEditAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3593F72241A76E600760E20 /* CollectionEditAction.swift */; };
Expand Down Expand Up @@ -1367,7 +1366,6 @@
B307A2722704A87D005986B3 /* IdleTimerController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IdleTimerController.swift; sourceTree = "<group>"; };
B307E55D22D4A87B00592B3C /* SyncActionsSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SyncActionsSpec.swift; sourceTree = "<group>"; };
B30A44A529B8799600332B4E /* MasterContainerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MasterContainerViewController.swift; sourceTree = "<group>"; };
B30A44A729B882EB00332B4E /* MasterCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MasterCoordinator.swift; sourceTree = "<group>"; };
B30A44AD29B88E7200332B4E /* TagFilterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TagFilterViewController.swift; sourceTree = "<group>"; };
B30A8C662582690900EC56FB /* HighlightAnnotation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HighlightAnnotation.swift; sourceTree = "<group>"; };
B30B405E2490CAFC00FAAF6D /* ItemCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ItemCell.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1619,7 +1617,7 @@
B3593F172668E6A700FA4BB2 /* StyleParserDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StyleParserDelegate.swift; sourceTree = "<group>"; };
B3593F1B2668E86100FA4BB2 /* SyncStylesDbRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SyncStylesDbRequest.swift; sourceTree = "<group>"; };
B3593F61241A62DD00760E20 /* DetailCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailCoordinator.swift; sourceTree = "<group>"; };
B3593F63241A62E400760E20 /* MasterTopCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MasterTopCoordinator.swift; sourceTree = "<group>"; };
B3593F63241A62E400760E20 /* MasterCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MasterCoordinator.swift; sourceTree = "<group>"; };
B3593F6F241A76E600760E20 /* CollectionEditActionHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionEditActionHandler.swift; sourceTree = "<group>"; };
B3593F71241A76E600760E20 /* CollectionEditError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionEditError.swift; sourceTree = "<group>"; };
B3593F72241A76E600760E20 /* CollectionEditAction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionEditAction.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3125,8 +3123,7 @@
B374911F2488D7040042FF85 /* Settings */,
B30A44A929B88E3200332B4E /* TagFiltering */,
B30A44A529B8799600332B4E /* MasterContainerViewController.swift */,
B30A44A729B882EB00332B4E /* MasterCoordinator.swift */,
B3593F63241A62E400760E20 /* MasterTopCoordinator.swift */,
B3593F63241A62E400760E20 /* MasterCoordinator.swift */,
);
path = Master;
sourceTree = "<group>";
Expand Down Expand Up @@ -4879,7 +4876,7 @@
B3343079282A563B00FB41BC /* NSUserActivity+Activities.swift in Sources */,
B305661C23FC051E003304F2 /* LoadUploadDataSyncAction.swift in Sources */,
B31EEE5A24EBD18200E3B3AD /* ReadAnnotationsDbRequest.swift in Sources */,
B3593F64241A62E400760E20 /* MasterTopCoordinator.swift in Sources */,
B3593F64241A62E400760E20 /* MasterCoordinator.swift in Sources */,
B3597D0227DA5C280069F019 /* AnnotationSplitter.swift in Sources */,
B3422F48289BB65900C53DD2 /* ItemDetailFieldMultilineEditContentView.swift in Sources */,
B3E8FE182714297200F51458 /* CiteState.swift in Sources */,
Expand Down Expand Up @@ -5121,7 +5118,6 @@
B35FC1F02628490C00858772 /* MarkFileAsDownloadedDbRequest.swift in Sources */,
B3593F7C241A76E600760E20 /* CollectionEditView.swift in Sources */,
B3981B76258399AA00F8D15A /* NoteAnnotation.swift in Sources */,
B30A44A829B882EB00332B4E /* MasterCoordinator.swift in Sources */,
B30565E223FC051E003304F2 /* StoreCollectionsDbRequest.swift in Sources */,
B30565FA23FC051E003304F2 /* KeyGenerator.swift in Sources */,
B30566B923FC051F003304F2 /* SchemaResponse.swift in Sources */,
Expand Down
25 changes: 15 additions & 10 deletions Zotero/Scenes/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ final class AppCoordinator: NSObject {
}

DDLogInfo("AppCoordinator: show main screen logged \(isLogged ? "in" : "out"); animated=\(animated)")
self.show(viewController: viewController, in: window, animated: animated)
process(urlContext: urlContext, data: data)
show(viewController: viewController, in: window, animated: animated) { [weak self] in
self?.process(urlContext: urlContext, data: data)
}
}

private func preprocess(connectionOptions: UIScene.ConnectionOptions?, session: UISceneSession?) -> (UIOpenURLContext?, RestoredStateData?) {
Expand Down Expand Up @@ -162,7 +163,8 @@ final class AppCoordinator: NSObject {
self.showItemDetail(key: key, library: library, selectChildKey: preselectedChildKey, animated: animated)

case .pdfReader(let attachment, let library, let page, let annotation, let parentKey, let isAvailable):
DDLogInfo("AppCoordinator: show custom url - pdf reader; key=\(attachment.key); library=\(library.identifier); page=\(page.flatMap(String.init) ?? "nil"); annotation=\(annotation ?? "nil"); parentKey=\(parentKey ?? "nil")")
DDLogInfo("AppCoordinator: show custom url - pdf reader; key=\(attachment.key); library=\(library.identifier); page=\(page.flatMap(String.init) ?? "nil");" +
" annotation=\(annotation ?? "nil"); parentKey=\(parentKey ?? "nil")")
if isAvailable {
self.open(attachment: attachment, library: library, on: page, annotation: annotation, parentKey: parentKey, animated: animated)
return
Expand Down Expand Up @@ -194,9 +196,9 @@ final class AppCoordinator: NSObject {
guard let mainController = self.window?.rootViewController as? MainViewController else { return }

// Show "All" collection in given library/group
if mainController.masterCoordinator?.topCoordinator.visibleLibraryId != library.identifier ||
(mainController.masterCoordinator?.topCoordinator.navigationController?.visibleViewController as? CollectionsViewController)?.selectedIdentifier != .custom(.all) {
mainController.masterCoordinator?.topCoordinator.showCollections(for: library.identifier, preselectedCollection: .custom(.all), animated: animated)
if mainController.masterCoordinator?.visibleLibraryId != library.identifier ||
(mainController.masterCoordinator?.navigationController?.visibleViewController as? CollectionsViewController)?.selectedIdentifier != .custom(.all) {
mainController.masterCoordinator?.showCollections(for: library.identifier, preselectedCollection: .custom(.all), animated: animated)
}

// Show item detail of given key
Expand Down Expand Up @@ -383,12 +385,15 @@ final class AppCoordinator: NSObject {
self.window?.rootViewController?.present(controller, animated: true, completion: nil)
}

private func show(viewController: UIViewController?, in window: UIWindow, animated: Bool = false) {
private func show(viewController: UIViewController?, in window: UIWindow, animated: Bool = false, completion: @escaping () -> Void) {
window.rootViewController = viewController

guard animated else { return }
guard animated else {
completion()
return
}

UIView.transition(with: window, duration: 0.2, options: .transitionCrossDissolve, animations: {}, completion: { _ in })
UIView.transition(with: window, duration: 0.2, options: .transitionCrossDissolve, animations: {}, completion: { _ in completion() })
}

private func presentActivityViewController(with items: [Any], completed: @escaping () -> Void) {
Expand Down Expand Up @@ -449,7 +454,7 @@ final class AppCoordinator: NSObject {

extension AppCoordinator: AppDelegateCoordinatorDelegate {
func showMainScreen(isLoggedIn: Bool) {
self.showMainScreen(isLogged: isLoggedIn, options: self.tmpConnectionOptions, session: self.tmpSession, animated: true)
self.showMainScreen(isLogged: isLoggedIn, options: self.tmpConnectionOptions, session: self.tmpSession, animated: false)
self.tmpConnectionOptions = nil
self.tmpSession = nil
}
Expand Down
28 changes: 18 additions & 10 deletions Zotero/Scenes/Main/Views/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,16 @@ final class MainViewController: UISplitViewController {

private func showItems(for collection: Collection, in library: Library, searchItemKeys: [String]?) {
let navigationController = UINavigationController()
let tagFilterController = (self.viewControllers.first as? MasterContainerViewController)?.bottomController as? TagFilterViewController

let coordinator = DetailCoordinator(library: library, collection: collection, searchItemKeys: searchItemKeys, navigationController: navigationController, itemsTagFilterDelegate: tagFilterController, controllers: self.controllers)
let tagFilterController = (self.viewControllers.first as? MasterContainerViewController)?.bottomController as? ItemsTagFilterDelegate

let coordinator = DetailCoordinator(
library: library,
collection: collection,
searchItemKeys: searchItemKeys,
navigationController: navigationController,
itemsTagFilterDelegate: tagFilterController,
controllers: self.controllers
)
coordinator.start(animated: false)
self.detailCoordinator = coordinator

Expand All @@ -105,14 +112,15 @@ final class MainViewController: UISplitViewController {
// MARK: - Setups

private func setupControllers() {
let masterCoordinator = MasterCoordinator(mainController: self, controllers: self.controllers)
masterCoordinator.start()
let masterController = MasterContainerViewController()
let masterCoordinator = MasterCoordinator(navigationController: masterController, mainCoordinatorDelegate: self, controllers: self.controllers)
masterController.coordinatorDelegate = masterCoordinator
masterCoordinator.start(animated: false)
self.viewControllers = [masterController]
self.masterCoordinator = masterCoordinator

if let progressObservable = self.controllers.userControllers?.syncScheduler.syncController.progressObservable,
let dbStorage = self.controllers.userControllers?.dbStorage,
let navigationController = masterCoordinator.topCoordinator.navigationController {
self.syncToolbarController = SyncToolbarController(parent: navigationController, progressObservable: progressObservable, dbStorage: dbStorage)
if let progressObservable = self.controllers.userControllers?.syncScheduler.syncController.progressObservable, let dbStorage = self.controllers.userControllers?.dbStorage {
self.syncToolbarController = SyncToolbarController(parent: masterController, progressObservable: progressObservable, dbStorage: dbStorage)
self.syncToolbarController?.coordinatorDelegate = self
}
}
Expand Down Expand Up @@ -148,7 +156,7 @@ extension MainViewController: MainCoordinatorSyncToolbarDelegate {

guard let library = library, let collectionType = collectionType else { return }

self.masterCoordinator?.topCoordinator.showCollections(for: libraryId, preselectedCollection: .custom(collectionType), animated: true)
self.masterCoordinator?.showCollections(for: libraryId, preselectedCollection: .custom(collectionType), animated: true)
self.showItems(for: Collection(custom: collectionType), in: library, searchItemKeys: keys)
} catch let error {
DDLogError("MainViewController: can't load searched keys - \(error)")
Expand Down
Loading

0 comments on commit d878e2a

Please sign in to comment.