<commit_before>//
//  StartInteractor.swift
//  AppliverySDK
//
//  Created by <NAME> on 4/10/15.
//  Copyright © 2015 Applivery S.L. All rights reserved.
//

import Foundation


protocol StartInteractorOutput {
    func forceUpdate()
    func otaUpdate()
    func feedbackEvent()
    func credentialError(message: String)
}


class StartInteractor {
    
    var output: StartInteractorOutput!
    
    private let configDataManager: PConfigDataManager
    private let globalConfig: GlobalConfig
    private let eventDetector: EventDetector
    private let sessionPersister: SessionPersister
    
    
    // MARK: Initializers
    
    init(configDataManager: PConfigDataManager = ConfigDataManager(),
         globalConfig: GlobalConfig = GlobalConfig.shared,
         eventDetector: EventDetector = ScreenshotDetector(),
         sessionPersister: SessionPersister = SessionPersister(userDefaults: UserDefaults.standard)
        ) {
        self.configDataManager = configDataManager
        self.globalConfig = globalConfig
        self.eventDetector = eventDetector
        self.sessionPersister =  sessionPersister
    }
    
    
    // MARK: Internal Methods
    
    func start() {
        logInfo("Applivery is starting...")
        logInfo("SDK Version: \(GlobalConfig.SDKVersion)")
        guard !self.globalConfig.appToken.isEmpty
            else { return self.output.credentialError(message: "You must set the app token") }
        
        self.eventDetector.listenEvent(self.output.feedbackEvent)
        
        guard !self.globalConfig.appStoreRelease else {
            return logWarn("The build is marked like an AppStore Release. Applivery won't present any update message to the user")
        }
        
        self.updateConfig()
    }
    
    func disableFeedback() {
        guard self.globalConfig.feedbackEnabled else { return }
        
        self.globalConfig.feedbackEnabled = false
        self.eventDetector.endListening()
    }
    
    
    // MARK: Private Methods
    
    fileprivate func updateConfig() {
        self.configDataManager.updateConfig { response in
            self.globalConfig.accessToken = self.sessionPersister.loadAccessToken()
            
            switch response {
            case .success(let config, let version):
                if !self.checkForceUpdate(config, version: version) {
                    self.checkOtaUpdate(config, version: version)<commit_msg>Fix: Login is requested in every session<commit_after>//
//  StartInteractor.swift
//  AppliverySDK
//
//  Created by <NAME> on 4/10/15.
//  Copyright © 2015 Applivery S.L. All rights reserved.
//

import Foundation


protocol StartInteractorOutput {
    func forceUpdate()
    func otaUpdate()
    func feedbackEvent()
    func credentialError(message: String)
}


class StartInteractor {
    
    var output: StartInteractorOutput!
    
    private let configDataManager: PConfigDataManager
    private let globalConfig: GlobalConfig
    private let eventDetector: EventDetector
    private let sessionPersister: SessionPersister
    
    
    // MARK: Initializers
    
    init(configDataManager: PConfigDataManager = ConfigDataManager(),
         globalConfig: GlobalConfig = GlobalConfig.shared,
         eventDetector: EventDetector = ScreenshotDetector(),
         sessionPersister: SessionPersister = SessionPersister(userDefaults: UserDefaults.standard)
        ) {
        self.configDataManager = configDataManager
        self.globalConfig = globalConfig
        self.eventDetector = eventDetector
        self.sessionPersister =  sessionPersister
    }
    
    
    // MARK: Internal Methods
    
    func start() {
        logInfo("Applivery is starting...")
        logInfo("SDK Version: \(GlobalConfig.SDKVersion)")
        guard !self.globalConfig.appToken.isEmpty
            else { return self.output.credentialError(message: "You must set the app token") }
        
        self.eventDetector.listenEvent(self.output.feedbackEvent)
        
        guard !self.globalConfig.appStoreRelease else {
            return logWarn("The build is marked like an AppStore Release. Applivery won't present any update message to the user")
        }
        
        self.updateConfig()
    }
    
    func disableFeedback() {
        guard self.globalConfig.feedbackEnabled else { return }
        
        self.globalConfig.feedbackEnabled = false
        self.eventDetector.endListening()
    }
    
    
    // MARK: Private Methods
    
    fileprivate func updateConfig() {
        self.globalConfig.accessToken = self.sessionPersister.loadAccessToken()
        self.configDataManager.updateConfig { response in
            switch response {
            case .success(let config, let version):
                if !self.checkForceUpdate(config, version: version) {
                    self.checkOtaUpdate(config, version: version)
{'fraction_non_alphanumeric': 0.07602459016393442, 'fraction_numerical': 0.0036885245901639345, 'mean_word_length': 2.5014347202295553, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3952105', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1248, 'n_words': 309}