Large diffs are not rendered by default.

@@ -8,16 +8,266 @@
import UIKit

class OperationEditorViewController: UIViewController
protocol OperationEditorViewControllerDelegate
{
@IBAction func userPressedCancel(sender: UIBarButtonItem)
func userDidPressCancel(operationEditor: OperationEditorViewController)
func userDidPressDone(operationEditor: OperationEditorViewController)
}

class OperationEditorViewController: UIViewController, UIScrollViewDelegate
{
func userPressedCancel()
{
if delegate != nil
{
delegate!.userDidPressCancel(self)
}
}
var scrollView: UIScrollView?
var pageControl: UIPageControl?
var progressView: UIProgressView?

var initialized = false
var screens: [UITableViewController?] = []

var screenOne: DateAndTimePickerTableViewController?
var screenTwo: SelectorTableViewController?
var screenThree: SelectorTableViewController?
var screenFour: DateAndTimePickerTableViewController?
var screenFive: ValuePickerTableViewController?
var screenSix: ValuePickerTableViewController?
var screenSeven: SelectorTableViewController?
var screenEight: SelectorTableViewController?
var screenNine: DateAndTimePickerTableViewController?
var screenTen: DeathTableViewController?

var delegate: OperationEditorViewControllerDelegate?
var operation: Operation?
var doneButton: UIBarButtonItem?
var nextButton: UIBarButtonItem?

func userPressedDone()
{
println("done")
if delegate != nil
{
// IMPLEMENT THIS SHIT (save info to operatoin)
delegate!.userDidPressDone(self)
}
}

func setupScrollView() -> UIScrollView
{
var toReturn = UIScrollView(frame: CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64))
toReturn.contentInset = UIEdgeInsetsZero
toReturn.showsHorizontalScrollIndicator = false
toReturn.showsVerticalScrollIndicator = false
toReturn.pagingEnabled = true
toReturn.bounces = false
toReturn.directionalLockEnabled = true
toReturn.contentSize = CGSize(width: toReturn.frame.size.width * 10, height: toReturn.frame.size.height)
toReturn.delegate = self
return toReturn
}

func userPressedNext()
{
if pageControl!.currentPage < 9
{
var targetX = (pageControl!.currentPage + 1) * Int(scrollView!.frame.size.width)
scrollView!.setContentOffset(CGPointMake(CGFloat(targetX), 0), animated: true)
}
}

func updateIndicatorAndTitle()
{
var screen = scrollView!.contentOffset.x / scrollView!.frame.size.width
pageControl!.currentPage = Int(screen)
var progress = Float(Int(screen) + 1) / Float(10)
progressView!.setProgress(progress, animated: true)
title = "Question \((Int(screen) + 1))/10"
}

func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView)
{
updateIndicatorAndTitle()
ensureScreensInitialized()

}
@IBOutlet weak var topRightButton: UIBarButtonItem!
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {

func scrollViewDidEndDecelerating(scrollView: UIScrollView)
{
updateIndicatorAndTitle()
ensureScreensInitialized()
}

func initializeScreen(index: Int) -> UITableViewController?
{
println("initializing screen \(index)")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
switch(index)
{
case 0:
screenOne = storyboard.instantiateViewControllerWithIdentifier("DateTimePicker") as? DateAndTimePickerTableViewController
return screenOne
case 1:
screenTwo = storyboard.instantiateViewControllerWithIdentifier("Selector") as? SelectorTableViewController
return screenTwo
case 2:
screenThree = storyboard.instantiateViewControllerWithIdentifier("Selector") as? SelectorTableViewController
return screenThree
case 3:
screenFour = storyboard.instantiateViewControllerWithIdentifier("DateTimePicker") as? DateAndTimePickerTableViewController
return screenFour
case 4:
screenFive = storyboard.instantiateViewControllerWithIdentifier("ValuePicker") as? ValuePickerTableViewController
return screenFive
case 5:
screenSix = storyboard.instantiateViewControllerWithIdentifier("ValuePicker") as? ValuePickerTableViewController
return screenSix
case 6:
screenSeven = storyboard.instantiateViewControllerWithIdentifier("Selector") as? SelectorTableViewController
return screenSeven
case 7:
screenEight = storyboard.instantiateViewControllerWithIdentifier("Selector") as? SelectorTableViewController
return screenEight
case 8:
screenNine = storyboard.instantiateViewControllerWithIdentifier("DateTimePicker") as? DateAndTimePickerTableViewController
return screenNine
case 9:
screenTen = storyboard.instantiateViewControllerWithIdentifier("Death") as? DeathTableViewController
return screenTen
default:
return nil
}
}

func setupScreen(index: Int)
{
println("setting up screen \(index)")
switch(index)
{
case 1:
screenTwo!.prompt = "Type of Approach"
screenTwo!.options = ["Minimally Invasive", "Thoracotomy"]
screenTwo!.mode = .Single
break
case 2:
screenThree!.prompt = "Type of Resection"
screenThree!.options = ["Lobectomy", "Segmentectomy", "Pneumonectomy", "Broncho- or Vasculo-plastic", "Nonanatomical resection"]
screenThree!.mode = .Single
break
case 4:
screenFive!.prompt = "Blood Loss / mL"
screenFive!.min = 0
screenFive!.max = 100
screenFive!.interval = 1
screenFive!.initial = 20
break
case 5:
screenSix!.prompt = "Total Time in Hospital / days"
screenSix!.min = 0
screenSix!.max = 30
screenSix!.interval = 1
screenSix!.initial = 7
break
case 6:
screenSeven!.prompt = "Complications during hospital stay"
screenSeven!.options = ["Pneumonia", "Respiratory failure", "Empyema", "Prolonged air leak", "Pulmonary embolism", "Arrythmia", "Myocardial infarction", "Delirium", "Cerebral infarktion/bleeding"]
screenSeven!.mode = .Multiple
break
case 7:
screenEight!.prompt = "Admission to ICU"
screenEight!.options = ["Yes", "No"]
screenEight!.mode = .Single
break
default:
break
}
}

func insertIntoScrollView(index: Int)
{
var tvc = screens[index]!
tvc.view.frame = CGRectMake(scrollView!.frame.size.width * CGFloat(index), 0, scrollView!.frame.size.width, scrollView!.frame.size.height)
scrollView!.addSubview(tvc.view)
}

func ensureScreensInitialized()
{
if(!initialized)
{
initialized = true
for i in 0...9
{
if screens[i] == nil
{
initialized = false
if i <= pageControl!.currentPage + 1
{
screens[i] = initializeScreen(i)
setupScreen(i)
insertIntoScrollView(i)
}
}
}
}
}

override func loadView()
{
var screenRect = UIScreen.mainScreen().bounds
self.view = UIView(frame: CGRectMake(0, 0, screenRect.size.width, screenRect.size.height))

// scrollview
scrollView = setupScrollView()
view.addSubview(scrollView!)
}

override func viewDidLoad()
{
if operation == nil
{
if delegate != nil
{
delegate!.userDidPressCancel(self)
}
}
var screenRect = UIScreen.mainScreen().bounds

// bar buttons
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Cancel, target: self, action: "userPressedCancel")
doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "userPressedDone")
nextButton = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.Plain, target: self, action: "userPressedNext")

// page control
pageControl = UIPageControl(frame: CGRectMake(screenRect.size.width / 2.0 - 100, screenRect.size.height - 37 - 20, 200, 37))
pageControl!.numberOfPages = 10
pageControl!.currentPage = 0
pageControl!.pageIndicatorTintColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.8)
pageControl!.currentPageIndicatorTintColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8)
view.addSubview(pageControl!)

// progress view
progressView = UIProgressView(progressViewStyle: UIProgressViewStyle.Bar)
progressView!.frame = CGRectMake(0, 64, screenRect.size.width, 2)
progressView!.progress = 0
view.addSubview(progressView!)

// setup screens array
for _ in 0...9
{
screens.append(nil)
}

// update
updateIndicatorAndTitle()

// add screens
ensureScreensInitialized()
navigationItem.rightBarButtonItem = nextButton
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false

// Do any additional setup after loading the view.
}
@@ -26,16 +276,4 @@ class OperationEditorViewController: UIViewController
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}
@@ -94,15 +94,16 @@ class PatientEditorViewController: UIViewController, UITextFieldDelegate
}
else
{
println(patient!)
if(patient!.name != nil)
{
nameField.text = patient!.name
name = patient!.name
}
if(patient!.patientID != nil)
{
idField.text = patient!.name
patientID = patient!.name
idField.text = patient!.patientID
patientID = patient!.patientID
}
if(patient!.age != nil)
{
@@ -131,16 +132,17 @@ class PatientEditorViewController: UIViewController, UITextFieldDelegate

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
var newString = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
switch(textField)
{
case nameField:
name = textField.text
name = newString
break
case idField:
patientID = textField.text
patientID = newString
break
case ageField:
let processedAge = NSNumberFormatter().numberFromString(textField.text)
let processedAge = NSNumberFormatter().numberFromString(newString)
if processedAge != nil
{
age = processedAge!.integerValue
@@ -13,7 +13,6 @@ class PatientListTableViewController: UITableViewController, NSFetchedResultsCon

var searchController: UISearchController?
var results: NSFetchedResultsController?

var managedObjectContext: NSManagedObjectContext?
{
didSet
@@ -116,7 +115,6 @@ class PatientListTableViewController: UITableViewController, NSFetchedResultsCon
let patientTVC = storyboard.instantiateViewControllerWithIdentifier("PatientViewer") as PatientTableViewController
patientTVC.setup(managedObjectContext: managedObjectContext!, patient: currentPatient)
let parentNVC = self.parentViewController as UINavigationController
println("created patient tvc \(patientTVC)")
parentNVC.pushViewController(patientTVC, animated: true)
}

@@ -8,15 +8,14 @@
import UIKit

class PatientTableViewController: UITableViewController, NSFetchedResultsControllerDelegate, PatientEditorViewControllerDelegate
class PatientTableViewController: UITableViewController, NSFetchedResultsControllerDelegate, PatientEditorViewControllerDelegate, OperationEditorViewControllerDelegate
{
var patient: Patient?
var results: NSFetchedResultsController?
var managedObjectContext: NSManagedObjectContext?

func userDidPressCancel(patientEditor: PatientEditorViewController)
{
managedObjectContext!.reset()
self.dismissViewControllerAnimated(true, completion: nil)
}

@@ -26,29 +25,45 @@ class PatientTableViewController: UITableViewController, NSFetchedResultsControl
self.dismissViewControllerAnimated(true, completion: nil)
}

func userDidPressCancel(operationEditor: OperationEditorViewController)
{
managedObjectContext!.deleteObject(operationEditor.operation!)
self.dismissViewControllerAnimated(true, completion: nil)
}

func userDidPressDone(operationEditor: OperationEditorViewController)
{
patient!.addOperations(NSSet(object: operationEditor.operation!))
managedObjectContext!.save(nil)
self.dismissViewControllerAnimated(true, completion: nil)
}

func setup(managedObjectContext moc:NSManagedObjectContext, patient patientValue:Patient)
{
self.patient = patientValue
patient = patientValue
managedObjectContext = moc
let request = NSFetchRequest(entityName:"Operation")
request.predicate = NSPredicate(format: "patient = %@", patient!)
request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)]
self.results = NSFetchedResultsController(fetchRequest: request, managedObjectContext: managedObjectContext!, sectionNameKeyPath: "year", cacheName: nil)
self.results!.delegate = self
results = NSFetchedResultsController(fetchRequest: request, managedObjectContext: managedObjectContext!, sectionNameKeyPath: "year", cacheName: nil)
results!.delegate = self
}

func userPressedEdit()
func userPressedAdd()
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let patientEditorNVC = storyboard.instantiateViewControllerWithIdentifier("PatientEditor") as UINavigationController
let patientEditor = patientEditorNVC.visibleViewController as PatientEditorViewController
patientEditor.patient = patient!
self.presentViewController(patientEditorNVC, animated: true, completion:nil)
let operationEditor = OperationEditorViewController()
let entityDescript = NSEntityDescription.entityForName("Operation", inManagedObjectContext: managedObjectContext!)!
let newOperation = NSManagedObject(entity: entityDescript, insertIntoManagedObjectContext: managedObjectContext!) as Operation
newOperation.patient = patient!
operationEditor.delegate = self
operationEditor.operation = newOperation
let nvc = UINavigationController(rootViewController: operationEditor)
self.presentViewController(nvc, animated: true, completion:nil)
}

override func viewDidLoad()
{
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Edit, target: self, action: "userPressedEdit")
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "userPressedAdd")
self.title = patient!.name
results!.performFetch(nil)
super.viewDidLoad()
@@ -73,7 +88,8 @@ class PatientTableViewController: UITableViewController, NSFetchedResultsControl
return self.results!.sections!.count + 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
// #warning Incomplete method implementation.
// Return the number of rows in the section.
if section == 0
@@ -92,7 +108,6 @@ class PatientTableViewController: UITableViewController, NSFetchedResultsControl
for obj in cell.contentView.subviews
{
let view = obj as UIView
println(view.tag)
switch(view.tag)
{
case 10:
@@ -121,6 +136,20 @@ class PatientTableViewController: UITableViewController, NSFetchedResultsControl
}


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
if indexPath.section == 0
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let patientEditorNVC = storyboard.instantiateViewControllerWithIdentifier("PatientEditor") as UINavigationController
let patientEditor = patientEditorNVC.visibleViewController as PatientEditorViewController
patientEditor.patient = patient!
patientEditor.delegate = self
self.presentViewController(patientEditorNVC, animated: true, completion: nil)
tableView.deselectRowAtIndexPath(indexPath, animated: false)
}
}

/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
@@ -0,0 +1,165 @@
//
// SelectorTableViewController.swift
// HUSApp
//
// Created by Bandi Enkh-Amgalan on 3/5/15.
// Copyright (c) 2015 ucl. All rights reserved.
//
import UIKit

enum SelectorMode
{
case Multiple
case Single
}

protocol SingleSelectorTableViewControllerDelegate
{
func userDidSelectChoice(sender: SelectorTableViewController)
}

class SelectorTableViewController: UITableViewController
{
var options: [String]?
var mode = SelectorMode.Multiple
var selection: [String] = []
var prompt = ""
var delegate: SingleSelectorTableViewControllerDelegate?

override func viewDidLoad()
{
tableView.scrollEnabled = false

super.viewDidLoad()

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return options == nil ? 0 : options!.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("selectorCell", forIndexPath: indexPath) as UITableViewCell

// Configure the cell...
cell.textLabel!.text = options![indexPath.row]

return cell
}


override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String
{
return prompt
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let cell = tableView.cellForRowAtIndexPath(indexPath)
if mode == .Multiple
{
if cell!.accessoryType == .Checkmark
{
cell!.accessoryType = UITableViewCellAccessoryType.None
cell!.textLabel!.textColor = UIColor.blackColor()
selection.removeAtIndex(find(selection, options![indexPath.row])!)
}
else
{
cell!.accessoryType = .Checkmark
cell!.textLabel!.textColor = UIColor(red: 69.0/255.0, green: 174.0/255.0, blue: 172.0/255.0, alpha: 1.0)
selection.append(options![indexPath.row])
}
}
else if mode == .Single
{
for row in 0...tableView.numberOfRowsInSection(0) - 1
{
var currentCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: 0))
currentCell!.accessoryType = UITableViewCellAccessoryType.None
currentCell!.textLabel!.textColor = UIColor.blackColor()
}
cell!.accessoryType = UITableViewCellAccessoryType.Checkmark
cell!.textLabel!.textColor = UIColor(red: 69.0/255.0, green: 174.0/255.0, blue: 172.0/255.0, alpha: 1.0)

selection = [options![indexPath.row]]

if delegate != nil
{
delegate!.userDidSelectChoice(self)
}
}
tableView.deselectRowAtIndexPath(indexPath, animated: false)
println(selection)
}

/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
*/

/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/

/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/

/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the item to be re-orderable.
return true
}
*/

/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

}
@@ -0,0 +1,69 @@
//
// ValuePickerTableViewController.swift
// HUSApp
//
// Created by Bandi Enkh-Amgalan on 3/5/15.
// Copyright (c) 2015 ucl. All rights reserved.
//
import UIKit

class ValuePickerTableViewController: UITableViewController, UIPickerViewDataSource, UIPickerViewDelegate
{
var prompt = ""
var min = 0
var initial = 50
var max = 100
var interval = 1

override func viewDidLoad() {
super.viewDidLoad()
picker.dataSource = self
picker.delegate = self
picker.selectRow(Int((initial - min) / interval), inComponent: 0, animated: false)

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
{
return 1
}

func pickerView(pickerView: UIPickerView,
numberOfRowsInComponent component: Int) -> Int
{
return Int((max - min + 1) / interval)
}

func pickerView(pickerView: UIPickerView,
titleForRow row: Int,
forComponent component: Int) -> String!
{
return String(row * interval + min)
}

func pickerView(pickerView: UIPickerView,
didSelectRow row: Int,
inComponent component: Int)
{

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBOutlet weak var picker: UIPickerView!

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
return prompt
}

}