Skip to content

Commit

Permalink
Add UIActivityIndicator
Browse files Browse the repository at this point in the history
  • Loading branch information
sydcanem committed Feb 13, 2017
1 parent f5da247 commit 0c4ff1d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 12 deletions.
10 changes: 10 additions & 0 deletions SwiftIOSExperiments/AuthService.swift
Expand Up @@ -9,6 +9,16 @@
import Foundation
import Auth0

public struct Auth {
public var email: String
public var password: String

public init(email: String, password: String) {
self.email = email
self.password = password
}
}

class AuthService {
static func authenticate(_ email: String, _ password: String,
completion: @escaping (_ result: Result<Credentials>) -> Void) {
Expand Down
28 changes: 28 additions & 0 deletions SwiftIOSExperiments/Extensions.swift
Expand Up @@ -7,9 +7,17 @@
//

import Foundation
import UIKit

protocol LoginValidation {}

protocol LoadingAuthentication {
var spinner: UIActivityIndicatorView { get }

func success(_ token: String)
func failed(_ error: Any)
}

extension LoginValidation {
func isValidEmail(_ email: String) -> Bool {
// swiftlint:disable:next line_length
Expand All @@ -28,3 +36,23 @@ extension LoginValidation {
return true
}
}

extension LoadingAuthentication where Self: UIViewController {
func authenticate(auth: Auth) {
spinner.startAnimating()

AuthService.authenticate(auth.email, auth.password,
completion: { [weak self] result in
switch result {
case .success(let credentials):
guard let idToken = credentials.idToken else { return }

self?.success(idToken)
case .failure(let error):
self?.failed(error)
}

self?.spinner.stopAnimating()
})
}
}
29 changes: 18 additions & 11 deletions SwiftIOSExperiments/ViewController.swift
Expand Up @@ -8,7 +8,8 @@

import UIKit

class ViewController: UIViewController, LoginValidation {
class ViewController: UIViewController, LoginValidation, LoadingAuthentication {
let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)

@IBOutlet var email: UITextField!
@IBOutlet var password: UITextField!
Expand All @@ -18,6 +19,11 @@ class ViewController: UIViewController, LoginValidation {

override func viewDidLoad() {
super.viewDidLoad()

spinner.hidesWhenStopped = true
spinner.center = view.center

view.addSubview(spinner)
}

@IBAction func login(_ button: UIButton) {
Expand All @@ -37,17 +43,18 @@ class ViewController: UIViewController, LoginValidation {
emailValidationMessage = nil
passwordValidationMessage = nil
} else {
AuthService.authenticate(email, password, completion: { result in
switch result {
case .success(let credentials):
guard let idToken = credentials.idToken else { return }

print("Successfully logged-in: \(idToken)")
case .failure(let error):
print("Error: \(error)")
}
})
let resource = Auth(email: email, password: password)

authenticate(auth: resource)
}
}
}

func success(_ token: String) {
print("Token: \(token)")
}

func failed(_ error: Any) {
print("Error: \(error)")
}
}
3 changes: 2 additions & 1 deletion docs/learning-points.md
Expand Up @@ -5,13 +5,14 @@
- [ ] [Stack Views](https://www.raywenderlich.com/114552/uistackview-tutorial-introducing-stack-views)

## SDK Components
- [ ] UIActivityIndicatorView
- [x] UIActivityIndicatorView
- [ ] UITableView
- [ ] Collection Views

## Navigation
- [ ] UINavigationController
- [ ] [Multiple Storyboard](https://code.tutsplus.com/tutorials/ios-9-staying-organized-with-storyboard-references--cms-24226)
- [x] [Connecting View Controllers](https://talk.objc.io/episodes/S01E05-connecting-view-controllers)

## Architecture/Patterns
- [x] [Viper Architecture](https://www.ckl.io/blog/ios-project-architecture-using-viper/)
Expand Down

0 comments on commit 0c4ff1d

Please sign in to comment.