Skip to content

Commit

Permalink
context menu in document gallery now works
Browse files Browse the repository at this point in the history
  • Loading branch information
nayooti authored and cyBerta committed Jan 4, 2021
1 parent da92ef8 commit e3762a3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
36 changes: 25 additions & 11 deletions deltachat-ios/Controller/DocumentGalleryController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ class DocumentGalleryController: UIViewController {
return label
}()

private lazy var contextMenuConfiguration: ContextMenuConfiguration = {
let deleteItem = ContextMenuConfiguration.ContextMenuItem(
private lazy var contextMenu: ContextMenuProvider = {
let deleteItem = ContextMenuProvider.ContextMenuItem(
title: String.localized("delete"),
imageNames: ("trash", nil),
option: .delete,
action: #selector(GalleryCell.itemDelete(_:)),
action: #selector(DocumentGalleryFileCell.itemDelete(_:)),
onPerform: { [weak self] indexPath in
self?.askToDeleteItem(at: indexPath)
}
)
let showInChatItem = ContextMenuConfiguration.ContextMenuItem(
let showInChatItem = ContextMenuProvider.ContextMenuItem(
title: String.localized("show_in_chat"),
imageNames: ("doc.text.magnifyingglass", nil),
option: .showInChat,
action: #selector(GalleryCell.showInChat(_:)),
action: #selector(DocumentGalleryFileCell.showInChat(_:)),
onPerform: { [weak self] indexPath in
self?.redirectToMessage(of: indexPath)
}
)

let config = ContextMenuConfiguration()
config.setMenu([showInChatItem, deleteItem])
return config
let menu = ContextMenuProvider()
menu.setMenu([showInChatItem, deleteItem])
return menu
}()

init(context: DcContext, fileMessageIds: [Int]) {
Expand All @@ -67,6 +67,10 @@ class DocumentGalleryController: UIViewController {
}
}

override func viewWillAppear(_ animated: Bool) {
setupContextMenuIfNeeded()
}

// MARK: - layout
private func setupSubviews() {
view.addSubview(tableView)
Expand All @@ -84,6 +88,11 @@ class DocumentGalleryController: UIViewController {
emptyStateView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
}

private func setupContextMenuIfNeeded() {
UIMenuController.shared.menuItems = contextMenu.menuItems
UIMenuController.shared.update()
}

// MARK: - actions
private func askToDeleteItem(at indexPath: IndexPath) {
let title = String.localized(stringID: "ask_delete_messages", count: 1)
Expand Down Expand Up @@ -128,12 +137,17 @@ extension DocumentGalleryController: UITableViewDelegate, UITableViewDataSource

// MARK: - context menu
// context menu for iOS 11, 12
func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return true
}

func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return contextMenuConfiguration.canPerformAction(action: action)
let action = contextMenu.canPerformAction(action: action)
return action
}

func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
contextMenuConfiguration.performAction(action: action, indexPath: indexPath)
contextMenu.performAction(action: action, indexPath: indexPath)
}

// context menu for iOS 13+
Expand All @@ -143,7 +157,7 @@ extension DocumentGalleryController: UITableViewDelegate, UITableViewDataSource
identifier: nil,
previewProvider: nil,
actionProvider: { [weak self] _ in
self?.contextMenuConfiguration.actionProvider(indexPath: indexPath)
self?.contextMenu.actionProvider(indexPath: indexPath)
}
)
}
Expand Down
24 changes: 12 additions & 12 deletions deltachat-ios/Controller/GalleryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class GalleryViewController: UIViewController {
return label
}()

private lazy var contextMenuConfiguration: ContextMenuConfiguration = {
let deleteItem = ContextMenuConfiguration.ContextMenuItem(
private lazy var contextMenu: ContextMenuProvider = {
let deleteItem = ContextMenuProvider.ContextMenuItem(
title: String.localized("delete"),
imageNames: ("trash", nil),
option: .delete,
Expand All @@ -56,7 +56,7 @@ class GalleryViewController: UIViewController {
self?.askToDeleteItem(at: indexPath)
}
)
let showInChatItem = ContextMenuConfiguration.ContextMenuItem(
let showInChatItem = ContextMenuProvider.ContextMenuItem(
title: String.localized("show_in_chat"),
imageNames: ("doc.text.magnifyingglass", nil),
option: .showInChat,
Expand All @@ -66,7 +66,7 @@ class GalleryViewController: UIViewController {
}
)

let config = ContextMenuConfiguration()
let config = ContextMenuProvider()
config.setMenu([showInChatItem, deleteItem])
return config
}()
Expand Down Expand Up @@ -124,7 +124,7 @@ class GalleryViewController: UIViewController {
}

private func setupContextMenuIfNeeded() {
UIMenuController.shared.menuItems = contextMenuConfiguration.menuItems
UIMenuController.shared.menuItems = contextMenu.menuItems
UIMenuController.shared.update()
}

Expand Down Expand Up @@ -218,19 +218,19 @@ extension GalleryViewController: UICollectionViewDataSource, UICollectionViewDel
timeLabel.hide(animated: true)
}

// MARK: - context menu
// context menu for iOS 11, 12
func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
return true
}

// MARK: - context menu
// context menu for iOS 11, 12
func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return contextMenuConfiguration.canPerformAction(action: action)
return contextMenu.canPerformAction(action: action)
}

func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {

contextMenuConfiguration.performAction(action: action, indexPath: indexPath)
contextMenu.performAction(action: action, indexPath: indexPath)
}

// context menu for iOS 13+
Expand All @@ -250,7 +250,7 @@ extension GalleryViewController: UICollectionViewDataSource, UICollectionViewDel
return contextMenuController
},
actionProvider: { [weak self] _ in
self?.contextMenuConfiguration.actionProvider(indexPath: indexPath)
self?.contextMenu.actionProvider(indexPath: indexPath)
}
)
}
Expand Down Expand Up @@ -330,7 +330,7 @@ private extension GalleryViewController {
}
}

class ContextMenuConfiguration {
class ContextMenuProvider {

var menu: [ContextMenuItem] = []

Expand Down Expand Up @@ -394,7 +394,7 @@ class ContextMenuConfiguration {
}
}

extension ContextMenuConfiguration {
extension ContextMenuProvider {
typealias ImageSystemName = String
struct ContextMenuItem {
var title: String
Expand Down
19 changes: 19 additions & 0 deletions deltachat-ios/View/Cell/DocumentGalleryFileCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ class DocumentGalleryFileCell: UITableViewCell {
let controller = UIDocumentInteractionController(url: url)
fileImageView.image = controller.icons.first ?? placeholder
}
}

// needed for iOS 12 context men
@objc func itemDelete(_ sender: Any) {
self.performAction(#selector(DocumentGalleryFileCell.itemDelete(_:)), with: sender)
}

@objc func showInChat(_ sender: Any) {
self.performAction(#selector(DocumentGalleryFileCell.showInChat(_:)), with: sender)
}

func performAction(_ action: Selector, with sender: Any?) {
if let tableView = self.superview as? UITableView, let indexPath = tableView.indexPath(for: self) {
tableView.delegate?.tableView?(
tableView,
performAction: action,
forRowAt: indexPath,
withSender: sender
)
}
}
}

0 comments on commit e3762a3

Please sign in to comment.