Skip to content

Commit

Permalink
Fix keyboard scroll behavior & session refresh (#119)
Browse files Browse the repository at this point in the history
* Scroll to button for last textfield

* Fix empty session while registration

* Set default value when did disappear
  • Loading branch information
kvld authored Sep 21, 2017
1 parent 4fbfa23 commit 77ee316
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 55 deletions.
69 changes: 39 additions & 30 deletions Stepic/Base.lproj/Auth.storyboard

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions Stepic/EmailAuthViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import UIKit
import SVProgressHUD
import IQKeyboardManagerSwift

extension EmailAuthViewController: EmailAuthView {
func update(with result: EmailAuthResult) {
Expand Down Expand Up @@ -99,6 +100,8 @@ class EmailAuthViewController: UIViewController {
}

@IBAction func onLogInClick(_ sender: Any) {
view.endEditing(true)

AnalyticsReporter.reportEvent(AnalyticsEvents.SignIn.onSignInScreen, parameters: ["LoginInteractionType": "button"])

let email = emailTextField.text ?? ""
Expand Down Expand Up @@ -152,6 +155,13 @@ class EmailAuthViewController: UIViewController {
prefill()
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)

// Reset to default value (see AppDelegate)
IQKeyboardManager.sharedManager().keyboardDistanceFromTextField = 24
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

Expand Down Expand Up @@ -209,6 +219,8 @@ class EmailAuthViewController: UIViewController {
extension EmailAuthViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
AnalyticsReporter.reportEvent(AnalyticsEvents.SignIn.Fields.tap, parameters: nil)
// 24 - default value in app (see AppDelegate), 60 - offset with button
IQKeyboardManager.sharedManager().keyboardDistanceFromTextField = textField == passwordTextField ? 60 : 24
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
Expand Down
23 changes: 23 additions & 0 deletions Stepic/Images.xcassets/Dismiss Auth.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"info" : {
"author" : "zeplin",
"version" : "1"
},
"images" : [
{
"idiom" : "universal",
"filename" : "Dismiss Auth.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "Dismiss Auth@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "Dismiss Auth@3x.png",
"scale" : "3x"
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 31 additions & 25 deletions Stepic/RegistrationPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,40 @@ class RegistrationPresenter {
func register(with name: String, email: String, password: String) {
view?.state = .loading

authManager.signUpWith(name, lastname: " ", email: email, password: password, success: {
self.authManager.logInWithUsername(email, password: password, success: { token in
AuthInfo.shared.token = token

NotificationRegistrator.sharedInstance.registerForRemoteNotifications()

self.stepicsAPI.retrieveCurrentUser(success: { user in
AuthInfo.shared.user = user
User.removeAllExcept(user)

AnalyticsReporter.reportEvent(AnalyticsEvents.Login.success, parameters: ["provider": "registered"])
self.view?.update(with: .success)
}, error: { _ in
print("registration: successfully signed in, but could not get user")

AnalyticsReporter.reportEvent(AnalyticsEvents.Login.success, parameters: ["provider": "registered"])
self.view?.update(with: .success)
performRequest({
self.authManager.signUpWith(name, lastname: " ", email: email, password: password, success: {
self.authManager.logInWithUsername(email, password: password, success: { token in
AuthInfo.shared.token = token

NotificationRegistrator.sharedInstance.registerForRemoteNotifications()

self.stepicsAPI.retrieveCurrentUser(success: { user in
AuthInfo.shared.user = user
User.removeAllExcept(user)

AnalyticsReporter.reportEvent(AnalyticsEvents.Login.success, parameters: ["provider": "registered"])
self.view?.update(with: .success)
}, error: { _ in
print("registration: successfully signed in, but could not get user")

AnalyticsReporter.reportEvent(AnalyticsEvents.Login.success, parameters: ["provider": "registered"])
self.view?.update(with: .success)
})
}, failure: { _ in
self.view?.update(with: .error)
})
}, failure: { _ in
self.view?.update(with: .error)
}, error: { _, registrationErrorInfo in
if let message = registrationErrorInfo?.firstError {
self.view?.state = .validationError(message: message)
} else {
self.view?.update(with: .error)
}
})
}, error: { _, registrationErrorInfo in
if let message = registrationErrorInfo?.firstError {
self.view?.state = .validationError(message: message)
} else {
self.view?.update(with: .error)
}, error: { err in
if err == PerformRequestError.noAccessToRefreshToken {
AuthInfo.shared.token = nil
}
self.view?.update(with: .error)
})

}
}
12 changes: 12 additions & 0 deletions Stepic/RegistrationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import UIKit
import SVProgressHUD
import IQKeyboardManagerSwift

extension RegistrationViewController: RegistrationView {
func update(with result: RegistrationResult) {
Expand Down Expand Up @@ -95,6 +96,8 @@ class RegistrationViewController: UIViewController {
}

@IBAction func onRegisterClick(_ sender: Any) {
view.endEditing(true)

AnalyticsReporter.reportEvent(AnalyticsEvents.SignUp.onSignUpScreen, parameters: ["LoginInteractionType": "button"])

let name = nameTextField.text ?? ""
Expand Down Expand Up @@ -122,6 +125,13 @@ class RegistrationViewController: UIViewController {
setup()
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)

// Reset to default value (see AppDelegate)
IQKeyboardManager.sharedManager().keyboardDistanceFromTextField = 24
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

Expand Down Expand Up @@ -190,6 +200,8 @@ class RegistrationViewController: UIViewController {
extension RegistrationViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
AnalyticsReporter.reportEvent(AnalyticsEvents.SignUp.Fields.tap, parameters: nil)
// 24 - default value in app (see AppDelegate), 60 - offset with button
IQKeyboardManager.sharedManager().keyboardDistanceFromTextField = textField == passwordTextField ? 60 : 24
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
Expand Down

0 comments on commit 77ee316

Please sign in to comment.