# CutBox.app

## Coverage Report

#### Created: 2024-03-12 03:40

    .../Source/App/Services/HotKeyService.swift

    Line

    Count

    Source (jump to first uncovered line)

    1

    //

    2

    //  HotKeyService.swift

    3

    //  CutBox

    4

    //

    5

    //  Created by Jason Milkins on 3/4/18.

    6

    //  Copyright © 2018-2023 ocodo. All rights reserved.

    7

    //

    8

    9

    import Magnet

    10

    import RxSwift

    11

    12

    enum HotKeyEvents {

    13

        case search

    14

    }

    15

    16

    class CutBoxHotkeyProvider: NSObject {

    17

        var testing: Bool = false

    18

        var createWasCalled: Bool = false

    19

    20

        func create(identifier: String,

    21

                    keyCombo: KeyCombo,

    22

                    target: AnyObject,

    23

                    action: Selector

    24

    3

        ) -> HotKey? {

    25

    3

            self.createWasCalled = true

    26

    3

            guard !self.testing  else { return nil }

    27

    1

            return HotKey(

    28

    1

                identifier: identifier,

    29

    1

                keyCombo: keyCombo,

    30

    1

                target: target,

    31

    1

                action: action

    32

    1

            )

    33

    3

        }

    34

    }

    35

    36

    class HotKeyService: NSObject {

    37

        static let shared = HotKeyService(hotkeyProvider: CutBoxHotkeyProvider())

    38

        var hotkeyProvider: CutBoxHotkeyProvider

    39

    26

        var searchKeyCombo = PublishSubject<KeyCombo>()

    40

    26

        var events = PublishSubject<HotKeyEvents>()

    41

    42

    26

        private let disposeBag = DisposeBag()

    43

    44

    26

        init(hotkeyProvider: CutBoxHotkeyProvider) {

    45

    26

            self.hotkeyProvider = hotkeyProvider

    46

    26

        }

    47

    48

    1

        func configure() {

    49

    1

            self.searchKeyCombo

    50

    1

                .distinctUntilChanged { $0 == $1 }

    51

    1

                .subscribe(onNext: {

    52

    1

                    self.changeGlobalToggle(keyCombo: $0)

    53

    1

                })

    54

    1

                .disposed(by: disposeBag)

    55

    1

    56

    1

            self.resetDefaultGlobalToggle()

    57

    1

        }

    58

    59

    2

        func resetDefaultGlobalToggle() {

    60

    2

            if let savedKeyCombo = KeyCombo.loadUserDefaults(identifier: Constants.cutBoxToggleKeyCombo) {

    61

    0

                self.searchKeyCombo

    62

    0

                    .onNext(savedKeyCombo)

    63

    2

            } else {

    64

    2

                self.searchKeyCombo

    65

    2

                    .onNext(Constants.defaultCutBoxToggleKeyCombo)

    66

    2

            }

    67

    2

        }

    68

    69

    1

        @objc func search(_ sender: Any) {

    70

    1

            self.events.onNext(.search)

    71

    1

        }

    72

    73

    1

        fileprivate func changeGlobalToggle(keyCombo: KeyCombo) {

    74

    1

            if let hotKey = hotkeyProvider.create(

    75

    1

                identifier: Constants.cutBoxToggleKeyCombo,

    76

    1

                keyCombo: keyCombo,

    77

    1

                target: self,

    78

    1

                action: #selector(search(_:))

    79

    1

            ) {

    80

    0

                hotKey.register()

    81

    0

                keyCombo.saveUserDefaults(identifier: Constants.cutBoxToggleKeyCombo)

    82

    1

            }

    83

    1

        }

    84

    }