All Projects → ValeryPonomarenko → ComponentsManager

ValeryPonomarenko / ComponentsManager

Licence: MIT license
A service locator that makes work with Dagger2 in multi-module project easier. Allows binding components life to Activity/Fragment life-cycle.

Programming Languages

kotlin
9241 projects
java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to ComponentsManager

Android App Architecture Mvvm Databinding
A simple but complete project (in both Java & Kotlin) to demonstrate the Android application architecture with MVVM pattern, a client app for The Movie DB Web API. Dagger2 is used for dependency injection and RxJava is used for RFP (Reactive Functional Programming).
Stars: ✭ 69 (-1.43%)
Mutual labels:  dependency-injection, dagger2
Kotlin Mvvm Architecture
Android Architecture Design Patterns using Kotlin, MVVM, Dagger2, LiveData, Room, MediatorLiveData, NetworkBoundResources, Retrofit, AndroidX, ViewModels, Dependency Injection using Dagger2, Repository pattern.
Stars: ✭ 126 (+80%)
Mutual labels:  dependency-injection, dagger2
Price Tracker
Price Tracking Application - An experimental Kotlin Android project with complex android app requirements.
Stars: ✭ 80 (+14.29%)
Mutual labels:  dependency-injection, dagger2
Scabbard
🗡 A tool to visualize Dagger 2 dependency graphs
Stars: ✭ 615 (+778.57%)
Mutual labels:  dependency-injection, dagger2
ThinkRchive
An app showing all details for various Lenovo Thinkpad models. Made to try out Jepack Compose for Android.
Stars: ✭ 84 (+20%)
Mutual labels:  dependency-injection, dagger2
Android Mvvm Rxjava2 Dagger2
This repository contains a detailed sample application that uses MVVM as its presentation layer pattern. Essential dependencies are Dagger2 with Dagger-android, RxJava2 with RxAndroid, Room, Retrofit and Espresso.
Stars: ✭ 50 (-28.57%)
Mutual labels:  dependency-injection, dagger2
Dagger2 Mvp Sample
Android Sample to explain Dagger 2 and MVP in android applications.
Stars: ✭ 106 (+51.43%)
Mutual labels:  dependency-injection, dagger2
dagger-track
Gradle plugin to add clock trackings to your dagger components and subcomponents
Stars: ✭ 59 (-15.71%)
Mutual labels:  dependency-injection, dagger2
Movies Kotlin Kata
Katas for practice Kotlin( Coroutines, dataclasses, delegate properties...) Clean Architecture and best practices in Android(DI, Dagger, MVP, Espresso) implemented by Jorge Sánchez (Xurxodev)
Stars: ✭ 240 (+242.86%)
Mutual labels:  dependency-injection, dagger2
Android Clean Architecture
Showcasing a Clean Architecture approach from our Android applications framework!
Stars: ✭ 160 (+128.57%)
Mutual labels:  dependency-injection, dagger2
Dagger Android Injection
Sample project explains Dependency Injection in Android using dagger-android framework.
Stars: ✭ 524 (+648.57%)
Mutual labels:  dependency-injection, dagger2
Kata-Dagger2-Android
Kata to practice Dependency injection using Dagger 2.
Stars: ✭ 21 (-70%)
Mutual labels:  dependency-injection, dagger2
Instant Weather
An Android weather application implemented using the MVVM pattern, Retrofit2, Dagger2, LiveData, ViewModel, Coroutines, Room, Navigation Components, Data Binding and some other libraries from the Android Jetpack.
Stars: ✭ 473 (+575.71%)
Mutual labels:  dependency-injection, dagger2
Daggraph
Dagger dependency graph generator for Android Developers
Stars: ✭ 1,140 (+1528.57%)
Mutual labels:  dependency-injection, dagger2
Android Dagger2 Example
This project implements the dagger 2 in android for dependency injection
Stars: ✭ 453 (+547.14%)
Mutual labels:  dependency-injection, dagger2
Mvpandroid
Sample app to demonstrate MVP (Model - View - Presenter) architecture in android
Stars: ✭ 91 (+30%)
Mutual labels:  dependency-injection, dagger2
movie-booking
An example for booking movie seat, combined of Android Data Binding, State Design Pattern and Multibinding + Autofactory. iOS version is: https://github.com/lizhiquan/MovieBooking
Stars: ✭ 80 (+14.29%)
Mutual labels:  dependency-injection, dagger2
catchflicks
🎬 Kitchen sink project for learning android concepts 🎬
Stars: ✭ 12 (-82.86%)
Mutual labels:  dependency-injection, dagger2
Dagger2
Kotlin Dagger2 example project
Stars: ✭ 145 (+107.14%)
Mutual labels:  dependency-injection, dagger2
MultipleAppModules
An example how to use multiple application modules in one android project
Stars: ✭ 105 (+50%)
Mutual labels:  dependency-injection, dagger2

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].