Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code clean up #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,6 @@ class ReposViewController: UIViewController {
}
}

extension ReposViewController: ReposViewModelDelegate {
func reposViewModel(_ reposViewModel: ReposViewModel, isLoading: Bool) {
UIApplication.shared.isNetworkActivityIndicatorVisible = isLoading
}

func reposViewModel(_ reposViewModel: ReposViewModel, didReceiveRepos repos: [RepoViewModel]) {
data = repos
tableView.reloadData()
}

func reposViewModel(_ reposViewModel: ReposViewModel, didSelectId id: Int) {
let alertController = UIAlertController(title: "\(id)", message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
}
}

extension ReposViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data?.count ?? 0
Expand All @@ -100,3 +83,22 @@ extension ReposViewController: UISearchResultsUpdating {
viewModel.didChangeQuery(searchController.searchBar.text ?? "")
}
}

// MARK: ViewModel Delegate

extension ReposViewController: ReposViewModelDelegate {
func reposViewModel(isLoading: Bool) {
UIApplication.shared.isNetworkActivityIndicatorVisible = isLoading
}

func reposViewModel(didReceiveRepos repos: [RepoViewModel]) {
data = repos
tableView.reloadData()
}

func reposViewModel(didSelectId id: Int) {
let alertController = UIAlertController(title: "\(id)", message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
}
}
27 changes: 13 additions & 14 deletions mvvm-delegates/MVVMDelegates/App/ReposScene/ReposViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
import Foundation

protocol ReposViewModelDelegate: class {
func reposViewModel(_ reposViewModel: ReposViewModel,
isLoading: Bool)
func reposViewModel(_ reposViewModel: ReposViewModel,
didReceiveRepos repos: [RepoViewModel])
func reposViewModel(_ reposViewModel: ReposViewModel,
didSelectId id: Int)
func reposViewModel(isLoading: Bool)
func reposViewModel(didReceiveRepos repos: [RepoViewModel])
func reposViewModel(didSelectId id: Int)
}

final class ReposViewModel {
Expand All @@ -25,16 +22,17 @@ final class ReposViewModel {
private let throttle = Throttle(minimumDelay: 0.3)
private var currentSearchNetworkTask: URLSessionDataTask?
private var lastQuery: String?
// Dependencies

// MARK: Dependencies
private let networkingService: NetworkingService

init(networkingService: NetworkingService) {
self.networkingService = networkingService
}

// Inputs
// MARK: Inputs
func ready() {
delegate?.reposViewModel(self, isLoading: true)
delegate?.reposViewModel(isLoading: true)
networkingService.searchRepos(withQuery: "swift") { [weak self] repos in
guard let strongSelf = self else { return }
strongSelf.finishSearching(with: repos)
Expand All @@ -53,14 +51,15 @@ final class ReposViewModel {

func didSelectRow(at indexPath: IndexPath) {
guard let repos = repos else { return }
delegate?.reposViewModel(self, didSelectId: repos[indexPath.item].id)
delegate?.reposViewModel(didSelectId: repos[indexPath.item].id)
}

// Private
// MARK: Private methods

private func startSearchWithQuery(_ query: String) {
currentSearchNetworkTask?.cancel() // cancel previous pending request

delegate?.reposViewModel(self, isLoading: true)
delegate?.reposViewModel(isLoading: true)

currentSearchNetworkTask = networkingService.searchRepos(withQuery: query) { [weak self] repos in
guard let strongSelf = self else { return }
Expand All @@ -69,12 +68,12 @@ final class ReposViewModel {
}

private func finishSearching(with repos: [Repo]) {
delegate?.reposViewModel(self, isLoading: false)
delegate?.reposViewModel(isLoading: false)

self.repos = repos
let repoViewModels = repos.map { RepoViewModel(repo: $0) }

delegate?.reposViewModel(self, didReceiveRepos: repoViewModels)
delegate?.reposViewModel(didReceiveRepos: repoViewModels)
}
}

Expand Down