<commit_before>//
//  PhoneIdRefreshMonitor
//  phoneid_iOS
//
//  Copyright 2015 <NAME>
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//


import Foundation

class PhoneIdRefreshMonitor{
    var timer:NSTimer?
    var notificationCenter:NSNotificationCenter!
    weak var phoneId:PhoneIdService!
    var reachability = Reachability.reachabilityForInternetConnection()
    
    var isRunning:Bool
    let maxRefreshRetryCount = 5
    let refreshRetrySleepSeconds = 2.0
    var refreshRetryCount:Int
    
    
    init(phoneIdService:PhoneIdService, notificationCenter:NSNotificationCenter){
        phoneId = phoneIdService
        isRunning = false
        refreshRetryCount = 0
        self.notificationCenter = notificationCenter
        
        notificationCenter.addObserver(self, selector: "willEnterForeground", name: UIApplicationWillEnterForegroundNotification, object: nil)
        notificationCenter.addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
    }
    
    convenience init(phoneIdService:PhoneIdService){
        self.init(phoneIdService:phoneIdService, notificationCenter: NSNotificationCenter.defaultCenter())
    }
    
    deinit{
        stop()
        notificationCenter.removeObserver(self)
    }
    
    func start()-> Void {
        
        guard !isRunning else {return}
        
        isRunning = true
        
        guard phoneId.token != nil else {return}
        
        let token = phoneId.token!
        
        guard token.isValid() else {return}
        
        startReachabilityMonitoring()
        
        resetTimer(token)
        
    }
    
    func startReachabilityMonitoring(){
        reachability.whenReachable = {[unowned self] reachability in
            self.start()
        }
        reachability.whenUnreachable = {[unowned self] reachability in
           self.stop()
        }
        reachability.startNotifier()
    }
    
    func resetTimer(token:TokenInfo){
        refreshRetryCount = 0
        timer?.invalidate()
        timer = nil
        
        let fireTime =  token.expirationTime!.timeIntervalSince1970 - NSTimeInterval(token.expirationPeriod!/3)<commit_msg>Fixed crash on reset timer<commit_after>//
//  PhoneIdRefreshMonitor
//  phoneid_iOS
//
//  Copyright 2015 <NAME>
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//


import Foundation

class PhoneIdRefreshMonitor{
    var timer:NSTimer?
    var notificationCenter:NSNotificationCenter!
    weak var phoneId:PhoneIdService!
    var reachability = Reachability.reachabilityForInternetConnection()
    
    var isRunning:Bool
    let maxRefreshRetryCount = 5
    let refreshRetrySleepSeconds = 2.0
    var refreshRetryCount:Int
    
    
    init(phoneIdService:PhoneIdService, notificationCenter:NSNotificationCenter){
        phoneId = phoneIdService
        isRunning = false
        refreshRetryCount = 0
        self.notificationCenter = notificationCenter
        
        notificationCenter.addObserver(self, selector: "willEnterForeground", name: UIApplicationWillEnterForegroundNotification, object: nil)
        notificationCenter.addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
    }
    
    convenience init(phoneIdService:PhoneIdService){
        self.init(phoneIdService:phoneIdService, notificationCenter: NSNotificationCenter.defaultCenter())
    }
    
    deinit{
        stop()
        notificationCenter.removeObserver(self)
    }
    
    func start()-> Void {
        
        guard !isRunning else {return}
        
        isRunning = true
        
        guard phoneId.token != nil else {return}
        
        let token = phoneId.token!
        
        guard token.isValid() else {return}
        
        startReachabilityMonitoring()
        
        resetTimer(token)
        
    }
    
    func startReachabilityMonitoring(){
        reachability.whenReachable = {[unowned self] reachability in
            self.start()
        }
        reachability.whenUnreachable = {[unowned self] reachability in
           self.stop()
        }
        reachability.startNotifier()
    }
    
    func resetTimer(token:TokenInfo){
        refreshRetryCount = 0
        timer?.invalidate()
        timer = nil
        
        var fireTime = 0.0
        
        if let expirationTime = token.expirationTime {
            fireTime = expirationTime.timeIntervalSince1970 - NSTimeInterval(token.expirationPeriod!/3)
        }
{'fraction_non_alphanumeric': 0.07562117392870003, 'fraction_numerical': 0.0068419157364061935, 'mean_word_length': 2.5838709677419356, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '923431', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1435, 'n_words': 387}