Connect training and sign-up view controller

This commit is contained in:
Raimund Wege
2017-08-22 16:32:30 +02:00
parent 371a7874f7
commit b823dc206f
5 changed files with 123 additions and 86 deletions
@@ -9,9 +9,11 @@
import UIKit
protocol LoginViewControllerDelegate {
func loginViewController(_ controller: LoginViewController, willLoginWithUserName userName: String, password: String)
func loginViewControllerWillCancel(_ controller: LoginViewController)
}
class LoginViewController: FormularViewController {
@@ -53,9 +55,9 @@ class LoginViewController: FormularViewController {
// MARK: - Actions
@IBAction func cancelButtonTapped(_ sender: Any) {
presentingViewController!.dismiss(animated: true, completion: nil)
signUpController?.loginViewControllerWillCancel(self)
}
@IBAction func loginButtonTapped(_ sender: Any) {
signUpController?.loginViewController(
self,
@@ -318,7 +318,7 @@
<scene sceneID="e3W-CJ-soQ">
<objects>
<navigationController storyboardIdentifier="rootViewController" id="pqQ-jd-OwW" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" misplaced="YES" translucent="NO" id="x3H-m5-yJB">
<navigationBar key="navigationBar" contentMode="scaleToFill" translucent="NO" id="x3H-m5-yJB">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
@@ -417,7 +417,6 @@
<userDefinedRuntimeAttribute type="boolean" keyPath="layer.masksToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
<connections>
<action selector="trainingButtonTapped:" destination="ulQ-cJ-h3D" eventType="touchUpInside" id="8is-aQ-a7u"/>
<segue destination="1ba-kX-Lnq" kind="show" identifier="Training" id="TgF-p5-r1E"/>
</connections>
</button>
@@ -452,7 +451,7 @@
<scene sceneID="6Ty-cK-spl">
<objects>
<navigationController id="Pn3-0n-ZdQ" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" misplaced="YES" translucent="NO" id="McN-2f-sK6">
<navigationBar key="navigationBar" contentMode="scaleToFill" translucent="NO" id="McN-2f-sK6">
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
@@ -831,7 +830,7 @@
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Mark Name Label" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" minimumFontSize="12" adjustsLetterSpacingToFitWidth="YES" preferredMaxLayoutWidth="0.0" translatesAutoresizingMaskIntoConstraints="NO" id="BQe-Id-ec3">
<rect key="frame" x="101" y="290.5" width="173" height="22"/>
<rect key="frame" x="101.5" y="290.5" width="173" height="22"/>
<constraints>
<constraint firstAttribute="height" constant="22" id="g3A-7J-yFy"/>
</constraints>
@@ -1037,7 +1036,7 @@
<nil key="highlightedColor"/>
</label>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="6rV-hv-c3D" userLabel="Countdown View">
<rect key="frame" x="6" y="38" width="363" height="50"/>
<rect key="frame" x="6.5" y="38" width="363" height="50"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="pjH-rV-MGI" userLabel="Days View">
<rect key="frame" x="0.0" y="0.0" width="121" height="50"/>
@@ -1352,7 +1351,7 @@
<scene sceneID="s4S-5p-4o1">
<objects>
<navigationController id="I54-NN-JAf" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" misplaced="YES" translucent="NO" id="Qb2-vc-6Pl">
<navigationBar key="navigationBar" contentMode="scaleToFill" translucent="NO" id="Qb2-vc-6Pl">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
@@ -1452,13 +1451,13 @@
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Status Title Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="0.0" translatesAutoresizingMaskIntoConstraints="NO" id="r6d-OU-cRI">
<rect key="frame" x="20" y="14.5" width="134.5" height="23.5"/>
<rect key="frame" x="20" y="15" width="134.5" height="23.5"/>
<fontDescription key="fontDescription" name="OpenSans" family="Open Sans" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Status Label" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="0.0" translatesAutoresizingMaskIntoConstraints="NO" id="5Xu-6g-tqP">
<rect key="frame" x="259" y="14.5" width="96" height="23.5"/>
<rect key="frame" x="259" y="15" width="96" height="23.5"/>
<fontDescription key="fontDescription" name="OpenSans" family="Open Sans" pointSize="17"/>
<nil key="highlightedColor"/>
</label>
@@ -1484,13 +1483,13 @@
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Mode Title Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="0.0" translatesAutoresizingMaskIntoConstraints="NO" id="Xww-rc-pOA">
<rect key="frame" x="20" y="14.5" width="130.5" height="23.5"/>
<rect key="frame" x="20" y="15" width="130.5" height="23.5"/>
<fontDescription key="fontDescription" name="OpenSans" family="Open Sans" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Mode Label" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="0.0" translatesAutoresizingMaskIntoConstraints="NO" id="tJY-RR-vpe">
<rect key="frame" x="262" y="14.5" width="93" height="23.5"/>
<rect key="frame" x="262" y="15" width="93" height="23.5"/>
<fontDescription key="fontDescription" name="OpenSans" family="Open Sans" pointSize="17"/>
<nil key="highlightedColor"/>
</label>
@@ -1516,13 +1515,13 @@
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="GPS Fixes Title Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="0.0" translatesAutoresizingMaskIntoConstraints="NO" id="Ujw-jg-ilY">
<rect key="frame" x="20" y="14.5" width="161" height="23.5"/>
<rect key="frame" x="20" y="15" width="161" height="23.5"/>
<fontDescription key="fontDescription" name="OpenSans" family="Open Sans" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="GPS Fixes Label" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="0.0" translatesAutoresizingMaskIntoConstraints="NO" id="dz3-w7-CCh">
<rect key="frame" x="232" y="14.5" width="123" height="23.5"/>
<rect key="frame" x="232" y="15" width="123" height="23.5"/>
<fontDescription key="fontDescription" name="OpenSans" family="Open Sans" pointSize="17"/>
<nil key="highlightedColor"/>
</label>
@@ -1548,13 +1547,13 @@
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="GPS Accuracy Title Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="0.0" translatesAutoresizingMaskIntoConstraints="NO" id="Z1d-5I-pbj">
<rect key="frame" x="20" y="14.5" width="191.5" height="23.5"/>
<rect key="frame" x="20" y="15" width="191.5" height="23.5"/>
<fontDescription key="fontDescription" name="OpenSans" family="Open Sans" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="GPS Accuracy Label" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="DiM-MR-SGV">
<rect key="frame" x="181.5" y="14.5" width="153.5" height="23.5"/>
<rect key="frame" x="181.5" y="15" width="153.5" height="23.5"/>
<fontDescription key="fontDescription" name="OpenSans" family="Open Sans" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
@@ -1666,7 +1665,7 @@
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Value Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="48" preferredMaxLayoutWidth="0.0" translatesAutoresizingMaskIntoConstraints="NO" id="mUm-EE-6Is" userLabel="Time Label">
<rect key="frame" x="3" y="244" width="369" height="98.5"/>
<rect key="frame" x="3" y="243.5" width="369" height="98.5"/>
<fontDescription key="fontDescription" name="OpenSans-Light" family="Open Sans" pointSize="72"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
@@ -1708,7 +1707,7 @@
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Value Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="l5u-i3-029">
<rect key="frame" x="3" y="244" width="369" height="98.5"/>
<rect key="frame" x="3" y="243.5" width="369" height="98.5"/>
<fontDescription key="fontDescription" name="OpenSans-Light" family="Open Sans" pointSize="72"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
@@ -1749,7 +1748,7 @@
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Value Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="sGK-xV-9a0">
<rect key="frame" x="3" y="244" width="369" height="98.5"/>
<rect key="frame" x="3" y="243.5" width="369" height="98.5"/>
<fontDescription key="fontDescription" name="OpenSans-Light" family="Open Sans" pointSize="72"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
@@ -2114,7 +2113,7 @@
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="4Fb-v3-mkB" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" misplaced="YES" translucent="NO" id="opw-zV-Qak">
<navigationBar key="navigationBar" contentMode="scaleToFill" translucent="NO" id="opw-zV-Qak">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
@@ -2132,7 +2131,7 @@
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="dfN-Ch-vpm" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" misplaced="YES" translucent="NO" id="IvG-lp-FEV">
<navigationBar key="navigationBar" contentMode="scaleToFill" translucent="NO" id="IvG-lp-FEV">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
@@ -2200,7 +2199,7 @@
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="Njg-ZD-Ycd" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" misplaced="YES" id="bNh-Bg-ROM">
<navigationBar key="navigationBar" contentMode="scaleToFill" id="bNh-Bg-ROM">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
@@ -57,14 +57,28 @@ class SignUpController: NSObject {
extension SignUpController: LoginViewControllerDelegate {
func loginViewController(_ controller: LoginViewController, willLoginWithUserName userName: String, password: String) {
requestManager.postAccessToken(userName: userName, password: password, success: { (userName, accessToken) in
}) { (error, message) in
self.showAlert(forError: self.loginViewControllerError(forError: error), andMessage: message, withViewController: controller)
requestManager.postAccessToken(userName: userName, password: password, success: { [weak self] userName, accessToken in
self?.loginViewController(controller, didFinishLoginWithUserName: userName, password: password)
}) { [weak self] error, message in
self?.loginViewController(controller, didFailLoginWithError: error, message: message)
}
}
private func loginViewControllerError(forError error: Error) -> Error {
func loginViewControllerWillCancel(_ controller: LoginViewController) {
controller.presentingViewController?.dismiss(animated: true)
self.delegate?.signUpControllerDidCancel(self)
}
fileprivate func loginViewController(_ controller: LoginViewController, didFinishLoginWithUserName: String, password: String) {
controller.presentingViewController?.dismiss(animated: true)
self.delegate?.signUpControllerDidFinish(self)
}
fileprivate func loginViewController(_ controller: LoginViewController, didFailLoginWithError error: Error, message: String?) {
self.showAlert(forError: SignUpController.loginViewControllerError(forError: error), andMessage: message, withViewController: controller)
}
fileprivate static func loginViewControllerError(forError error: Error) -> Error {
if (error as NSError).domain == NSURLErrorDomain {
return error
}
@@ -85,20 +99,34 @@ extension SignUpController: SignUpViewControllerDelegate {
company: String,
password: String)
{
requestManager.postCreateUser(userName: userName, email: email, fullName: fullName, company: company, password: password, success: { userName, accessToken in
do {
try Keychain.userName.savePassword(userName)
try Keychain.userPassword.savePassword(password)
try Keychain.userAccessToken.savePassword(accessToken)
self.delegate?.signUpControllerDidFinish(self)
} catch {
self.showAlert(forError: error, andMessage: nil, withViewController: controller)
}
}) { (error, message) in
self.showAlert(forError: error, andMessage: message, withViewController: controller)
requestManager.postCreateUser(userName: userName, email: email, fullName: fullName, company: company, password: password, success: { [weak self] userName, accessToken in
self?.signUpViewController(controller, didFinishSignUpWithUserName: userName, password: password, accessToken: accessToken)
}) { [weak self] error, message in
self?.signUpViewController(controller, didFailSignUpWithError: error, message: message)
}
}
fileprivate func signUpViewController(
_ controller: SignUpViewController,
didFinishSignUpWithUserName userName: String,
password: String,
accessToken: String)
{
do {
try Keychain.userName.savePassword(userName)
try Keychain.userPassword.savePassword(password)
try Keychain.userAccessToken.savePassword(accessToken)
} catch {
self.showAlert(forError: error, andMessage: nil, withViewController: controller)
}
controller.presentingViewController?.dismiss(animated: true)
self.delegate?.signUpControllerDidFinish(self)
}
fileprivate func signUpViewController(_ controller: SignUpViewController, didFailSignUpWithError error: Error, message: String?) {
self.showAlert(forError: error, andMessage: message, withViewController: controller)
}
}
// MARK: - ForgotPasswordViewControllerDelegate
@@ -106,19 +134,27 @@ extension SignUpController: SignUpViewControllerDelegate {
extension SignUpController: ForgotPasswordViewControllerDelegate {
func forgotPasswordViewController(_ controller: ForgotPasswordViewController, willChangePasswordForEmail email: String) {
requestManager.postForgotPassword(email: email, success: {
}) { (error, message) in
self.showAlert(forError: error, andMessage: message, withViewController: controller)
requestManager.postForgotPassword(email: email, success: { [weak self] in
self?.forgotPasswordViewControllerDidFinishChangePassword(controller)
}) { [weak self] error, message in
self?.forgotPasswordViewController(controller, didFailChangePasswordWithError: error, message: message)
}
}
func forgotPasswordViewController(_ controller: ForgotPasswordViewController, willChangePasswordForUserName userName: String) {
requestManager.postForgotPassword(userName: userName, success: {
}) { (error, message) in
self.showAlert(forError: error, andMessage: message, withViewController: controller)
requestManager.postForgotPassword(userName: userName, success: { [weak self] in
self?.forgotPasswordViewControllerDidFinishChangePassword(controller)
}) { [weak self] error, message in
self?.forgotPasswordViewController(controller, didFailChangePasswordWithError: error, message: message)
}
}
fileprivate func forgotPasswordViewControllerDidFinishChangePassword(_ controller: ForgotPasswordViewController) {
// TODO
}
fileprivate func forgotPasswordViewController(_ controller: ForgotPasswordViewController, didFailChangePasswordWithError error: Error, message: String?) {
self.showAlert(forError: error, andMessage: message, withViewController: controller)
}
}
@@ -26,32 +26,4 @@ class StartViewController: UIViewController {
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: UIImageView(image: UIImage(named: "sap_logo")))
}
// MARK: - Actions
@IBAction func trainingButtonTapped(_ sender: Any) {
signUpController.loginWithViewController(self)
}
// MARK: - Properties
fileprivate lazy var signUpController: SignUpController = {
let signUpController = SignUpController()
signUpController.delegate = self
return signUpController
}()
}
// MARK: - SignUpControllerDelegate
extension StartViewController: SignUpControllerDelegate {
func signUpControllerDidFinish(_ controller: SignUpController) {
}
func signUpControllerDidCancel(_ controller: SignUpController) {
}
}
@@ -9,23 +9,29 @@
import UIKit
class TrainingTableViewController: UIViewController {
var login = true
@IBOutlet weak var headerTitleLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var footerTextView: UITextView!
@IBOutlet weak var addButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
setup()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if login {
signUpController.loginWithViewController(self)
login = false
}
}
// MARK: - Setup
fileprivate func setup() {
setupAddButton()
}
@@ -33,7 +39,15 @@ class TrainingTableViewController: UIViewController {
fileprivate func setupAddButton() {
makeViewRoundWithShadow(addButton)
}
// MARK: - Properties
fileprivate lazy var signUpController: SignUpController = {
let signUpController = SignUpController()
signUpController.delegate = self
return signUpController
}()
}
// MARK: - UITableViewDataSource
@@ -65,3 +79,17 @@ extension TrainingTableViewController: UITableViewDataSource {
extension TrainingTableViewController: UITableViewDelegate {
}
// MARK: - SignUpControllerDelegate
extension TrainingTableViewController: SignUpControllerDelegate {
func signUpControllerDidFinish(_ controller: SignUpController) {
}
func signUpControllerDidCancel(_ controller: SignUpController) {
navigationController?.popViewController(animated: true)
}
}