Skip to content

Commit

Permalink
Fix collection context menu when collection size is not shown (#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvasilak authored Oct 21, 2024
1 parent 3bdad24 commit c35ba2f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ protocol SplitControllerDelegate: AnyObject {

final class CollectionsViewController: UICollectionViewController {
let viewModel: ViewModel<CollectionsActionHandler>
private unowned let dbStorage: DbStorage
private unowned let syncScheduler: SynchronizationScheduler
private unowned let dragDropController: DragDropController
private let disposeBag: DisposeBag
Expand All @@ -30,8 +31,15 @@ final class CollectionsViewController: UICollectionViewController {
return self.viewModel.state.selectedCollectionId
}

init(viewModel: ViewModel<CollectionsActionHandler>, dragDropController: DragDropController, syncScheduler: SynchronizationScheduler, coordinatorDelegate: MasterCollectionsCoordinatorDelegate) {
init(
viewModel: ViewModel<CollectionsActionHandler>,
dbStorage: DbStorage,
dragDropController: DragDropController,
syncScheduler: SynchronizationScheduler,
coordinatorDelegate: MasterCollectionsCoordinatorDelegate
) {
self.viewModel = viewModel
self.dbStorage = dbStorage
self.syncScheduler = syncScheduler
self.dragDropController = dragDropController
self.coordinatorDelegate = coordinatorDelegate
Expand Down Expand Up @@ -64,6 +72,7 @@ final class CollectionsViewController: UICollectionViewController {

self.collectionViewHandler = ExpandableCollectionsCollectionViewHandler(
collectionView: self.collectionView,
dbStorage: dbStorage,
dragDropController: self.dragDropController,
viewModel: self.viewModel,
splitDelegate: self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import UIKit
final class ExpandableCollectionsCollectionViewHandler: NSObject {
private let collectionsSection: Int = 0
private unowned let collectionView: UICollectionView
private unowned let dbStorage: DbStorage
private unowned let dragDropController: DragDropController
private unowned let viewModel: ViewModel<CollectionsActionHandler>

Expand All @@ -31,8 +32,9 @@ final class ExpandableCollectionsCollectionViewHandler: NSObject {
return self.dataSource.snapshot(for: self.collectionsSection).rootItems.contains(where: { $0.identifier == self.viewModel.state.selectedCollectionId })
}

init(collectionView: UICollectionView, dragDropController: DragDropController, viewModel: ViewModel<CollectionsActionHandler>, splitDelegate: SplitControllerDelegate?) {
init(collectionView: UICollectionView, dbStorage: DbStorage, dragDropController: DragDropController, viewModel: ViewModel<CollectionsActionHandler>, splitDelegate: SplitControllerDelegate?) {
self.collectionView = collectionView
self.dbStorage = dbStorage
self.dragDropController = dragDropController
self.viewModel = viewModel
self.splitDelegate = splitDelegate
Expand Down Expand Up @@ -126,12 +128,12 @@ final class ExpandableCollectionsCollectionViewHandler: NSObject {
actions.append(delete)
}

if collection.itemCount > 0 {
if !isCollectionEmpty(collection, dbStorage: dbStorage) {
actions.insert(contentsOf: [downloadAttachmentsAction(for: collection.identifier, in: viewModel), removeDownloadsAction(for: collection.identifier, in: viewModel)], at: 0)
}

case .custom(let type):
guard collection.itemCount > 0 else { break }
guard !isCollectionEmpty(collection, dbStorage: dbStorage) else { break }
actions.append(removeDownloadsAction(for: collection.identifier, in: viewModel))

switch type {
Expand All @@ -154,6 +156,21 @@ final class ExpandableCollectionsCollectionViewHandler: NSObject {
guard !actions.isEmpty else { return nil }
return UIMenu(children: actions)

func isCollectionEmpty(_ collection: Collection, dbStorage: DbStorage) -> Bool {
if Defaults.shared.showCollectionItemCounts {
return collection.itemCount == 0
}

switch collection.identifier {
case .collection, .custom:
let libraryId = viewModel.state.library.identifier
return (try? dbStorage.perform(request: ReadItemsDbRequest(collectionId: collection.identifier, libraryId: libraryId), on: .main))?.isEmpty ?? true

case .search:
return false
}
}

func downloadAttachmentsAction(for identifier: CollectionIdentifier, in viewModel: ViewModel<CollectionsActionHandler>) -> UIAction {
UIAction(title: L10n.Collections.downloadAttachments, image: UIImage(systemName: "arrow.down.to.line.compact")) { [weak viewModel] _ in
viewModel?.process(action: .downloadAttachments(identifier))
Expand Down
2 changes: 1 addition & 1 deletion Zotero/Scenes/Master/MasterCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ final class MasterCoordinator: NSObject, Coordinator {
let handler = CollectionsActionHandler(dbStorage: dbStorage, fileStorage: controllers.fileStorage, attachmentDownloader: attachmentDownloader, fileCleanupController: fileCleanupController)
let state = CollectionsState(libraryId: libraryId, selectedCollectionId: selectedCollectionId)
let viewModel = ViewModel(initialState: state, handler: handler)
return CollectionsViewController(viewModel: viewModel, dragDropController: controllers.dragDropController, syncScheduler: syncScheduler, coordinatorDelegate: self)
return CollectionsViewController(viewModel: viewModel, dbStorage: dbStorage, dragDropController: controllers.dragDropController, syncScheduler: syncScheduler, coordinatorDelegate: self)
}

private func storeIfNeeded(libraryId: LibraryIdentifier, preselectedCollection collectionId: CollectionIdentifier? = nil) -> CollectionIdentifier {
Expand Down

0 comments on commit c35ba2f

Please sign in to comment.