Skip to content

Commit

Permalink
Refactor to properly honor the delegate pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnross committed Jul 4, 2020
1 parent 2ff2545 commit 3cbb640
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
15 changes: 7 additions & 8 deletions Bluetility/PasteboardBrowser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

import Cocoa

protocol IndexPathPasteboardDelegate {
func pasteboardStringForIndexPath(_ indexPath:IndexPath) -> String?
func menu(for cell: NSBrowserCell, at indexPath: IndexPath) -> NSMenu?
protocol PasteboardBrowserDelegate {
func browser(_ browser: PasteboardBrowser, pasteboardStringFor indexPath: IndexPath) -> String?
func browser(_ browser: PasteboardBrowser, menuFor cell: NSBrowserCell, atRow row: Int, column: Int) -> NSMenu?
}

class PasteboardBrowser: NSBrowser {

@IBAction
func copy(_ sender:AnyObject) {
var string:String? = nil
if let pasteboardDelegate = self.delegate as? IndexPathPasteboardDelegate,
if let pasteboardDelegate = self.delegate as? PasteboardBrowserDelegate,
let indexPath = self.selectionIndexPath
{
string = pasteboardDelegate.pasteboardStringForIndexPath(indexPath)
string = pasteboardDelegate.browser(self, pasteboardStringFor: indexPath)
}
if string == nil {
string = (self.selectedCell() as? NSBrowserCell)?.title
Expand Down Expand Up @@ -49,12 +49,11 @@ class PasteboardBrowser: NSBrowser {

guard
let cell = selectedCell() as? NSBrowserCell,
let indexPath = selectionIndexPath,
let delegate = delegate as? IndexPathPasteboardDelegate
let delegate = delegate as? PasteboardBrowserDelegate
else {
return nil
}
return delegate.menu(for: cell, at: indexPath)
return delegate.browser(self, menuFor: cell, atRow: row, column: column)
}

}
12 changes: 6 additions & 6 deletions Bluetility/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,8 @@ extension ViewController: NSViewToolTipOwner {
}
}

extension ViewController : IndexPathPasteboardDelegate {
func pasteboardStringForIndexPath(_ indexPath: IndexPath) -> String? {
extension ViewController : PasteboardBrowserDelegate {
func browser(_ browser: PasteboardBrowser, pasteboardStringFor indexPath: IndexPath) -> String? {
if indexPath.count == 1 {
let row = indexPath[0]
guard let tag = tooltipTagForRow[row] else {return nil}
Expand All @@ -450,20 +450,20 @@ extension ViewController : IndexPathPasteboardDelegate {
return nil
}

func menu(for cell: NSBrowserCell, at indexPath: IndexPath) -> NSMenu? {
func browser(_ browser: PasteboardBrowser, menuFor cell: NSBrowserCell, atRow row: Int, column: Int) -> NSMenu? {
let menu = NSMenu()
let copyItem = NSMenuItem(title: "Copy", action: #selector(copyCellContents(sender:)), keyEquivalent: "")
menu.addItem(copyItem)

if indexPath.count == 1 {
if column == 0 {
if selectedDevice?.peripheral.macAddr != nil {
menu.addItem(NSMenuItem(title: "Copy MAC Address", action: #selector(copyDeviceMAC(sender:)), keyEquivalent: ""))
}
menu.addItem(NSMenuItem(title: "Copy Device Identifier", action: #selector(copyDeviceIdentifier(sender:)), keyEquivalent: ""))
menu.addItem(NSMenuItem(title: "Copy Device Tooltip", action: #selector(copyDeviceTooltip(sender:)), keyEquivalent: ""))
} else if indexPath.count == 2 {
} else if column == 1 {
menu.addItem(NSMenuItem(title: "Copy Service UUID", action: #selector(copyServiceUUID(sender:)), keyEquivalent: ""))
} else if indexPath.count == 3 {
} else if column == 2 {
menu.addItem(NSMenuItem(title: "Copy Characteristic UUID", action: #selector(copyCharacteristicUUID(sender:)), keyEquivalent: ""))
}

Expand Down

0 comments on commit 3cbb640

Please sign in to comment.