Created
April 1, 2019 15:43
-
-
Save khatv911/3635b8ceef5faf0f0e23b6e9d697689b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// blueprint | |
open class EpoxyModel { | |
fun bind() {} | |
} | |
class Factory( | |
val key: Int, | |
val createModel: (Any) -> EpoxyModel | |
) | |
class StrategyExpoxyController { // extend from Type2Controller<List<Any>,Boolean> | |
private val factories: SparseArray<Factory> = SparseArray(); | |
fun registerFactory(factory: Factory) { | |
factories.append(factory.key, factory) | |
} | |
fun buildModels(list: List<*>) { | |
for (data in list) { | |
data?.run { | |
factories[this::class.java.hashCode()]?.createModel?.invoke(this) // add bind this | |
} | |
} | |
} | |
//usage | |
class Data1 {} | |
class Data1Model : EpoxyModel() {} | |
// declare factory | |
val modelCreation: (Data1) -> EpoxyModel = { Data1Model() } | |
val factory1 = Factory(key = Data1::class.java.hashCode(), createModel = modelCreation as (Any) -> EpoxyModel) | |
// in fragment | |
val controller = StrategyExpoxyController().apply { | |
registerFactory(factory1) | |
// more registration here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment