Created
December 8, 2014 09:05
-
-
Save Cellane/03899cd786bd1229460d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class FilterManager { | |
class var shared: FilterManager { | |
struct Static { | |
static let instance: FilterManager = FilterManager() | |
} | |
return Static.instance | |
} | |
func createFilterString(formFields: [UIView], defaultCondition: String) -> String { | |
var filterStrings = [String]() | |
filterStrings.append(processTextFields(formFields)) | |
filterStrings.append(processRadioButtons(formFields)) | |
filterStrings.append(processInvalidDateSwitches(formFields)) | |
let filterString = "\(defaultCondition) \(filterStrings.reduce(String(), +))" | |
return filterString | |
} | |
private func processTextFields(formFields: [UIView]) -> String { | |
return (formFields.filter { field in | |
if let textField = field as? UITextField { | |
return textField.type? == "like" && !textField.text.trimWhitespace().isEmpty | |
} | |
return false | |
} as [UITextField]).reduce(String()) { filter, textField in | |
return "\(filter)and \(textField.name) like '%\(textField.text.trimWhitespace())%' " | |
} | |
} | |
private func processRadioButtons(formFields: [UIView]) -> String { | |
return (formFields.filter { field in | |
if let button = field as? UIButton { | |
return button.type == "radio" && button.selected | |
} | |
return false | |
} as [UIButton]).reduce(String()) { filter, button in | |
if button.value == VariableManager.Generic.AllValues { | |
let allValues = (formFields.filter { field in | |
if let innerButton = field as? UIButton { | |
return innerButton.name == button.name && innerButton.selected == false | |
} | |
return false | |
} as [UIButton]).reduce(String()) { values, button in | |
return "\(values)\(button.name) = '\(button.value)' or " | |
} | |
return "\(filter)and (\(allValues.removeCharactersFromEnd(4)))" | |
} else { | |
return "\(filter)and \(button.name) = '\(button.value)' " | |
} | |
} | |
} | |
private func processInvalidDateSwitches(formFields: [UIView]) -> String { | |
return (formFields.filter { field in | |
if let switchView = field as? UISwitch { | |
return switchView.type == "invalidDate" && !switchView.on | |
} | |
return false | |
} as [UISwitch]).reduce(String()) { filter, switchView in | |
return "\(filter)and (\(switchView.name) > \(DateManager.shared.isoDateFromDate(NSDate())) or \(switchView.name) = null) " | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment