๐
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 ViewModel { | |
| class Input { | |
| let fetch = PublishSubject<()>() | |
| } | |
| class Output { | |
| let friends: Driver<[User]> | |
| } | |
| let apiClient: APIClient | |
| let input: Input | |
| let output: Output |
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
| let facebookFriends: Observable<[User]> // network request | |
| let twitterFriends: Observable<[User]> // network request | |
| let totalFriends: Observable<[User]> = Observable.combineLatest([facebookFriends, twitterFriends]) { friends in | |
| return Array(friends.joined()) | |
| } |
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
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| viewModel.friends.subscribe(onNext: { friends in | |
| self.friendsCountLabel.text = "\(friends.count)" | |
| }) | |
| } |
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 ViewModel { | |
| let friends: Observable<[User]> | |
| init() { | |
| let client = APIClient() | |
| friends = Observable<[User]>.create({ subscriber in | |
| client.getFacebookFriends(completion: { friends in | |
| subscriber.onNext(friends) | |
| subscriber.onCompleted() | |
| }) | |
| return Disposables.create() |
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
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| viewModel.friends.bind { friends in | |
| self.friendsCountLabel.text = "\(friends.count)" | |
| } | |
| } |
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 ViewModel { | |
| let friends = Binding<[User]>(value: []) | |
| init() { | |
| getFacebookFriends { | |
| friends.value = $0 | |
| } | |
| } | |
| func getFacebookFriends(completion: ([User]) -> Void) { | |
| // Do the work | |
| } |
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 Binding<T> { | |
| var value: T { | |
| didSet { | |
| listener?(value) | |
| } | |
| } | |
| private var listener: ((T) -> Void)? | |
| init(value: T) { | |
| self.value = value | |
| } |
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 ViewModel { | |
| func getFacebookFriends(completion: [User] -> Void) { | |
| let client = APIClient() | |
| client.getFacebookFriends(for: user) { friends in | |
| DispatchQueue.main.async { | |
| completion(friends) | |
| } | |
| } | |
| } | |
| } |
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
| viewModel.getFacebookFriends { friends in | |
| self.friendCountLabel.text = "\(friends.count)" | |
| } |
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 ProfileController: UIViewController { | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| let viewModel = ViewModel(user: user) | |
| nameLabel.text = viewModel.name | |
| birthdayLabel.text = viewModel.birthdayString | |
| salaryLabel.text = viewModel.salary | |
| piLabel.text = viewModel.millionthDigitOfPi | |
| } | |
| } |