<commit_before>//
//  AnalyticsCoordinator.swift
//  Rocket.Chat
//
//  Created by <NAME> on 11/12/16.
//  Copyright © 2016 Rocket.Chat. All rights reserved.
//

import Foundation
import Fabric
import Crashlytics
import Firebase

#if BETA || DEBUG
import Instabug
#endif

let kCrashReportingDisabledKey = "kCrashReportingDisabledKey"

struct AnalyticsCoordinator: LauncherProtocol {

    static var isUsageDataLoggingDisabled: Bool {
        return UserDefaults.standard.bool(forKey: kCrashReportingDisabledKey)
    }

    static func toggleCrashReporting(disabled: Bool) {
        UserDefaults.standard.set(disabled, forKey: kCrashReportingDisabledKey)

        if disabled {
            anonymizeCrashReports()
        } else {
            AnalyticsCoordinator().prepareToLaunch(with: nil)
        }
    }

    func prepareToLaunch(with options: [UIApplicationLaunchOptionsKey: Any]?) {
        if AnalyticsCoordinator.isUsageDataLoggingDisabled {
            return
        }

        launchFabric()
        launchInstabug()
        launchFirebase()
    }

    private func launchFirebase() {
        #if RELEASE || BETA
        guard
            let path = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist"),
            NSDictionary(contentsOfFile: path) != nil
        else {
            return
        }

        FirebaseApp.configure()
        #endif
    }

    private func launchInstabug() {
        guard
            let path = Bundle.main.path(forResource: "Keys", ofType: "plist"),
            let keys = NSDictionary(contentsOfFile: path),
            let instabug = keys["Instabug"] as? String
        else {
            return
        }

        #if BETA || DEBUG
        Instabug.start(withToken: instabug, invocationEvent: .floatingButton)
        #endif
    }

    private func launchFabric() {
        Fabric.with([Crashlytics.self])

        if let currentUser = AuthManager.currentUser() {
            AnalyticsCoordinator.identifyCrashReports(withUser: currentUser)
        } else {
            AnalyticsCoordinator.anonymizeCrashReports()
        }
    }
<commit_msg>Fixed crash on toggling reports<commit_after>//
//  AnalyticsCoordinator.swift
//  Rocket.Chat
//
//  Created by <NAME> on 11/12/16.
//  Copyright © 2016 Rocket.Chat. All rights reserved.
//

import Foundation
import Fabric
import Crashlytics
import Firebase

let kCrashReportingDisabledKey = "kCrashReportingDisabledKey"

private var isFirebaseInitialized = false

struct AnalyticsCoordinator: LauncherProtocol {

    static var isUsageDataLoggingDisabled: Bool {
        return UserDefaults.standard.bool(forKey: kCrashReportingDisabledKey)
    }

    static func toggleCrashReporting(disabled: Bool) {
        UserDefaults.standard.set(disabled, forKey: kCrashReportingDisabledKey)

        if disabled {
            anonymizeCrashReports()
        } else {
            AnalyticsCoordinator().prepareToLaunch(with: nil)
        }
    }

    func prepareToLaunch(with options: [UIApplicationLaunchOptionsKey: Any]?) {
        if AnalyticsCoordinator.isUsageDataLoggingDisabled {
            return
        }

        launchFabric()
        launchFirebase()
    }

    private func launchFirebase() {
        #if RELEASE || BETA
        guard
            !isFirebaseInitialized,
            let path = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist"),
            NSDictionary(contentsOfFile: path) != nil
        else {
            return
        }

        isFirebaseInitialized = true
        FirebaseApp.configure()
        #endif
    }

    private func launchFabric() {
        Fabric.with([Crashlytics.self])

        if let currentUser = AuthManager.currentUser() {
            AnalyticsCoordinator.identifyCrashReports(withUser: currentUser)
        } else {
            AnalyticsCoordinator.anonymizeCrashReports()
        }
    }

{'fraction_non_alphanumeric': 0.08395763368638595, 'fraction_numerical': 0.005166623611469905, 'mean_word_length': 2.423519009725906, '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': '5932948', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1071, 'n_words': 229}