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

refactor the two existing switch statements into polymorphic dispatch #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Binary file not shown.
35 changes: 30 additions & 5 deletions SwiftyViewModels/CellViewModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,38 @@
import Foundation
import UIKit

protocol CellViewModelType { }
protocol AnyCellViewModelType {
var reuseIdentifier: String { get }
}

protocol CellViewModelType: AnyCellViewModelType {
associatedtype CellType: AnyCellConfigurable
}

protocol CellConfigurable {
extension CellViewModelType {
var reuseIdentifier: String {
return CellType.self.reuseIdentifier
}
}

protocol AnyCellConfigurable: NibLoadable {
func configure(with viewModel: AnyCellViewModelType)
}

protocol CellConfigurable: AnyCellConfigurable {
associatedtype ViewModelType: CellViewModelType
func configure(with viewModel: ViewModelType)
}

struct KeyBoardCellViewModel: CellViewModelType {
extension CellConfigurable {
func configure(with viewModel: AnyCellViewModelType) {
configure(with: viewModel as! ViewModelType)
}
}

struct KeyBoardCellViewModel: CellViewModelType {
typealias CellType = KeyboardTableViewCell

let titleField: String
let synthesizerField: String
let numberOfKeyField: String
Expand All @@ -32,7 +55,8 @@ struct KeyBoardCellViewModel: CellViewModelType {
}

struct GuitarCellViewModel: CellViewModelType {

typealias CellType = GuitarTableViewCell

let titleField: String
let stringField: String
let image: UIImage
Expand All @@ -52,7 +76,8 @@ struct GuitarCellViewModel: CellViewModelType {
}

struct AccessoryViewModel: CellViewModelType {

typealias CellType = AccessoryItemTableViewCell

let titleField: String
let detailField: String
let image: UIImage
Expand Down
32 changes: 5 additions & 27 deletions SwiftyViewModels/InstrumentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class InstrumentViewController: UIViewController {
}

func registerCells() {
tableView.register(cell: GuitarTableViewCell.self)
tableView.register(cell: KeyboardTableViewCell.self)
tableView.register(cell: AccessoryItemTableViewCell.self)
for cellType in InstrumentViewModel.cellTypes {
tableView.register(cell: cellType)
}
}
}

Expand All @@ -42,38 +42,16 @@ extension InstrumentViewController: UITableViewDataSource {
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cellType = viewModel.cellType(for: indexPath)
let cell = tableView.dequeueReusableCell(withIdentifier: cellType.reuseIdentifier, for: indexPath)

let cellViewModel = viewModel.viewModel(for: indexPath)

configureCell(cell, cellViewModel: cellViewModel)


let cell = tableView.dequeueReusableCell(withIdentifier: cellViewModel.reuseIdentifier, for: indexPath)
(cell as! AnyCellConfigurable).configure(with: cellViewModel)
return cell
}


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 128.0
}

func configureCell(_ cell: UITableViewCell, cellViewModel: CellViewModelType) {

switch (cell, cellViewModel) {
case (let cell as GuitarTableViewCell, let cellViewModel as GuitarCellViewModel):
cell.configure(with: cellViewModel)
case (let cell as KeyboardTableViewCell, let cellViewModel as KeyBoardCellViewModel):
cell.configure(with: cellViewModel)
case (let cell as AccessoryItemTableViewCell, let cellViewModel as AccessoryViewModel):
cell.configure(with: cellViewModel)
default:
fatalError("Mismatched cell and view model")

}

}
}

extension InstrumentViewController: UITableViewDelegate {
Expand Down
23 changes: 7 additions & 16 deletions SwiftyViewModels/InstrumentViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ struct InstrumentViewModel {
let keyboardViewModels: [KeyBoardCellViewModel]
let accessoryViewModels: [AccessoryViewModel]

static let cellTypes: [AnyCellConfigurable.Type] = [
GuitarTableViewCell.self,
KeyboardTableViewCell.self,
AccessoryItemTableViewCell.self
]

var sectionCount: Int {
return Section.all.count
}
Expand All @@ -48,7 +54,7 @@ struct InstrumentViewModel {
}
}

func viewModel(for indexPath: IndexPath) -> CellViewModelType {
func viewModel(for indexPath: IndexPath) -> AnyCellViewModelType {
guard let section = Section(rawValue: indexPath.section) else {
fatalError("Invalid section")
}
Expand All @@ -63,19 +69,4 @@ struct InstrumentViewModel {
}

}

func cellType(for indexPath: IndexPath) -> NibLoadable.Type {
guard let section = Section(rawValue: indexPath.section) else {
fatalError("Invalid section")
}

switch section {
case .guitar:
return GuitarTableViewCell.self
case .keyboard:
return KeyboardTableViewCell.self
case .accessory:
return AccessoryItemTableViewCell.self
}
}
}