Skip to content

Commit

Permalink
use more meaningful errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jdev7 committed Jun 10, 2020
1 parent 7ec7ee8 commit fa2138c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion nef/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

let config = ClipboardConfig(clipboard: .general, notificationCenter: .current())

assembler.resolveCarbon(code: code).env()^
assembler.resolveCarbon(code: code).env()^.mapError { _ in .carbon }
.flatMap(pasteboardCarbonIO)^
.provide(config)
.unsafeRunAsync(on: .global(qos: .userInitiated)) { output in
Expand Down
26 changes: 18 additions & 8 deletions nef/Extensions/AppDelegate+Notifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ struct ClipboardConfig {
}

extension AppDelegate {
func pasteboardCarbonIO(data: Data) -> EnvIO<ClipboardConfig, AppDelegate.Error, NSImage> {
func makeImage(_ data: Data) -> IO<AppDelegate.Error, NSImage> {
data.makeImage().mapError { _ in AppDelegate.Error.carbon }
func pasteboardCarbonIO(data: Data) -> EnvIO<ClipboardConfig, Clipboard.Error, NSImage> {
func makeImage(_ data: Data) -> IO<Clipboard.Error, NSImage> {
data.makeImage().mapError { _ in .invalidData }
}

let image = EnvIO<ClipboardConfig, AppDelegate.Error, NSImage>.var()
let image = EnvIO<ClipboardConfig, Clipboard.Error, NSImage>.var()

return binding(
image <- makeImage(data).env(),
Expand All @@ -29,10 +29,12 @@ extension AppDelegate {
yield:image.get)^
}

private func writeToPasteboard(_ image: NSImage) -> EnvIO<ClipboardConfig, AppDelegate.Error, Void> {
private func writeToPasteboard(_ image: NSImage) -> EnvIO<ClipboardConfig, Clipboard.Error, Void> {
EnvIO.invoke { config in
config.clipboard.clearContents()
config.clipboard.writeObjects([image])
if !config.clipboard.writeObjects([image]) {
throw Clipboard.Error.writeToClipboard
}
}^
}
}
Expand All @@ -50,13 +52,13 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
NefNotification.center.requestAuthorization(options: [.alert, .sound]) { granted, _ in }
}

func removeOldNotifications() -> EnvIO<ClipboardConfig, AppDelegate.Error, Void> {
func removeOldNotifications() -> EnvIO<ClipboardConfig, Clipboard.Error, Void> {
EnvIO.invoke { config in
config.notificationCenter.removeAllDeliveredNotifications()
}^
}

func showNotification(title: String, body: String, imageData: Data? = nil, actions: [NefNotification.Action] = [], id: String = UUID().uuidString) -> EnvIO<ClipboardConfig,AppDelegate.Error, Void> {
func showNotification(title: String, body: String, imageData: Data? = nil, actions: [NefNotification.Action] = [], id: String = UUID().uuidString) -> EnvIO<ClipboardConfig, Clipboard.Error, Void> {
EnvIO.invoke { config in
let content = UNMutableNotificationContent()
content.title = title
Expand Down Expand Up @@ -111,6 +113,14 @@ enum NefNotification {
static var center: UNUserNotificationCenter { .current() }
}

enum Clipboard {
enum Error: Swift.Error {
case invalidData
case writeToClipboard
case carbon
}
}

extension NefNotification {
enum Action: Equatable {
case saveImage
Expand Down

0 comments on commit fa2138c

Please sign in to comment.