Skip to content

Commit

Permalink
Revert back to opening metadata on cell tap (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
michalrentka authored Aug 28, 2023
1 parent a72249b commit a712ec5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
65 changes: 38 additions & 27 deletions Zotero/Scenes/Detail/Items/Views/ItemsTableViewHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ final class ItemsTableViewHandler: NSObject {
case attachment(attachment: Attachment, parentKey: String?)
case doi(String)
case url(URL)
case selectItem(String)
}

private static let cellId = "ItemCell"
Expand Down Expand Up @@ -248,6 +249,36 @@ final class ItemsTableViewHandler: NSObject {
})
}

private func tapAction(for indexPath: IndexPath) -> TapAction? {
guard let item = self.snapshot?[indexPath.row] else { return nil }

if self.viewModel.state.isEditing {
return .selectItem(item.key)
}

guard let accessory = self.viewModel.state.itemAccessories[item.key] else {
switch item.rawType {
case ItemTypes.note:
return .note(item)

default:
return .metadata(item)
}
}

switch accessory {
case .attachment(let attachment):
let parentKey = item.key == attachment.key ? nil : item.key
return .attachment(attachment: attachment, parentKey: parentKey)

case .doi(let doi):
return .doi(doi)

case .url(let url):
return .url(url)
}
}

// MARK: - Setups

private func setupTableView() {
Expand Down Expand Up @@ -350,37 +381,17 @@ extension ItemsTableViewHandler: UITableViewDataSource {

extension ItemsTableViewHandler: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let item = self.snapshot?[indexPath.row] else { return }

if self.viewModel.state.isEditing {
self.viewModel.process(action: .selectItem(item.key))
return
}
guard let action = self.tapAction(for: indexPath) else { return }

tableView.deselectRow(at: indexPath, animated: true)
switch action {
case .attachment, .doi, .metadata, .note, .url:
tableView.deselectRow(at: indexPath, animated: true)

guard let accessory = self.viewModel.state.itemAccessories[item.key] else {
switch item.rawType {
case ItemTypes.note:
self.tapObserver.on(.next(.note(item)))

default:
break
}
return
case .selectItem:
break
}

switch accessory {
case .attachment(let attachment):
let parentKey = item.key == attachment.key ? nil : item.key
self.tapObserver.on(.next(.attachment(attachment: attachment, parentKey: parentKey)))

case .doi(let doi):
self.tapObserver.on(.next(.doi(doi)))

case .url(let url):
self.tapObserver.on(.next(.url(url)))
}
self.tapObserver.on(.next(action))
}

func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
Expand Down
3 changes: 3 additions & 0 deletions Zotero/Scenes/Detail/Items/Views/ItemsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ final class ItemsViewController: UIViewController {
case .url(let url):
self.coordinatorDelegate?.show(url: url)

case .selectItem(let key):
self.viewModel.process(action: .selectItem(key))

case .note(let item):
guard let note = Note(item: item) else { return }
let tags = Array(item.tags.map({ Tag(tag: $0) }))
Expand Down

0 comments on commit a712ec5

Please sign in to comment.