-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppDelegate.swift
111 lines (100 loc) · 4.09 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import Cocoa
import Frinkiac
@NSApplicationMain
// MARK: - App Delegate -
//------------------------------------------------------------------------------
class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate {
// MARK: - Private -
//--------------------------------------------------------------------------
fileprivate var frameImage: FrameImage<Frinkiac>? = nil {
didSet {
setImage(frameImage?.image, text: frameImage?.caption?.lines ?? "")
}
}
fileprivate let memeGenerator = Frinkiac()
fileprivate var searchProvider: FrameSearchProvider<Frinkiac>!
// MARK: - Outlets -
//--------------------------------------------------------------------------
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var imageView: NSImageView!
@IBOutlet weak var searchTextField: NSSearchField!
@IBOutlet weak var textField: NSTextField!
@IBOutlet weak var shareButton: NSButton!
}
// MARK: - Extension, Initialization -
//------------------------------------------------------------------------------
extension AppDelegate {
override func awakeFromNib() {
super.awakeFromNib()
//----------------------------------------------------------------------
searchProvider = FrameSearchProvider(memeGenerator) { [weak self] in
self?.frameImage = $0.first
self?.frameImage?.caption {
do { self?.frameImage = try $0() }
catch { print(error.localizedDescription) }
}
}
}
}
// MARK: - Extension, Actions -
//------------------------------------------------------------------------------
extension AppDelegate {
@IBAction func search(_ sender: Any?) {
searchProvider.find(searchTextField.stringValue)
}
@IBAction func random(_ sender: Any?) {
searchProvider.random { [weak self] in
do { self?.frameImage = try $0() }
catch { print(error.localizedDescription) }
}
}
@IBAction func share(_ sender: Any) {
if let image = frameImage?.image, let view = sender as? NSView {
let sharingServicePicker = NSSharingServicePicker(items: [image])
sharingServicePicker.delegate = self
sharingServicePicker.show(relativeTo: view.bounds, of: view, preferredEdge: .minY)
}
}
}
// MARK: - Extension, Share Services -
//------------------------------------------------------------------------------
extension AppDelegate: NSSharingServicePickerDelegate {
}
// MARK: - Extension, Set Image -
//------------------------------------------------------------------------------
extension AppDelegate {
// MARK: - Private -
//--------------------------------------------------------------------------
fileprivate func setImage(_ image: ImageType?, text: String = "") {
DispatchQueue.main.async { [weak self] in
self?.imageView.image = image
self?.textField.stringValue = text
}
}
}
// MARK: - Extension, Control Callbacks -
// https://developer.apple.com/library/content/qa/qa1454/_index.html
//------------------------------------------------------------------------------
extension AppDelegate {
/// Sent when the text in the receiving control changes.
override func controlTextDidChange(_ obj: Notification) {
switch obj.object {
case let searchField as NSSearchField where searchField.isEqual(searchTextField):
search(obj.object)
case let textField as NSTextField where textField.isEqual(textField):
let text = textField.stringValue
frameImage?.update(text: text.memeText) { [weak self] in
do { self?.setImage(try $0()?.image, text: text) }
catch { print(error.localizedDescription) }
}
default: ()
}
}
func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
if commandSelector == #selector(NSResponder.insertNewline(_:)) {
textView.insertNewlineIgnoringFieldEditor(self)
return true
}
return false
}
}