Created
October 17, 2020 08:33
-
-
Save IsaAliev/0437fe80cd566f76bf9cf47769de472c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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