b

    BMG

    2 years ago
    Hi guys, What is the recommended way to use Kotlin interface delegation in Android classes? I use a Fragment and want to "delegate" some portion of logic in that fragment to separate class. This way, I can use Kotlin interface delegation to reduce code in main fragment. Basically,
    class SomeFragment(viewManager: SomeViewManager) : SomeViewInterface by viewManager {
    
    }
    Now I want to pass a
    View
    to
    SomeViewManager
    class. What is the best way to do it? Should I have a setter in
    SomeViewManger?
    Ianmedeiros

    Ianmedeiros

    2 years ago
    It’s not possible to do that with classes that are managed by the framework: i.e. fragments / dialogfragments / activities / etc.
    What you can do is to remove the fragment logic to another class and compose it instead.
    And the fragment is the one that build this class. Fragments / Activities are composition roots to your classes
    g

    ghedeon

    2 years ago
    maybe with a FragmentFactory tho..
    Ianmedeiros

    Ianmedeiros

    2 years ago
    Only API 28+
    g

    ghedeon

    2 years ago
    aren't you talking about
    AppComponentFactory
    ?
    FragmentFactory
    is from androidx and I can't see any restrictions.
    b

    BMG

    2 years ago
    @ghedeon @Ianmedeiros Thanks for the points! I didn't know about
    FragmentFactory
    ! With that, can I inject
    SomeViewManager
    and have a
    setter
    to set the view once
    onViewCreated
    is called?
    ahulyk

    ahulyk

    2 years ago
    with
    FragmentFactory
    you can inject
    SomeViewManager
    Ianmedeiros

    Ianmedeiros

    2 years ago
    FragmentFactory only works on devices with API28+
    are you willing to set your minimum target as api 28 and have no user base?
    hehe