<commit_before>//
//  TKUserProfileHelper.swift
//  TripGo
//
//  Created by <NAME> on 9/02/2016.
//  Copyright © 2016 SkedGo Pty Ltd. All rights reserved.
//

import Foundation

private enum DefaultsKey: String {
  case SortedEnabled = "profileSortedModeIdentifiers"
  case Minimized = "profileMinimizedModeIdentifiers"
  case Hidden = "profileHiddenModeIdentifiers"
  case Disliked = "profileDislikedTransitMode"
}

public class TKUserProfileHelper: NSObject {
  public typealias Identifier = String
  
  //MARK: - Transport modes
  
  /**
  Overwrites user preferences for each non-nil value.
  */
  public class func updateTransportModesWithEnabledOrder(enabled: [Identifier]?, minimized: Set<Identifier>?, hidden: Set<Identifier>?)
  {
    let shared = NSUserDefaults.sharedDefaults()
    if let enabled = enabled {
      shared.setObject(enabled, forKey: DefaultsKey.SortedEnabled.rawValue)
    }
    if let minimized = minimized {
      shared.setObject(Array(minimized), forKey: DefaultsKey.Minimized.rawValue)
    }
    if let hidden = hidden {
      shared.setObject(Array(hidden), forKey: DefaultsKey.Hidden.rawValue)
    }
  }
  
  public class func modeIdentifierIsMinimized(modeIdentifier: Identifier) -> Bool {
    return minimizedModeIdentifiers.contains(modeIdentifier)
  }
  
  public class func setModeIdentifier(modeIdentifier: Identifier, toMinimized minimized: Bool) {
    update(minimizedModeIdentifiers, forKey: .Minimized, modeIdentifier: modeIdentifier, include: minimized)
  }
  
  public class func modeIdentifierIsHidden(modeIdentifier: Identifier) -> Bool {
    return hiddenModeIdentifiers.contains(modeIdentifier)
  }
  
  public class func setModeIdentifier(modeIdentifier: Identifier, toHidden hidden: Bool) {
    update(hiddenModeIdentifiers, forKey: .Hidden, modeIdentifier: modeIdentifier, include: hidden)
  }
  
  private class func update(identifiers: Set<Identifier>, forKey key: DefaultsKey, modeIdentifier: Identifier, include: Bool) {
    var modes = identifiers
    if include {
      modes.insert(modeIdentifier)
    } else {
      modes.remove(modeIdentifier)
    }
    NSUserDefaults.sharedDefaults().setObject(Array(modes), forKey: key.rawValue)
  }
  
  public class func orderedEnabledModeIdentifiersForAvailableModeIdentifiers(available: [Identifier]) -> [Identifier] {
    let hidden = hiddenModeIdentifiers
    var ordered = available.filter { !hidden.contains($0) }
    
    if let sorted = NSUserDefaults.sharedDefaults().objectForKey(DefaultsKey.SortedEnabled.rawValue) as? [Identifier] {
      ordered.sortInPlace { sorted.indexOf($0) < sorted.indexOf($1) }
    }
    
    return ordered
  }
  
  public class var minimizedModeIdentifiers: Set<Identifier> {
    if let minimized = NSUserDefaults.sharedDefaults().objectForKey(DefaultsKey.Minimized.rawValue) as? [Identifier] {
      return Set(minimized)
    } else {
      return [SVKTransportModeIdentifierMotorbike, SVKTransportModeIdentifierTaxi, SVKTransportModeIdentifierWalking]
    }
  }
  
  public class var hiddenModeIdentifiers: Set<Identifier> {
    if let hidden = NSUserDefaults.sharedDefaults().objectForKey(DefaultsKey.Hidden.rawValue) as? [Identifier] {
      return Set(hidden)
    } else {
      return [SVKTransportModeIdentifierSchoolBuses]
    }
  }
  <commit_msg>Fix for sorting modes<commit_after>//
//  TKUserProfileHelper.swift
//  TripGo
//
//  Created by <NAME> on 9/02/2016.
//  Copyright © 2016 SkedGo Pty Ltd. All rights reserved.
//

import Foundation

private enum DefaultsKey: String {
  case SortedEnabled = "profileSortedModeIdentifiers"
  case Minimized = "profileMinimizedModeIdentifiers"
  case Hidden = "profileHiddenModeIdentifiers"
  case Disliked = "profileDislikedTransitMode"
}

public class TKUserProfileHelper: NSObject {
  public typealias Identifier = String
  
  //MARK: - Transport modes
  
  /**
  Overwrites user preferences for each non-nil value.
  */
  public class func updateTransportModesWithEnabledOrder(enabled: [Identifier]?, minimized: Set<Identifier>?, hidden: Set<Identifier>?)
  {
    let shared = NSUserDefaults.sharedDefaults()
    if let enabled = enabled {
      shared.setObject(enabled, forKey: DefaultsKey.SortedEnabled.rawValue)
    }
    if let minimized = minimized {
      shared.setObject(Array(minimized), forKey: DefaultsKey.Minimized.rawValue)
    }
    if let hidden = hidden {
      shared.setObject(Array(hidden), forKey: DefaultsKey.Hidden.rawValue)
    }
  }
  
  public class func modeIdentifierIsMinimized(modeIdentifier: Identifier) -> Bool {
    return minimizedModeIdentifiers.contains(modeIdentifier)
  }
  
  public class func setModeIdentifier(modeIdentifier: Identifier, toMinimized minimized: Bool) {
    update(minimizedModeIdentifiers, forKey: .Minimized, modeIdentifier: modeIdentifier, include: minimized)
  }
  
  public class func modeIdentifierIsHidden(modeIdentifier: Identifier) -> Bool {
    return hiddenModeIdentifiers.contains(modeIdentifier)
  }
  
  public class func setModeIdentifier(modeIdentifier: Identifier, toHidden hidden: Bool) {
    update(hiddenModeIdentifiers, forKey: .Hidden, modeIdentifier: modeIdentifier, include: hidden)
  }
  
  private class func update(identifiers: Set<Identifier>, forKey key: DefaultsKey, modeIdentifier: Identifier, include: Bool) {
    var modes = identifiers
    if include {
      modes.insert(modeIdentifier)
    } else {
      modes.remove(modeIdentifier)
    }
    NSUserDefaults.sharedDefaults().setObject(Array(modes), forKey: key.rawValue)
  }
  
  public class func orderedEnabledModeIdentifiersForAvailableModeIdentifiers(available: [Identifier]) -> [Identifier] {
    let hidden = hiddenModeIdentifiers
    let ordered = available.filter { !hidden.contains($0) }
    
    // Once we let users sort them again, do something like this:
//    if let sorted = NSUserDefaults.sharedDefaults().objectForKey(DefaultsKey.SortedEnabled.rawValue) as? [Identifier] {
//      ordered.sortInPlace { sorted.indexOf($0) < sorted.indexOf($1) }
//    }
    
    return ordered
  }
  
  public class var minimizedModeIdentifiers: Set<Identifier> {
    if let minimized = NSUserDefaults.sharedDefaults().objectForKey(DefaultsKey.Minimized.rawValue) as? [Identifier] {
      return Set(minimized)
    } else {
      return [SVKTransportModeIdentifierMotorbike, SVKTransportModeIdentifierTaxi, SVKTransportModeIdentifierWalking]
    }
  }
  
  public class var hiddenModeIdentifiers: Set<Identifier> {
    if let hidden = NSUserDefaults.sharedDefaults().objectForKey(DefaultsKey.Hidden.rawValue) as? [Identifier] {
      return Set(hidden)
    } else {
      return [SVKTransportModeIdentifierSchoolBuses]
    }
  }
  
{'fraction_non_alphanumeric': 0.09055472263868065, 'fraction_numerical': 0.004197901049475262, 'mean_word_length': 4.219874804381846, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, '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': '7646982', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1884, 'n_words': 440}