Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Country selector #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added initializer requirement to PostalAddressType
This allows us to initialize an empty object of the PostalAddressType used - instead of the non-generic implementation. We also made sure the onChange event is only fired once - and not multiple times (this was the case if the PostalAddress value was not initialized before).
Marco Betschart committed Dec 11, 2017
commit 9fb48a086a7f4d93234319954e934d48e52627d9
47 changes: 25 additions & 22 deletions Sources/PostalAddressCell.swift
Original file line number Diff line number Diff line change
@@ -107,23 +107,25 @@ open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAd
.onChange{ [weak self] in
guard let this = self else{ return }

if this.row.baseValue == nil{
this.row.baseValue = PostalAddress()
}
var rowValue = this.row.value ?? T()
rowValue.country = $0.value
this.row.value = rowValue

this.row.value?.country = $0.value
$0.title = $0.displayValueFor?($0.value) ?? $0.noValueDisplayText ?? (this.row as? PostalAddressRowConformance)?.countryPlaceholder

this.row.updateCell()

}.cellSetup{ [weak self] cell, row in
cell.selectionStyle = .none
cell.detailTextLabel?.isHidden = true
cell.textLabel?.textColor = self?.row.value?.country == nil ? .lightGray : (self?.row.isDisabled == true ? .gray : .black)
cell.textLabel?.textColor = self?.row.value?.country == nil ? UIColor(red: 196.0/255.0, green: 196.0/255.0, blue: 196.0/255.0, alpha: 1.0) : (self?.row.isDisabled == true ? .gray : .black)

}.cellUpdate{ [weak self] cell, row in
cell.selectionStyle = .none
cell.textLabel?.textColor = self?.row.value?.country == nil ? .lightGray : (self?.row.isDisabled == true ? .gray : .black)
cell.textLabel?.textColor = self?.row.value?.country == nil ? UIColor(red: 196.0/255.0, green: 196.0/255.0, blue: 196.0/255.0, alpha: 1.0) : (self?.row.isDisabled == true ? .gray : .black)

}.onPresent{ from, to in
to.sectionKeyForValue = { _ in return selectorRow.selectorTitle ?? "" }
}

selectorRow.baseCell.setup()
@@ -258,11 +260,7 @@ open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAd
}

@objc open func textFieldDidChange(_ textField : UITextField){
if row.baseValue == nil{
row.baseValue = PostalAddress()
}

guard let textValue = textField.text else {
guard let textValue = textField.text else {
switch(textField){
case let field where field == streetTextField:
row.value?.street = nil
@@ -317,18 +315,19 @@ open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAd
let value: AutoreleasingUnsafeMutablePointer<AnyObject?> = AutoreleasingUnsafeMutablePointer<AnyObject?>.init(UnsafeMutablePointer<T>.allocate(capacity: 1))
let errorDesc: AutoreleasingUnsafeMutablePointer<NSString?>? = nil
if formatter.getObjectValue(value, for: textValue, errorDescription: errorDesc) {
var rowValue = row.value ?? T()

switch(textField){
case let field where field == streetTextField:
row.value?.street = value.pointee as? String
rowValue.street = value.pointee as? String
case let field where field == stateTextField:
row.value?.state = value.pointee as? String
rowValue.state = value.pointee as? String
case let field where field == postalCodeTextField:
row.value?.postalCode = value.pointee as? String
rowValue.postalCode = value.pointee as? String
case let field where field == cityTextField:
row.value?.city = value.pointee as? String
rowValue.city = value.pointee as? String
case let field where field == countryTextField:
row.value?.country = value.pointee as? String
rowValue.country = value.pointee as? String
default:
break
}
@@ -341,6 +340,8 @@ open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAd
}
textField.selectedTextRange = textField.textRange(from: selStartPos, to: selStartPos)
}

row.value = rowValue
return
}
}
@@ -363,21 +364,23 @@ open class _PostalAddressCell<T: PostalAddressType>: Cell<T>, CellType, PostalAd
}
return
}


var rowValue = row.value ?? T()
switch(textField){
case let field where field == streetTextField:
row.value?.street = textValue
rowValue.street = textValue
case let field where field == stateTextField:
row.value?.state = textValue
rowValue.state = textValue
case let field where field == postalCodeTextField:
row.value?.postalCode = textValue
rowValue.postalCode = textValue
case let field where field == cityTextField:
row.value?.city = textValue
rowValue.city = textValue
case let field where field == countryTextField:
row.value?.country = textValue
rowValue.country = textValue
default:
break
}
row.value = rowValue
}

//MARK: TextFieldDelegate
31 changes: 15 additions & 16 deletions Sources/PostalAddressCell.xib
Original file line number Diff line number Diff line change
@@ -21,87 +21,86 @@
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="o5S-WV-gmI">
<rect key="frame" x="26" y="11" width="333" height="32.5"/>
<rect key="frame" x="26" y="11" width="333" height="25.5"/>
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="zxA-BK-d42">
<rect key="frame" x="0.0" y="43.5" width="375" height="1"/>
<rect key="frame" x="0.0" y="40.5" width="375" height="1"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="height" constant="1" id="TdQ-t3-lW8"/>
</constraints>
</view>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="DdI-zm-cRt">
<rect key="frame" x="26" y="44.5" width="107.5" height="32"/>
<rect key="frame" x="26" y="45.5" width="107.5" height="26.5"/>
<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="Wb4-F5-tW5">
<rect key="frame" x="143.5" y="43.5" width="215.5" height="32"/>
<rect key="frame" x="143.5" y="45.5" width="215.5" height="26.5"/>
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="WKJ-zQ-8R6">
<rect key="frame" x="0.0" y="75.5" width="375" height="1"/>
<rect key="frame" x="0.0" y="76" width="375" height="1"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="height" constant="1" id="BdP-Sq-c1v"/>
</constraints>
</view>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="pdn-aV-W4Q">
<rect key="frame" x="143.5" y="76.5" width="215.5" height="32"/>
<rect key="frame" x="143.5" y="83" width="215.5" height="26"/>
<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="Lu6-hl-9pm">
<rect key="frame" x="26" y="76.5" width="107.5" height="32.5"/>
<rect key="frame" x="26" y="85" width="107.5" height="25.5"/>
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" scrollEnabled="NO" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="-1" estimatedSectionHeaderHeight="-1" sectionFooterHeight="-1" estimatedSectionFooterHeight="-1" contentViewInsetsToSafeArea="NO" translatesAutoresizingMaskIntoConstraints="NO" id="rNh-KU-ipC" customClass="FormViewController">
<rect key="frame" x="133.5" y="76.5" width="225.5" height="32.5"/>
<rect key="frame" x="133.5" y="77" width="241.5" height="32"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</tableView>
</subviews>
<constraints>
<constraint firstAttribute="bottomMargin" secondItem="Lu6-hl-9pm" secondAttribute="bottom" id="2CL-QA-OP0"/>
<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="o5S-WV-gmI" secondAttribute="bottom" constant="4" id="3m8-71-U9I"/>
<constraint firstItem="DdI-zm-cRt" firstAttribute="width" secondItem="Wb4-F5-tW5" secondAttribute="width" multiplier="0.5" id="4Q5-tS-RKi"/>
<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"/>
<constraint firstItem="o5S-WV-gmI" firstAttribute="leading" secondItem="WPE-jg-X35" secondAttribute="leadingMargin" constant="10" id="Azs-EM-5Pr"/>
<constraint firstItem="WKJ-zQ-8R6" firstAttribute="top" secondItem="Wb4-F5-tW5" secondAttribute="bottom" id="C9Z-ni-hbg"/>
<constraint firstItem="WKJ-zQ-8R6" firstAttribute="top" secondItem="Wb4-F5-tW5" secondAttribute="bottom" constant="4" 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"/>
<constraint firstItem="Wb4-F5-tW5" firstAttribute="trailing" secondItem="o5S-WV-gmI" secondAttribute="trailing" id="M2X-vX-kJi"/>
<constraint firstItem="Wb4-F5-tW5" firstAttribute="top" secondItem="zxA-BK-d42" secondAttribute="bottom" constant="4" id="OsS-q5-Orm"/>
<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 firstItem="rNh-KU-ipC" firstAttribute="leading" secondItem="Lu6-hl-9pm" secondAttribute="trailing" id="ZmU-AE-iXk"/>
<constraint firstItem="rNh-KU-ipC" firstAttribute="top" secondItem="WKJ-zQ-8R6" secondAttribute="bottom" id="abv-SY-xi2"/>
<constraint firstAttribute="bottomMargin" secondItem="pdn-aV-W4Q" secondAttribute="bottom" id="duk-kE-Q1B"/>
<constraint firstAttribute="bottomMargin" secondItem="rNh-KU-ipC" secondAttribute="bottom" id="eM8-cn-1Yx"/>
<constraint firstItem="Lu6-hl-9pm" firstAttribute="top" secondItem="DdI-zm-cRt" secondAttribute="bottom" id="j8e-yP-pap"/>
<constraint firstItem="Lu6-hl-9pm" firstAttribute="top" secondItem="WKJ-zQ-8R6" secondAttribute="bottom" constant="8" id="j8e-yP-pap"/>
<constraint firstItem="Wb4-F5-tW5" firstAttribute="leading" secondItem="pdn-aV-W4Q" secondAttribute="leading" id="kGf-7m-VRp"/>
<constraint firstItem="Lu6-hl-9pm" firstAttribute="height" secondItem="o5S-WV-gmI" secondAttribute="height" id="kPl-j6-9Di"/>
<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 firstItem="pdn-aV-W4Q" firstAttribute="top" secondItem="WKJ-zQ-8R6" secondAttribute="bottom" constant="6" id="lWs-vF-VD5"/>
<constraint firstAttribute="trailing" secondItem="zxA-BK-d42" secondAttribute="trailing" id="omM-qO-r0i"/>
<constraint firstItem="rNh-KU-ipC" firstAttribute="trailing" secondItem="WPE-jg-X35" secondAttribute="trailingMargin" id="qpM-qa-7i8"/>
<constraint firstItem="rNh-KU-ipC" firstAttribute="trailing" secondItem="WPE-jg-X35" secondAttribute="trailing" id="qpM-qa-7i8"/>
<constraint firstItem="Lu6-hl-9pm" firstAttribute="leading" secondItem="o5S-WV-gmI" secondAttribute="leading" id="r5j-iN-Cwo"/>
<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"/>
<constraint firstItem="DdI-zm-cRt" firstAttribute="top" secondItem="zxA-BK-d42" secondAttribute="bottom" constant="4" id="zEH-0P-Xzz"/>
</constraints>
</tableViewCellContentView>
<connections>
2 changes: 2 additions & 0 deletions Sources/PostalAddressRow.swift
Original file line number Diff line number Diff line change
@@ -20,6 +20,8 @@ public protocol PostalAddressType: Equatable {
var postalCode: String? { get set }
var city: String? { get set }
var country: String? { get set }

init()
}

public func == <T: PostalAddressType>(lhs: T, rhs: T) -> Bool {