diff --git a/Zotero/Scenes/Detail/PDF/Models/PDFThumbnailsState.swift b/Zotero/Scenes/Detail/PDF/Models/PDFThumbnailsState.swift index 9039c71c7..23bc440c7 100644 --- a/Zotero/Scenes/Detail/PDF/Models/PDFThumbnailsState.swift +++ b/Zotero/Scenes/Detail/PDF/Models/PDFThumbnailsState.swift @@ -27,13 +27,23 @@ struct PDFThumbnailsState: ViewModelState { case fromSidebar } + struct Page: Hashable { + let id: UUID + let title: String + + init(title: String) { + self.id = UUID() + self.title = title + } + } + let thumbnailSize: CGSize let document: Document let key: String let libraryId: LibraryIdentifier let cache: NSCache - var pages: [String] + var pages: [Page] var isDark: Bool var loadedThumbnail: Int? var selectedPageIndex: Int diff --git a/Zotero/Scenes/Detail/PDF/ViewModels/PDFThumbnailsActionHandler.swift b/Zotero/Scenes/Detail/PDF/ViewModels/PDFThumbnailsActionHandler.swift index add2fd249..eb3b8d26b 100644 --- a/Zotero/Scenes/Detail/PDF/ViewModels/PDFThumbnailsActionHandler.swift +++ b/Zotero/Scenes/Detail/PDF/ViewModels/PDFThumbnailsActionHandler.swift @@ -59,7 +59,8 @@ struct PDFThumbnailsActionHandler: ViewModelActionHandler { } private func loadPages(viewModel: ViewModel) { - let labels = (0.. 0 else { return } + let labels = (0.. private let disposeBag: DisposeBag - private var dataSource: UICollectionViewDiffableDataSource! + private var dataSource: UICollectionViewDiffableDataSource! - private lazy var cellRegistration: UICollectionView.CellRegistration = { - return UICollectionView.CellRegistration { [weak self] cell, indexPath, label in + private lazy var cellRegistration: UICollectionView.CellRegistration = { + return UICollectionView.CellRegistration { [weak self] cell, indexPath, page in let image = self?.viewModel.state.cache.object(forKey: NSNumber(value: indexPath.row)) if image == nil { self?.viewModel.process(action: .load(UInt(indexPath.row))) } - cell.contentConfiguration = PDFThumbnailsCell.ContentConfiguration(label: label, image: image) + cell.contentConfiguration = PDFThumbnailsCell.ContentConfiguration(label: page.title, image: image) } }() @@ -42,11 +42,11 @@ class PDFThumbnailsViewController: UICollectionViewController { super.init(collectionViewLayout: layout) } - + required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } - + override func viewDidLoad() { super.viewDidLoad() @@ -88,8 +88,8 @@ class PDFThumbnailsViewController: UICollectionViewController { } private func update(state: PDFThumbnailsState) { - if state.changes.contains(.pages) { - var snapshot = NSDiffableDataSourceSnapshot() + if state.changes.contains(.pages) && !state.pages.isEmpty { + var snapshot = NSDiffableDataSourceSnapshot() snapshot.appendSections([0]) snapshot.appendItems(state.pages) dataSource.apply(snapshot) { [weak self] in