Skip to content

Commit

Permalink
Major refactor and simplification in value pickers
Browse files Browse the repository at this point in the history
  • Loading branch information
bphenriques committed Aug 8, 2015
1 parent d9a5fbf commit 7e7159e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 57 deletions.
4 changes: 0 additions & 4 deletions malaria-ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
20FC56EB1B72C91300D51801 /* UserSettingsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20FC56EA1B72C91300D51801 /* UserSettingsManager.swift */; };
20FC56F31B72C9AF00D51801 /* UIStoryboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20FC56F21B72C9AF00D51801 /* UIStoryboard.swift */; };
20FC56F51B72C9CF00D51801 /* UIColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20FC56F41B72C9CF00D51801 /* UIColor.swift */; };
20FC56F71B72C9E700D51801 /* UIPickView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20FC56F61B72C9E700D51801 /* UIPickView.swift */; };
20FC56F91B72C9FB00D51801 /* UIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20FC56F81B72C9FB00D51801 /* UIView.swift */; };
20FC56FB1B72CA1200D51801 /* UITabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20FC56FA1B72CA1200D51801 /* UITabBarController.swift */; };
20FC56FD1B72CA2800D51801 /* Globals.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20FC56FC1B72CA2800D51801 /* Globals.swift */; };
Expand Down Expand Up @@ -224,7 +223,6 @@
20FC56EA1B72C91300D51801 /* UserSettingsManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UserSettingsManager.swift; path = "malaria-ios/ViewControllers/SupportingFiles/UserSettingsManager.swift"; sourceTree = SOURCE_ROOT; };
20FC56F21B72C9AF00D51801 /* UIStoryboard.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UIStoryboard.swift; path = SupportingFiles/Extensions/UIStoryboard.swift; sourceTree = "<group>"; };
20FC56F41B72C9CF00D51801 /* UIColor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UIColor.swift; path = SupportingFiles/Extensions/UIColor.swift; sourceTree = "<group>"; };
20FC56F61B72C9E700D51801 /* UIPickView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UIPickView.swift; path = SupportingFiles/Extensions/UIPickView.swift; sourceTree = "<group>"; };
20FC56F81B72C9FB00D51801 /* UIView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UIView.swift; path = SupportingFiles/Extensions/UIView.swift; sourceTree = "<group>"; };
20FC56FA1B72CA1200D51801 /* UITabBarController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UITabBarController.swift; path = SupportingFiles/Extensions/UITabBarController.swift; sourceTree = "<group>"; };
20FC56FC1B72CA2800D51801 /* Globals.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Globals.swift; path = SupportingFiles/Extensions/Globals.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -555,7 +553,6 @@
20FC56FC1B72CA2800D51801 /* Globals.swift */,
20FC56FA1B72CA1200D51801 /* UITabBarController.swift */,
20FC56F81B72C9FB00D51801 /* UIView.swift */,
20FC56F61B72C9E700D51801 /* UIPickView.swift */,
20FC56F41B72C9CF00D51801 /* UIColor.swift */,
20FC56F21B72C9AF00D51801 /* UIStoryboard.swift */,
20EF684F1B72DF1600286A0F /* UIButton.swift */,
Expand Down Expand Up @@ -934,7 +931,6 @@
20FC57311B72CA8D00D51801 /* CVScrollDirection.swift in Sources */,
20C7188A1B763573007424F6 /* ToolbarWithDone.swift in Sources */,
200A18C31B64F48E00EC2A65 /* PillsStatsViewController.swift in Sources */,
20FC56F71B72C9E700D51801 /* UIPickView.swift in Sources */,
200C72C21B40368600998100 /* CollectionPosts.swift in Sources */,
20EA06DB1B2234DA00D6840E /* Model.xcdatamodeld in Sources */,
20FC56F51B72C9CF00D51801 /* UIColor.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PlanTripViewController: UIViewController {
var arrivalDay = NSDate()
var items = [(String, Bool)]()

private var toolBar: UIToolbar!
private var toolBar: ToolbarWithDone!

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -48,13 +48,13 @@ class PlanTripViewController: UIViewController {
historyTextField.inputAccessoryView = toolBar

//Setting up departure
departureDatePickerview = TimePickerView(selectCallback: {(date: NSDate) in
departureDatePickerview = TimePickerView(pickerMode: .Date, startDate: departureDay, selectCallback: {(date: NSDate) in
self.updateDeparture(date)
})
departure.inputAccessoryView = toolBar

//Setting up arrival date picker
arrivalDatePickerview = TimePickerView(selectCallback: {(date: NSDate) in
arrivalDatePickerview = TimePickerView(pickerMode: .Date, startDate: arrivalDay, selectCallback: {(date: NSDate) in
self.updateArrival(date)
})
arrival.inputAccessoryView = toolBar
Expand All @@ -79,8 +79,8 @@ class PlanTripViewController: UIViewController {
updateDeparture(departureDay)

//update input views
arrival.inputView = arrivalDatePickerview.generateInputView(.Date, startDate: arrivalDay)
departure.inputView = departureDatePickerview.generateInputView(.Date, startDate: departureDay)
arrival.inputView = toolBar.generateInputView(arrivalDatePickerview)
departure.inputView = toolBar.generateInputView(departureDatePickerview)

//update history
prepareHistoryValuePicker()
Expand All @@ -91,7 +91,7 @@ class PlanTripViewController: UIViewController {
self.updateLocation(object)
})

historyTextField.inputView = tripLocationHistoryPickerViewer.generateInputView()
historyTextField.inputView = toolBar.generateInputView(tripLocationHistoryPickerViewer)
}

func selectItemsCallback(medicine: Medicine.Pill, listItems: [(String, Bool)]){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SetupScreenViewController : UIViewController{
private var medicinePicker: MedicinePickerView!
private var timePickerview: TimePickerView!

private var toolBar: UIToolbar!
private var toolBar: ToolbarWithDone!

//mangagers
private var viewContext: NSManagedObjectContext!
Expand All @@ -39,15 +39,15 @@ class SetupScreenViewController : UIViewController{
medicinePicker = MedicinePickerView(context: viewContext, selectCallback: {(object: String) in
self.medicineName.text = object
})
medicineName.inputView = medicinePicker.generateInputView()
medicineName.inputView = toolBar.generateInputView(medicinePicker)
medicineName.inputAccessoryView = toolBar

//Setting up DatePickerView
timePickerview = TimePickerView(selectCallback: {(date: NSDate) in
timePickerview = TimePickerView(pickerMode: .Time, startDate: pillReminderNotificationTime, selectCallback: {(date: NSDate) in
self.pillReminderNotificationTime = date
self.refreshDate()
})
reminderTime.inputView = timePickerview.generateInputView(.Time, startDate: pillReminderNotificationTime)
reminderTime.inputView = toolBar.generateInputView(timePickerview)
reminderTime.inputAccessoryView = toolBar

medicineName.text = medicinePicker.selectedValue
Expand Down

This file was deleted.

14 changes: 14 additions & 0 deletions malaria-ios/ViewControllers/SupportingFiles/ToolbarWithDone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import UIKit
public class ToolbarWithDone: UIToolbar {
public var viewsWithToolbar: [UIView] = []

private let DoneButtonHeight: CGFloat = 40.0
private let DoneButtonWidth: CGFloat = 100.0

public init (viewsWithToolbar: [UIView]) {
self.viewsWithToolbar = viewsWithToolbar
super.init(frame: CGRectZero)
Expand All @@ -17,6 +20,17 @@ public class ToolbarWithDone: UIToolbar {
super.init(coder: aDecoder)
}

public func generateInputView(view: UIView, inputHeight: CGFloat = 200) -> UIView{
let screenWidth = UIScreen.mainScreen().bounds.width
let inputView = UIView(frame: CGRectMake(0, 0, screenWidth, inputHeight + DoneButtonHeight))
let inputViewSize = view.frame.size.width

view.frame.origin.x = screenWidth*0.5 - inputViewSize*0.5
inputView.addSubview(view)

return inputView
}

//sender is a UIBarButtonItem
internal func dismissInputView(sender: UIView){
viewsWithToolbar.map({ $0.endEditing(true) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,27 @@ import UIKit

public class TimePickerView: UIDatePicker{
private var selectCallback: ((date: NSDate) -> ())!
private var DoneButtonHeight : CFloat { get { return 40.0 } }
private var DoneButtonWidth : CFloat { get { return 100.0 } }
private var ValuePickerHeight : CGFloat { get { return CGFloat(200) + CGFloat(DoneButtonHeight) } }

/// initialization
/// :param: `UIDatePickerMode`: picker mode .Date or .Time
/// :param: `NSDate`: start date
/// :param: `(object: String) -> ()`: on selection callback. Usually to change a view element content
public init(selectCallback: (date: NSDate) -> ()){
public init(pickerMode: UIDatePickerMode, startDate: NSDate, selectCallback: (date: NSDate) -> ()){
super.init(frame: CGRectZero)
self.selectCallback = selectCallback

let screenWidth = UIScreen.mainScreen().bounds.width
let inputViewWidth = self.frame.size.width

self.frame = CGRectMake(screenWidth*0.5 - inputViewWidth*0.5, 0, 0, 0)
self.datePickerMode = pickerMode
self.date = startDate
self.addTarget(self, action: "onDateChange:", forControlEvents: .AllEvents)
}

public required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

/// Generates inputview
/// :param: `UIDatePickerMode`: The mode of the picker
/// :param: `NSDate`: the starting day
/// :returns: `UIView`: the generated view
public func generateInputView(pickerMode: UIDatePickerMode, startDate: NSDate) -> UIView{
let screenWidth = UIScreen.mainScreen().bounds.width
let inputView = UIView(frame: CGRectMake(0, 0, screenWidth, CGFloat(ValuePickerHeight)))
let inputViewSize = self.frame.size.width

var datePickerView = UIDatePicker(frame: CGRectMake(screenWidth*0.5 - inputViewSize*0.5, CGFloat(DoneButtonHeight), 0, 0))
datePickerView.datePickerMode = pickerMode
datePickerView.date = startDate
datePickerView.addTarget(self, action: "onDateChange:", forControlEvents: .AllEvents)

inputView.addSubview(datePickerView) // add date picker to UIView

return inputView
}

internal func onDateChange(sender: UIDatePicker){
selectCallback(date: sender.date)
}
Expand Down

0 comments on commit 7e7159e

Please sign in to comment.