From 025d1d40ae8a2d81155538709c64744cabedfb5e Mon Sep 17 00:00:00 2001 From: Graeme Read Date: Fri, 25 Nov 2016 21:44:34 +0000 Subject: [PATCH] Stub asynchronous REST call --- UI Testing.xcodeproj/project.pbxproj | 11 ++++++--- UI Testing/API.swift | 34 ++++++++++++++++++++++++++++ UI Testing/ViewController.swift | 9 ++++++-- 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 UI Testing/API.swift diff --git a/UI Testing.xcodeproj/project.pbxproj b/UI Testing.xcodeproj/project.pbxproj index b805326..53092e2 100644 --- a/UI Testing.xcodeproj/project.pbxproj +++ b/UI Testing.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ AA1E29431DE8E24100B0CC3A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AA1E29411DE8E24100B0CC3A /* Main.storyboard */; }; AA1E29451DE8E24100B0CC3A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AA1E29441DE8E24100B0CC3A /* Assets.xcassets */; }; AA1E29481DE8E24100B0CC3A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AA1E29461DE8E24100B0CC3A /* LaunchScreen.storyboard */; }; + AA99E4F31DE8E51900C83A69 /* API.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA99E4F21DE8E51900C83A69 /* API.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -22,6 +23,7 @@ AA1E29441DE8E24100B0CC3A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; AA1E29471DE8E24100B0CC3A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; AA1E29491DE8E24100B0CC3A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + AA99E4F21DE8E51900C83A69 /* API.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = API.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -54,12 +56,13 @@ AA1E293C1DE8E24100B0CC3A /* UI Testing */ = { isa = PBXGroup; children = ( + AA99E4F21DE8E51900C83A69 /* API.swift */, AA1E293D1DE8E24100B0CC3A /* AppDelegate.swift */, - AA1E293F1DE8E24100B0CC3A /* ViewController.swift */, - AA1E29411DE8E24100B0CC3A /* Main.storyboard */, AA1E29441DE8E24100B0CC3A /* Assets.xcassets */, - AA1E29461DE8E24100B0CC3A /* LaunchScreen.storyboard */, AA1E29491DE8E24100B0CC3A /* Info.plist */, + AA1E29461DE8E24100B0CC3A /* LaunchScreen.storyboard */, + AA1E29411DE8E24100B0CC3A /* Main.storyboard */, + AA1E293F1DE8E24100B0CC3A /* ViewController.swift */, ); path = "UI Testing"; sourceTree = ""; @@ -138,6 +141,7 @@ buildActionMask = 2147483647; files = ( AA1E29401DE8E24100B0CC3A /* ViewController.swift in Sources */, + AA99E4F31DE8E51900C83A69 /* API.swift in Sources */, AA1E293E1DE8E24100B0CC3A /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -302,6 +306,7 @@ AA1E294E1DE8E24100B0CC3A /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/UI Testing/API.swift b/UI Testing/API.swift new file mode 100644 index 0000000..9cbcd3a --- /dev/null +++ b/UI Testing/API.swift @@ -0,0 +1,34 @@ +// +// API.swift +// UI Testing +// +// Created by Graeme Read on 25/11/2016. +// Copyright © 2016 Graeme Read. All rights reserved. +// + +import Foundation + + +struct API { + + // MARK: - Structs + struct Constants { + static let delay = 4 + } + + + // MARK: - Instance methods + func retrieveData(completion: @escaping (String) -> Void) { + delay(Constants.delay) { + completion("Hello World") + } + } + + + // MARK: - Private methods + private func delay(_ delay: Int, block: @escaping () -> Void) { + let delay = DispatchTime.now() + Double(Int64(UInt64(delay) * NSEC_PER_SEC)) / Double(NSEC_PER_SEC) + DispatchQueue.main.asyncAfter(deadline: delay, execute: block) + } + +} diff --git a/UI Testing/ViewController.swift b/UI Testing/ViewController.swift index cdd120d..37df6b8 100644 --- a/UI Testing/ViewController.swift +++ b/UI Testing/ViewController.swift @@ -14,6 +14,7 @@ class ViewController: UIViewController { // MARK: - Properties @IBOutlet private weak var label: UILabel! @IBOutlet private weak var spinner: UIActivityIndicatorView! + private let api = API() // MARK: - UIViewController @@ -25,8 +26,12 @@ class ViewController: UIViewController { // MARK: - IBActions @IBAction private func doSomething() { - label.text = "Hello world" + spinner.startAnimating() + api.retrieveData { [weak self] text in + self?.label.text = text + + self?.spinner.stopAnimating() + } } - }