Git Product home page Git Product logo

kompost's Introduction

Kompost: Gardening-Inspired Scoping and DI 🌱

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.

Features

  • 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.

Quick Start

Planting your first seed is easy! Just follow the steps below and watch your dependencies sprout.

Step 1: Add Kompost to Your Project

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")
}

Step 2: Create Your Application Farm

class KompostSampleApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        kompostSampleApplicationFarm()
    }
}

private fun Application.kompostSampleApplicationFarm() = createApplicationFarm {
    // Application-wide dependencies
    produce { DependencyAvailableApplicationWide() }
}

Step 3: Set Up Activity and Fragment Farms

fun ApplicationFarm.activities() = createActivitiesFarm {
    // Activities dependencies
    produce { ActivityRepository(supply()) }
}

fun ComponentActivity.setupFragmentScopedFarm() = getOrCreateActivityScopedFarm {
    produceFragmentScopedFarm {
        // Fragment-scoped dependencies
        produce { FragmentRepository(supply()) }
    }
}

Scopes of Dependency Injection

Kompost supports various scopes to ensure your dependencies grow where they’re needed.

Application Scope

Dependencies available to all other scopes within the application.

private fun Application.kompostSampleApplicationFarm() = createApplicationFarm {
    singleton(dependency = Database())
}

Activity Scope

Dependencies tied to the lifecycle of an activity.

fun ApplicationFarm.activities() = createActivitiesFarm {
    produce { ActivityRepository(supply()) }
}

Fragment Scope

Dependencies tied to the lifecycle of a fragment.

fun ComponentActivity.setupFragmentScopedFarm() = getOrCreateActivityScopedFarm {
    produceFragmentScopedFarm {
        produce { FragmentRepository(supply()) }
    }
}

ViewModel Scope

ViewModels with and without SavedStateHandle.

fun RootActivitiesFarm.viewModels() = createViewModelsFarm {
    produce { MainViewModel(supply()) }
    produceViewModelWithSavedState { savedStateHandle ->
        MainViewModelWithSavedStateHandle(supply(), savedStateHandle)
    }
}

Custom Scopes

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
}

Scope Hierarchy

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

Contributing

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.

License

Apache License.

Happy planting! 🌻

kompost's People

Contributors

mustafadakhel avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.