-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathMainContentCoordinator+Pagination.swift
More file actions
112 lines (91 loc) · 3.52 KB
/
MainContentCoordinator+Pagination.swift
File metadata and controls
112 lines (91 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// MainContentCoordinator+Pagination.swift
// TablePro
//
// Pagination operations for MainContentCoordinator
//
import Foundation
extension MainContentCoordinator {
// MARK: - Pagination
/// Navigate to next page
func goToNextPage() {
guard let tabIndex = tabManager.selectedTabIndex,
tabIndex < tabManager.tabs.count,
tabManager.tabs[tabIndex].pagination.hasNextPage else { return }
paginateAfterConfirmation(tabIndex: tabIndex) { pagination in
pagination.goToNextPage()
}
}
/// Navigate to previous page
func goToPreviousPage() {
guard let tabIndex = tabManager.selectedTabIndex,
tabIndex < tabManager.tabs.count,
tabManager.tabs[tabIndex].pagination.hasPreviousPage else { return }
paginateAfterConfirmation(tabIndex: tabIndex) { pagination in
pagination.goToPreviousPage()
}
}
/// Navigate to first page
func goToFirstPage() {
guard let tabIndex = tabManager.selectedTabIndex,
tabIndex < tabManager.tabs.count,
tabManager.tabs[tabIndex].pagination.hasPreviousPage else { return }
paginateAfterConfirmation(tabIndex: tabIndex) { pagination in
pagination.goToFirstPage()
}
}
/// Navigate to last page
func goToLastPage() {
guard let tabIndex = tabManager.selectedTabIndex,
tabIndex < tabManager.tabs.count else { return }
let tab = tabManager.tabs[tabIndex]
guard tab.pagination.currentPage != tab.pagination.totalPages else { return }
paginateAfterConfirmation(tabIndex: tabIndex) { pagination in
pagination.goToLastPage()
}
}
/// Update page size (limit) and reload
func updatePageSize(_ newSize: Int) {
guard let tabIndex = tabManager.selectedTabIndex,
tabIndex < tabManager.tabs.count,
newSize > 0 else { return }
paginateAfterConfirmation(tabIndex: tabIndex) { pagination in
pagination.updatePageSize(newSize)
}
}
/// Update offset and reload
func updateOffset(_ newOffset: Int) {
guard let tabIndex = tabManager.selectedTabIndex,
tabIndex < tabManager.tabs.count,
newOffset >= 0 else { return }
paginateAfterConfirmation(tabIndex: tabIndex) { pagination in
pagination.updateOffset(newOffset)
}
}
/// Apply both limit and offset changes and reload
func applyPaginationSettings() {
reloadCurrentPage()
}
// MARK: - Private
/// Confirm discard if needed, then mutate pagination state and reload.
private func paginateAfterConfirmation(
tabIndex: Int,
mutate: @escaping (inout PaginationState) -> Void
) {
let tabId = tabManager.tabs[tabIndex].id
confirmDiscardChangesIfNeeded(action: .pagination) { [weak self] confirmed in
guard let self, confirmed else { return }
guard let idx = self.tabManager.tabs.firstIndex(where: { $0.id == tabId }) else { return }
mutate(&self.tabManager.tabs[idx].pagination)
self.tabManager.tabs[idx].paginationVersion += 1
self.pendingScrollToTopAfterReplace.insert(tabId)
self.reloadCurrentPage()
}
}
private func reloadCurrentPage() {
guard let tabIndex = tabManager.selectedTabIndex,
tabIndex < tabManager.tabs.count else { return }
rebuildTableQuery(at: tabIndex)
runQuery()
}
}