GithubHelp home page GithubHelp logo

fknives / android-lib-reloadablehiltmodule Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 127 KB

Annotation processor which can generates modules, integrated with Hilt, that can be reload.

License: Apache License 2.0

Kotlin 95.62% Java 4.38%

android-lib-reloadablehiltmodule's Introduction

ReloadableHiltModule

Annotation processor which can generates modules integrated with Hilt that can be reload. Reload means all the objects provided by the Module will be kept until it's reloaded. At that point the provided instances are recreated.

Why

Hilt is opinionated and clear on it's rules. However with dagger I was able to have Components scoped for a Logged In User, or for a specific flow.

Today the equivalent can be done as detailed here: https://medium.com/androiddevelopers/hilt-adding-components-to-the-hierarchy-96f207d6d92d

However this requires to inject a UserManager, and inject your dependencies from EntryPoints. Which doesn't seem to worth it for me.

Of course if you have Single Activity for your logged in state, Hilt works just fine.

My case

In my case however, I still have multiple activities on some projects, so It would be great to easily clean caches when my user logs out.

I had the idea to borrow the concept loadKoinModules with override which can be used just to do that. So in this annotation processor I generate a module definition which works similarly.

Setup

Latest version: Latest release

// top level build.gradle //..

subprojects {
    repositories {
        // ...
        maven {
            url "https://maven.pkg.github.com/fknives/ReloadableHiltModule"
            credentials {
                username = project.findProperty("GITHUB_USERNAME") ?: System.getenv("GITHUB_USERNAME")
                password = project.findProperty("GITHUB_TOKEN") ?: System.getenv("GITHUB_TOKEN")
            }
            // how to get token
            // https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token
        }
    }
}

// local build.gradle

kapt {
    correctErrorTypes = true
}

dependencies {
    implementation "org.fnives.library.reloadable.module:annotation:$latest_version"
    kapt "org.fnives.library.reloadable.module:annotation-processor:$latest_version"
}

Usage

Create your ModuleAnnotation, example:

import org.fnives.library.reloadable.module.annotation.ReloadableModule

@ReloadableModule
@Target(AnnotationTarget.CONSTRUCTOR)
@Retention(AnnotationRetention.SOURCE)
annotation class LoggedInModuleInject

Apply to your classes such as:

class ContentRepository @LoggedInModuleInject constructor(private val contentGenerator: ContentGenerator) {

Where you want to reload the module, inject the generated Helper class:

import javax.inject.Inject

class LogoutUseCase @Inject constructor(
    private val reloadLoggedInModuleInjectModule: ReloadLoggedInModuleInjectModule
) {

    fun invoke() {
        reloadLoggedInModuleInjectModule.reload()
    }
}

android-lib-reloadablehiltmodule's People

Contributors

fknives avatar dependabot[bot] avatar

Watchers

 avatar

android-lib-reloadablehiltmodule's Issues

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.