Skip to content

Commit

Permalink
Fixed stability issues with page thumbnail controller (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
michalrentka authored Jan 19, 2024
1 parent ec66100 commit 8d0fda9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
12 changes: 11 additions & 1 deletion Zotero/Scenes/Detail/PDF/Models/PDFThumbnailsState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSNumber, UIImage>
var pages: [String]
var pages: [Page]
var isDark: Bool
var loadedThumbnail: Int?
var selectedPageIndex: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ struct PDFThumbnailsActionHandler: ViewModelActionHandler {
}

private func loadPages(viewModel: ViewModel<PDFThumbnailsActionHandler>) {
let labels = (0..<viewModel.state.document.pageCount).map({ viewModel.state.document.pageLabelForPage(at: $0, substituteWithPlainLabel: true) ?? "" })
guard viewModel.state.document.pageCount > 0 else { return }
let labels = (0..<viewModel.state.document.pageCount).map({ PDFThumbnailsState.Page(title: viewModel.state.document.pageLabelForPage(at: $0, substituteWithPlainLabel: true) ?? "") })
update(viewModel: viewModel) { state in
state.pages = labels
state.changes = .pages
Expand Down
16 changes: 8 additions & 8 deletions Zotero/Scenes/Detail/PDF/Views/PDFThumbnailsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class PDFThumbnailsViewController: UICollectionViewController {
private let viewModel: ViewModel<PDFThumbnailsActionHandler>
private let disposeBag: DisposeBag

private var dataSource: UICollectionViewDiffableDataSource<Int, String>!
private var dataSource: UICollectionViewDiffableDataSource<Int, PDFThumbnailsState.Page>!

private lazy var cellRegistration: UICollectionView.CellRegistration<PDFThumbnailsCell, String> = {
return UICollectionView.CellRegistration<PDFThumbnailsCell, String> { [weak self] cell, indexPath, label in
private lazy var cellRegistration: UICollectionView.CellRegistration<PDFThumbnailsCell, PDFThumbnailsState.Page> = {
return UICollectionView.CellRegistration<PDFThumbnailsCell, PDFThumbnailsState.Page> { [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)
}
}()

Expand All @@ -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()

Expand Down Expand Up @@ -88,8 +88,8 @@ class PDFThumbnailsViewController: UICollectionViewController {
}

private func update(state: PDFThumbnailsState) {
if state.changes.contains(.pages) {
var snapshot = NSDiffableDataSourceSnapshot<Int, String>()
if state.changes.contains(.pages) && !state.pages.isEmpty {
var snapshot = NSDiffableDataSourceSnapshot<Int, PDFThumbnailsState.Page>()
snapshot.appendSections([0])
snapshot.appendItems(state.pages)
dataSource.apply(snapshot) { [weak self] in
Expand Down

0 comments on commit 8d0fda9

Please sign in to comment.