Git Product home page Git Product logo

Comments (5)

MichaelRocks avatar MichaelRocks commented on June 16, 2024

@alexeysuvorov Thank you for reporting this issue. Unfortunately, injection into library project isn't supported at the moment, which is definitely not good. I'm going to support Android library projects in the nearest future.

from lightsaber.

alex-y-su avatar alex-y-su commented on June 16, 2024

I am not sure this problem is in library. If all dependencies are defined inside library - everything just fine, but when I want a dependency to be defined out of library - on level of application which host the library - it stops to compile. The more I think about this problem the more I understand that it is quite hard to resolve this on compile time. However I still think it is quite often case and probably it is good to have option to "Promise" some dependencies inside a library module and then provide it by application injector or etc.

I've tried to imagine how could it be:

//------------------------------------------------------------------------------------
//In android library
//------------------------------------------------------------------------------------
@Module
@ProxyModule
class LibraryModule(val hostInjector: Injector) {
    @Provides
    fun proxyComponent() : IComponent = hostInjector.getInstance<IComponent>()
}

@Component
class LibraryComponent() {
    @Provides
    @Proxy
    fun getLibraryModule(hostInjector: Injector) : LibraryModule(hostInjector)
}


class SomeFragmentInLibrary : Fragment {
  override fun onCreate() {
    //get 
    val appInjector = ... //somehow get app injector
    val libInjector = Lightsaber.get().createChildInjector(appInjector, LibraryComponent())
    //resolve everything we need in lib
  }
}


//------------------------------------------------------------------------------------
//In anroid app
//------------------------------------------------------------------------------------

@Module
class ApplicationModule() {
    @Provides
    fun component() : IComponent = ComponentImpl()
}

@Component
class ApplicationComponent {
    @Provides
    fun appModule() = ApplicationModule()
}

//We should pass this instance to library
val appInjector = Lightsaber.get().createInjector(ApplicationComponent())

from lightsaber.

MichaelRocks avatar MichaelRocks commented on June 16, 2024

Actually you don't need to pass an Injector to LibraryModule. Instead you can just use the app injector and inject dependencies into SomeFragmentInLibrary. I think I've already fixed this issue so an updated version is going to be published soon.

from lightsaber.

MichaelRocks avatar MichaelRocks commented on June 16, 2024

@alexeysuvorov Now everything should work fine.

from lightsaber.

alex-y-su avatar alex-y-su commented on June 16, 2024

Works great in 0.8.3, thank you.

from lightsaber.

Related Issues (8)

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.