Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save IsaAliev/feb7c2d394a32a2ffd29974815387c04 to your computer and use it in GitHub Desktop.
Save IsaAliev/feb7c2d394a32a2ffd29974815387c04 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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment