Skip to content

Commit

Permalink
refactor: Extract office error handling outside errorWhilePreviewing
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Weidmann <[email protected]>
  • Loading branch information
PhilippeWeidmann committed Dec 6, 2024
1 parent ca41e24 commit 58e408e
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions kDrive/UI/Controller/Files/Preview/PreviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -449,27 +449,9 @@ final class PreviewViewController: UIViewController, PreviewContentCellDelegate,
}

let file = previewFiles[index]
let previewError = OfficePreviewError(fileId: fileId)
if file.convertedType == .spreadsheet || file.convertedType == .presentation || file.convertedType == .text {
previewError.pdfGenerationProgress = Progress(totalUnitCount: 10)
PdfPreviewCache.shared.retrievePdf(for: file, driveFileManager: driveFileManager) { downloadTask in
previewError.addDownloadTask(downloadTask)
Task { @MainActor [weak self] in
self?.collectionView.reloadItems(at: [IndexPath(item: index, section: 0)])
}
} completion: { url, error in
previewError.removeDownloadTask()
if let url {
previewError.pdfUrl = url
} else {
previewError.downloadError = error
}
Task { @MainActor [weak self] in
self?.collectionView.reloadItems(at: [IndexPath(item: index, section: 0)])
}
}
handleOfficePreviewError(error, previewIndex: index)
}
previewErrors[fileId] = previewError

// We have to delay reload because errorWhilePreviewing can be called when the collectionView requests a new cell in
// cellForItemAt and iOS 18 seems unhappy about this.
Expand All @@ -478,6 +460,31 @@ final class PreviewViewController: UIViewController, PreviewContentCellDelegate,
}
}

func handleOfficePreviewError(_ error: Error, previewIndex: Int) {
let file = previewFiles[previewIndex]

let previewError = OfficePreviewError(fileId: file.id, pdfGenerationProgress: Progress(totalUnitCount: 10))

PdfPreviewCache.shared.retrievePdf(for: file, driveFileManager: driveFileManager) { downloadTask in
previewError.addDownloadTask(downloadTask)
Task { @MainActor [weak self] in
self?.collectionView.reloadItems(at: [IndexPath(item: previewIndex, section: 0)])
}
} completion: { url, error in
previewError.removeDownloadTask()
if let url {
previewError.pdfUrl = url
} else {
previewError.downloadError = error
}
Task { @MainActor [weak self] in
self?.collectionView.reloadItems(at: [IndexPath(item: previewIndex, section: 0)])
}
}

previewErrors[file.id] = previewError
}

func openWith(from: UIView) {
let frame = from.convert(from.bounds, to: view)
floatingPanelViewController.dismiss(animated: true)
Expand Down

0 comments on commit 58e408e

Please sign in to comment.