Skip to content

Commit 36ecbd8

Browse files
committed
updated classes names, and added ImagePicker
1 parent 65b1604 commit 36ecbd8

File tree

5 files changed

+165
-50
lines changed

5 files changed

+165
-50
lines changed

iOSFormUtils/DropDown.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ open class DropDown: OwnView {
4848

4949
var picker: SBPickerSelector = SBPickerSelector.picker()
5050
var values: [String]!
51-
var currentTextField: FormInput!
52-
var selectedIndex: Int!
51+
var currentTextField: TextInput!
52+
public var selectedIndex: Int!
5353

5454
var mainColor: UIColor = UIColor.blue
5555

5656
var rightIcon: UIImageView!
57-
open var titleTextField: FormInput!
57+
open var titleTextField: TextInput!
5858
var mainButton: UIButton!
5959

6060
override open func loadView() {
61-
self.titleTextField = FormInput(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
61+
self.titleTextField = TextInput(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
6262
self.titleTextField.borderStyle = .none
6363
self.addSubview(self.titleTextField)
6464

@@ -174,7 +174,7 @@ open class DropDown: OwnView {
174174
}
175175

176176
func textFieldBecameFirstResponder(_ notification: Notification) {
177-
if let textField: FormInput = notification.object as? FormInput {
177+
if let textField: TextInput = notification.object as? TextInput {
178178
if textField != titleTextField {
179179
currentTextField = textField
180180
} else {

iOSFormUtils/Form.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public protocol FormDelegate {
2525

2626
- Return: The first input.
2727
*/
28-
func getFirstInput(_ form: Form) -> FormInput
28+
func getFirstInput(_ form: Form) -> TextInput
2929

3030
/*
3131
Returns the following input of a form input
@@ -35,7 +35,7 @@ public protocol FormDelegate {
3535

3636
- Return: If the current input is the last one, nil. If not, the following input.
3737
*/
38-
func getNextInput(_ form: Form, currentInput: FormInput) -> FormInput?
38+
func getNextInput(_ form: Form, currentInput: TextInput) -> TextInput?
3939
}
4040

4141
// MARK: Class
@@ -56,7 +56,7 @@ open class Form: UIScrollView {
5656
public var formDelegate: FormDelegate!
5757

5858
/// The current input which has been focused
59-
fileprivate var currentInput: FormInput! {
59+
fileprivate var currentInput: TextInput! {
6060
didSet {
6161
minimizeScrollingZone(currentInput)
6262
}
@@ -76,8 +76,8 @@ open class Form: UIScrollView {
7676
override open func addSubview(_ view: UIView) {
7777
super.addSubview(view)
7878

79-
if let input: FormInput = view as? FormInput {
80-
input.formInputDelegate = self
79+
if let input: TextInput = view as? TextInput {
80+
input.textInputDelegate = self
8181
}
8282
}
8383

@@ -133,8 +133,8 @@ open class Form: UIScrollView {
133133
private func handleInputsReturnKeys() {
134134
let inputs = getOrderedInputs()
135135
for input in inputs {
136-
if let input: FormInput = input as? FormInput, nil == input.formInputDelegate {
137-
input.formInputDelegate = self
136+
if let input: TextInput = input as? TextInput, nil == input.textInputDelegate {
137+
input.textInputDelegate = self
138138
}
139139

140140
if let textField: UITextField = input as? UITextField {
@@ -151,7 +151,7 @@ open class Form: UIScrollView {
151151
Updates the scrollview frame when keyboard appears.
152152
Scrolls to make the current field visible.
153153
*/
154-
fileprivate func minimizeScrollingZone(_ input: FormInput) {
154+
fileprivate func minimizeScrollingZone(_ input: TextInput) {
155155
if (!viewScrolledForKeyboard) {
156156
viewScrolledForKeyboard = true
157157
self.snp.updateConstraints({ (maker) in
@@ -186,7 +186,7 @@ open class Form: UIScrollView {
186186
- Parameter notification: the received notification.
187187
*/
188188
func textFieldReturnedFired(_ notification: Notification) {
189-
if let textfield = notification.object as? FormInput {
189+
if let textfield = notification.object as? TextInput {
190190
if isLastInput(textfield) {
191191
textfield.stopEditing()
192192
resetScrollingZone()
@@ -231,7 +231,7 @@ open class Form: UIScrollView {
231231
- Parameter notification: the received notification
232232
*/
233233
func textFieldBecameFirstResponder(_ notification: Notification) {
234-
if let textfield = notification.object as? FormInput {
234+
if let textfield = notification.object as? TextInput {
235235
currentInput = textfield
236236
}
237237
}
@@ -241,9 +241,9 @@ open class Form: UIScrollView {
241241

242242
- Parameter input: the input to compare
243243
*/
244-
fileprivate func isLastInput(_ input: FormInput) -> Bool {
244+
fileprivate func isLastInput(_ input: TextInput) -> Bool {
245245
if let _ = formDelegate {
246-
if let nextInput: FormInput = formDelegate.getNextInput(self, currentInput: currentInput) {
246+
if let nextInput: TextInput = formDelegate.getNextInput(self, currentInput: currentInput) {
247247
return false
248248
}
249249
}
@@ -256,10 +256,10 @@ open class Form: UIScrollView {
256256

257257
- Return: the ordered inputs
258258
*/
259-
func getOrderedInputs() -> [FormInput] {
260-
var inputs: [FormInput] = []
259+
func getOrderedInputs() -> [TextInput] {
260+
var inputs: [TextInput] = []
261261
if let _ = formDelegate {
262-
var inputToAdd: FormInput? = formDelegate.getFirstInput(self)
262+
var inputToAdd: TextInput? = formDelegate.getFirstInput(self)
263263
while nil != inputToAdd {
264264
inputs.append(inputToAdd!)
265265
inputToAdd = formDelegate.getNextInput(self, currentInput: inputToAdd!)
@@ -271,12 +271,12 @@ open class Form: UIScrollView {
271271
}
272272

273273
// MARK: Extensions
274-
extension Form: FormInputDelegate {
275-
public func didEnterEditionMode(_ input: FormInput) {
274+
extension Form: TextInputDelegate {
275+
public func didEnterEditionMode(_ input: TextInput) {
276276
DispatchQueue.main.async {
277277
self.minimizeScrollingZone(input)
278278
}
279279
}
280280

281-
public func didExitEditionMode(_ input: FormInput) {}
281+
public func didExitEditionMode(_ input: TextInput) {}
282282
}

iOSFormUtils/ImagePicker.swift

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//
2+
// ImagePicker.swift
3+
// Pods
4+
//
5+
// Created by Nicolas LELOUP on 01/09/2017.
6+
//
7+
//
8+
9+
import Foundation
10+
import UIKit
11+
12+
public protocol ImagePickerDataSource {
13+
func controllerForDisplaying() -> UIViewController
14+
}
15+
16+
public protocol ImagePickerDelegate {
17+
func imagePicker(_ picker: ImagePicker, selectedImage image: UIImage)
18+
func imagePickerReturnsNoImages(_ picker: ImagePicker)
19+
}
20+
21+
public class ImagePicker: UIButton {
22+
public var delegate: ImagePickerDelegate!
23+
public var dataSource: ImagePickerDataSource!
24+
public var selectedImage: UIImage!
25+
26+
let imagePicker: UIImagePickerController = UIImagePickerController()
27+
var actionSheet: UIAlertController!
28+
29+
public var importButtonTitle = "Import picture"
30+
public var captureButtonTitle = "Take picture"
31+
public var cancelButtonTitle = "Cancel"
32+
33+
public required init?(coder aDecoder: NSCoder) {
34+
super.init(coder: aDecoder)
35+
36+
imagePicker.delegate = self
37+
}
38+
39+
public func reloadData() {
40+
actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
41+
let cancelAction = UIAlertAction(title: cancelButtonTitle, style: .cancel) { (_) in
42+
}
43+
actionSheet.addAction(cancelAction)
44+
let importAction = UIAlertAction(title: importButtonTitle, style: .default) { (_) in
45+
self.importPhoto()
46+
}
47+
actionSheet.addAction(importAction)
48+
let captureAction = UIAlertAction(title: captureButtonTitle, style: .default) { (_) in
49+
self.capturePhoto()
50+
}
51+
actionSheet.addAction(captureAction)
52+
actionSheet.popoverPresentationController?.delegate = self
53+
54+
self.addTarget(self, action: #selector(ImagePicker.selfTouched(_:)), for: .touchUpInside)
55+
}
56+
57+
private func importPhoto() {
58+
imagePicker.sourceType = .photoLibrary
59+
presentImagePicker()
60+
}
61+
62+
private func capturePhoto() {
63+
imagePicker.sourceType = .camera
64+
presentImagePicker()
65+
}
66+
67+
private func presentImagePicker() {
68+
if let _ = dataSource {
69+
dataSource.controllerForDisplaying().present(imagePicker, animated: true)
70+
} else {
71+
print("iOSFormUtils/ImagePicker: No data source controller set for image picker displaying.")
72+
}
73+
}
74+
75+
func selfTouched(_ sender: Any) {
76+
if let _ = dataSource {
77+
dataSource.controllerForDisplaying().present(actionSheet, animated: true)
78+
} else {
79+
print("iOSFormUtils/ImagePicker: No data source controller set for action sheet displaying.")
80+
}
81+
}
82+
}
83+
84+
extension ImagePicker: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
85+
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
86+
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
87+
if let _ = delegate {
88+
self.selectedImage = pickedImage
89+
delegate.imagePicker(self, selectedImage: pickedImage)
90+
} else {
91+
print("iOSFormUtils/ImagePicker: No delegate set for picked image using.")
92+
}
93+
} else {
94+
if let _ = delegate {
95+
self.selectedImage = nil
96+
delegate.imagePickerReturnsNoImages(self)
97+
} else {
98+
print("iOSFormUtils/ImagePicker: No delegate set for image picker.")
99+
}
100+
}
101+
imagePicker.dismiss(animated: true, completion: nil)
102+
}
103+
104+
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
105+
}
106+
}
107+
108+
extension ImagePicker: UIPopoverPresentationControllerDelegate {
109+
public func prepareForPopoverPresentation(_ popoverPresentationController: UIPopoverPresentationController) {
110+
if let _ = dataSource {
111+
popoverPresentationController.sourceView = self
112+
popoverPresentationController.sourceRect = self.bounds
113+
}
114+
}
115+
}
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@ let tfReturnedNotifName = "textFieldReturned"
1313

1414
// MARK: Protocols
1515

16-
/// Delegate protocol for FormInput
17-
public protocol FormInputDelegate {
18-
func didEnterEditionMode(_ input: FormInput)
19-
func didExitEditionMode(_ input: FormInput)
16+
/// Delegate protocol for TextInput
17+
public protocol TextInputDelegate {
18+
func didEnterEditionMode(_ input: TextInput)
19+
func didExitEditionMode(_ input: TextInput)
2020
}
2121

22-
/// Data source protocol for FormInput
23-
public protocol FormInputDataSource {
24-
func applyCustomInit(_ input: FormInput)
22+
/// Data source protocol for TextInput
23+
public protocol TextInputDataSource {
24+
func applyCustomInit(_ input: TextInput)
2525
}
2626

2727
// MARK: Class
2828
/// UITextfield child class for easy form inputs handling
29-
open class FormInput: UITextField {
29+
open class TextInput: UITextField {
3030
// MARK: Class variables
31-
open var inputDataSource: FormInputDataSource!
32-
open var formInputDelegate: FormInputDelegate!
31+
open var inputDataSource: TextInputDataSource!
32+
open var textInputDelegate: TextInputDelegate!
3333
fileprivate var inputAccessory: UIView!
34-
fileprivate var validationHandler: ValidatedFormInput!
34+
fileprivate var validationHandler: ValidatedTextInput!
3535
fileprivate var limit: Int!
36-
open var validationDelegate: ValidatedFormInputDelegate!
37-
open var validationDataSource: ValidatedFormInputDataSource!
36+
open var validationDelegate: ValidatedTextInputDelegate!
37+
open var validationDataSource: ValidatedTextInputDataSource!
3838

3939
// MARK: Superclass overrides
4040
override init(frame: CGRect) {
@@ -80,10 +80,10 @@ open class FormInput: UITextField {
8080
}
8181

8282
// MARK: Extensions
83-
extension FormInput: UITextFieldDelegate {
83+
extension TextInput: UITextFieldDelegate {
8484
public func textFieldDidBeginEditing(_ textField: UITextField) {
85-
if let _ = formInputDelegate {
86-
formInputDelegate.didEnterEditionMode(self)
85+
if let _ = textInputDelegate {
86+
textInputDelegate.didEnterEditionMode(self)
8787
}
8888
if let _ = validationDelegate {
8989
validationDelegate.didExitErrorMode(self)
@@ -92,8 +92,8 @@ extension FormInput: UITextFieldDelegate {
9292
}
9393

9494
public func textFieldDidEndEditing(_ textField: UITextField) {
95-
if let _ = formInputDelegate {
96-
formInputDelegate.didExitEditionMode(self)
95+
if let _ = textInputDelegate {
96+
textInputDelegate.didExitEditionMode(self)
9797
}
9898
}
9999

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// ValidatedFormInput.swift
2+
// ValidatedTextInput.swift
33
// Pods
44
//
55
// Created by Nicolas LELOUP on 16/09/2016.
@@ -18,13 +18,13 @@ import Foundation
1818
- Phone: For french phone numbers
1919
- Date: For basic dd/mm/yyy format
2020
*/
21-
public enum ValidatedFormInputType: String {
21+
public enum ValidatedTextInputType: String {
2222
case NoValidation, NotBlank, Email, ZipCode, Phone, Date
2323
}
2424

2525
// MARK: Protocols
2626
/// Validate input protocol
27-
public protocol ValidatedFormInput {
27+
public protocol ValidatedTextInput {
2828
/**
2929
Applies validation on the input
3030

@@ -34,35 +34,35 @@ public protocol ValidatedFormInput {
3434
}
3535

3636
/// Delegate protocol for validated form
37-
public protocol ValidatedFormInputDelegate {
37+
public protocol ValidatedTextInputDelegate {
3838
/**
3939
To update the input displaying for error mode.
4040

4141
- Parameter input: The input
4242
- Parameter errorType: The error description
4343
*/
44-
func didEnterErrorMode(_ input: ValidatedFormInput, errorType: String)
44+
func didEnterErrorMode(_ input: ValidatedTextInput, errorType: String)
4545

4646
/**
4747
To update the input without the error mode.
4848

4949
- Parameter input: The input
5050
*/
51-
func didExitErrorMode(_ input: ValidatedFormInput)
51+
func didExitErrorMode(_ input: ValidatedTextInput)
5252
}
5353

5454
/// Data Source protocol for validated form
55-
public protocol ValidatedFormInputDataSource {
55+
public protocol ValidatedTextInputDataSource {
5656
/**
5757
Gives a validation type for an input.
5858

5959
- Parameter input: The input
6060
*/
61-
func validationTypeForInput(_ input: ValidatedFormInput) -> ValidatedFormInputType
61+
func validationTypeForInput(_ input: ValidatedTextInput) -> ValidatedTextInputType
6262
}
6363

6464
// MARK: Extensions
65-
extension FormInput: ValidatedFormInput {
65+
extension TextInput: ValidatedTextInput {
6666
public func validateFormat() -> Bool {
6767
if let _ = validationDataSource {
6868
switch validationDataSource.validationTypeForInput(self) {

0 commit comments

Comments
 (0)