-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathComposeViewController.swift
160 lines (124 loc) · 4.65 KB
/
ComposeViewController.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// ComposeViewController.swift
// Peach
//
// Created by Stephen Radford on 12/01/2016.
// Copyright © 2016 Cocoon Development Ltd. All rights reserved.
//
import Cocoa
import PeachKit
import ImgurKit
class ComposeViewController: NSViewController {
@IBOutlet var textView: PeachComposeTextView!
@IBOutlet weak var magicButton: MagicButton!
/// The controller that we can interact with to create some magic input ✨
var magicInputController: MagicInputViewController?
/// These are the message fragments that will be posted to Peach
var messages: [Message] = []
/// The location manager for fetching the current location
let locationManager = LocationManager()
let magicWords = [
"gif": "Search for a GIF",
// "image": "Search for images",
"time": "Add current time",
"weather": "Add current weather",
"goodnight": "Good night!",
"goodmorning": "Good morning!",
// "here": "Add current location",
"mood": "How are you feeling?",
"rate": "Rate something 1-5 stars",
"battery": "Current charge %",
"dice": "Roll the dice",
"date": "Add the current date",
"movie": "Add movie",
"tv": "Add TV show",
"game": "Add video game",
// "book": "Add book",
// "events": "# of events today",
"song": "What's currently playing",
// "draw": "Draw something",
// "shout": "Say something with big words"
]
/// These are magic words that require input from the user
let requiresInput = ["gif", "image", "movie", "tv", "game", "book"]
/// These are magic words that access async services
let deferred = ["goodmorning", "weather"]
override func viewDidLoad() {
super.viewDidLoad()
textView.textStorage?.delegate = self
}
deinit {
textView.textStorage?.setAttributedString(NSAttributedString(string: ""))
textView = nil
}
// MARK: - Save Action
/**
Process the fragments, upload any images etc.
- parameter callback: Callback with optional `NSError`
*/
func save(callback: (NSError?) -> Void) {
let fragments = textView.getFragments()
var requests = 0
for frag in fragments {
// We can just add a text fragment straight into the array
if let text = frag as? String {
let msg = TextMessage()
msg.text = text
messages.append(msg)
}
// If this is a dictionary then we can do a few things
if let dict = frag as? [String:String] {
if let track = dict["track"] {
let msg = MusicMessage()
msg.title = track
messages.append(msg)
}
}
// Handle an image fragment upload
if let img = frag as? NSData {
messages.append(ImageMessage())
let index = messages.endIndex-1
requests++
Imgur.upload(img) { result, error in
requests--
if result != nil {
let msg = self.messages[index] as! ImageMessage
msg.type = (result!.type == "image/gif") ? .GIF : .Image
msg.src = result?.url
msg.width = result?.width
msg.height = result?.height
}
if requests == 0 {
self.saveMessage { error in
callback(error)
}
}
}
}
}
if requests == 0 {
saveMessage { error in
callback(error)
}
}
}
/**
Send the message to Peach
- parameter callback: Callback with optional `NSError`
*/
func saveMessage(callback: (NSError?) -> Void) {
Peach.createPost(messages) { error in
if error != nil {
callback(error)
} else {
self.view.window?.close()
}
}
}
override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "embedInput" {
magicInputController = segue.destinationController as? MagicInputViewController
magicInputController?.view.hidden = true
}
}
}