Skip to content

Commit a5b0875

Browse files
committed
Fix loading initial pdf page if not yet synced (#1233)
1 parent a426072 commit a5b0875

File tree

6 files changed

+49
-10
lines changed

6 files changed

+49
-10
lines changed

Zotero/Controllers/Database/Requests/ReadDocumentDataDbRequest.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ struct ReadDocumentDataDbRequest: DbResponseRequest {
1515

1616
let attachmentKey: String
1717
let libraryId: LibraryIdentifier
18+
let defaultPageValue: String
1819

1920
var needsWrite: Bool { return false }
2021

2122
func process(in database: Realm) throws -> String {
22-
guard let pageIndex = database.objects(RPageIndex.self).uniqueObject(key: attachmentKey, libraryId: libraryId) else { return "" }
23+
guard let pageIndex = database.objects(RPageIndex.self).uniqueObject(key: attachmentKey, libraryId: libraryId) else { return defaultPageValue }
2324
return pageIndex.index
2425
}
2526
}

Zotero/Scenes/Detail/HTML:EPUB/Models/HtmlEpubReaderState.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ struct HtmlEpubReaderState: ViewModelState {
9191
return L10n.Errors.unknown
9292
}
9393
}
94+
95+
var documentShouldClose: Bool {
96+
return false
97+
}
9498
}
9599

96100
let readerURL: URL?

Zotero/Scenes/Detail/HTML:EPUB/ViewModels/HtmlEpubReaderActionHandler.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,10 @@ final class HtmlEpubReaderActionHandler: ViewModelActionHandler, BackgroundDbPro
788788

789789
func loadItemAnnotationsAndPage(in viewModel: ViewModel<HtmlEpubReaderActionHandler>) -> (RItem, Results<RItem>, String)? {
790790
do {
791+
let defaultPageValue = defaultPageValue(forExt: viewModel.state.documentFile.ext.lowercased())
791792
let itemRequest = ReadItemDbRequest(libraryId: viewModel.state.library.identifier, key: viewModel.state.key)
792793
let item = try dbStorage.perform(request: itemRequest, on: .main)
793-
let pageIndexRequest = ReadDocumentDataDbRequest(attachmentKey: viewModel.state.key, libraryId: viewModel.state.library.identifier)
794+
let pageIndexRequest = ReadDocumentDataDbRequest(attachmentKey: viewModel.state.key, libraryId: viewModel.state.library.identifier, defaultPageValue: defaultPageValue)
794795
let pageIndex = try dbStorage.perform(request: pageIndexRequest, on: .main)
795796
let annotationsRequest = ReadAnnotationsDbRequest(attachmentKey: viewModel.state.key, libraryId: viewModel.state.library.identifier)
796797
let items = try dbStorage.perform(request: annotationsRequest, on: .main)
@@ -799,6 +800,19 @@ final class HtmlEpubReaderActionHandler: ViewModelActionHandler, BackgroundDbPro
799800
DDLogError("HtmlEpubReaderActionHandler: can't load annotations - \(error)")
800801
return nil
801802
}
803+
804+
func defaultPageValue(forExt ext: String) -> String {
805+
switch ext {
806+
case "epub":
807+
return "_start"
808+
809+
case "html", "htm":
810+
return "0"
811+
812+
default:
813+
return ""
814+
}
815+
}
802816
}
803817

804818
func processAnnotations(items: Results<RItem>) -> ([String], [String: HtmlEpubAnnotation], String) {
@@ -818,11 +832,10 @@ final class HtmlEpubReaderActionHandler: ViewModelActionHandler, BackgroundDbPro
818832
func loadTypeAndPage(from file: File, rawPage: String) throws -> (String, HtmlEpubReaderState.DocumentData.Page?) {
819833
switch viewModel.state.documentFile.ext.lowercased() {
820834
case "epub":
821-
let cfi = rawPage.isEmpty ? "_start" : rawPage
822-
return ("epub", .epub(cfi: cfi))
835+
return ("epub", .epub(cfi: rawPage))
823836

824837
case "html", "htm":
825-
if let scrollYPercent = Double(rawPage.isEmpty ? "0" : rawPage) {
838+
if let scrollYPercent = Double(rawPage) {
826839
return ("snapshot", .html(scrollYPercent: scrollYPercent))
827840
} else {
828841
DDLogError("HtmlEPubReaderActionHandler: incompatible lastIndexPage stored for \(viewModel.state.key) - \(rawPage)")

Zotero/Scenes/Detail/PDF/Models/PDFReaderState.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ struct PDFReaderState: ViewModelState {
6262
case pageNotInt
6363
case unknown
6464
case documentEmpty
65+
case unknownLoading
6566

6667
var title: String {
6768
switch self {
68-
case .cantDeleteAnnotation, .cantAddAnnotations, .cantUpdateAnnotation, .pageNotInt, .unknown, .documentEmpty:
69+
case .cantDeleteAnnotation, .cantAddAnnotations, .cantUpdateAnnotation, .pageNotInt, .unknown, .documentEmpty, .unknownLoading:
6970
return L10n.error
7071

7172
case .mergeTooBig:
@@ -90,13 +91,23 @@ struct PDFReaderState: ViewModelState {
9091
case .pageNotInt:
9192
return L10n.Errors.Pdf.pageIndexNotInt
9293

93-
case .unknown:
94+
case .unknown, .unknownLoading:
9495
return L10n.Errors.unknown
9596

9697
case .documentEmpty:
9798
return L10n.Errors.Pdf.emptyDocument
9899
}
99100
}
101+
102+
var documentShouldClose: Bool {
103+
switch self {
104+
case .documentEmpty, .pageNotInt, .unknownLoading:
105+
return true
106+
107+
case .cantDeleteAnnotation, .cantAddAnnotations, .cantUpdateAnnotation, .mergeTooBig, .unknown:
108+
return false
109+
}
110+
}
100111
}
101112

102113
enum DefaultAnnotationPageLabel {

Zotero/Scenes/Detail/PDF/ViewModels/PDFReaderActionHandler.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,8 +1875,10 @@ final class PDFReaderActionHandler: ViewModelActionHandler, BackgroundDbProcessi
18751875

18761876
observeDocument(in: viewModel)
18771877
} catch let error {
1878-
// TODO: - Show error
18791878
DDLogError("PDFReaderActionHandler: failed to load PDF: \(error)")
1879+
update(viewModel: viewModel) { state in
1880+
state.error = (error as? PDFReaderState.Error) ?? .unknownLoading
1881+
}
18801882
}
18811883

18821884
func observe(library: Library, viewModel: ViewModel<PDFReaderActionHandler>, handler: PDFReaderActionHandler) {
@@ -1935,7 +1937,7 @@ final class PDFReaderActionHandler: ViewModelActionHandler, BackgroundDbProcessi
19351937

19361938
try dbStorage.perform(on: .main, with: { coordinator in
19371939
item = try coordinator.perform(request: ReadItemDbRequest(libraryId: libraryId, key: key))
1938-
pageStr = try coordinator.perform(request: ReadDocumentDataDbRequest(attachmentKey: key, libraryId: libraryId))
1940+
pageStr = try coordinator.perform(request: ReadDocumentDataDbRequest(attachmentKey: key, libraryId: libraryId, defaultPageValue: "0"))
19391941
results = try coordinator.perform(request: ReadAnnotationsDbRequest(attachmentKey: key, libraryId: libraryId))
19401942
})
19411943

Zotero/Scenes/Detail/ReaderCoordinator.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import RxSwift
1313
protocol ReaderError: Error {
1414
var title: String { get }
1515
var message: String { get }
16+
17+
var documentShouldClose: Bool { get }
1618
}
1719

1820
protocol ReaderState {
@@ -100,7 +102,13 @@ protocol ReaderCoordinator: Coordinator, ReaderCoordinatorDelegate, ReaderSideba
100102
extension ReaderCoordinator {
101103
func show(error: ReaderError) {
102104
let controller = UIAlertController(title: error.title, message: error.message, preferredStyle: .alert)
103-
controller.addAction(UIAlertAction(title: L10n.ok, style: .default))
105+
if error.documentShouldClose {
106+
controller.addAction(UIAlertAction(title: L10n.close, style: .default, handler: { [weak self] _ in
107+
self?.navigationController?.dismiss(animated: true)
108+
}))
109+
} else {
110+
controller.addAction(UIAlertAction(title: L10n.ok, style: .default))
111+
}
104112
navigationController?.present(controller, animated: true)
105113
}
106114

0 commit comments

Comments
 (0)