<commit_before>//
//  IDFYAddAndDecideDataManager.swift
//  iDecide4U
//
//  Created by <NAME> on 26/04/2015.
//  Copyright (c) 2015 <EMAIL>. All rights reserved.
//

import Foundation
import CoreData

class IDFYAddAndDecideDataManager: NSObject {
  
  private var currentList: IDFYOptionList?
  private var managedOptionListForCurrentList: IDFYManagedOptionList?
  private var optionListEntityName = "IDFYManagedOptionList"
  private let managedObjectContext = IDFYCoreDataStack.sharedCoreDataStack().managedObjectContext
  
  // This function needs to be used when the app starts and the last used list needs to be retrieved.
  func getListWithName(listName: String) -> IDFYOptionList {
    let predicate = NSPredicate(format: "name == " + listName)
    fetchListWithPredicate(predicate)
    // TODO: Is it necessary to check if that name existed?
    // The name might be empty (list was never saved, but user expects the last used list to be shown).
    transferManagedOptionListDataToCurrentList()
    return currentList!
  }

  func getCurrentList() -> IDFYOptionList {
    if nil == currentList {
      fetchListWithPredicate(nil)
      transferManagedOptionListDataToCurrentList()
    }
    return currentList!
  }
  
  func updateCurrentList(optionList: IDFYOptionList) {
    currentList = optionList
    managedOptionListForCurrentList?.name = currentList!.name
    managedOptionListForCurrentList?.options = currentList!.options
    IDFYCoreDataStack.sharedCoreDataStack().saveContext()
  }
  
  func fetchListWithPredicate(predicate: NSPredicate?) {
    let fetchRequest = NSFetchRequest(entityName: optionListEntityName)
    fetchRequest.predicate = predicate
    var error: NSError?
    let fetchResult = managedObjectContext?.executeFetchRequest(fetchRequest, error: &error)
    
    if let error = error {
      println("Error loading option list from data base: " + error.description)
    } else if let fetchResult = fetchResult where 0 < fetchResult.count {
      managedOptionListForCurrentList = fetchResult[0] as? IDFYManagedOptionList
    } else {
      managedOptionListForCurrentList = NSEntityDescription.insertNewObjectForEntityForName(optionListEntityName, inManagedObjectContext: managedObjectContext!) as? IDFYManagedOptionList
      managedOptionListForCurrentList!.name = ""
      managedOptionListForCurrentList!.options = [String]()
    }
  }
  
  func transferManagedOptionListDataToCurrentList() {
    currentList = IDFYOptionList()
    currentList!.name = managedOptionListForCurrentList!.name
    currentList!.options = managedOptionListForCurrentList!.options
  }
  
}<commit_msg>IDFY-18: Use the IDFYCoreDataStack's list name constant in IDFYAddAndDecideDataManger instead of having it's own constant there.<commit_after>//
//  IDFYAddAndDecideDataManager.swift
//  iDecide4U
//
//  Created by <NAME> on 26/04/2015.
//  Copyright (c) 2015 <EMAIL>. All rights reserved.
//

import Foundation
import CoreData

class IDFYAddAndDecideDataManager {
  
  private let managedObjectContext = IDFYCoreDataStack.sharedCoreDataStack().managedObjectContext
  private var currentList: IDFYOptionList?
  private var managedOptionListForCurrentList: IDFYManagedOptionList?
  
  // This function needs to be used when the app starts and the last used list needs to be retrieved.
  func getListWithName(listName: String) -> IDFYOptionList {
    let predicate = NSPredicate(format: "name == " + listName)
    fetchListWithPredicate(predicate)
    // TODO: Is it necessary to check if that name existed?
    // The name might be empty (list was never saved, but user expects the last used list to be shown).
    transferManagedOptionListDataToCurrentList()
    return currentList!
  }

  func getCurrentList() -> IDFYOptionList {
    if nil == currentList {
      fetchListWithPredicate(nil)
      transferManagedOptionListDataToCurrentList()
    }
    return currentList!
  }
  
  func updateCurrentList(optionList: IDFYOptionList) {
    currentList = optionList
    managedOptionListForCurrentList?.name = currentList!.name
    managedOptionListForCurrentList?.options = currentList!.options
    IDFYCoreDataStack.sharedCoreDataStack().saveContext()
  }
  
  func fetchListWithPredicate(predicate: NSPredicate?) {
    let fetchRequest = NSFetchRequest(entityName: IDFYCoreDataStack.sharedCoreDataStack().optionListEntityName)
    fetchRequest.predicate = predicate
    var error: NSError?
    let fetchResult = managedObjectContext?.executeFetchRequest(fetchRequest, error: &error)
    
    if let error = error {
      println("Error loading option list from data base: " + error.description)
    } else if let fetchResult = fetchResult where 0 < fetchResult.count {
      managedOptionListForCurrentList = fetchResult[0] as? IDFYManagedOptionList
    } else {
      managedOptionListForCurrentList = NSEntityDescription.insertNewObjectForEntityForName(IDFYCoreDataStack.sharedCoreDataStack().optionListEntityName, inManagedObjectContext: managedObjectContext!) as? IDFYManagedOptionList
      managedOptionListForCurrentList!.name = ""
      managedOptionListForCurrentList!.options = [String]()
    }
  }
  
  func transferManagedOptionListDataToCurrentList() {
    currentList = IDFYOptionList()
    currentList!.name = managedOptionListForCurrentList!.name
    currentList!.options = managedOptionListForCurrentList!.options
  }
  
}
{'fraction_non_alphanumeric': 0.07444029850746268, 'fraction_numerical': 0.005970149253731343, 'mean_word_length': 4.492827868852459, 'pattern_counts': {'":': 0, '<': 9, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1986864', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1455, 'n_words': 366}