Code Monkey home page Code Monkey logo

contributer's Introduction

Contributer

Introduction

Dagger android injection is awesome right? But it only works for a limited number of types.. With contributer you can use the @ContributesAndroidInjector pattern for each type such as views or conductor controllers.

Download

// in your root gradle
allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}
dependencies {
         // for view
         implementation 'com.github.IVIanuu.Contributer:contributer-view:LATEST-VERSION'
         
         // for conductor
         implementation 'com.github.IVIanuu.Contributer:contributer-conductor:LATEST-VERSION'
         
         // for custom types
         implementation 'com.github.IVIanuu.Contributer:contributer-annotations:LATEST-VERSION'
         
         // Required
         annotationProcessor 'com.github.IVIanuu.Contributer:contributer-processor:LATEST-VERSION'
	 
	 // remove this line!!
         annotationProcessor 'com.google.dagger:dagger-android-processor:2.13'
}

Views

Assume that your project contains a basic dagger setup.

First add the @AndroidInjectorKeyRegistry annotation to a class in your project for example your app component. This the only extra step required compared to regular android injection. Here you have to add the @ViewKey.

@AndroidInjectorKeyRegistry(keys = arrayOf(ViewKey::class))
@Singleton
@Component(modules = {
      // modules
})
public interface AppComponent {
}

Next create your binding modules like you would with dagger android. If you're not familiar with dagger android read this. https://google.github.io/dagger/android.html

@Module abstract class ViewBindingModule {

    @SomeScope
    @ContributesAndroidInjector(modules = [LoginModule::class, UiModule::class])
    abstract fun bindLoginView(): LoginView
    
    @ContributesAndroidInjector
    abstract fun bindPlayPauseView(): PlayPauseView

}

Add your binding module and the ViewInjectionModule to your app component.

@Singleton
@Component(modules = [
        AppModule::class,
        ViewBindingModule::class,
        ViewInjectionModule::class
        ]
))
interface AppComponent {
    fun inject(app: App)

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(application: Application): Builder

        fun build(): AppComponent
    }
}

Add the HasViewInjector interface to your app or base activity.

class App : Application(), HasViewInjector {

    @Inject lateinit var viewInjector: DispatchingAndroidInjector<View>

    override fun onCreate() {
        super.onCreate()

        DaggerAppComponent.builder()
                    .application(this)
                    .build()
                    .inject(this)
    }
    
    override fun viewInjector() = viewInjector
}

Now you're done you can now inject your views with ViewInjection.inject(view)

class MyView(context: Context, attrs: AttributeSet?) : View(context, attrs) {

    @Inject lateinit var audioManager: AudioManager
    @Inject lateinit var packageManager: PackageManager

    override fun onFinishInflate() {
        super.onFinishInflate()
        ViewInjection.inject(this)

        // todo use injected stuff
    }
}

Conductor

Absolutely the same as with views but replace the ViewKey with ControllerKey the HasViewInjector with HasControllerInjector and so on.

Custom types

  1. Create a annotation class annotated with @MapKey and a value with the supertype of the classes
  2. Replicate the HasSomeClassInjector, SomeClassInjection and SomeClassInjectionModule part
  3. Done:D

You can check out the view or conductor module to look how they are implemented.

Fun fact

Found out to late that contributer is a misspelling:DD

License

Copyright 2017 Manuel Wrage

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

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.