forked from EurekaCommunity/PostalAddressRow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostalAddressRow.swift
161 lines (125 loc) · 5.6 KB
/
PostalAddressRow.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// PostalAddressRow.swift
// Eureka
//
// Created by Martin Barreto on 2/23/16.
// Copyright © 2016 Xmartlabs. All rights reserved.
//
import Foundation
import Eureka
// MARK: Type
/**
* Protocol to be implemented by PostalAddress types.
*/
public protocol PostalAddressType: Equatable {
var street: String? { get set }
var state: String? { get set }
var postalCode: String? { get set }
var city: String? { get set }
var country: String? { get set }
}
public func == <T: PostalAddressType>(lhs: T, rhs: T) -> Bool {
return lhs.street == rhs.street && lhs.state == rhs.state && lhs.postalCode == rhs.postalCode && lhs.city == rhs.city && lhs.country == rhs.country
}
/// Row type for PostalAddressRow
public struct PostalAddress: PostalAddressType {
public var street: String?
public var state: String?
public var postalCode: String?
public var city: String?
public var country: String?
public init(){}
public init(street: String?, state: String?, postalCode: String?, city: String?, country: String?) {
self.street = street
self.state = state
self.postalCode = postalCode
self.city = city
self.country = country
}
}
public protocol PostalAddressFormatterConformance: class {
var streetUseFormatterDuringInput: Bool { get set }
var streetFormatter: Formatter? { get set }
var stateUseFormatterDuringInput: Bool { get set }
var stateFormatter: Formatter? { get set }
var postalCodeUseFormatterDuringInput: Bool { get set }
var postalCodeFormatter: Formatter? { get set }
var cityUseFormatterDuringInput: Bool { get set }
var cityFormatter: Formatter? { get set }
var countryUseFormatterDuringInput: Bool { get set }
var countryFormatter: Formatter? { get set }
}
public protocol PostalAddressRowConformance: PostalAddressFormatterConformance {
var postalAddressPercentage : CGFloat? { get set }
var placeholderColor : UIColor? { get set }
var streetPlaceholder : String? { get set }
var statePlaceholder : String? { get set }
var postalCodePlaceholder : String? { get set }
var cityPlaceholder : String? { get set }
var countryPlaceholder : String? { get set }
}
//MARK: PostalAddressRow
open class _PostalAddressRow<Cell: CellType>: Row<Cell>, PostalAddressRowConformance, KeyboardReturnHandler where Cell: BaseCell, Cell: PostalAddressCellConformance {
/// Configuration for the keyboardReturnType of this row
open var keyboardReturnType : KeyboardReturnTypeConfiguration?
/// The proportional width that the text fields on the left should have compared to those on the right.
open var postalAddressPercentage: CGFloat?
/// The textColor for the textField's placeholder
open var placeholderColor : UIColor?
/// The placeholder for the street textField
open var streetPlaceholder : String?
/// The placeholder for the state textField
open var statePlaceholder : String?
/// The placeholder for the zip textField
open var postalCodePlaceholder : String?
/// The placeholder for the city textField
open var cityPlaceholder : String?
/// The placeholder for the country textField
open var countryPlaceholder : String?
/// A formatter to be used to format the user's input for street
open var streetFormatter: Formatter?
/// A formatter to be used to format the user's input for state
open var stateFormatter: Formatter?
/// A formatter to be used to format the user's input for zip
open var postalCodeFormatter: Formatter?
/// A formatter to be used to format the user's input for city
open var cityFormatter: Formatter?
/// A formatter to be used to format the user's input for country
open var countryFormatter: Formatter?
/// If the formatter should be used while the user is editing the street.
open var streetUseFormatterDuringInput: Bool
/// If the formatter should be used while the user is editing the state.
open var stateUseFormatterDuringInput: Bool
/// If the formatter should be used while the user is editing the zip.
open var postalCodeUseFormatterDuringInput: Bool
/// If the formatter should be used while the user is editing the city.
open var cityUseFormatterDuringInput: Bool
/// If the formatter should be used while the user is editing the country.
open var countryUseFormatterDuringInput: Bool
public required init(tag: String?) {
streetUseFormatterDuringInput = false
stateUseFormatterDuringInput = false
postalCodeUseFormatterDuringInput = false
cityUseFormatterDuringInput = false
countryUseFormatterDuringInput = false
super.init(tag: tag)
}
}
/// A PostalAddress valued row where the user can enter a postal address.
public final class PostalAddressRow: _PostalAddressRow<PostalAddressCell>, RowType {
public required init(tag: String? = nil) {
super.init(tag: tag)
// load correct bundle for cell nib file
var bundle: Bundle
if let bundleWithIdentifier = Bundle(identifier: "com.xmartlabs.PostalAddressRow") {
// Example or Carthage
bundle = bundleWithIdentifier
} else {
// Cocoapods
let podBundle = Bundle(for: PostalAddressRow.self)
let bundleURL = podBundle.url(forResource: "PostalAddressRow", withExtension: "bundle")
bundle = Bundle(url: bundleURL!)!
}
cellProvider = CellProvider<PostalAddressCell>(nibName: "PostalAddressCell", bundle: bundle)
}
}