@@ -29,19 +29,53 @@ class AccountInfoViewController: BaseViewController<InfoViewModel> {
2929 viewModel? . trackEvents ( action: AccountEventAction . openLoginInfo)
3030 }
3131
32+ override func viewWillAppear( _ animated: Bool ) {
33+ super. viewWillAppear ( animated)
34+ NotificationCenter . default. addObserver ( self , selector: #selector( keyboardWillShow ( _: ) ) , name: UIResponder . keyboardWillShowNotification, object: nil )
35+ NotificationCenter . default. addObserver ( self , selector: #selector( keyboardWillHide ( _: ) ) , name: UIResponder . keyboardWillHideNotification, object: nil )
36+ }
37+
38+ override func viewWillDisappear( _ animated: Bool ) {
39+ super. viewWillDisappear ( animated)
40+ NotificationCenter . default. removeObserver ( self , name: UIResponder . keyboardWillShowNotification, object: nil )
41+ NotificationCenter . default. removeObserver ( self , name: UIResponder . keyboardWillHideNotification, object: nil )
42+ }
43+
44+ @objc private func keyboardWillShow( _ notification: Notification ) {
45+ guard let keyboardFrame = notification. userInfo ? [ UIResponder . keyboardFrameEndUserInfoKey] as? CGRect ,
46+ let duration = notification. userInfo ? [ UIResponder . keyboardAnimationDurationUserInfoKey] as? Double else { return }
47+ let inset = keyboardFrame. height - view. safeAreaInsets. bottom
48+ UIView . animate ( withDuration: duration) {
49+ self . scrollView. contentInset. bottom = inset
50+ self . scrollView. verticalScrollIndicatorInsets. bottom = inset
51+ }
52+ }
53+
54+ @objc private func keyboardWillHide( _ notification: Notification ) {
55+ guard let duration = notification. userInfo ? [ UIResponder . keyboardAnimationDurationUserInfoKey] as? Double else { return }
56+ UIView . animate ( withDuration: duration) {
57+ self . scrollView. contentInset. bottom = 0
58+ self . scrollView. verticalScrollIndicatorInsets. bottom = 0
59+ }
60+ }
61+
62+ @objc private func moveToNextFromPhone( ) {
63+ addressView. textField? . becomeFirstResponder ( )
64+ }
65+
3266 private func initUI( ) {
3367 title = . accountInfo
3468 view. backgroundColor = . white
3569
36- accountNameView. configureElementUI ( label: . accountName, returnKey: UIReturnKeyType . done )
37- primaryEmailView. configureElementUI ( label: . primaryEmail, returnKey: UIReturnKeyType . done )
38- mobilePhoneView. configureElementUI ( label: . mobilePhone, returnKey: UIReturnKeyType . done , keyboardType: . numbersAndPunctuation )
39- addressView. configureElementUI ( label: " Address Line 1 " . localized ( ) , returnKey: UIReturnKeyType . done )
40- addressView2. configureElementUI ( label: " Address Line 2 " . localized ( ) , returnKey: UIReturnKeyType . done )
41- cityView. configureElementUI ( label: . city, returnKey: UIReturnKeyType . done )
42- stateView. configureElementUI ( label: . stateOrRegion, returnKey: UIReturnKeyType . done )
43- postalCodeView. configureElementUI ( label: . postalcode, returnKey: UIReturnKeyType . done )
44- countryView. configureElementUI ( label: . country, returnKey: UIReturnKeyType . done)
70+ accountNameView. configureElementUI ( label: . accountName, returnKey: . next )
71+ primaryEmailView. configureElementUI ( label: . primaryEmail, returnKey: . next )
72+ mobilePhoneView. configureElementUI ( label: . mobilePhone, returnKey: . next , keyboardType: . phonePad )
73+ addressView. configureElementUI ( label: " Address Line 1 " . localized ( ) , returnKey: . next )
74+ addressView2. configureElementUI ( label: " Address Line 2 " . localized ( ) , returnKey: . next )
75+ cityView. configureElementUI ( label: . city, returnKey: . next )
76+ stateView. configureElementUI ( label: . stateOrRegion, returnKey: . next )
77+ postalCodeView. configureElementUI ( label: . postalcode, returnKey: . next )
78+ countryView. configureElementUI ( label: . country, returnKey: . done)
4579 contentUpdateButton. configureActionButtonUI ( title: . save)
4680 deleteAccountButton. configureActionButtonUI ( title: " Delete Account " . localized ( ) , bgColor: . deepRed)
4781
@@ -54,10 +88,27 @@ class AccountInfoViewController: BaseViewController<InfoViewModel> {
5488 stateView. delegate = self
5589 postalCodeView. delegate = self
5690 countryView. delegate = self
57-
91+
92+ let tapGesture = UITapGestureRecognizer ( target: self , action: #selector( dismissKeyboard) )
93+ tapGesture. cancelsTouchesInView = false
94+ scrollView. addGestureRecognizer ( tapGesture)
95+
5896 getUserDetails ( )
97+
98+
99+ //toolbar for having a next button on phone number introduction
100+ let toolbar = UIToolbar ( )
101+ toolbar. sizeToFit ( )
102+ let nextButton = UIBarButtonItem ( title: " Next " , style: . done, target: self , action: #selector( moveToNextFromPhone) )
103+ let spacer = UIBarButtonItem ( barButtonSystemItem: . flexibleSpace, target: nil , action: nil )
104+ toolbar. items = [ spacer, nextButton]
105+ mobilePhoneView. textField? . inputAccessoryView = toolbar
59106 }
60107
108+ @objc private func dismissKeyboard( ) {
109+ view. endEditing ( true )
110+ }
111+
61112 @IBAction func pressedUpdateButton( _ sender: RoundedButton ) {
62113 attemptValuesChange ( )
63114 }
@@ -147,41 +198,32 @@ extension AccountInfoViewController: UITextFieldDelegate {
147198 }
148199
149200 func textFieldShouldReturn( _ textField: UITextField ) -> Bool {
150- textField. resignFirstResponder ( )
151-
152- switch textField {
153- case postalCodeView. textField, countryView. textField, cityView. textField, stateView. textField:
154- let point = CGPoint . zero
155- scrollView. setContentOffset ( point, animated: true )
156-
157- default :
158- break
201+ let fields = [
202+ accountNameView. textField,
203+ primaryEmailView. textField,
204+ mobilePhoneView. textField,
205+ addressView. textField,
206+ addressView2. textField,
207+ cityView. textField,
208+ stateView. textField,
209+ postalCodeView. textField,
210+ countryView. textField
211+ ]
212+ if let index = fields. firstIndex ( of: textField) , index < fields. count - 1 {
213+ fields [ index + 1 ] ? . becomeFirstResponder ( )
214+ } else {
215+ textField. resignFirstResponder ( )
159216 }
160217 return true
161218 }
162219
163220 func textFieldDidBeginEditing( _ textField: UITextField ) {
164- let changePosition : [ CGFloat ] = [ view. frame. height / 10 , view. frame. height / 7 ]
165221 ( textField as? PETextField ) ? . toggleBorder ( active: true )
166-
167- let point : CGPoint
168- switch textField {
169- case cityView. textField:
170- point = CGPoint ( x: 0 , y: textField. frame. origin. y + changePosition[ 0 ] )
171-
172- case stateView. textField:
173- point = CGPoint ( x: 0 , y: textField. frame. origin. y + changePosition[ 0 ] )
174-
175- case postalCodeView. textField:
176- point = CGPoint ( x: 0 , y: textField. frame. origin. y + changePosition[ 1 ] )
177-
178- case countryView. textField:
179- point = CGPoint ( x: 0 , y: textField. frame. origin. y + changePosition[ 1 ] )
180-
181- default :
182- point = CGPoint . zero
222+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.3 ) {
223+ let fieldFrame = self . scrollView. convert ( textField. bounds, from: textField)
224+ let visibleRect = fieldFrame. insetBy ( dx: 0 , dy: - 16 )
225+ self . scrollView. scrollRectToVisible ( visibleRect, animated: true )
183226 }
184- scrollView. setContentOffset ( point, animated: true )
185227 }
186228
187229 func textFieldDidEndEditing( _ textField: UITextField ) {
0 commit comments