Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 67 additions & 21 deletions Sources/PostalAddressCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import Eureka
* Protocol for cells that contain a postal address
*/
public protocol PostalAddressCellConformance {
var streetTextField: UITextField? { get }
var street1TextField: UITextField? { get }
var street2TextField: UITextField? { get }
var poboxTextField: UITextField? { get }
var stateTextField: UITextField? { get }
var postalCodeTextField: UITextField? { get }
var cityTextField: UITextField? { get }
Expand All @@ -25,7 +27,9 @@ public protocol PostalAddressCellConformance {
/// Base class that implements the cell logic for the PostalAddressRow
open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAddressCellConformance, UITextFieldDelegate {

@IBOutlet open var streetTextField: UITextField?
@IBOutlet open var street1TextField: UITextField?
@IBOutlet open var street2TextField: UITextField?
@IBOutlet open var poboxTextField: UITextField?
@IBOutlet open var firstSeparatorView: UIView?
@IBOutlet open var stateTextField: UITextField?
@IBOutlet open var postalCodeTextField: UITextField?
Expand All @@ -47,12 +51,16 @@ open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAd

open override func awakeFromNib() {
super.awakeFromNib()
textFieldOrdering = [streetTextField, postalCodeTextField, cityTextField, stateTextField, countryTextField]
textFieldOrdering = [street1TextField, street2TextField, poboxTextField, postalCodeTextField, cityTextField, stateTextField, countryTextField]
}

deinit {
streetTextField?.delegate = nil
streetTextField?.removeTarget(self, action: nil, for: .allEvents)
street1TextField?.delegate = nil
street1TextField?.removeTarget(self, action: nil, for: .allEvents)
street2TextField?.delegate = nil
street2TextField?.removeTarget(self, action: nil, for: .allEvents)
poboxTextField?.delegate = nil
poboxTextField?.removeTarget(self, action: nil, for: .allEvents)
stateTextField?.delegate = nil
stateTextField?.removeTarget(self, action: nil, for: .allEvents)
postalCodeTextField?.delegate = nil
Expand Down Expand Up @@ -97,8 +105,14 @@ open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAd
textField?.autocapitalizationType = .words
}

streetTextField?.text = row.value?.street
streetTextField?.keyboardType = .asciiCapable
street1TextField?.text = row.value?.street1
street1TextField?.keyboardType = .asciiCapable

street2TextField?.text = row.value?.street2
street2TextField?.keyboardType = .asciiCapable

poboxTextField?.text = row.value?.pobox
poboxTextField?.keyboardType = .asciiCapable

stateTextField?.text = row.value?.state
stateTextField?.keyboardType = .asciiCapable
Expand All @@ -113,7 +127,9 @@ open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAd
countryTextField?.keyboardType = .asciiCapable

if let rowConformance = row as? PostalAddressRowConformance {
setPlaceholderToTextField(textField: streetTextField, placeholder: rowConformance.streetPlaceholder)
setPlaceholderToTextField(textField: street1TextField, placeholder: rowConformance.street1Placeholder)
setPlaceholderToTextField(textField: street2TextField, placeholder: rowConformance.street2Placeholder)
setPlaceholderToTextField(textField: poboxTextField, placeholder: rowConformance.poboxPlaceholder)
setPlaceholderToTextField(textField: stateTextField, placeholder: rowConformance.statePlaceholder)
setPlaceholderToTextField(textField: postalCodeTextField, placeholder: rowConformance.postalCodePlaceholder)
setPlaceholderToTextField(textField: cityTextField, placeholder: rowConformance.cityPlaceholder)
Expand All @@ -133,7 +149,9 @@ open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAd

open override func cellCanBecomeFirstResponder() -> Bool {
return !row.isDisabled && (
streetTextField?.canBecomeFirstResponder == true ||
street1TextField?.canBecomeFirstResponder == true ||
street2TextField?.canBecomeFirstResponder == true ||
poboxTextField?.canBecomeFirstResponder == true ||
stateTextField?.canBecomeFirstResponder == true ||
postalCodeTextField?.canBecomeFirstResponder == true ||
cityTextField?.canBecomeFirstResponder == true ||
Expand All @@ -146,7 +164,9 @@ open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAd
}

open override func cellResignFirstResponder() -> Bool {
return streetTextField?.resignFirstResponder() ?? true
return street1TextField?.resignFirstResponder() ?? true
&& street2TextField?.resignFirstResponder() ?? true
&& poboxTextField?.resignFirstResponder() ?? true
&& stateTextField?.resignFirstResponder() ?? true
&& postalCodeTextField?.resignFirstResponder() ?? true
&& stateTextField?.resignFirstResponder() ?? true
Expand Down Expand Up @@ -210,8 +230,14 @@ open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAd

guard let textValue = textField.text else {
switch(textField){
case let field where field == streetTextField:
row.value?.street = nil
case let field where field == street1TextField:
row.value?.street1 = nil

case let field where field == street2TextField:
row.value?.street2 = nil

case let field where field == poboxTextField:
row.value?.pobox = nil

case let field where field == stateTextField:
row.value?.state = nil
Expand All @@ -235,9 +261,17 @@ open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAd
var valueFormatter: Formatter?

switch(textField){
case let field where field == streetTextField:
useFormatterDuringInput = rowConformance.streetUseFormatterDuringInput
valueFormatter = rowConformance.streetFormatter
case let field where field == street1TextField:
useFormatterDuringInput = rowConformance.street1UseFormatterDuringInput
valueFormatter = rowConformance.street1Formatter

case let field where field == street2TextField:
useFormatterDuringInput = rowConformance.street2UseFormatterDuringInput
valueFormatter = rowConformance.street2Formatter

case let field where field == poboxTextField:
useFormatterDuringInput = rowConformance.poboxUseFormatterDuringInput
valueFormatter = rowConformance.poboxFormatter

case let field where field == stateTextField:
useFormatterDuringInput = rowConformance.stateUseFormatterDuringInput
Expand Down Expand Up @@ -265,8 +299,12 @@ open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAd
if formatter.getObjectValue(value, for: textValue, errorDescription: errorDesc) {

switch(textField){
case let field where field == streetTextField:
row.value?.street = value.pointee as? String
case let field where field == street1TextField:
row.value?.street1 = value.pointee as? String
case let field where field == street2TextField:
row.value?.street2 = value.pointee as? String
case let field where field == poboxTextField:
row.value?.pobox = value.pointee as? String
case let field where field == stateTextField:
row.value?.state = value.pointee as? String
case let field where field == postalCodeTextField:
Expand Down Expand Up @@ -294,8 +332,12 @@ open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAd

guard !textValue.isEmpty else {
switch(textField){
case let field where field == streetTextField:
row.value?.street = nil
case let field where field == street1TextField:
row.value?.street1 = nil
case let field where field == street2TextField:
row.value?.street2 = nil
case let field where field == poboxTextField:
row.value?.pobox = nil
case let field where field == stateTextField:
row.value?.state = nil
case let field where field == postalCodeTextField:
Expand All @@ -311,8 +353,12 @@ open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAd
}

switch(textField){
case let field where field == streetTextField:
row.value?.street = textValue
case let field where field == street1TextField:
row.value?.street1 = textValue
case let field where field == street2TextField:
row.value?.street2 = textValue
case let field where field == poboxTextField:
row.value?.pobox = textValue
case let field where field == stateTextField:
row.value?.state = textValue
case let field where field == postalCodeTextField:
Expand Down
44 changes: 42 additions & 2 deletions Sources/PostalAddressCell.xib
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="eR5-P1-6WY">
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="G9x-mK-J3Q">
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="zxA-BK-d42">
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
Expand Down Expand Up @@ -56,30 +66,58 @@
</textField>
</subviews>
<constraints>
<constraint firstItem="zxA-BK-d42" firstAttribute="top" secondItem="o5S-WV-gmI" secondAttribute="bottom" id="3m8-71-U9I"/>
<!-- Seperators top is connectd to the bottom of the street 1 but should be pobox -->
<!-- <constraint firstItem="zxA-BK-d42" firstAttribute="top" secondItem="o5S-WV-gmI" secondAttribute="bottom" id="3m8-71-U9I"/> -->
<constraint firstItem="zxA-BK-d42" firstAttribute="top" secondItem="G9x-mK-J3Q" secondAttribute="bottom" id="3m8-71-U9I"/>
<!-- the top of pobox should be the bottom of stree2 -->
<constraint firstItem="G9x-mK-J3Q" firstAttribute="top" secondItem="eR5-P1-6WY" secondAttribute="bottom" id="49M-71-zk4"/>
<!-- the top of street2 should be the bottom of street1 -->
<constraint firstItem="eR5-P1-6WY" firstAttribute="top" secondItem="o5S-WV-gmI" secondAttribute="bottom" id="Jx5-0I-TVf"/>

<constraint firstItem="DdI-zm-cRt" firstAttribute="width" secondItem="Wb4-F5-tW5" secondAttribute="width" multiplier="0.5" id="4Q5-tS-RKi"/>

<!-- The top of ?? is connected to street1 -->
<constraint firstAttribute="topMargin" secondItem="o5S-WV-gmI" secondAttribute="top" id="84h-Rq-4Ai"/>

<constraint firstItem="pdn-aV-W4Q" firstAttribute="height" secondItem="DdI-zm-cRt" secondAttribute="height" id="9Fr-py-YZv"/>
<constraint firstItem="pdn-aV-W4Q" firstAttribute="top" secondItem="DdI-zm-cRt" secondAttribute="bottom" id="A9R-DC-dbb"/>
<constraint firstItem="zxA-BK-d42" firstAttribute="leading" secondItem="WPE-jg-X35" secondAttribute="leading" id="Aly-9u-sp2"/>
<constraint firstItem="WKJ-zQ-8R6" firstAttribute="leading" secondItem="zxA-BK-d42" secondAttribute="leading" id="Aod-23-UKH"/>
<!-- Leading of Street 1 is connected to super view -->
<constraint firstItem="o5S-WV-gmI" firstAttribute="leading" secondItem="WPE-jg-X35" secondAttribute="leadingMargin" constant="10" id="Azs-EM-5Pr"/>
<!-- Street 2 and PO Box have a leading of Street 1 -->
<constraint firstItem="eR5-P1-6WY" firstAttribute="leading" secondItem="o5S-WV-gmI" secondAttribute="leading" id="z3p-0N-dWc"/>
<constraint firstItem="G9x-mK-J3Q" firstAttribute="leading" secondItem="o5S-WV-gmI" secondAttribute="leading" id="jK0-5j-DmL"/>

<constraint firstItem="WKJ-zQ-8R6" firstAttribute="top" secondItem="Wb4-F5-tW5" secondAttribute="bottom" id="C9Z-ni-hbg"/>
<constraint firstItem="Wb4-F5-tW5" firstAttribute="leading" secondItem="DdI-zm-cRt" secondAttribute="trailing" constant="10" id="HtK-0d-d5M"/>
<constraint firstItem="Wb4-F5-tW5" firstAttribute="height" secondItem="DdI-zm-cRt" secondAttribute="height" id="Iir-j8-rhE"/>
<constraint firstItem="Wb4-F5-tW5" firstAttribute="baseline" secondItem="DdI-zm-cRt" secondAttribute="baseline" id="Jml-yT-60Z"/>
<constraint firstAttribute="trailingMargin" secondItem="Wb4-F5-tW5" secondAttribute="trailing" id="Km3-mq-aPx"/>
<!-- City has a trailing of street1 -->
<constraint firstItem="Wb4-F5-tW5" firstAttribute="trailing" secondItem="o5S-WV-gmI" secondAttribute="trailing" id="M2X-vX-kJi"/>
<!-- Street 2 and PO Box have a trailing of Street 1 -->
<constraint firstItem="eR5-P1-6WY" firstAttribute="trailing" secondItem="o5S-WV-gmI" secondAttribute="trailing" id="4Gn-b0-zIT"/>
<constraint firstItem="G9x-mK-J3Q" firstAttribute="trailing" secondItem="o5S-WV-gmI" secondAttribute="trailing" id="9DS-Jm-4DC"/>

<constraint firstItem="Wb4-F5-tW5" firstAttribute="trailing" secondItem="pdn-aV-W4Q" secondAttribute="trailing" id="PJ8-45-ui0"/>
<constraint firstItem="DdI-zm-cRt" firstAttribute="trailing" secondItem="Lu6-hl-9pm" secondAttribute="trailing" id="PQD-gP-1hl"/>
<constraint firstAttribute="bottomMargin" secondItem="pdn-aV-W4Q" secondAttribute="bottom" id="duk-kE-Q1B"/>
<constraint firstItem="Wb4-F5-tW5" firstAttribute="leading" secondItem="pdn-aV-W4Q" secondAttribute="leading" id="kGf-7m-VRp"/>
<!-- state has a height of street1 -->
<constraint firstItem="Lu6-hl-9pm" firstAttribute="height" secondItem="o5S-WV-gmI" secondAttribute="height" id="kPl-j6-9Di"/>
<!-- Street 2 and PO Box have a height of Street 1 -->
<constraint firstItem="eR5-P1-6WY" firstAttribute="height" secondItem="o5S-WV-gmI" secondAttribute="height" id="3Dz-4n-BIL"/>
<constraint firstItem="G9x-mK-J3Q" firstAttribute="height" secondItem="o5S-WV-gmI" secondAttribute="height" id="5lM-dz-fod"/>


<constraint firstItem="DdI-zm-cRt" firstAttribute="height" secondItem="Lu6-hl-9pm" secondAttribute="height" id="kfN-fm-NE8"/>
<constraint firstItem="WKJ-zQ-8R6" firstAttribute="trailing" secondItem="zxA-BK-d42" secondAttribute="trailing" id="lNp-rP-8EN"/>
<constraint firstItem="pdn-aV-W4Q" firstAttribute="top" secondItem="WKJ-zQ-8R6" secondAttribute="bottom" id="lWs-vF-VD5"/>
<constraint firstAttribute="trailing" secondItem="zxA-BK-d42" secondAttribute="trailing" id="omM-qO-r0i"/>
<!-- state has a leading of street1 -->
<constraint firstItem="Lu6-hl-9pm" firstAttribute="leading" secondItem="o5S-WV-gmI" secondAttribute="leading" id="r5j-iN-Cwo"/>

<constraint firstItem="Lu6-hl-9pm" firstAttribute="baseline" secondItem="pdn-aV-W4Q" secondAttribute="baseline" id="t3z-eq-Rzb"/>
<constraint firstItem="DdI-zm-cRt" firstAttribute="leading" secondItem="Lu6-hl-9pm" secondAttribute="leading" id="xHp-MN-w6B"/>
<constraint firstItem="DdI-zm-cRt" firstAttribute="top" secondItem="zxA-BK-d42" secondAttribute="bottom" id="zEH-0P-Xzz"/>
Expand All @@ -93,7 +131,9 @@
<outlet property="postalPercentageConstraint" destination="4Q5-tS-RKi" id="yy5-Q8-uBs"/>
<outlet property="secondSeparatorView" destination="WKJ-zQ-8R6" id="r1V-za-UIl"/>
<outlet property="stateTextField" destination="Lu6-hl-9pm" id="7VA-3s-X5L"/>
<outlet property="streetTextField" destination="o5S-WV-gmI" id="FNV-0A-ukI"/>
<outlet property="street1TextField" destination="o5S-WV-gmI" id="FNV-0A-ukI"/>
<outlet property="street2TextField" destination="eR5-P1-6WY" id="LNT-K9-jaI"/>
<outlet property="poboxTextField" destination="G9x-mK-J3Q" id="QPZ-4E-iuM"/>
</connections>
<point key="canvasLocation" x="57.5" y="-372"/>
</tableViewCell>
Expand Down
Loading