<commit_before>//
//  ContentManager.swift
//  MetabolicCompass
//
//  Created by Inaiur on 5/11/16.
//  Copyright © 2016 <NAME>, <NAME>. All rights reserved.
//

import UIKit
import Async
import HealthKit
import MetabolicCompassKit

class ContentManager: NSObject {

    static let ContentDidUpdateNotification = "ContentDidUpdateNotification"

    private var aggregateFetchTask : Async? = nil    // Background task to fetch population aggregates.
    var isBackgroundWorkActive = false

    internal func initializeBackgroundWork() {
        if (!AccountManager.shared.isLogged() ||
            !AccountManager.shared.isAuthorized) {
            return
        }

        Async.main() {
            if (self.isBackgroundWorkActive) {
                return
            }
            self.fetchInitialAggregates()
            self.fetchRecentSamples()
            self.isBackgroundWorkActive = true
            HealthManager.sharedManager.registerObservers()
            AccountManager.shared.withHKCalAuth {
                HealthManager.sharedManager.collectDataForCharts()
            }
        }
    }<commit_msg>Added a flag to ensure we register observers only on the first time that we initialize background work in the ContentManager. This prevents re-registration (and forced upload availability checking) every time we switch screens.<commit_after>//
//  ContentManager.swift
//  MetabolicCompass
//
//  Created by Inaiur on 5/11/16.
//  Copyright © 2016 <NAME>, <NAME>. All rights reserved.
//

import UIKit
import Async
import HealthKit
import MetabolicCompassKit

class ContentManager: NSObject {

    static let ContentDidUpdateNotification = "ContentDidUpdateNotification"

    private var aggregateFetchTask : Async? = nil    // Background task to fetch population aggregates.
    var isBackgroundWorkActive = false
    var isObservationActive = false

    internal func initializeBackgroundWork() {
        if (!AccountManager.shared.isLogged() ||
            !AccountManager.shared.isAuthorized) {
            return
        }

        Async.main() {
            if (self.isBackgroundWorkActive) {
                return
            }
            self.fetchInitialAggregates()
            self.fetchRecentSamples()
            self.isBackgroundWorkActive = true
            if !self.isObservationActive {
                HealthManager.sharedManager.registerObservers()
                self.isObservationActive = true
            }
            AccountManager.shared.withHKCalAuth {
                HealthManager.sharedManager.collectDataForCharts()
            }
        }
    }
{'fraction_non_alphanumeric': 0.07316134000770119, 'fraction_numerical': 0.006931074316519061, 'mean_word_length': 2.330769230769231, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3102729', 'n_tokens_mistral': 661, 'n_tokens_neox': 619, 'n_words': 166}