From d2fa67e0e34e48a1ad9e445c068a588672b227e2 Mon Sep 17 00:00:00 2001 From: relikd Date: Sat, 5 Sep 2020 21:05:12 +0200 Subject: [PATCH] Reduce redundant code, cell copy menu --- main/Extensions/TableView.swift | 31 ++++++++++ main/GUI/Base.lproj/Recordings.storyboard | 8 +-- main/GUI/Base.lproj/Settings.storyboard | 4 +- main/Recordings/TVCRecordingDetails.swift | 69 ++++++++++------------- main/Requests/TVCOccurrenceContext.swift | 30 ++++------ main/Settings/TVCFilter.swift | 36 +++++------- 6 files changed, 93 insertions(+), 85 deletions(-) diff --git a/main/Extensions/TableView.swift b/main/Extensions/TableView.swift index 710c873..fb5b8df 100644 --- a/main/Extensions/TableView.swift +++ b/main/Extensions/TableView.swift @@ -90,3 +90,34 @@ extension EditActionsRemove where Self: UITableViewController { func editableRowActions(_: IndexPath) -> [(RowAction, String)] { [(.delete, "Remove")] } func editableRowActionColor(_: IndexPath, _: RowAction) -> UIColor? { nil } } + + +// MARK: - Table Cell Tap Menu + +struct TableCellTapMenu { + private var index: Int = Int.max + + mutating func reset() { index = Int.max } + + /// Create a new tap manu and shows it immediatelly. With optional buttons. + mutating func start(_ tableView: UITableView, _ indexPath: IndexPath, items: [UIMenuItem]? = nil) -> Bool { + let menu = UIMenuController.shared + if index == indexPath.row { + menu.setMenuVisible(false, animated: true) + reset() + return false + } + index = indexPath.row + let cell = tableView.cellForRow(at: indexPath)! + menu.setTargetRect(cell.bounds, in: cell) + menu.menuItems = items + menu.setMenuVisible(true, animated: true) + return true + } + + /// Returns the item if the array index is in bounds. + func getSelected(_ source: [T]) -> T? { + guard index < source.count else { return nil } + return source[index] + } +} diff --git a/main/GUI/Base.lproj/Recordings.storyboard b/main/GUI/Base.lproj/Recordings.storyboard index 9142a49..b92f7a1 100644 --- a/main/GUI/Base.lproj/Recordings.storyboard +++ b/main/GUI/Base.lproj/Recordings.storyboard @@ -196,7 +196,7 @@ - + @@ -220,7 +220,7 @@ - + @@ -244,7 +244,7 @@ - + @@ -268,7 +268,7 @@ - + diff --git a/main/GUI/Base.lproj/Settings.storyboard b/main/GUI/Base.lproj/Settings.storyboard index 51ed395..b9f6832 100644 --- a/main/GUI/Base.lproj/Settings.storyboard +++ b/main/GUI/Base.lproj/Settings.storyboard @@ -293,12 +293,12 @@ - + - + diff --git a/main/Recordings/TVCRecordingDetails.swift b/main/Recordings/TVCRecordingDetails.swift index 7a53f64..a2a34ef 100644 --- a/main/Recordings/TVCRecordingDetails.swift +++ b/main/Recordings/TVCRecordingDetails.swift @@ -122,59 +122,52 @@ class TVCRecordingDetails: UITableViewController, EditActionsRemove { // MARK: - Tap to Copy - private var rowToCopy: Int = Int.max - + private var cellMenu = TableCellTapMenu() + private var copyDomain: String? = nil + override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { - if rowToCopy == indexPath.row { - UIMenuController.shared.setMenuVisible(false, animated: true) - rowToCopy = Int.max - return nil - } - rowToCopy = indexPath.row - self.becomeFirstResponder() - let cell = tableView.cellForRow(at: indexPath)! - UIMenuController.shared.setTargetRect(cell.bounds, in: cell) - UIMenuController.shared.menuItems = [ - .init(title: "All requests", action: #selector(openInLogs)), -// .init(title: "CoOccurrence", action: #selector(openCoOccurrence)) + if noResults { return nil } + let buttons = [ + UIMenuItem(title: "All requests", action: #selector(openInLogs)), +// UIMenuItem(title: "CoOccurrence", action: #selector(openCoOccurrence)) ] - UIMenuController.shared.setMenuVisible(true, animated: true) + if cellMenu.start(tableView, indexPath, items: buttons) { + if showRaw { + copyDomain = cellMenu.getSelected(dataSourceRaw)?.domain + } else { + copyDomain = cellMenu.getSelected(dataSourceSum)?.domain + } + self.becomeFirstResponder() + } return nil } override var canBecomeFirstResponder: Bool { true } override func copy(_ sender: Any?) { - UIPasteboard.general.string = getDomain() - rowToCopy = Int.max + if let dom = copyDomain { + UIPasteboard.general.string = dom + } + cellMenu.reset() + copyDomain = nil } @objc private func openInLogs() { - guard let selectedDomain = getDomain(), - let tabBar = tabBarController as? TBCMain, - let tab = tabBar.openTab(0) as? TVCDomains else { - return - } - VCDateFilter.disableFilter() - tab.pushOpen(domain: selectedDomain) - } - - private func getDomain() -> String? { - if showRaw { - guard rowToCopy < dataSourceRaw.count else { return nil } - return dataSourceRaw[rowToCopy].domain - } else { - guard rowToCopy < dataSourceSum.count else { return nil } - return dataSourceSum[rowToCopy].domain + if let dom = copyDomain, let req = (tabBarController as? TBCMain)?.openTab(0) as? TVCDomains { + VCDateFilter.disableFilter() + req.pushOpen(domain: dom) } + cellMenu.reset() + copyDomain = nil } // @objc private func openCoOccurrence() { -// guard let vc: VCCoOccurrence = storyboard?.load("IBCoOccurrence") else { -// return +// if let dom = copyDomain, let vc: VCCoOccurrence = storyboard?.load("IBCoOccurrence") { +// vc.domainName = dom +// vc.isFQDN = true +// present(vc, animated: true) // } -// vc.domainName = getDomain() -// vc.isFQDN = true -// present(vc, animated: true) +// cellMenu.reset() +// copyDomain = nil // } } diff --git a/main/Requests/TVCOccurrenceContext.swift b/main/Requests/TVCOccurrenceContext.swift index 2819dde..4c4e914 100644 --- a/main/Requests/TVCOccurrenceContext.swift +++ b/main/Requests/TVCOccurrenceContext.swift @@ -66,32 +66,24 @@ class TVCOccurrenceContext: UITableViewController { // MARK: - Tap to Copy - private var rowToCopy: Int = Int.max - + private var cellMenu = TableCellTapMenu() + private var copyDomain: String? = nil + override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { - if firstOrLast(indexPath.row) { return nil } - if rowToCopy == indexPath.row { - UIMenuController.shared.setMenuVisible(false, animated: true) - rowToCopy = Int.max - return nil + if cellMenu.start(tableView, indexPath) { + copyDomain = cellMenu.getSelected(dataSource)?.domain + self.becomeFirstResponder() } - rowToCopy = indexPath.row - self.becomeFirstResponder() - let cell = tableView.cellForRow(at: indexPath)! - UIMenuController.shared.setTargetRect(cell.bounds, in: cell) - UIMenuController.shared.setMenuVisible(true, animated: true) return nil } override var canBecomeFirstResponder: Bool { true } - override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { - action == #selector(UIResponderStandardEditActions.copy) - } - override func copy(_ sender: Any?) { - guard rowToCopy < dataSource.count else { return } - UIPasteboard.general.string = dataSource[rowToCopy].domain - rowToCopy = Int.max + if let dom = copyDomain { + UIPasteboard.general.string = dom + } + cellMenu.reset() + copyDomain = nil } } diff --git a/main/Settings/TVCFilter.swift b/main/Settings/TVCFilter.swift index f5e7ee9..7543bf9 100644 --- a/main/Settings/TVCFilter.swift +++ b/main/Settings/TVCFilter.swift @@ -51,9 +51,6 @@ class TVCFilter: UITableViewController, EditActionsRemove { override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "DomainFilterCell")! cell.textLabel?.text = dataSource[indexPath.row] - if cell.gestureRecognizers?.isEmpty ?? true { - cell.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(didLongTap))) - } return cell } @@ -75,29 +72,24 @@ class TVCFilter: UITableViewController, EditActionsRemove { // MARK: - Long Press Gesture - private var cellTitleCopy: String? - - @objc private func didLongTap(_ sender: UILongPressGestureRecognizer) { - guard let cell = sender.view as? UITableViewCell else { - return - } - if sender.state == .began { - cellTitleCopy = cell.textLabel?.text + private var cellMenu = TableCellTapMenu() + private var copyDomain: String? = nil + + override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { + if cellMenu.start(tableView, indexPath) { + copyDomain = cellMenu.getSelected(dataSource) self.becomeFirstResponder() - let menu = UIMenuController.shared -// menu.setTargetRect(CGRect(origin: sender.location(in: cell), size: CGSize.zero), in: cell) - menu.setTargetRect(cell.bounds, in: cell) - menu.setMenuVisible(true, animated: true) } - } - override var canBecomeFirstResponder: Bool { get { true } } - - override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { - action == #selector(UIResponderStandardEditActions.copy) + return nil } + override var canBecomeFirstResponder: Bool { true } + override func copy(_ sender: Any?) { - UIPasteboard.general.string = cellTitleCopy - cellTitleCopy = nil + if let dom = copyDomain { + UIPasteboard.general.string = dom + } + cellMenu.reset() + copyDomain = nil } }