<commit_before>//
//  SubstitutionManager.swift
//  X Schedule
//
//  Created by <NAME> on 6/11/15.
//  Copyright (c) 2015 <NAME>.
//

import Foundation

public class SubstitutionManager {
    
    static let substitutionsKey: String = "substitutions"
    
    static let defaultSubstitutions: [(block: String, className: String)] =
    [("A", "A"),
     ("B", "B")]
    
    public class func substituteItemsInSchedule(schedule: Schedule, substitutions: [(block: String, className: String)]) -> Schedule {
        var outputSchedule: Schedule = schedule
        for (var i=0; i<schedule.items.count; i++) {
            for sub in substitutions {
                if outputSchedule.items[i].blockName == sub.block {
                    outputSchedule.items[i].blockName = sub.className
                }
            }
        }
        
        return outputSchedule
    }
    
    public class func saveSubstitutions(subs: [(block: String, className: String)]) {
        var arrayVersion: [[String]] = convertTupleToArray(subs)
        
        let defaults = NSUserDefaults.standardUserDefaults()
        defaults.setObject(arrayVersion, forKey: substitutionsKey)
    }
    private class func convertTupleToArray(tuple: [(block: String, className: String)]) -> [[String]] {
        var output: [[String]] = []
        for item in tuple {
            var tmpArray = ["",""]
            tmpArray[0] = item.block
            tmpArray[1] = item.className
            output.append(tmpArray)
        }
        
        return output
    }
    
    public class func loadSubstitutions() -> [(block: String, className: String)] {
        let defaults = NSUserDefaults.standardUserDefaults()
        var object: AnyObject? = defaults.objectForKey(substitutionsKey)
        
        var tupleVersion: [(block: String, className: String)]
        if let array = object as? [[String]] {
            tupleVersion = convertArrayToTuple(array)
        } else {
            tupleVersion = defaultSubstitutions
        }
        
        return tupleVersion
    }
    private class func convertArrayToTuple(array: [[String]]) -> [(block: String, className: String)] {
        var output: [(block: String, className: String)] = []
        for item in array {
            output.append(block: item[0], className: item[1])
        }
        
        return output
    }
}<commit_msg>Added methods to SubstitutionManager to handle toggling of substitutions.<commit_after>//
//  SubstitutionManager.swift
//  X Schedule
//
//  Created by <NAME> on 6/11/15.
//  Copyright (c) 2015 <NAME>.
//

import Foundation

public class SubstitutionManager {
    
    static let substitutionsKey: String = "substitutions"
    static let subSwitchKey: String = "substitutionsEnabled"
    
    static let defaultSubstitutions: [(block: String, className: String)] =
    [("A", "A"),
     ("B", "B")]
    
    public class func substituteItemsInScheduleIfEnabled(schedule: Schedule, substitutions: [(block: String, className: String)]) -> Schedule {
        var output: Schedule
        if (getEnabled()) {
            output = substituteItemsInSchedule(schedule, substitutions: substitutions)
        } else {
            output = schedule
        }
        
        return output
    }
    public class func substituteItemsInSchedule(schedule: Schedule, substitutions: [(block: String, className: String)]) -> Schedule {
        var outputSchedule: Schedule = schedule
        for (var i=0; i<schedule.items.count; i++) {
            for sub in substitutions {
                if outputSchedule.items[i].blockName == sub.block {
                    outputSchedule.items[i].blockName = sub.className
                }
            }
        }
        
        return outputSchedule
    }
    
    public class func saveSubstitutions(subs: [(block: String, className: String)]) {
        var arrayVersion: [[String]] = convertTupleToArray(subs)
        
        let defaults = NSUserDefaults.standardUserDefaults()
        defaults.setObject(arrayVersion, forKey: substitutionsKey)
    }
    private class func convertTupleToArray(tuple: [(block: String, className: String)]) -> [[String]] {
        var output: [[String]] = []
        for item in tuple {
            var tmpArray = ["",""]
            tmpArray[0] = item.block
            tmpArray[1] = item.className
            output.append(tmpArray)
        }
        
        return output
    }
    
    public class func loadSubstitutions() -> [(block: String, className: String)] {
        let defaults = NSUserDefaults.standardUserDefaults()
        var object: AnyObject? = defaults.objectForKey(substitutionsKey)
        
        var tupleVersion: [(block: String, className: String)]
        if let array = object as? [[String]] {
            tupleVersion = convertArrayToTuple(array)
        } else {
            tupleVersion = defaultSubstitutions
        }
        
        return tupleVersion
    }
    private class func convertArrayToTuple(array: [[String]]) -> [(block: String, className: String)] {
        var output: [(block: String, className: String)] = []
        for item in array {
            output.append(block: item[0], className: item[1])
        }
        
        return output
    }
    
    public class func setEnabled(bool: Bool) {
        let defaults = NSUserDefaults.standardUserDefaults()
        defaults.setBool(bool, forKey: subSwitchKey)
    }
    public class func getEnabled() -> Bool {
        let defaults = NSUserDefaults.standardUserDefaults()
        var object: Bool = defaults.boolForKey(subSwitchKey)
        
        return object
    }
    
}
{'fraction_non_alphanumeric': 0.10741004631278946, 'fraction_numerical': 0.004987531172069825, 'mean_word_length': 2.2513028372900985, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '2256299', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1442, 'n_words': 397}