-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create algebras for pasteboard and notification center
- Loading branch information
1 parent
59a0c93
commit a6a6d5c
Showing
9 changed files
with
188 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright © 2020 The nef Authors. | ||
|
||
import Foundation | ||
import AppKit | ||
import BowEffects | ||
|
||
protocol Notifications { | ||
func removeAllDelivered<D, E: Swift.Error>() -> EnvIO<D, E, Void> | ||
func show<D, E: Swift.Error>(title: String, body: String, options: NotificationOptions) -> EnvIO<D, E, Void> | ||
} | ||
|
||
|
||
struct NotificationOptions { | ||
let imageData: Data? | ||
let actions: [NefNotification.Action] | ||
let identifier: String | ||
|
||
init(imageData: Data? = nil, actions: [NefNotification.Action] = [], identifier: String = UUID().uuidString) { | ||
self.imageData = imageData | ||
self.actions = actions | ||
self.identifier = identifier | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright © 2020 The nef Authors. | ||
|
||
import Foundation | ||
import AppKit | ||
import BowEffects | ||
|
||
enum PasteboardError: Swift.Error { | ||
case writeToClipboard | ||
} | ||
|
||
protocol Pasteboard { | ||
func write<D>(_ image: NSImage) -> EnvIO<D, PasteboardError, Void> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright © 2020 The nef Authors. | ||
|
||
import Foundation | ||
import AppKit | ||
import UserNotifications | ||
|
||
import Bow | ||
import BowEffects | ||
|
||
class MacNotificationController: Notifications { | ||
let notificationCenter: UNUserNotificationCenter | ||
|
||
init(notificationCenter: UNUserNotificationCenter) { | ||
self.notificationCenter = notificationCenter | ||
} | ||
|
||
func removeAllDelivered<D, E: Swift.Error>() -> EnvIO<D, E, Void> { | ||
EnvIO.invoke { _ in | ||
self.notificationCenter.removeAllDeliveredNotifications() | ||
}^ | ||
} | ||
|
||
func show<D, E: Swift.Error>(title: String, body: String, options: NotificationOptions) -> EnvIO<D, E, Void> { | ||
EnvIO.invoke { _ in | ||
let content = UNMutableNotificationContent() | ||
content.title = title | ||
content.body = body | ||
content.categoryIdentifier = options.identifier | ||
|
||
if let imageData = options.imageData { | ||
content.userInfo = [NefNotification.UserInfoKey.imageData: imageData] | ||
} | ||
|
||
let category = UNNotificationCategory(identifier: options.identifier, | ||
actions: options.actions.map(\.unNotificationAction), | ||
intentIdentifiers: [], | ||
hiddenPreviewsBodyPlaceholder: "", | ||
options: .customDismissAction) | ||
|
||
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.5, repeats: false) | ||
let request = UNNotificationRequest(identifier: options.identifier, content: content, trigger: trigger) | ||
|
||
self.notificationCenter.setNotificationCategories([category]) | ||
self.notificationCenter.add(request) | ||
}^ | ||
} | ||
} | ||
|
||
// MARK: - Helpers | ||
private extension NefNotification.Action { | ||
var unNotificationAction: UNNotificationAction { | ||
.init(identifier: identifier, | ||
title: title, | ||
options: .foreground) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright © 2020 The nef Authors. | ||
|
||
import Foundation | ||
import AppKit | ||
import UserNotifications | ||
|
||
import Bow | ||
import BowEffects | ||
|
||
class MacPasteboard: Pasteboard { | ||
let pasteboard: NSPasteboard | ||
|
||
init(pasteboard: NSPasteboard) { | ||
self.pasteboard = pasteboard | ||
} | ||
|
||
func write<D>(_ image: NSImage) -> EnvIO<D, PasteboardError, Void> { | ||
EnvIO.invoke { _ in | ||
self.pasteboard.clearContents() | ||
if !self.pasteboard.writeObjects([image]) { | ||
throw PasteboardError.writeToClipboard | ||
} | ||
}^ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters