Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save IsaAliev/0437fe80cd566f76bf9cf47769de472c to your computer and use it in GitHub Desktop.
Save IsaAliev/0437fe80cd566f76bf9cf47769de472c to your computer and use it in GitHub Desktop.
protocol CollectionItemsViewModelDependencyManager {
var dependencies: [ViewDependency] { get }
func mapModelTypeNameToIdentifier(_ fullTypeName: String) -> String
func reuseIdentifier(for model: CollectionItemViewModel) -> String
func resolveIdentifier(forModelTypeUsingUnusualNaming fullTypeName: String) -> String
}
extension CollectionItemsViewModelDependencyManager {
func reuseIdentifier(for model: CollectionItemViewModel) -> String {
let typeName = String(describing: type(of: model))
return mapModelTypeNameToIdentifier(typeName)
}
func mapModelTypeNameToIdentifier(_ fullTypeName: String) -> String {
guard fullTypeName.hasSuffix("Model") else {
return resolveIdentifier(forModelTypeUsingUnusualNaming: fullTypeName)
}
let typeName = String(fullTypeName.split(separator: ".").last!)
let modelTailTrimmedName = typeName.dropLast(5)
return String(modelTailTrimmedName)
}
}
struct ViewDependency: ExpressibleByStringLiteral {
typealias StringLiteralType = String
var nibName: String
var identifier: String
var classType: AnyClass
var withNib = true
var isCell = true
var kind = ""
init(stringLiteral value: String) {
classType = swiftClassFromString(value)!
identifier = value
nibName = value
withNib = false
}
...
}
protocol ViewRepresentable: ViewModelTypeErasedViewRepresentable {
associatedtype ViewModelType: ViewModel
var model: ViewModelType! { get set }
func bindWithModel()
}
protocol ViewModelTypeErasedViewRepresentable: class {
var typeErasedViewModel: ViewModel? { get set }
}
extension ViewRepresentable {
var typeErasedViewModel: ViewModel? {
get {
model
}
set {
model = (newValue as! Self.ViewModelType)
}
}
}
extension ViewRepresentable {
func setupViewModel() {
model?.setup()
bindWithModel()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment