Skip to content

Instantly share code, notes, and snippets.

@serhatsezer
Last active October 3, 2019 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serhatsezer/9427bdd7971f57428fdc1e3dc37cf30a to your computer and use it in GitHub Desktop.
Save serhatsezer/9427bdd7971f57428fdc1e3dc37cf30a to your computer and use it in GitHub Desktop.
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