<commit_before>//
//  PHAssetHelper.swift
//  Memories
//
//  Created by <NAME> on 17/01/2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import Photos

public class PHAssetHelper {
    
    private static var datesMapCache : [Date : Int]?
    private static var eariestAssetYearCache : Int?
    
    private let userDefaults : UserDefaults
    
    struct Key {
        static let assetSourceTypesKey = "assetSourceTypes"
        static let includeCurrentYearKey = "includeCurrentYear"
    }

    public static let sourceTypesChangedNotification = "PHAssetHelperSourceTypesChangedNotification"
    
    public init() {
        userDefaults = UserDefaults.init(suiteName: "group.com.luacheia.memories")!
        let types: PHAssetSourceType = [.typeUserLibrary, .typeiTunesSynced, .typeCloudShared]
        userDefaults.register(defaults: [Key.assetSourceTypesKey : NSNumber(value: types.rawValue),
                                         Key.includeCurrentYearKey: true])
    }

    public var assetSourceTypes: PHAssetSourceType {
        get {
            return PHAssetSourceType(rawValue: (userDefaults.value(forKey: Key.assetSourceTypesKey) as! NSNumber).uintValue)
        }
        
        set {
            userDefaults.setValue(NSNumber(value: newValue.rawValue), forKey: Key.assetSourceTypesKey)
        }
    }
    
    public var includeCurrentYear: Bool {
        get {
            return userDefaults.bool(forKey: Key.includeCurrentYearKey)
        }
         
        set {
            userDefaults.set(newValue, forKey: Key.includeCurrentYearKey)
        }
    }
    
    private func earliestAssetYear() -> Int {
        guard PHAssetHelper.eariestAssetYearCache == nil else {
            return PHAssetHelper.eariestAssetYearCache!
        }
        
        // default to 2000
        var year = 2000
        
        let fetchResult = allAssetsInDateOrder()
        if let firstAsset = fetchResult.firstObject as PHAsset? {
            if let firstDate = firstAsset.creationDate {
                year = firstDate.year
            }
        }
        
        year = max(year, 1900)
        
        PHAssetHelper.eariestAssetYearCache = year;
        return year
    }
    
    public func refreshDatesMapCache() {
        PHAssetHelper.datesMapCache = nil
        
        let operation = BlockOperation {
            PHAssetHelper().datesMap()
        }
        
        let queue = OperationQueue()
        queue.addOperation(operation)
    }
    
    @discardableResult public func datesMap() -> [Date : Int] {
        guard PHAssetHelper.datesMapCache == nil else {
            return PHAssetHelper.datesMapCache!<commit_msg>Fixes bug when first asset creationDate has a year in the future (which in itself must be an Apple bug) :-/<commit_after>//
//  PHAssetHelper.swift
//  Memories
//
//  Created by <NAME> on 17/01/2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import Photos

public class PHAssetHelper {
    
    private static var datesMapCache : [Date : Int]?
    private static var eariestAssetYearCache : Int?
    
    private let userDefaults : UserDefaults
    
    struct Key {
        static let assetSourceTypesKey = "assetSourceTypes"
        static let includeCurrentYearKey = "includeCurrentYear"
    }

    public static let sourceTypesChangedNotification = "PHAssetHelperSourceTypesChangedNotification"
    
    public init() {
        userDefaults = UserDefaults.init(suiteName: "group.com.luacheia.memories")!
        let types: PHAssetSourceType = [.typeUserLibrary, .typeiTunesSynced, .typeCloudShared]
        userDefaults.register(defaults: [Key.assetSourceTypesKey : NSNumber(value: types.rawValue),
                                         Key.includeCurrentYearKey: true])
    }

    public var assetSourceTypes: PHAssetSourceType {
        get {
            return PHAssetSourceType(rawValue: (userDefaults.value(forKey: Key.assetSourceTypesKey) as! NSNumber).uintValue)
        }
        
        set {
            userDefaults.setValue(NSNumber(value: newValue.rawValue), forKey: Key.assetSourceTypesKey)
        }
    }
    
    public var includeCurrentYear: Bool {
        get {
            return userDefaults.bool(forKey: Key.includeCurrentYearKey)
        }
         
        set {
            userDefaults.set(newValue, forKey: Key.includeCurrentYearKey)
        }
    }
    
    private func earliestAssetYear() -> Int {
        guard PHAssetHelper.eariestAssetYearCache == nil else {
            return PHAssetHelper.eariestAssetYearCache!
        }
        
        var year = 1990
        
        let fetchResult = allAssetsInDateOrder()
        if let firstAsset = fetchResult.firstObject as PHAsset? {
            if let firstDate = firstAsset.creationDate {
                year = firstDate.year
            }
        }
        
        year = max(min(1990, year), 1900)
        
        PHAssetHelper.eariestAssetYearCache = year;
        return year
    }
    
    public func refreshDatesMapCache() {
        PHAssetHelper.datesMapCache = nil
        
        let operation = BlockOperation {
            PHAssetHelper().datesMap()
        }
        
        let queue = OperationQueue()
        queue.addOperation(operation)
    }
    
    @discardableResult public func datesMap() -> [Date : Int] {
        guard PHAssetHelper.datesMapCache == nil else {
            return PHAssetHelper.datesMapCache!
{'fraction_non_alphanumeric': 0.07678177810433505, 'fraction_numerical': 0.00881704628949302, 'mean_word_length': 2.132911392405063, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2469730', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1429, 'n_words': 337}