Forked from Fiser12/KeyboardShortcutsEnvironment.swift
Created
November 1, 2022 09:23
-
-
Save saroar/1aa43f8cb53b554b060ee9066eabaab1 to your computer and use it in GitHub Desktop.
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
import Combine | |
import ComposableArchitecture | |
import KeyboardShortcuts | |
public struct KeyboardShortcutManager { | |
public enum Action: Equatable { | |
case onKeyDown(UUID) | |
case onKeyUp(UUID) | |
} | |
var bind: ([UUID]) -> Effect<Action, Never> = { _ in _unimplemented("bind") } | |
public func bind(uuids: [UUID]) -> Effect<Action, Never> { | |
self.bind(uuids) | |
} | |
} | |
extension KeyboardShortcutManager { | |
public struct Properties: Equatable { | |
var uuid: UUID? = nil | |
public init(uuid: UUID? = nil) { | |
self.uuid = uuid | |
} | |
} | |
} | |
extension KeyboardShortcutManager { | |
private static var subscriber: Effect<KeyboardShortcutManager.Action, Never>.Subscriber? = nil | |
public static let live: () -> KeyboardShortcutManager = { | |
var manager = KeyboardShortcutManager() | |
manager.bind = { uuids in | |
Effect.run { subscriber in | |
KeyboardShortcutManager.subscriber = subscriber | |
uuids.forEach{ uuid in | |
KeyboardShortcuts.onKeyDown(for: KeyboardShortcuts.Name(uuid.uuidString)) { | |
subscriber.send(.onKeyDown(uuid)) | |
} | |
KeyboardShortcuts.onKeyUp(for: KeyboardShortcuts.Name(uuid.uuidString)) { | |
subscriber.send(.onKeyUp(uuid)) | |
} | |
} | |
return AnyCancellable { | |
KeyboardShortcutManager.subscriber = nil | |
} | |
} | |
} | |
return manager | |
} | |
} | |
public func _unimplemented( | |
_ function: StaticString, file: StaticString = #file, line: UInt = #line | |
) -> Never { | |
fatalError( | |
""" | |
`\(function)` was called but is not implemented. Be sure to provide an implementation for | |
this endpoint when creating the mock. | |
""", | |
file: file, | |
line: line | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment