Skip to content

Commit

Permalink
tap on menuContent now redirects to preview
Browse files Browse the repository at this point in the history
  • Loading branch information
nayooti committed Nov 27, 2020
1 parent 4c62081 commit 6994533
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 78 deletions.
44 changes: 1 addition & 43 deletions deltachat-ios/Controller/ContextMenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@ import AVFoundation
import SDWebImage
import DcCore

// TODO: probably not able to trigger touch events this way
// MARK: - ContextMenuDelegate
protocol ContextMenuDelegate: class {
func contextMenu(_: ContextMenuController, event: ContextMenuController.Event)
}

// MARK: - ContextMenuController
class ContextMenuController: UIViewController {

let item: GalleryItem
weak var delegate: ContextMenuDelegate?


init(item: GalleryItem) {
self.item = item
super.init(nibName: nil, bundle: nil)
Expand Down Expand Up @@ -45,33 +39,15 @@ class ContextMenuController: UIViewController {
return
}

let hitTestView = HitTestView()

view.addSubview(hitTestView)
view.addSubview(contentView)
hitTestView.translatesAutoresizingMaskIntoConstraints = false
contentView.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
hitTestView.leftAnchor.constraint(equalTo: view.leftAnchor),
hitTestView.rightAnchor.constraint(equalTo: view.rightAnchor),
hitTestView.topAnchor.constraint(equalTo: view.topAnchor),
hitTestView.bottomAnchor.constraint(equalTo: view.bottomAnchor),

contentView.leftAnchor.constraint(equalTo: view.leftAnchor),
contentView.rightAnchor.constraint(equalTo: view.rightAnchor),
contentView.topAnchor.constraint(equalTo: view.topAnchor),
contentView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])

let button = UIButton(frame: CGRect(x: view.frame.midX, y: view.frame.midY, width: 100, height: 100))
button.makeBorder()
button.setTitle("Tap me", for: .normal)
button.addTarget(self, action: #selector(handleThumbnailTap(_:)), for: .touchUpInside)
view.addSubview(button)
// let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleThumbnailTap(_:)))
// contentView.addGestureRecognizer(tapGesture)
// contentView.makeBorder()
}

// MARK: - thumbnailView creation
Expand Down Expand Up @@ -130,22 +106,4 @@ class ContextMenuController: UIViewController {
let height = image.size.height * (width / image.size.width)
self.preferredContentSize = CGSize(width: width, height: height)
}

// MARK: - actions
@objc private func handleThumbnailTap(_ tapGesture: UITapGestureRecognizer) {
delegate?.contextMenu(self, event: .tap(item))
}


}

// MARK: - inner class definitions
extension ContextMenuController {
enum Event {
case tap(GalleryItem)
case doupleTap(GalleryItem)
case longPress(GalleryItem)
// add event types if needed
}
}

44 changes: 9 additions & 35 deletions deltachat-ios/Controller/GalleryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class GalleryViewController: UIViewController {
return layout
}()

private lazy var grid: HitTestCollectionView = {
let collection = HitTestCollectionView(frame: .zero, collectionViewLayout: gridLayout)
private lazy var grid: UICollectionView = {
let collection = UICollectionView(frame: .zero, collectionViewLayout: gridLayout)
collection.dataSource = self
collection.delegate = self
collection.register(GalleryCell.self, forCellWithReuseIdentifier: GalleryCell.reuseIdentifier)
Expand Down Expand Up @@ -228,7 +228,6 @@ extension GalleryViewController: UICollectionViewDataSource, UICollectionViewDel
identifier: nil,
previewProvider: {
let contextMenuController = ContextMenuController(item: item)
contextMenuController.delegate = self
return contextMenuController
},
actionProvider: { [weak self] _ in
Expand All @@ -237,6 +236,13 @@ extension GalleryViewController: UICollectionViewDataSource, UICollectionViewDel
)
}

@available(iOS 13, *)
func collectionView(_ collectionView: UICollectionView, willEndContextMenuInteraction configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionAnimating?) {
if let msgId = (animator?.previewViewController as? ContextMenuController)?.item.msg.id {
self.showPreview(msgId: msgId)
}
}

@available(iOS 13, *)
private func makeContextMenu(indexPath: IndexPath) -> UIMenu {
let deleteAction = UIAction(
Expand Down Expand Up @@ -303,35 +309,3 @@ extension GalleryViewController {
present(previewController, animated: true, completion: nil)
}
}

extension GalleryViewController: ContextMenuDelegate {

func contextMenu(_: ContextMenuController, event: ContextMenuController.Event) {

switch event {
case .tap(let item):
let msgId = item.msg.id
showPreview(msgId: msgId)
default:
return
}
}
}

class HitTestCollectionView: UICollectionView {

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
print("collection hit test succeeded")
return self
}

}

class HitTestView: UIView {

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
print("thumbnail hit test succeeded")
return self
}

}

0 comments on commit 6994533

Please sign in to comment.