Created
May 15, 2015 11:05
-
-
Save Cellane/70a0c940182f3b84475c 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
// | |
// FilterManager.swift | |
// sPort | |
// | |
// Created by Milan Vít on 05.12.14. | |
// Copyright (c) 2014 AiNeuron. All rights reserved. | |
// | |
import Foundation | |
class FilterManager { | |
class var shared: FilterManager { | |
struct Static { | |
static let instance: FilterManager = FilterManager() | |
} | |
return Static.instance | |
} | |
internal func createFilterString(formFields: [UIView], defaultCondition: String) -> String { | |
var filterStrings = [String]() | |
filterStrings.append(processTextFields(formFields)) | |
filterStrings.append(processRadioButtons(formFields)) | |
filterStrings.append(processEnumButtons(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 processEnumButtons(formFields: [UIView]) -> String { | |
return (formFields.filter { field in | |
if let button = field as? UIButton { | |
return button.type == "enum" && button.value != VariableManager.Generic.AllValues && button.selected | |
} | |
return false | |
} as [UIButton]).reduce(String()) { filter, button in | |
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