This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for androidx.fragment.app.FragmentFactory
- Loading branch information
1 parent
9445425
commit b1b6f82
Showing
22 changed files
with
249 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...le/src/main/java/org/rewedigital/katana/android/example/fragment/FragmentFactoryModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.rewedigital.katana.android.example.fragment | ||
|
||
import org.rewedigital.katana.Module | ||
import org.rewedigital.katana.android.example.fragment.view.FirstFragment | ||
import org.rewedigital.katana.android.example.fragment.view.SecondFragment | ||
import org.rewedigital.katana.androidx.fragment.KatanaFragmentFactory | ||
import org.rewedigital.katana.dsl.component | ||
import org.rewedigital.katana.dsl.factory | ||
import org.rewedigital.katana.dsl.singleton | ||
|
||
val FragmentFactoryModule = Module( | ||
name = "FragmentFactoryModule" | ||
) { | ||
|
||
singleton { | ||
KatanaFragmentFactory(component) | ||
.handlesFragment<FirstFragment>() | ||
.handlesFragment<SecondFragment>() | ||
} | ||
|
||
factory { FirstFragment(component) } | ||
|
||
factory { SecondFragment(component) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Fragment constructor injection with FragmentFactory | ||
|
||
This artifact provides additional support for `androidx.fragment` enabling constructor injection in Fragments with | ||
`KatanaFragmentFactory`. | ||
|
||
```kotlin | ||
// In a module: | ||
|
||
val module = Module { | ||
|
||
singleton { | ||
KatanaFragmentFactory(component) | ||
.handlesFragment<FirstFragment>() | ||
.handlesFragment<SecondFragment>(name = "SecondFragment") | ||
} | ||
|
||
factory { FirstFragment(get()) } | ||
|
||
factory(name = "SecondFragment") { SecondFragment(get(), get()) } | ||
} | ||
|
||
// ... then in an Activity: | ||
|
||
class MyActivity : AppCompatActivity() { | ||
|
||
private val fragmentFactory by applicationComponent.inject<KatanaFragmentFactory>() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
// Must be set **before** super call for Fragment instantiation after orientation change | ||
supportFragmentManager.fragmentFactory = fragmentFactory | ||
|
||
super.onCreate(savedInstanceState) | ||
|
||
// ... | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
plugins { | ||
`base-android-plugin` | ||
} | ||
|
||
configureBase( | ||
artifactName = "katana-androidx-fragment", | ||
sourcePath = android.sourceSets["main"].java.srcDirs, | ||
publicationComponent = components["android"] | ||
) | ||
|
||
dependencies { | ||
api(project(":core")) | ||
api(Dependencies.androidXCollection) | ||
api(Dependencies.androidXFragment) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest package="org.rewedigital.katana.androidx.fragment"/> |
111 changes: 111 additions & 0 deletions
111
...ragment/src/main/kotlin/org/rewedigital/katana/androidx/fragment/KatanaFragmentFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package org.rewedigital.katana.androidx.fragment | ||
|
||
import androidx.collection.ArrayMap | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentFactory | ||
import org.rewedigital.katana.Component | ||
import org.rewedigital.katana.KatanaException | ||
|
||
/** | ||
* A [FragmentFactory] that utilizes Katana for [Fragment] instance resolution. | ||
* | ||
* The factory can only provide instances of Fragments that have been registered via [handlesFragment] or | ||
* [handlesFragmentVia]. The referred component(s) must provide bindings for these Fragments. | ||
* | ||
* ``` | ||
* val module = Module { | ||
* | ||
* singleton { | ||
* KatanaFragmentFactory(component) | ||
* .handlesFragment<FirstFragment>() | ||
* .handlesFragment<SecondFragment>(name = "SecondFragment") | ||
* } | ||
* | ||
* factory { FirstFragment(get()) } | ||
* | ||
* factory(name = "SecondFragment") { SecondFragment(get(), get()) } | ||
* } | ||
* ``` | ||
* | ||
* @param defaultComponent Optional default [Component] required for [handlesFragment] | ||
* @param delegateToSuper If `true`, will delegate requests to super FragmentFactory if this factory cannot resolve Fragment. Might be useful for third-party Fragments like `NavHostFragment` for instance. | ||
* | ||
* @see handlesFragment | ||
* @see handlesFragmentVia | ||
*/ | ||
@Suppress("unused", "MemberVisibilityCanBePrivate") | ||
class KatanaFragmentFactory( | ||
defaultComponent: Component? = null, | ||
private val delegateToSuper: Boolean = true | ||
) : FragmentFactory() { | ||
|
||
@PublishedApi | ||
internal val defaultComponentProvider = | ||
defaultComponent?.let { { it } } | ||
|
||
@PublishedApi | ||
internal val providers = ArrayMap<String, () -> Fragment>() | ||
|
||
/** | ||
* Declare that this factory handles Fragment instantiations of given type. | ||
* | ||
* A default [Component] **must** be passed to constructor of factory in order for this to work. | ||
* The default component must provide a binding for the requested Fragment which is bound to the class or | ||
* optional name. For instance a module might declare: | ||
* | ||
* ``` | ||
* factory { MyFragment(get(), get()) } | ||
* ``` | ||
* | ||
* Use [handlesFragmentVia] for adding a Fragment provider associated with a different component. | ||
* | ||
* @see handlesFragmentVia | ||
*/ | ||
inline fun <reified T : Fragment> handlesFragment( | ||
name: String? = null | ||
): KatanaFragmentFactory { | ||
defaultComponentProvider?.let { component -> | ||
val fragmentClassName = T::class.java.name | ||
providers[fragmentClassName] = { component().injectNow<T>(name = name) } | ||
} ?: throw KatanaException("No default component passed to constructor of KatanaFragmentFactory") | ||
return this | ||
} | ||
|
||
/** | ||
* Declare that this factory handles Fragment instantiations via provided [Component]. | ||
* | ||
* The supplied [Component] must provide a binding for the requested Fragment which is bound to the class or | ||
* optional name. For instance a module might declare: | ||
* | ||
* ``` | ||
* factory { MyFragment(get(), get()) } | ||
* ``` | ||
* | ||
* @see handlesFragment | ||
*/ | ||
inline fun <reified T : Fragment> handlesFragmentVia( | ||
name: String? = null, | ||
noinline component: () -> Component | ||
): KatanaFragmentFactory { | ||
val fragmentClassName = T::class.java.name | ||
providers[fragmentClassName] = { component().injectNow<T>(name = name) } | ||
return this | ||
} | ||
|
||
/** | ||
* Create a new instance of a Fragment with the given class name. | ||
* | ||
* If this Fragment has not been registered via [handlesFragment] or [handlesFragmentVia] and [delegateToSuper] is `true`, | ||
* will call super [FragmentFactory] for resolution. Else throws [KatanaException]. | ||
* | ||
* @throws KatanaException | ||
*/ | ||
override fun instantiate(classLoader: ClassLoader, className: String) = | ||
providers[className]?.let { provider -> | ||
provider() | ||
} ?: if (delegateToSuper) | ||
super.instantiate( | ||
classLoader, | ||
className | ||
) else throw KatanaException("No Fragment provider found for class $className (delegateToSuper == false)") | ||
} |
Empty file.
Oops, something went wrong.