Skip to content

Commit df729f5

Browse files
committed
Added the abilty to handle Row URL's
1 parent fceda39 commit df729f5

File tree

5 files changed

+109
-31
lines changed

5 files changed

+109
-31
lines changed

VinOutlineKit/Sources/VinOutlineKit/EntityID.swift

+9
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ public enum EntityID: CustomStringConvertible, Hashable, Equatable, Codable {
203203
}
204204
}
205205

206+
public var isRow: Bool {
207+
switch self {
208+
case .row(_, _, _):
209+
return true
210+
default:
211+
return false
212+
}
213+
}
214+
206215
private enum CodingKeys: String, CodingKey {
207216
case type
208217
case searchText

Zavala/Documents/DocumentsViewController.swift

+72-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import VinOutlineKit
1414
import VinUtility
1515

1616
protocol DocumentsDelegate: AnyObject {
17-
func documentSelectionDidChange(_: DocumentsViewController, documentContainers: [DocumentContainer], documents: [Document], isNew: Bool, isNavigationBranch: Bool, animated: Bool)
17+
func documentSelectionDidChange(_: DocumentsViewController, documentContainers: [DocumentContainer], documents: [Document], selectRow: EntityID?, isNew: Bool, isNavigationBranch: Bool, animated: Bool)
1818
func showGetInfo(_: DocumentsViewController, outline: Outline)
1919
func exportPDFDocs(_: DocumentsViewController, outlines: [Outline])
2020
func exportPDFLists(_: DocumentsViewController, outlines: [Outline])
@@ -170,16 +170,28 @@ class DocumentsViewController: UICollectionViewController, MainControllerIdentif
170170
}
171171
}
172172

173-
func selectDocument(_ document: Document?, isNew: Bool = false, isNavigationBranch: Bool = true, animated: Bool) {
173+
func selectDocument(_ document: Document?, selectRow: EntityID? = nil, isNew: Bool = false, isNavigationBranch: Bool = true, animated: Bool) {
174174
guard let documentContainers else { return }
175175

176176
collectionView.deselectAll()
177177

178178
if let document, let index = documents.firstIndex(of: document) {
179179
collectionView.selectItem(at: IndexPath(row: index, section: 0), animated: true, scrollPosition: .centeredVertically)
180-
delegate?.documentSelectionDidChange(self, documentContainers: documentContainers, documents: [document], isNew: isNew, isNavigationBranch: isNavigationBranch, animated: animated)
180+
delegate?.documentSelectionDidChange(self,
181+
documentContainers: documentContainers,
182+
documents: [document],
183+
selectRow: selectRow,
184+
isNew: isNew,
185+
isNavigationBranch: isNavigationBranch,
186+
animated: animated)
181187
} else {
182-
delegate?.documentSelectionDidChange(self, documentContainers: documentContainers, documents: [], isNew: isNew, isNavigationBranch: isNavigationBranch, animated: animated)
188+
delegate?.documentSelectionDidChange(self,
189+
documentContainers: documentContainers,
190+
documents: [],
191+
selectRow: selectRow,
192+
isNew: isNew,
193+
isNavigationBranch: isNavigationBranch,
194+
animated: animated)
183195
}
184196
}
185197

@@ -190,7 +202,13 @@ class DocumentsViewController: UICollectionViewController, MainControllerIdentif
190202
collectionView.selectItem(at: IndexPath(row: i, section: 0), animated: false, scrollPosition: [])
191203
}
192204

193-
delegate?.documentSelectionDidChange(self, documentContainers: documentContainers, documents: documents, isNew: false, isNavigationBranch: false, animated: true)
205+
delegate?.documentSelectionDidChange(self,
206+
documentContainers: documentContainers,
207+
documents: documents,
208+
selectRow: nil,
209+
isNew: false,
210+
isNavigationBranch: false,
211+
animated: true)
194212
}
195213

196214
func deleteCurrentDocuments() {
@@ -400,12 +418,24 @@ extension DocumentsViewController {
400418
guard let documentContainers else { return }
401419

402420
guard let selectedIndexPaths = collectionView.indexPathsForSelectedItems else {
403-
delegate?.documentSelectionDidChange(self, documentContainers: documentContainers, documents: [], isNew: false, isNavigationBranch: false, animated: true)
421+
delegate?.documentSelectionDidChange(self,
422+
documentContainers: documentContainers,
423+
documents: [],
424+
selectRow: nil,
425+
isNew: false,
426+
isNavigationBranch: false,
427+
animated: true)
404428
return
405429
}
406430

407431
let selectedDocuments = selectedIndexPaths.map { documents[$0.row] }
408-
delegate?.documentSelectionDidChange(self, documentContainers: documentContainers, documents: selectedDocuments, isNew: false, isNavigationBranch: true, animated: true)
432+
delegate?.documentSelectionDidChange(self,
433+
documentContainers: documentContainers,
434+
documents: selectedDocuments,
435+
selectRow: nil,
436+
isNew: false,
437+
isNavigationBranch: true,
438+
animated: true)
409439
}
410440

411441
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
@@ -422,12 +452,24 @@ extension DocumentsViewController {
422452
#endif
423453

424454
guard let selectedIndexPaths = collectionView.indexPathsForSelectedItems else {
425-
delegate?.documentSelectionDidChange(self, documentContainers: documentContainers, documents: [], isNew: false, isNavigationBranch: false, animated: true)
455+
delegate?.documentSelectionDidChange(self,
456+
documentContainers: documentContainers,
457+
documents: [],
458+
selectRow: nil,
459+
isNew: false,
460+
isNavigationBranch: false,
461+
animated: true)
426462
return
427463
}
428464

429465
let selectedDocuments = selectedIndexPaths.map { documents[$0.row] }
430-
delegate?.documentSelectionDidChange(self, documentContainers: documentContainers, documents: selectedDocuments, isNew: false, isNavigationBranch: true, animated: true)
466+
delegate?.documentSelectionDidChange(self,
467+
documentContainers: documentContainers,
468+
documents: selectedDocuments,
469+
selectRow: nil,
470+
isNew: false,
471+
isNavigationBranch: true,
472+
animated: true)
431473
}
432474

433475
private func createLayout() -> UICollectionViewLayout {
@@ -503,7 +545,13 @@ extension DocumentsViewController {
503545
guard animated else {
504546
self.documents = sortedDocuments
505547
self.collectionView.reloadData()
506-
self.delegate?.documentSelectionDidChange(self, documentContainers: documentContainers, documents: [], isNew: false, isNavigationBranch: isNavigationBranch, animated: true)
548+
self.delegate?.documentSelectionDidChange(self,
549+
documentContainers: documentContainers,
550+
documents: [],
551+
selectRow: nil,
552+
isNew: false,
553+
isNavigationBranch: isNavigationBranch,
554+
animated: true)
507555
return
508556
}
509557

@@ -536,7 +584,13 @@ extension DocumentsViewController {
536584
self.collectionView.selectItem(at: indexPath, animated: false, scrollPosition: [])
537585
self.collectionView.scrollToItem(at: indexPath, at: [], animated: true)
538586
} else {
539-
self.delegate?.documentSelectionDidChange(self, documentContainers: documentContainers, documents: [], isNew: false, isNavigationBranch: isNavigationBranch, animated: true)
587+
self.delegate?.documentSelectionDidChange(self,
588+
documentContainers: documentContainers,
589+
documents: [],
590+
selectRow: nil,
591+
isNew: false,
592+
isNavigationBranch: isNavigationBranch,
593+
animated: true)
540594
}
541595
}
542596

@@ -799,7 +853,13 @@ private extension DocumentsViewController {
799853
func delete() {
800854
let deselect = selectedDocuments.filter({ documents.contains($0) }).isEmpty
801855
if deselect, let documentContainers = self.documentContainers {
802-
self.delegate?.documentSelectionDidChange(self, documentContainers: documentContainers, documents: [], isNew: false, isNavigationBranch: true, animated: true)
856+
self.delegate?.documentSelectionDidChange(self,
857+
documentContainers: documentContainers,
858+
documents: [],
859+
selectRow: nil,
860+
isNew: false,
861+
isNavigationBranch: true,
862+
animated: true)
803863
}
804864
for document in documents {
805865
document.account?.deleteDocument(document)

Zavala/Editor/EditorViewController.swift

+10-4
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ class EditorViewController: UIViewController, DocumentsActivityItemsConfiguratio
875875
])
876876
}
877877

878-
func edit(_ newOutline: Outline?, isNew: Bool, searchText: String? = nil) {
878+
func edit(_ newOutline: Outline?, selectRow: EntityID? = nil, isNew: Bool, searchText: String? = nil) {
879879
guard outline != newOutline else { return }
880880
isOutlineNewFlag = isNew
881881

@@ -943,9 +943,15 @@ class EditorViewController: UIViewController, DocumentsActivityItemsConfiguratio
943943
}
944944

945945
updateUI()
946-
restoreScrollPosition()
947-
restoreOutlineCursorPosition()
948-
moveCursorToTitleOnNew()
946+
947+
if let selectRow {
948+
guard let index = outline.shadowTable?.first(where: { $0.entityID == selectRow })?.shadowTableIndex else { return }
949+
collectionView.selectItem(at: IndexPath(row: index, section: adjustedRowsSection), animated: false, scrollPosition: [.centeredVertically])
950+
} else {
951+
restoreScrollPosition()
952+
restoreOutlineCursorPosition()
953+
moveCursorToTitleOnNew()
954+
}
949955
}
950956

951957
func selectAllRows() {

Zavala/MainSplitViewController.swift

+14-11
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ class MainSplitViewController: UISplitViewController, MainCoordinator {
155155
}
156156

157157
func handle(_ userInfo: [AnyHashable: Any], isNavigationBranch: Bool) async {
158-
if let searchIdentifier = userInfo[CSSearchableItemActivityIdentifier] as? String, let documentID = EntityID(description: searchIdentifier) {
159-
await handleDocument(documentID, isNavigationBranch: isNavigationBranch)
158+
if let searchIdentifier = userInfo[CSSearchableItemActivityIdentifier] as? String, let entityID = EntityID(description: searchIdentifier) {
159+
await handleDocument(entityID, isNavigationBranch: isNavigationBranch)
160160
return
161161
}
162162

@@ -194,21 +194,23 @@ class MainSplitViewController: UISplitViewController, MainCoordinator {
194194
handleSelectDocument(document, isNavigationBranch: isNavigationBranch)
195195
}
196196

197-
func handleDocument(_ documentID: EntityID, isNavigationBranch: Bool) async {
198-
guard let account = AccountManager.shared.findAccount(accountID: documentID.accountID),
199-
let document = account.findDocument(documentID) else {
197+
func handleDocument(_ entityID: EntityID, isNavigationBranch: Bool) async {
198+
guard let account = AccountManager.shared.findAccount(accountID: entityID.accountID),
199+
let document = account.findDocument(entityID) else {
200200
presentError(title: .documentNotFoundTitle, message: .documentNotFoundMessage)
201201
return
202202
}
203203

204+
let selectRow = entityID.isRow ? entityID : nil
205+
204206
if let collectionsTags = selectedTags, document.hasAnyTag(collectionsTags) {
205-
self.handleSelectDocument(document, isNavigationBranch: isNavigationBranch)
207+
self.handleSelectDocument(document, selectRow: selectRow, isNavigationBranch: isNavigationBranch)
206208
} else if document.tagCount == 1, let tag = document.tags?.first {
207209
await collectionsViewController?.selectDocumentContainers([TagDocuments(account: account, tag: tag)], isNavigationBranch: true, animated: false)
208-
handleSelectDocument(document, isNavigationBranch: isNavigationBranch)
210+
handleSelectDocument(document, selectRow: selectRow, isNavigationBranch: isNavigationBranch)
209211
} else {
210212
await collectionsViewController?.selectDocumentContainers([AllDocuments(account: account)], isNavigationBranch: true, animated: false)
211-
handleSelectDocument(document, isNavigationBranch: isNavigationBranch)
213+
handleSelectDocument(document, selectRow: selectRow, isNavigationBranch: isNavigationBranch)
212214
}
213215
}
214216

@@ -486,6 +488,7 @@ extension MainSplitViewController: DocumentsDelegate {
486488
func documentSelectionDidChange(_: DocumentsViewController,
487489
documentContainers: [DocumentContainer],
488490
documents: [Document],
491+
selectRow: EntityID?,
489492
isNew: Bool,
490493
isNavigationBranch: Bool,
491494
animated: Bool) {
@@ -540,7 +543,7 @@ extension MainSplitViewController: DocumentsDelegate {
540543
pinWasVisited(Pin(containers: documentContainers, document: document))
541544
}
542545
} else {
543-
editorViewController?.edit(document.outline, isNew: isNew)
546+
editorViewController?.edit(document.outline, selectRow: selectRow, isNew: isNew)
544547
pinWasVisited(Pin(containers: documentContainers, document: document))
545548
}
546549
}
@@ -744,8 +747,8 @@ extension MainSplitViewController: OpenQuicklyViewControllerDelegate {
744747

745748
private extension MainSplitViewController {
746749

747-
func handleSelectDocument(_ document: Document, isNavigationBranch: Bool) {
748-
self.documentsViewController?.selectDocument(document, isNavigationBranch: isNavigationBranch, animated: false)
750+
func handleSelectDocument(_ document: Document, selectRow: EntityID? = nil, isNavigationBranch: Bool) {
751+
self.documentsViewController?.selectDocument(document, selectRow: selectRow, isNavigationBranch: isNavigationBranch, animated: false)
749752
self.lastMainControllerToAppear = .editor
750753
self.validateToolbar()
751754
}

Zavala/SceneDelegate.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
6666
return
6767
}
6868

69-
if let url = connectionOptions.urlContexts.first?.url, let documentID = EntityID(url: url) {
69+
if let url = connectionOptions.urlContexts.first?.url, let entityID = EntityID(url: url) {
7070
Task {
71-
await mainSplitViewController.handleDocument(documentID, isNavigationBranch: true)
71+
await mainSplitViewController.handleDocument(entityID, isNavigationBranch: true)
7272
}
7373
return
7474
}
@@ -103,9 +103,9 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
103103
}
104104

105105
func scene(_ scene: UIScene, openURLContexts urlContexts: Set<UIOpenURLContext>) {
106-
if let url = urlContexts.first?.url, let documentID = EntityID(url: url) {
106+
if let url = urlContexts.first?.url, let entityID = EntityID(url: url) {
107107
Task {
108-
await mainSplitViewController.handleDocument(documentID, isNavigationBranch: true)
108+
await mainSplitViewController.handleDocument(entityID, isNavigationBranch: true)
109109
}
110110
return
111111
}

0 commit comments

Comments
 (0)