# CutBox.app

## Coverage Report

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

    .../Source/App/Services/LoginItemsService.swift

    Line

    Count

    Source (jump to first uncovered line)

    1

    //

    2

    //  LoginItemsService.swift

    3

    //  CutBox

    4

    //

    5

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

    6

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

    7

    //

    8

    9

    import Foundation

    10

    import ServiceManagement

    11

    import RxCocoa

    12

    import RxSwift

    13

    14

    class LoginItemsService {

    15

        static var testing = false

    16

    17

        static let shared = LoginItemsService()

    18

    19

    1

        private let helperAppIdentifier = "info.ocodo.CutBoxHelper" as CFString

    20

        private let autoLoginEnabledKey = "CutBoxAutoLoginEnabled"

    21

    1

        private let disposeBag = DisposeBag()

    22

    23

        let autoLoginEnabled: BehaviorRelay<NSControl.StateValue>

    24

    25

    1

        init() {

    26

    1

            let saved: NSControl.StateValue = NSControl.StateValue(rawValue:

    27

    1

                UserDefaults.standard.integer(forKey: autoLoginEnabledKey)

    28

    1

            )

    29

    1

            autoLoginEnabled = BehaviorRelay(value: saved)

    30

    1

    31

    1

            autoLoginEnabled

    32

    1

                .asObservable()

    33

    50

                .subscribe(onNext: { self.setAutoLogin($0) })

    34

    1

                .disposed(by: disposeBag)

    35

    1

        }

    36

    37

    50

        private func setAutoLogin(_ state: NSControl.StateValue) {

    38

    50

            if !Self.testing {

    39

    2

                if SMLoginItemSetEnabled(self.helperAppIdentifier, state.rawValue == 1) {

    40

    0

                    UserDefaults.standard.set(state, forKey: autoLoginEnabledKey)

    41

    0

                    NSLog("\(state.rawValue == 1 ? "Added" : "Removed") login item \(helperAppIdentifier)")

    42

    2

                } else {

    43

    2

                    NSLog("Failed to configure login item \(helperAppIdentifier).")

    44

    2

                }

    45

    50

            }

    46

    50

        }

    47

    }