Skip to content

Commit

Permalink
inject dependencies into processNotification
Browse files Browse the repository at this point in the history
  • Loading branch information
jdev7 committed Jun 14, 2020
1 parent b130087 commit f327cf2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
9 changes: 6 additions & 3 deletions nef/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,23 @@ class AppDelegate: NSObject, NSApplicationDelegate {
private func notificationDidFinishLaunching(userInfo: [String: Any], action: String) {
emptyDidFinishLaunching()

processNotification(userInfo, action: action).env()
let config = NotificationConfig(workspace: .shared, openPanel: assembler.resolveOpenPanel())

processNotification(userInfo, action: action)
.flatMap(showClipboardFile)^
.provide(NSWorkspace.shared)
.provide(config)
.unsafeRunAsync(on: .global(qos: .userInitiated)) { _ in self.terminate() }
}

// MARK: Helper methods
private func carbonIO(code: String) -> IO<AppDelegate.Error, URL> {
let panel = assembler.resolveOpenPanel()
let image = IO<AppDelegate.Error, Data>.var()
let output = IO<AppDelegate.Error, URL>.var()

return binding(
image <- self.assembler.resolveCarbon(code: code),
output <- image.get.persistImage(command: .carbon(code: code)),
output <- image.get.persist(command: .carbon(code: code)).provide(panel).mapError { _ in .carbon },
yield: output.get)^
}

Expand Down
22 changes: 14 additions & 8 deletions nef/Extensions/AppDelegate+Notifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ struct ClipboardConfig {
let notificationCenter: UNUserNotificationCenter
}

struct NotificationConfig {
let workspace: NSWorkspace
let openPanel: OpenPanel
}

extension AppDelegate {
func pasteboardCarbonIO(data: Data) -> EnvIO<ClipboardConfig, Clipboard.Error, NSImage> {
func makeImage(_ data: Data) -> IO<Clipboard.Error, NSImage> {
Expand Down Expand Up @@ -95,27 +100,28 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
completionHandler()
}

func processNotification(_ userInfo: [String: Any], action: String) -> IO<NefNotification.Error, NefNotification.Response> {
guard let image = userInfo[NefNotification.UserInfoKey.imageData] as? Data else { return IO.raiseError(.noImageData)^ }
func processNotification(_ userInfo: [String: Any], action: String) -> EnvIO<NotificationConfig, NefNotification.Error, NefNotification.Response> {
guard let image = userInfo[NefNotification.UserInfoKey.imageData] as? Data else { return EnvIO.raiseError(.noImageData)^ }

switch action {
case NefNotification.Action.saveImage.identifier:
return image
.persistImage(command: .pasteboardCarbon())
.persist(command: .pasteboardCarbon())
.mapError { _ in .persistImage }
.contramap(\.openPanel)
.map { .saveImage($0) }^
case UNNotificationDismissActionIdentifier:
return IO.pure(.dismiss)^
return EnvIO.pure(.dismiss)^
default:
return IO.raiseError(.unsupportedAction)^
return EnvIO.raiseError(.unsupportedAction)^
}
}

func showClipboardFile(response: NefNotification.Response) -> EnvIO<NSWorkspace, NefNotification.Error, Void> {
func showClipboardFile(response: NefNotification.Response) -> EnvIO<NotificationConfig, NefNotification.Error, Void> {
guard case let .saveImage(url) = response else { return EnvIO.pure(())^ }

return EnvIO.invoke { workspace in
workspace.activateFileViewerSelecting([url])
return EnvIO.invoke { config in
config.workspace.activateFileViewerSelecting([url])
}^
}
}
Expand Down
18 changes: 10 additions & 8 deletions nef/Extensions/Data+IO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ extension Data {
}
}

func persistImage(command: Command, assembler: Assembler = Assembler()) -> IO<AppDelegate.Error, URL> {
assembler.resolveOpenPanel().writableFolder(create: true).use { folder in
let output = IO<OpenPanelError, URL>.var()
return binding(
output <- folder.outputURL(command: command),
|<-self.writeIO(to: output.get).mapError { _ in .unknown },
yield: output.get)
}^.mapError { _ in .carbon }^
func persist(command: Command) -> EnvIO<OpenPanel, OpenPanelError, URL> {
EnvIO.accessM { panel in
panel.writableFolder(create: true).use { folder in
let output = IO<OpenPanelError, URL>.var()
return binding(
output <- folder.outputURL(command: command),
|<-self.writeIO(to: output.get).mapError { _ in .unknown },
yield: output.get)
}^.env()
}^
}
}

0 comments on commit f327cf2

Please sign in to comment.