Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix collection context menu when collection size is not shown #1019

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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