Skip to content

Commit fb362b8

Browse files
committed
add loading button
1 parent ebf8fff commit fb362b8

3 files changed

Lines changed: 47 additions & 19 deletions

File tree

AnimalsApp/AnimalsApp/DesignSystem/Colors.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ extension UIColor {
1414
static let grayCellFrame: UIColor? = UIColor(named: "grayCellFrame")
1515
static let grayTextColor: UIColor? = UIColor(named: "grayTextColor")
1616
static let lightGray: UIColor? = UIColor(named: "lightGray")
17-
17+
18+
static let purpleButtonColor: UIColor? = UIColor(named: "purpleButtonColor")
1819
}

AnimalsApp/AnimalsApp/Views/RegisterViewController/RegisterViewController.swift

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class RegisterViewController: UIViewController {
2020
@IBOutlet weak var textFieldSpecie: UITextField!
2121
@IBOutlet weak var textFieldAge: UITextField!
2222
@IBOutlet weak var buttonRegister: UIButton!
23-
// @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
23+
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
2424

2525
// MARK: Overrides
2626
override func viewDidLoad() {
@@ -29,11 +29,14 @@ class RegisterViewController: UIViewController {
2929
setNavigationItems()
3030
delegateTextField()
3131
notificationCenter()
32-
// activityIndicator.hidesWhenStopped = true
32+
activityIndicator.hidesWhenStopped = true
3333
}
3434

3535
// MARK: Actions
3636
@IBAction func handlerButtonRegister(_ sender: Any) {
37+
activityIndicator.startAnimating()
38+
buttonRegister.backgroundColor = .grayCellFrame
39+
buttonRegister.isUserInteractionEnabled = false
3740

3841
guard let name = textFieldName.text?.testIfIsEmpty(),
3942
let description = textFieldDescription.text?.testIfIsEmpty(),
@@ -43,10 +46,8 @@ class RegisterViewController: UIViewController {
4346
showAlerts(alertTitle: "Erro", alertMessage: "Preencha todos os campos")
4447
return }
4548

46-
// activityIndicator.startAnimating()
47-
registerVM.registerAnimal(name: name, description: description, age: age, species: species, image: image) {
48-
// self.activityIndicator.stopAnimating()
49-
print("oi")
49+
registerVM.registerAnimal(name: name, description: description, age: age, species: species, image: image) { [weak self] in
50+
self?.registerSucceeded()
5051
}
5152
}
5253

@@ -72,11 +73,11 @@ class RegisterViewController: UIViewController {
7273
}
7374

7475
let attributes = [NSAttributedString.Key.foregroundColor: UIColor.grayTextColor ?? UIColor.systemGray, NSAttributedString.Key.font: UIFont(name: "OpenSans", size: 16) ?? UIFont.systemFont(ofSize: 16)]
75-
textFieldName.attributedPlaceholder = NSAttributedString(string: " Nome", attributes: attributes)
76-
textFieldImageLink.attributedPlaceholder = NSAttributedString(string: " Link da imagem", attributes: attributes)
77-
textFieldDescription.attributedPlaceholder = NSAttributedString(string: " Descrição", attributes: attributes)
78-
textFieldSpecie.attributedPlaceholder = NSAttributedString(string: " Espécie", attributes: attributes)
79-
textFieldAge.attributedPlaceholder = NSAttributedString(string: " Idade", attributes: attributes)
76+
textFieldName.attributedPlaceholder = NSAttributedString(string: "Nome", attributes: attributes)
77+
textFieldImageLink.attributedPlaceholder = NSAttributedString(string: "Link da imagem", attributes: attributes)
78+
textFieldDescription.attributedPlaceholder = NSAttributedString(string: "Descrição", attributes: attributes)
79+
textFieldSpecie.attributedPlaceholder = NSAttributedString(string: "Espécie", attributes: attributes)
80+
textFieldAge.attributedPlaceholder = NSAttributedString(string: "Idade", attributes: attributes)
8081
buttonRegister.layer.cornerRadius = 10
8182
}
8283

@@ -91,10 +92,24 @@ class RegisterViewController: UIViewController {
9192

9293
private func showAlerts(alertTitle: String?, alertMessage: String?) {
9394
let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
94-
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
95+
alert.addAction(UIAlertAction(title: "OK", style: .default) { [weak self] _ in
96+
self?.activityIndicator.stopAnimating()
97+
self?.buttonRegister.isUserInteractionEnabled = true
98+
self?.buttonRegister.backgroundColor = .purpleButtonColor
99+
})
100+
95101
present(alert, animated: true)
96102
}
97103

104+
private func registerSucceeded() {
105+
activityIndicator.stopAnimating()
106+
buttonRegister.isUserInteractionEnabled = true
107+
buttonRegister.backgroundColor = .purpleButtonColor
108+
[textFieldName, textFieldImageLink, textFieldDescription, textFieldSpecie, textFieldAge].forEach { textField in
109+
textField?.text = ""
110+
}
111+
}
112+
98113
private func notificationCenter() {
99114
let notificationCenter = NotificationCenter.default
100115
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)

AnimalsApp/AnimalsApp/Views/RegisterViewController/RegisterViewController.xib

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99
<capability name="System colors in document resources" minToolsVersion="11.0"/>
1010
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1111
</dependencies>
12+
<customFonts key="customFonts">
13+
<array key="OpenSans-Regular.ttf">
14+
<string>OpenSans</string>
15+
</array>
16+
</customFonts>
1217
<objects>
1318
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="RegisterViewController" customModule="AnimalsApp" customModuleProvider="target">
1419
<connections>
20+
<outlet property="activityIndicator" destination="ksy-aq-ngY" id="40E-Lw-8uq"/>
1521
<outlet property="buttonRegister" destination="IgQ-5e-alS" id="L9L-mM-03l"/>
1622
<outlet property="scrollView" destination="N6J-fj-czs" id="h1p-Eq-CBb"/>
1723
<outlet property="textFieldAge" destination="8vc-1n-XQI" id="ttV-kf-O7h"/>
@@ -67,13 +73,12 @@
6773
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="IgQ-5e-alS">
6874
<rect key="frame" x="0.0" y="425" width="349" height="60"/>
6975
<color key="backgroundColor" name="purpleButtonColor"/>
76+
<fontDescription key="fontDescription" name="OpenSans" family="Open Sans" pointSize="16"/>
7077
<color key="tintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
71-
<state key="normal" title="Button"/>
72-
<buttonConfiguration key="configuration" style="plain" title="Cadastrar" showsActivityIndicator="YES">
73-
<fontDescription key="titleFontDescription" name="OpenSans" family="Open Sans" pointSize="16"/>
74-
<directionalEdgeInsets key="contentInsets" top="0.0" leading="0.0" bottom="0.0" trailing="0.0"/>
75-
<color key="baseForegroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
76-
</buttonConfiguration>
78+
<inset key="imageEdgeInsets" minX="0.0" minY="0.0" maxX="2.2250738585072014e-308" maxY="0.0"/>
79+
<state key="normal" title="Cadastrar">
80+
<color key="titleColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
81+
</state>
7782
<connections>
7883
<action selector="handlerButtonRegister:" destination="-1" eventType="touchUpInside" id="y9D-zl-zk3"/>
7984
</connections>
@@ -102,11 +107,18 @@
102107
<constraint firstItem="aHz-3L-Vgj" firstAttribute="leading" secondItem="uTn-FQ-xzf" secondAttribute="leading" constant="13" id="xRd-jr-jgx"/>
103108
</constraints>
104109
</view>
110+
<activityIndicatorView opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" style="medium" translatesAutoresizingMaskIntoConstraints="NO" id="ksy-aq-ngY">
111+
<rect key="frame" x="230" y="465" width="20" height="20"/>
112+
<color key="tintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
113+
<color key="color" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
114+
</activityIndicatorView>
105115
</subviews>
106116
<constraints>
107117
<constraint firstAttribute="trailing" secondItem="uTn-FQ-xzf" secondAttribute="trailing" id="BJB-RD-aKj"/>
118+
<constraint firstAttribute="bottom" secondItem="ksy-aq-ngY" secondAttribute="bottom" constant="40" id="CuJ-R8-L72"/>
108119
<constraint firstItem="uTn-FQ-xzf" firstAttribute="leading" secondItem="N6J-fj-czs" secondAttribute="leading" id="PTA-73-6eZ"/>
109120
<constraint firstAttribute="bottom" secondItem="uTn-FQ-xzf" secondAttribute="bottom" id="Q0Y-XA-bKD"/>
121+
<constraint firstAttribute="trailing" secondItem="ksy-aq-ngY" secondAttribute="trailing" constant="125" id="bOI-fY-9mE"/>
110122
<constraint firstItem="uTn-FQ-xzf" firstAttribute="centerX" secondItem="N6J-fj-czs" secondAttribute="centerX" id="iOb-6w-6gZ"/>
111123
<constraint firstItem="uTn-FQ-xzf" firstAttribute="top" secondItem="N6J-fj-czs" secondAttribute="top" id="xs9-MT-6ho"/>
112124
</constraints>

0 commit comments

Comments
 (0)