Skip to content

Commit

Permalink
Stub asynchronous REST call
Browse files Browse the repository at this point in the history
  • Loading branch information
graemer957 committed Nov 25, 2016
1 parent 1cf0d35 commit 025d1d4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
11 changes: 8 additions & 3 deletions UI Testing.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -22,6 +23,7 @@
AA1E29441DE8E24100B0CC3A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
AA1E29471DE8E24100B0CC3A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
AA1E29491DE8E24100B0CC3A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
AA99E4F21DE8E51900C83A69 /* API.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = API.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -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 = "<group>";
Expand Down Expand Up @@ -138,6 +141,7 @@
buildActionMask = 2147483647;
files = (
AA1E29401DE8E24100B0CC3A /* ViewController.swift in Sources */,
AA99E4F31DE8E51900C83A69 /* API.swift in Sources */,
AA1E293E1DE8E24100B0CC3A /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -302,6 +306,7 @@
AA1E294E1DE8E24100B0CC3A /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
34 changes: 34 additions & 0 deletions UI Testing/API.swift
Original file line number Diff line number Diff line change
@@ -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)
}

}
9 changes: 7 additions & 2 deletions UI Testing/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
}


}

0 comments on commit 025d1d4

Please sign in to comment.