private func convert(movies: [Movie]) -> MoviesViewModel {
let movieCellViewModels = movies.map { movie in
MovieCellViewModel(
textLabel: "\(movie.title) (\(movie.year))",
detailTextLabel: movie.genre.joined(separator: ", ")
)
}
return MoviesViewModel(movies: movieCellViewModels)
}
//searchbar observable
var searchBarText: Observable<String> {
return searchBar
.rx
.text
.map { $0! }
.filter { $0.characters.count > 0 }
.throttle(1, scheduler: MainScheduler.instance)
.distinctUntilChanged()
}
viewModel
.fetchRepositories(for: searchBarText) //view model method to fetch the repositories that returns a Driver
.map { result -> [Repository] in //Map the response and handle an error if exists
switch result {
case .success(let repositories):
return repositories
case .failure(let error):
print(error.localizedDescription)
return []
}
}
//bind the response to the table view using a drive method
.drive(tableView.rx.items(cellIdentifier: String(describing: RepositoryCell.self), cellType: RepositoryCell.self)) { row, repository, cell in
cell.titleLabel.text = repository.name
}
.addDisposableTo(disposeBag)
https://medium.com/@alejandro-filtercode/scalable-modern-architecture-for-ios-projects-part-3-8321b1142789
아키텍쳐에서 코디네이터의 역할
MVVM
ViewModel -> CellViewModel