<commit_before>//
//  CoreDataClient.swift
//  TripPlanner
//
//  Created by <NAME> on 7/21/15.
//  Copyright © 2015 Make School. All rights reserved.
//

import Foundation
import CoreData

typealias TripServerID = String

class CoreDataClient {
  
  let context: NSManagedObjectContext
  let stack: CoreDataStack
  
  init(stack: CoreDataStack) {
    self.stack = stack
    self.context = stack.managedObjectContext
  }
  
  func allTrips() -> [Trip] {
    return try! self.context.executeFetchRequest(NSFetchRequest(entityName: "Trip")) as! [Trip]
  }
  
  func allWaypoints() -> [Waypoint] {
    return try! self.context.executeFetchRequest(NSFetchRequest(entityName: "Waypoint")) as! [Waypoint]
  }
  
  func tripWithServerID(serverID: String) -> Trip? {
    let fetchRequest = NSFetchRequest(entityName: "Trip")
    fetchRequest.predicate = NSPredicate(format: "serverID = %@", serverID)
    let trips = try! self.context.executeFetchRequest(fetchRequest) as! [Trip]
    
    if (trips.count > 0) {
      return trips[0]
    } else {
      return nil
    }
  }
  
  func waypointWithServerID(serverID: String) -> Waypoint? {
    let fetchRequest = NSFetchRequest(entityName: "Waypoint")
    fetchRequest.predicate = NSPredicate(format: "serverID = %@", serverID)
    let waypoints = try! self.context.executeFetchRequest(fetchRequest) as! [Waypoint]
    
    if (waypoints.count > 0) {
      return waypoints[0]
    } else {
      return nil
    }
  }
  
  func unsyncedTrips() -> [Trip] {
    let fetchRequest = NSFetchRequest(entityName: "Trip")
    fetchRequest.predicate = NSPredicate(format: "serverID = nil")
    let unsyncedTrips = try! self.context.executeFetchRequest(fetchRequest) as! [Trip]
   
    return unsyncedTrips
  }
  
  func unsyncedTripDeletions() -> [TripServerID] {
    let syncInfo = syncInformation()
    return syncInfo.unsyncedDeletedTripsArray
  }
  
  func tripsThatChangedSince(timestamp: NSTimeInterval) -> [Trip] {
    let fetchRequest = NSFetchRequest(entityName: "Trip")
    fetchRequest.predicate = NSPredicate(format: "lastUpdate > %f", timestamp)
    let updatedTrips = try! self.context.executeFetchRequest(fetchRequest) as! [Trip]
    
    return updatedTrips
  }
  
  func markTripAsDeleted(trip: Trip) {
    let syncInfo = syncInformation()
    
    // remember deleted trips for synchronization
    if let tripServerID = trip.serverID {
      var deletedTripsArray = syncInfo.unsyncedDeletedTripsArray
      deletedTripsArray.append(tripServerID)
      syncInfo.unsyncedDeletedTripsArray = deletedTripsArray
    }
    
    // delete trip locally
    context.deleteObject(trip)<commit_msg>[CoreDataClient] Fix: Delete in main context<commit_after>//
//  CoreDataClient.swift
//  TripPlanner
//
//  Created by <NAME> on 7/21/15.
//  Copyright © 2015 Make School. All rights reserved.
//

import Foundation
import CoreData

typealias TripServerID = String

class CoreDataClient {
  
  let context: NSManagedObjectContext
  let stack: CoreDataStack
  
  init(stack: CoreDataStack) {
    self.stack = stack
    self.context = stack.managedObjectContext
  }
  
  func allTrips() -> [Trip] {
    return try! self.context.executeFetchRequest(NSFetchRequest(entityName: "Trip")) as! [Trip]
  }
  
  func allWaypoints() -> [Waypoint] {
    return try! self.context.executeFetchRequest(NSFetchRequest(entityName: "Waypoint")) as! [Waypoint]
  }
  
  func tripWithServerID(serverID: String) -> Trip? {
    let fetchRequest = NSFetchRequest(entityName: "Trip")
    fetchRequest.predicate = NSPredicate(format: "serverID = %@", serverID)
    let trips = try! self.context.executeFetchRequest(fetchRequest) as! [Trip]
    
    if (trips.count > 0) {
      return trips[0]
    } else {
      return nil
    }
  }
  
  func waypointWithServerID(serverID: String) -> Waypoint? {
    let fetchRequest = NSFetchRequest(entityName: "Waypoint")
    fetchRequest.predicate = NSPredicate(format: "serverID = %@", serverID)
    let waypoints = try! self.context.executeFetchRequest(fetchRequest) as! [Waypoint]
    
    if (waypoints.count > 0) {
      return waypoints[0]
    } else {
      return nil
    }
  }
  
  func unsyncedTrips() -> [Trip] {
    let fetchRequest = NSFetchRequest(entityName: "Trip")
    fetchRequest.predicate = NSPredicate(format: "serverID = nil")
    let unsyncedTrips = try! self.context.executeFetchRequest(fetchRequest) as! [Trip]
   
    return unsyncedTrips
  }
  
  func unsyncedTripDeletions() -> [TripServerID] {
    let syncInfo = syncInformation()
    return syncInfo.unsyncedDeletedTripsArray
  }
  
  func tripsThatChangedSince(timestamp: NSTimeInterval) -> [Trip] {
    let fetchRequest = NSFetchRequest(entityName: "Trip")
    fetchRequest.predicate = NSPredicate(format: "lastUpdate > %f", timestamp)
    let updatedTrips = try! self.context.executeFetchRequest(fetchRequest) as! [Trip]
    
    return updatedTrips
  }
  
  func markTripAsDeleted(trip: Trip) {
    let syncInfo = syncInformation()
    
    // remember deleted trips for synchronization
    if let tripServerID = trip.serverID {
      var deletedTripsArray = syncInfo.unsyncedDeletedTripsArray
      deletedTripsArray.append(tripServerID)
      syncInfo.unsyncedDeletedTripsArray = deletedTripsArray
    }
    
    // delete trip locally
    let tripInMainContext = context.objectWithID(trip.objectID) as! Trip
    context.deleteObject(tripInMainContext)
{'fraction_non_alphanumeric': 0.1026545387042881, 'fraction_numerical': 0.004826434007796547, 'mean_word_length': 3.5583756345177666, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '3300310', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1713, 'n_words': 369}