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 { | |
| // 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
| 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
| 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
| /// 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
| struct FollowerNetworkingMock: FollowNetworkHandler { | |
| let mockFollowers: [User] | |
| let followResponse: Response | |
| func follow(user: User) -> Observable<Response> { | |
| return .just(followResponse) | |
| } | |
| func followers() -> Observable<[User]> { | |
| return .just(mockFollowers) | |
| } | |
| } |
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
| func simpleCombineStreams( | |
| resource: Observable<Bool>, | |
| buttonTap: Observable<Void> | |
| ) -> Observable<Bool> { | |
| return buttonTap | |
| .withLatestFrom(resource) | |
| } |
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
| enum TrainLine { … } | |
| enum TrainStation { … } | |
| protocol TrainNetworkHandler { | |
| func departureTime(forLine: TrainLine, departingFrom: TrainStation) -> Observable<Date> | |
| } |
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 TrainDepartureService { | |
| func departureTimes(forLine: TrainLine, departingFrom: TrainStation) -> Observable<Date> { | |
| return Observable<TimeInterval>.timer(0, period: 60, scheduler: scheduler) | |
| .flatMapLatest { _ -> Observable<Date> in | |
| network.departureTime(forLine: forLine, departingFrom: departingFrom) | |
| } | |
| } | |
| private let scheduler: SchedulerType |