Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AndroidX] Introducing koin-androidx-fragment with Koin fragmentFactory #647

Closed
arnaudgiuliani opened this issue Nov 21, 2019 · 6 comments
Closed
Milestone

Comments

@arnaudgiuliani
Copy link
Member

Hi All,

the 2.1.0-alpha-3 contains a new package: koin-androidx-fragment

This will allow to inject your Fragment with Koin 👍

How to use that?

  1. setup your KoinFragmentFactory at start with fragmentFactory():
 startKoin {
    androidLogger(Level.DEBUG)
    androidContext(this@MainApplication)

    // setup a KoinFragmentFactory instance
    fragmentFactory()

    modules(...)
}
  1. Use constructor for you Fragment class:
class MyFragment(val myService: MyService) : Fragment()
  1. Declare it in Koin (factory or scoped instance):
val appModule = module {
    
    factory { MyFragment(get()) }
}
  1. Setup the factory from your activity & just use it:

Setup the Koin factory from your activity:

class MyActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        // Koin Fragment Factory
        setupKoinFragmentFactory()

        super.onCreate(savedInstanceState)
        //...
    }
}

Get your Fragment:

supportFragmentManager.beginTransaction()
            .replace(R.id.mvvm_frame, MyFragment::class.java, null, null)
            .commit()

For more details (like using it with scope): https://doc.insert-koin.io/#/koin-android/fragment-factory

The package should be available soon 🎉

@mqueiroz1995
Copy link

Amazing work!

For some reason, the package contains org.koin.androidx.viewmodel.BuildConfig.

This results in the following error when adding both koin-androidx-fragment and koin-androidx-viewmodel as dependencies:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkDebugDuplicateClasses'.
> 1 exception was raised by workers:
  java.lang.RuntimeException: Duplicate class org.koin.androidx.viewmodel.BuildConfig found in modules classes.jar (org.koin:koin-androidx-fragment:2.1.0-alpha-3) and classes.jar (org.koin:koin-androidx-viewmodel:2.1.0-alpha-3)

Am I doing anything wrong?

@arnaudgiuliani
Copy link
Member Author

nothing, manifest is buggy in alpha-3. Check alpha-4 soon this week for a fix 👍

@anuragdalia
Copy link

alpha-6 is here and the duplicate class issue seems to persist?
Is there something I should be doing ?

@arnaudgiuliani
Copy link
Member Author

Yep sorry. The commit has been screwed :/
I will repush with right manifest fix 👍

Check alpha-7

@anuragdalia
Copy link

Can you elaborate on this
setupKoinFragmentFactory() this doesnt exist?!

Setup the Koin factory from your activity:

class MyActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    // Koin Fragment Factory
    setupKoinFragmentFactory()

    super.onCreate(savedInstanceState)
    //...
}

}

@arnaudgiuliani
Copy link
Member Author

setupKoinFragmentFactory() this is an extension function. Be sure to have your cache clear if you had previous glitched version.

@arnaudgiuliani arnaudgiuliani pinned this issue Dec 17, 2019
@arnaudgiuliani arnaudgiuliani unpinned this issue Dec 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants