agoda-com / Kakao

This repo is no longer supported. Please visit a https://github.com/KakaoCup/Kakao
Apache License 2.0
1.11k stars 102 forks source link

Question: same class to be used with or without RecyclerView #178

Closed plnice closed 4 years ago

plnice commented 4 years ago

In my project I have a collection of reusable custom views and I want to prepare a library of Kakao views for them. Each of the custom views can be used within any ViewGroup, or within RecyclerView. For now for each of the views I prepare two separate classes for that two use cases:

// To be used in regular cases, e.g. ViewGroups
class KMyView(function: ViewBuilder.() -> Unit) : KBaseView<KMyView>(function) {
    val child: KTextView = KTextView { 
        isDescendantOfA(function)
        withId(R.id.child)
    }
}

// To be used with KRecyclerView
class RMyView(parent: Matcher<View>) : KRecyclerItem<RMyView>(parent) {
    val child = KTextView(parent) { withId(R.id.child) }
}

Is there a way to have one class only, instead of two?

Unlimity commented 4 years ago

There is no direct necessity for that. You can extend KRecyclerItem, since basically it duplicates the functionality of KBaseView, but replaces constructor to the parent matcher. So what you need to do, is to provide additional constructors to your class, that will accept the DSL.

plnice commented 4 years ago

OK, what should I pass to the super constructor invocation in such case?

class KMyView : KRecyclerItem<KMyView> {
    val child: KTextView

    // RecylerItem version
    constructor(parent: Matcher<View>) : super(parent) {
        child = KTextView(parent) { withId(R.id.child) }
    }

    // Regular usage version
    constructor(function: ViewBuilder.() -> Unit) : super(function) { // Does not compile, super constructor needs Matcher<View>
        child = KTextView {
            isDescendantOfA(function)
            withId(R.id.child)
        }
    }
}
Unlimity commented 4 years ago

super(Matchers.any()) {

Unlimity commented 4 years ago

Closing this issue due to no activity