Show notification after post published

This commit is contained in:
Matt Baer 2017-07-29 15:02:46 -04:00
parent 378d335549
commit aa97cc04b7

View File

@ -8,7 +8,7 @@
import Cocoa import Cocoa
class ViewController: NSViewController, NSTextViewDelegate { class ViewController: NSViewController, NSTextViewDelegate, NSUserNotificationCenterDelegate {
@IBOutlet var writerText: NSTextView! @IBOutlet var writerText: NSTextView!
@ -25,6 +25,8 @@ class ViewController: NSViewController, NSTextViewDelegate {
writerText.isAutomaticQuoteSubstitutionEnabled = false writerText.isAutomaticQuoteSubstitutionEnabled = false
writerText.isAutomaticDashSubstitutionEnabled = false writerText.isAutomaticDashSubstitutionEnabled = false
NSUserNotificationCenter.default.delegate = self
// Do any additional setup after loading the view. // Do any additional setup after loading the view.
writerText.delegate = self writerText.delegate = self
} }
@ -90,14 +92,32 @@ class ViewController: NSViewController, NSTextViewDelegate {
func publish() { func publish() {
saveDocument() saveDocument()
let task = Process() DispatchQueue.global(qos: .background).async {
task.launchPath = Bundle.main.executablePath! + "/../../Resources/writeas" let task = Process()
let pipe = Pipe() task.launchPath = Bundle.main.executablePath! + "/../../Resources/writeas"
task.standardInput = pipe let pipe = Pipe()
task.launch() task.standardInput = pipe
let fh: FileHandle = pipe.fileHandleForWriting task.launch()
fh.write(writerText.textStorage!.string.data(using: .utf8)!) let fh: FileHandle = pipe.fileHandleForWriting
fh.closeFile() fh.write(self.writerText.textStorage!.string.data(using: .utf8)!)
fh.closeFile()
task.waitUntilExit()
DispatchQueue.main.async {
var notification = NSUserNotification()
// All these values are optional
notification.title = "Published!"
notification.informativeText = "The link is copied — press ⌘+V to share it."
notification.soundName = NSUserNotificationDefaultSoundName
NSUserNotificationCenter.default.deliver(notification)
}
}
}
func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
return true
} }
func toggle(isNight: Bool) { func toggle(isNight: Bool) {