Welcome to Kompost, the scoping and dependency injection library named while I was gardening. Have fun figuring out what everything means. This isn't a serious project, just something from my free time to make DI a bit more amusing.
- Application Scope: Dependencies available to all other scopes within the application.
- Activity Scope: Dependencies tied to the lifecycle of an activity.
- Fragment Scope: Dependencies tied to the lifecycle of a fragment.
- ViewModel Scope: Manage ViewModels with or without
SavedStateHandle
. - Custom Scopes: Flexible scopes to meet specific application needs.
Planting your first seed is easy! Just follow the steps below and watch your dependencies sprout.
dependencies {
// Core
implementation("com.mustafadakhel.kompost:kompost-core:1.1.1")
// Android application support
implementation("com.mustafadakhel.kompost:kompost-android:1.1.1")
// Lifecycle support
implementation("com.mustafadakhel.kompost:kompost-lifecycle:1.1.1")
}
class KompostSampleApplication : Application() {
override fun onCreate() {
super.onCreate()
kompostSampleApplicationFarm()
}
}
private fun Application.kompostSampleApplicationFarm() = createApplicationFarm {
// Application-wide dependencies
produce { DependencyAvailableApplicationWide() }
}
fun ApplicationFarm.activities() = createActivitiesFarm {
// Activities dependencies
produce { ActivityRepository(supply()) }
}
fun ComponentActivity.setupFragmentScopedFarm() = getOrCreateActivityScopedFarm {
produceFragmentScopedFarm {
// Fragment-scoped dependencies
produce { FragmentRepository(supply()) }
}
}
Kompost supports various scopes to ensure your dependencies grow where theyβre needed.
Dependencies available to all other scopes within the application.
private fun Application.kompostSampleApplicationFarm() = createApplicationFarm {
singleton(dependency = Database())
}
Dependencies tied to the lifecycle of an activity.
fun ApplicationFarm.activities() = createActivitiesFarm {
produce { ActivityRepository(supply()) }
}
Dependencies tied to the lifecycle of a fragment.
fun ComponentActivity.setupFragmentScopedFarm() = getOrCreateActivityScopedFarm {
produceFragmentScopedFarm {
produce { FragmentRepository(supply()) }
}
}
ViewModels with and without SavedStateHandle
.
fun RootActivitiesFarm.viewModels() = createViewModelsFarm {
produce { MainViewModel(supply()) }
produceViewModelWithSavedState { savedStateHandle ->
MainViewModelWithSavedStateHandle(supply(), savedStateHandle)
}
}
Kompost allows you to create custom scopes tailored to your application's specific needs.
class CustomScopeFarm : Producer {
// Custom scope implementation
}
fun Application.createCustomScopeFarm() {
val customScopeFarm = CustomScopeFarm()
// Custom scope setup
}
Hereβs a text diagram to show you how all these scopes stack up:
Application Scope
βββ Activity Scope
βββ Fragment Scope
βββ ViewModel Scope (Activity)
βββ ViewModel without SavedStateHandle
βββ ViewModel with SavedStateHandle
This project is done in free time and isnβt meant to be taken too seriously. However, if you find it useful and have ideas to improve it, feel free to fork, contribute, or open a pull request.
Apache License.
Happy planting! π»