This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Any old user class | |
| class User { … } | |
| /// Response enum to notify us about a success or failure | |
| enum Response { | |
| case success | |
| case fail(Error) | |
| } | |
| /// Types that implement this protocol |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FollowerViewModelTest: XCTestCase { | |
| let disposeBag = DisposeBag() | |
| func testFollowersUpdate() { | |
| let testFollower = User() | |
| let mockNetworking = FollowerNetworkingMock(mockFollowers: [testFollower]) | |
| // Create a scheduler that starts at 0 | |
| let scheduler = TestScheduler(initialClock: 0) | |
| let viewModel = FollowUserViewModel(networkHandler: mockNetworking, scheduler: scheduler) | |
| let testCountObserver = scheduler.createTestObserver(Int.self) | |
| let testLatestFollowObserver = scheduler.createTestObserver(User.self) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FollowerViewModelTest: XCTestCase { | |
| // retain subscriptions while testing | |
| let disposeBag = DisposeBag() | |
| func testFollowersUpdate() { | |
| let testFollower = User() | |
| let mockNetworking = FollowerNetworkingMock( | |
| mockFollowers: [testFollower], | |
| followResponse: .success | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final class FollowUserViewModel { | |
| // same properties as above | |
| init( | |
| networkHandler: FollowNetworkHandler, | |
| scheduler: SchedulerType // inject a scheduler type to the initializer | |
| ) { | |
| followSubject | |
| .observeOn(scheduler) | |
| // transformation | |
| .disposed(by: disposeBag) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // FollowUserViewModel.swift | |
| // YourFavoriteInstruments | |
| // | |
| // Created by Dean Silfen on 2/2/19. | |
| // Copyright © 2019 Dean Silfen. All rights reserved. | |
| // | |
| import RxSwift | |
| import RxCocoa |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final class FollowUserViewModel { | |
| // Observer for our view to bind to when our user | |
| // wants to follow another user. | |
| let followUserObserver: AnyObserver<User> { | |
| return followSubject.asObserver() | |
| } | |
| private let disposeBag = DisposeBag() | |
| // This subject will listen for users to follow. | |
| private let followSubject = PublishSubject<User>() | |
| init(user: User, networkHandler: FollowNetworkHandler) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: A UIKit based Playground for presenting user interface | |
| import UIKit | |
| import PlaygroundSupport | |
| protocol GreetService { | |
| func greeting() -> String | |
| } | |
| class Greeter: GreetService{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: A UIKit based Playground for presenting user interface | |
| import UIKit | |
| import PlaygroundSupport | |
| protocol GreetService { | |
| func greeting() -> String | |
| } | |
| class Greeter: GreetService{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Dog { | |
| let name: String | |
| } | |
| let possibleDogNames: [String?] = [ | |
| "barry", | |
| "scooby", | |
| "scrappy", | |
| nil, | |
| nil, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| `map` is a method on sequence that will iterate over a sequence | |
| and apply a closure to each element of that sequence. It will then | |
| return the new sequence with the closure applied. | |
| Using `map` simplifies code that needs to transform lists of data | |
| for example, it could be used here to simplify your code. | |
| */ |