<commit_before>//
//  DeviceManager.swift
//  SmartCar
//
//  Created by <NAME> on 4/27/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import RxSwift
import RxBluetoothKit
import CoreBluetooth
import NSObject_Rx

public class DeviceManager: NSObject {
    public static let manager = DeviceManager()
    
    let bluetoothManager = BluetoothManager(queue: .global(qos: .userInitiated), options: [CBCentralManagerOptionRestoreIdentifierKey : "carSmartsSenderRestoreIdent" as AnyObject])
    
    public override init() {
        super.init()
        
        bluetoothManager.listenOnRestoredState().subscribe(onNext: { restoredState in
            
            restoredState.peripherals.map(CarSmartsDevice.init(with:)).forEach { device in
                self.knownDevices.value.append(device)
            }
            
            print("Restored: \(restoredState)")
        }).disposed(by: rx_disposeBag)
    }
    
    //MARK: scan switch
    public var attemptScan: Observable<BluetoothState> {
        
        let rx_state = bluetoothManager.rx_state
                
        let scan = bluetoothManager.scanForPeripherals(withServices: CarSmartsService.services).do(onNext: {
            let device = CarSmartsDevice(with: $0)
            
            self.knownDevices.value.append(device)
        })
        
        // kick off scan when subscribed, and dispose when state subscription is disposed
        return rx_state.flatMapLatest { state -> Observable<BluetoothState> in
            
            if state == .poweredOn {
                // if powered on, return state, but also start the scan
                return Observable.using({
                    CompositeDisposable(disposables: [scan.subscribe()])
                }) { _ in Observable.never().startWith(state) } // Observable that never completes but sends out state
            }
            
            // if anything other than .poweredOn, just return the state
            return .just(state)
        }
    }
    
    //MARK: devices
    public var knownDevices = Variable<[CarSmartsDevice]>([])
    
}
<commit_msg>Change restoration to append devices all at once, rather than iterating<commit_after>//
//  DeviceManager.swift
//  SmartCar
//
//  Created by <NAME> on 4/27/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import RxSwift
import RxBluetoothKit
import CoreBluetooth
import NSObject_Rx

public class DeviceManager: NSObject {
    public static let manager = DeviceManager()
    
    let bluetoothManager = BluetoothManager(queue: .global(qos: .userInitiated), options: [CBCentralManagerOptionRestoreIdentifierKey : "carSmartsSenderRestoreIdent" as AnyObject])
    
    public override init() {
        super.init()
        
        bluetoothManager.listenOnRestoredState().subscribe(onNext: { restoredState in
            
            let restoredDevices = restoredState.peripherals.map(CarSmartsDevice.init(with:))
            
            self.knownDevices.value += restoredDevices
            
            print("Restored: \(restoredState)")
        }).disposed(by: rx_disposeBag)
    }
    
    //MARK: scan switch
    public var attemptScan: Observable<BluetoothState> {
        
        let rx_state = bluetoothManager.rx_state
                
        let scan = bluetoothManager.scanForPeripherals(withServices: CarSmartsService.services).do(onNext: {
            let device = CarSmartsDevice(with: $0)
            
            self.knownDevices.value.append(device)
        })
        
        // kick off scan when subscribed, and dispose when state subscription is disposed
        return rx_state.flatMapLatest { state -> Observable<BluetoothState> in
            
            if state == .poweredOn {
                // if powered on, return state, but also start the scan
                return Observable.using({
                    CompositeDisposable(disposables: [scan.subscribe()])
                }) { _ in Observable.never().startWith(state) } // Observable that never completes but sends out state
            }
            
            // if anything other than .poweredOn, just return the state
            return .just(state)
        }
    }
    
    //MARK: devices
    public var knownDevices = Variable<[CarSmartsDevice]>([])
    
}

{'fraction_non_alphanumeric': 0.08625086946440992, 'fraction_numerical': 0.004637143519591932, 'mean_word_length': 2.1604395604395603, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6246390', 'n_tokens_mistral': 1155, 'n_tokens_neox': 1110, 'n_words': 289}