From aa97cc04b7e3e9a8f4874900bafb6911445f0b5d Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Sat, 29 Jul 2017 15:02:46 -0400 Subject: [PATCH] Show notification after post published --- writeas/ViewController.swift | 38 +++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/writeas/ViewController.swift b/writeas/ViewController.swift index a166bdb..b34c5ca 100644 --- a/writeas/ViewController.swift +++ b/writeas/ViewController.swift @@ -8,7 +8,7 @@ import Cocoa -class ViewController: NSViewController, NSTextViewDelegate { +class ViewController: NSViewController, NSTextViewDelegate, NSUserNotificationCenterDelegate { @IBOutlet var writerText: NSTextView! @@ -25,6 +25,8 @@ class ViewController: NSViewController, NSTextViewDelegate { writerText.isAutomaticQuoteSubstitutionEnabled = false writerText.isAutomaticDashSubstitutionEnabled = false + NSUserNotificationCenter.default.delegate = self + // Do any additional setup after loading the view. writerText.delegate = self } @@ -90,14 +92,32 @@ class ViewController: NSViewController, NSTextViewDelegate { func publish() { saveDocument() - let task = Process() - task.launchPath = Bundle.main.executablePath! + "/../../Resources/writeas" - let pipe = Pipe() - task.standardInput = pipe - task.launch() - let fh: FileHandle = pipe.fileHandleForWriting - fh.write(writerText.textStorage!.string.data(using: .utf8)!) - fh.closeFile() + DispatchQueue.global(qos: .background).async { + let task = Process() + task.launchPath = Bundle.main.executablePath! + "/../../Resources/writeas" + let pipe = Pipe() + task.standardInput = pipe + task.launch() + let fh: FileHandle = pipe.fileHandleForWriting + 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) {