Model
struct GroupsList {
// groups that are part of this model
let groups: [Group]
// category of a group (enum)
let groupsCategory: GroupCategory
}
Swift
복사
ViewModel
final class GroupsViewModel {
private let interactor: GroupsInteractorProtocol
private let coordinator: GroupsCoordinatorProtocol
private var groupsList: GroupsList {
return interactor.groupsList
}
init(interactor: GroupsInteractorProtocol, coordinator: GroupsCoordinatorProtocol) {
self.interactor = interactor
self.coordinator = coordinator
}
var groupsCategory: String {
return groupsList.groupsCategory.localizedString
}
func fetchGroups() {
// interaction to be handled within calçot
interactor.fetchGroups()
}
func dismissGroup() {
// interaction leading to a different screen or calçot
coordinator.dismissGroup()
}
}
Swift
복사
View
final class GroupsViewController: UIViewController {
var viewModel: GroupsViewModel {
didSet {
updateUI()
}
}
init(viewModel: GroupsViewModel) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
}
private func updateUI() {
// update UI
}
// ...
}
Swift
복사
Interactor
protocol GroupsInteractorProtocol {
var groupsList: GroupsList { get }
func fetchGroups()
}
final class GroupsInteractor: GroupsInteractorProtocol {
private(set) var groupsList: GroupsList
private let dataProvider: GroupDataProvider
// ...
func fetchGroups() {
// fetch data from a data provider
dataProvider.fetchGroups() { (groups, error) in
// Error handling? Naaah, this stuff always works :D
// Update the model with new data
self.groupsList = GroupsList(groups: groups, groupsCategory: .joined)
}
}
}
Swift
복사
Coordinator
protocol GroupsCoordinatorProtocol: class {
func present(group: Group)
func dismissGroup()
}
final class GroupsCoordinator: GroupsCoordinatorProtocol {
weak var navigationController: UINavigationController?
func present(group: Group) {
// Preparing the new calçot
let groupCoordinator = GroupCoordinator(navigationController: navigationController)
let groupInteractor = GroupInteractor(group: group)
let groupViewModel = GroupViewModel(interactor: groupInteractor, coordinator: groupCoordinator)
let groupViewController = GroupViewController(viewModel: groupViewModel)
// Navigate to the new screen
navigationController?.pushViewController(groupViewController, animated: true)
}
// ...
}
Swift
복사