Skip to content

Instantly share code, notes, and snippets.

@premedios
Last active August 29, 2015 14:16
Show Gist options
  • Save premedios/b8d3b447426aba6517a8 to your computer and use it in GitHub Desktop.
Save premedios/b8d3b447426aba6517a8 to your computer and use it in GitHub Desktop.
//
// BusNumberListViewController.swift
// ETA
//
// Created by Pedro Remedios on 16/01/15.
// Copyright (c) 2015 Pedro Remedios. All rights reserved.
//
import UIKit
import CoreData
class BusNumberListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, ImportOperationDelegate {
@IBOutlet weak var tableView: UITableView!
var progressHUD: M13ProgressHUD? = nil
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.progressHUD = M13ProgressHUD(andShowWithProgressView: M13ProgressViewRing(), progress: 0.5, indeterminate: false, status: "", mask: M13ProgressHUDMaskTypeSolidColor , inView: self.tableView)
self.progressHUD!.progressViewSize = CGSize(width: 100, height: 100)
var progressViewRing = self.progressHUD!.progressView as M13ProgressViewRing
progressViewRing.progressRingWidth = 5.0
self.progressHUD!.progressView = progressViewRing
var importOperation = ImportOperation(database: ETADatabase(), delegate: self)
// Here is where I update the progress HUD
importOperation.progressCallback = { (progress) -> () in
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
self.progressHUD!.setProgress(progress, animated: true)
self.navigationController!.setProgress(progress, animated: true)
self.navigationController!.showProgress()
if progress >= 1 {
self.progressHUD!.performAction(M13ProgressViewActionSuccess, animated: true)
self.progressHUD!.hide(true)
self.navigationController!.finishProgress()
}
})
}
importOperation.start()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
return cell
}
func didGetNumberOfLines(numberOfLines: Int) {
self.progressHUD!.status = "Importing \(numberOfLines) records"
self.navigationController!.setProgressTitle("Importing \(numberOfLines) records")
}
}
//
// ImportOperation.swift
// ETA
//
// Created by Pedro Remedios on 22/02/15.
// Copyright (c) 2015 Pedro Remedios. All rights reserved.
//
import UIKit
import CoreData
typealias ProgressCallback = (_:CGFloat) -> Void
class ImportOperation: NSOperation {
let etaDatabase:ETADatabase!
let delegate: ImportOperationDelegate? = nil
var progressCallback:ProgressCallback? = nil
init(database:ETADatabase, delegate:ImportOperationDelegate) {
self.etaDatabase = database
self.delegate = delegate
}
override func main() {
importCSV()
}
func importCSV() {
let temposDeEsperaPath = NSBundle.mainBundle().pathForResource("tempos_de_espera", ofType: "csv")
let fileContents = NSString(contentsOfFile: temposDeEsperaPath!, encoding: NSUTF8StringEncoding , error: nil)
let fileLines = fileContents!.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) as [String]
self.delegate?.didGetNumberOfLines(fileLines.count)
var privateContext = self.etaDatabase.createPrivateContext()
var error: NSError?
for (index, line) in enumerate(fileLines) {
let lineContents = line.componentsSeparatedByString(",") as [String]
privateContext.performBlock({ () -> Void in
var bus = Bus(entity:NSEntityDescription.entityForName("Bus", inManagedObjectContext: privateContext)!, insertIntoManagedObjectContext:privateContext)
bus.carreira = lineContents[0]
bus.sentido = lineContents[1]
bus.numero = lineContents[2]
bus.paragem = lineContents[3]
bus.localizacao = lineContents[4]
bus.destino = lineContents[5]
bus.codigo = lineContents[6]
//println("\(CGFloat(index)) \(CGFloat(fileLines.count))")
// Here is where I set the progress to pass back to the callback
let importProgress:CGFloat = CGFloat(index) / CGFloat(fileLines.count)
println(importProgress)
//println("index: \(index), count: \(fileLines.count)")
if let callback = self.progressCallback {
callback(importProgress)
}
})
}
privateContext.save(&error)
if let callback = self.progressCallback {
callback(1)
}
}
}
protocol ImportOperationDelegate {
func didGetNumberOfLines(numbeOfLines: Int)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment