Last active
October 3, 2019 20:39
-
-
Save serhatsezer/9427bdd7971f57428fdc1e3dc37cf30a to your computer and use it in GitHub Desktop.
This file contains 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
import UIKit | |
protocol ViewModelType { | |
} | |
protocol Service { | |
func request(_ path: String, completion: @escaping ([String]) -> Void) | |
} | |
struct MainViewModel: ViewModelType { | |
private(set) var service: Service | |
init(_ service: Service) { | |
self.service = service | |
} | |
func getMainArticles(completion: @escaping ([String]) -> Void) { | |
service.request("/path/articles", completion: completion) | |
} | |
func articlesLoaded() { | |
} | |
} | |
class MainViewController: UIViewController { | |
private(set) var viewModel: MainViewModel | |
private let indicator = UIActivityIndicatorView(frame: .zero) | |
private let tableView = UITableView(frame: .zero) | |
init(_ viewModel: MainViewModel) { | |
self.viewModel = viewModel | |
super.init(nibName: nil, bundle: nil) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.addSubview(indicator) | |
view.addSubview(tableView) | |
} | |
// triggered somewhere | |
private func callArticles() { | |
indicator.startAnimating() | |
viewModel.getMainArticles { [weak self] articles in | |
// do something | |
// lets assume we're notifiying our view model for updating for it's own logic | |
self?.viewModel.articlesLoaded() | |
// after loading/parsing complete dissmiss animation | |
self?.indicator.stopAnimating() | |
self?.tableView.reloadData() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment