Giter Club home page Giter Club logo

componentsmanager's Introduction

ComponentsManager

VERSION Codacy Badge Android Arsenal

README for the 1.x.x of the library lives here.

Benefits

  • No need to save the components somewhere
  • No need to remove the component when the Activity/Fragment is going to be destroyed
  • The components will be saved while the rotation changes

Getting started

This library is available on jitpack and jcenter.

To download it from the jitpack, add these lines in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

If you are using AndroidX

implementation "com.github.valeryponomarenko.componentsmanager:androidx:LATEST_VERSION"

If you are using AppCompat

implementation "com.github.valeryponomarenko.componentsmanager:appcompat:LATEST_VERSION"

Idea

The idea of the library is to save dagger components and return them when they are needed. Every component is saved in the static store and removed when the owner is going to be destroyed.

What's new

2.1.0

*InjectionManagers have two new methods to find a component. The methods return null if a component was not found and no exceptions are thrown.

// finds a component by type
XInjectionManager
    .findComponentOrNull<SomeComponent>()
    ?.someMethod()

// finds a component by predicate
XInjectionManager
    .findComponentOrNull { /* predicate */ }
    ?.someMethod()

The ComponentNotFoundException class that is inside me.vponomarenko.injectionmanager.exeptions package is deprecated, because the exeptions was misspelled, so use ComponentNotFoundException that is inside me.vponomarenko.injectionmanager.exceptions package. The new ComponentNotFoundException class is inherited from the old one.

2.0.1

If you use the *InjectionManager.findComponent() method and the component was not found, the ComponentNotFoundException will be more informative, beucase the type of the component will be printed.

//before
Caused by: me.vponomarenko.injectionmanager.exeptions.ComponentNotFoundException:
    Component for the Function1<java.lang.Object, java.lang.Boolean> was not found
...

//after
Caused by: me.vponomarenko.injectionmanager.exeptions.ComponentNotFoundException: 
    Component of the FragmentChildB type was not found
...

But if you use the *InjectionManager.findComponent(predicate) method, the exception's massage will be the same as it was in 2.0.0.

2.0.0

The main difference between the 2.0.0 version and the 1.1.0 version that the IHasComponent interface is a generic one. Therefore, you must specify the class of the component.

//before
class MyFragment : Fragment(), IHasComponent {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        XInjectionManager
            .bindComponent<MyComponent>(this)
            .inject(this)
    }

    override fun getComponent(): Any = DaggerMyComponent.create()
}

//after
class MyFragment : Fragment(), IHasComponent<MyComponent> {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        XInjectionManager
            .bindComponent(this)
            .inject(this)
    }

    override fun getComponent(): MyComponent = DaggerMyComponent.create()
}

1.1.0

Add static methods into the (X/Compat)InjectionManager, so there will no need to get the instance and then call the needed method. Examples:

//before
fun foo() {
    XInjectionManager.instance.init(this)
    XInjectionManager.instance.bindComponent<AppComponent>(this)
}

//after
fun foo() {
    XInjectionManager.init(this)
    XInjectionManager.bindComponent<AppComponent>(this)
}

How to use

The following example will be for the AndroidX. If you want to use this library for the AppCompat packages, just change XInjectionManager to CompatInjectionManager.

First thing first, add the lifecycle callbacks listeners. At this step the library registers the lifecycle listener for the future activities and the fragments so the components that are bound to the activity or fragment will be destroyed right after the destruction of the owner.

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        XInjectionManager.init(this)
    }
}

For example, the FirstFragment (also it works for the activities too) has a component, so you must implement the IHasComponent interface and call the bindComponent method of the XInjectionManager class. When the component is bound, it is available for other classes, but make sure, that these classes will not live longer than the owner of the component.

class FirstFragment : Fragment(), IHasComponent<FirstFeatureComponent> {
    //code...
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        XInjectionManager.bindComponent(this).inject(this)
    }

    override fun getComponent(): FirstFeatureComponent =
        DaggerFirstFeatureComponent.builder()
            .build()
}

If the fragment doesn’t have its own component and uses the AppComponent to inject the dependencies, just call the findComponent method and specify the class of the component and that is all.

class SecondFragment : Fragment() {
    //code...
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        XInjectionManager.findComponent<AppComponent>().inject(this)
    }
}

Also, this method might be used for getting dagger dependencies to build some components.

class AnotherFragment : Fragment(), IHasComponent<AnotherFeatureComponent> {
    //code...
    override fun getComponent(): AnotherFeatureComponent =
        DaggerAnotherFeatureComponent.builder()
            .appDependency(XInjectionManager.findComponent())
            .build()
}

That's all. There is no need to write code that will save, search or remove components anymore.

For more information, please, read the wiki pages.

Links

Lifecycle aware Dagger components - ProAndroidDev

If you have any questions, feel free to ask me on LinkedIn.

componentsmanager's People

Contributors

dmitry-borodin avatar kostyalyapochkin avatar valeryponomarenko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

componentsmanager's Issues

IHasComponent::getComponent is named "createComponent" in wiki

This page states that the function's name is createComponent():

https://github.com/ValeryPonomarenko/ComponentsManager/wiki/Bind-to-the-Application

While in fact this method is currently named getComponent():

PS: I know it's easier to change wiki texts, but I really like createComponent() better 🙂

How to unbind already bounded component and rebind another one

I have some component.

CompatInjectionManager.instance.bindComponent(object : IHasComponent<NetworkComponent> {
            override fun getComponent() = DaggerNetworkComponent.builder()
                    .apiModule(**apiModuleOffline**)     // here i have another variant
                    .iNetworkDependencies(CompatInjectionManager.instance.findComponent())
                    .build()

In some optional case i want to rebind that component with another parameter:

CompatInjectionManager.instance.bindComponent(object : IHasComponent<NetworkComponent> {
            override fun getComponent() = DaggerNetworkComponent.builder()
                    .apiModule(apiModuleONLINE**) // another parameter
                     ...

Could you please tell me how to do that ?
Thanks a lot!

Can you add more example code

Hello ValeryPonomarenko! Thanks for your work! It's really nice work! i have question about android services and dagger depends.. can you add more example about usability in custom android view / services and so on...

Change configuration

Good afternoon, when using your library, only the top fragment survives the configuration change, I think this is due to the use of FragmentManager.FragmentLifecycleCallbacks ().

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.