{"id":35718180,"url":"https://github.com/mercari/nav-entry-scope-android","last_synced_at":"2026-01-13T20:36:33.984Z","repository":{"id":329065208,"uuid":"1097854651","full_name":"mercari/nav-entry-scope-android","owner":"mercari","description":"This project offers an implementation for the NavEntryScope pattern used for sharing dependencies across multiple ViewModels on the same screen.","archived":false,"fork":false,"pushed_at":"2025-12-18T06:29:46.000Z","size":138,"stargazers_count":4,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-20T18:16:14.206Z","etag":null,"topics":["android","dependency-injection","hilt","kotlin","library"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mercari.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-11-16T23:42:45.000Z","updated_at":"2025-12-17T05:30:10.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/mercari/nav-entry-scope-android","commit_stats":null,"previous_names":["mercari/nav-entry-scope-android"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/mercari/nav-entry-scope-android","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mercari%2Fnav-entry-scope-android","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mercari%2Fnav-entry-scope-android/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mercari%2Fnav-entry-scope-android/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mercari%2Fnav-entry-scope-android/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mercari","download_url":"https://codeload.github.com/mercari/nav-entry-scope-android/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mercari%2Fnav-entry-scope-android/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28399895,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T14:36:09.778Z","status":"ssl_error","status_checked_at":"2026-01-13T14:35:19.697Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","dependency-injection","hilt","kotlin","library"],"created_at":"2026-01-06T06:27:01.769Z","updated_at":"2026-01-13T20:36:33.977Z","avatar_url":"https://github.com/mercari.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NavEntryScope library and annotation processor\n\n[![CI](https://github.com/mercari/nav-entry-scope-android/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/mercari/nav-entry-scope-android/actions/workflows/ci.yml)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE.txt)\n[![Latest version (lib)](https://img.shields.io/maven-central/v/com.mercari/nav-entry-scope?label=nav-entry-scope)](https://central.sonatype.com/artifact/com.mercari/nav-entry-scope)\n[![Latest version (processor)](https://img.shields.io/maven-central/v/com.mercari/nav-entry-scope-processor?label=nav-entry-scope-processor)](https://central.sonatype.com/artifact/com.mercari/nav-entry-scope-processor)\n![Android](https://img.shields.io/badge/Platform-Android-green.svg)\n![Kotlin](https://img.shields.io/badge/Kotlin-2.0-blue.svg)\n\nThis project offers an implementation for the **NavEntryScope** pattern used for sharing dependencies across multiple ViewModels on the same screen, along with a sample app.\n\nTo learn more, check out the droidcon Italy Slides: [Slide Deck](https://speakerdeck.com/bxttx/beyond-hilts-built-in-scopes-scope-shared-dependencies-to-the-current-screen)\n\n## What is NavEntryScope?\n\nIn single-activity Android apps with Compose navigation, screens often have multiple ViewModels that need to share data. The existing Hilt scopes don't fit this use case well:\n\n- `@Singleton` / `@ActivityRetainedScoped`: Too broad because shared across all screens, causing state leakage between multiple instances of the same screen in the back stack\n- `@ViewModelScoped`: Too narrow because each ViewModel gets its own instance, making sharing impossible\n\n**NavEntryScope** is a custom Hilt scope that provides screen-level scoping. Dependencies annotated with `@NavEntryScoped` are:\n- Shared across all ViewModels on the same screen\n- Isolated per navigation entry: each screen instance gets its own dependencies\n- Automatically cleaned up when the screen is removed from the back stack\n\n## When to Use NavEntryScope\n\nUse NavEntryScope when you have:\n- Multiple ViewModels on the same screen that depend on shared data\n- Complex screens where a single ViewModel would become too large\n- Reusable UI components with their own ViewModels that need access to screen-level data\n- Multiple instances of the same screen in the navigation back stack\n\n## How to Use\n\n### 1. Add Dependencies\n\nAdd the library and annotation processor to your module's `build.gradle.kts`:\n\n```kotlin\ndependencies {\n    implementation(\"com.mercari:nav-entry-scope:\u003cversion\u003e\")\n    ksp(\"com.mercari:nav-entry-scope-processor:\u003cversion\u003e\")\n}\n```\n\nReplace `\u003cversion\u003e` with the latest version shown in the badge above.\n\n### 2. Annotate Shared Dependencies\n\n```kotlin\n@Module\n@InstallIn(NavEntryComponent::class)\ninterface YourModule {\n\n    @Binds\n    @NavEntryScoped\n    fun bindRepository(impl: YourRepositoryImpl): YourRepository\n}\n```\n\n### 3. Inject into ViewModels\n\nViewModels remain standard `@HiltViewModel` classes. Injecting a dependency that depends on `@NavEntryScoped` into your ViewModel is also supported.\n\n```kotlin\n@HiltViewModel\nclass YourViewModel @Inject constructor(\n    private val repository: YourRepository  // NavEntryScoped\n) : ViewModel()\n```\n\n### 4. Use in Compose\n\nReplace `hiltViewModel()` with `navEntryScopedViewModel()`:\n\n```kotlin\n@Composable\nfun YourScreen() {\n    val viewModel: YourViewModel = navEntryScopedViewModel()\n    // ...\n}\n```\n\nThe annotation processor automatically generates the necessary Dagger modules (`NavEntry_Module`) and entry points (`NavEntry_EntryPoint`) to bridge `NavEntryComponent` and `ViewModelComponent`.\n\n## Project Structure\n\n### [`app`](app/)\nSample application demonstrating NavEntryScope usage with:\n- `SharedCounterRepository` - A `@NavEntryScoped` repository holding the shared value\n- `CounterLabelViewModel` - Displays the counter value\n- `IncrementButtonViewModel` - Increments the counter\n\nBoth ViewModels access the same repository instance, demonstrating data sharing on a single screen.\n\n### [`nav-entry-scope/lib`](nav-entry-scope/lib/)\nCore library providing:\n- `@NavEntryScoped` - Scope annotation for navigation entry lifetime\n- `NavEntryComponent` - Custom Dagger component definition\n- `NavEntryComponentOwner` - ViewModel managing component lifecycle\n- `navEntryScopedViewModel()` - Compose function for injecting ViewModels with NavEntryScoped dependencies\n\n### [`nav-entry-scope/processor`](nav-entry-scope/processor/)\nAnnotation processor (KSP) that generates:\n- `NavEntry_EntryPoint` - Hilt EntryPoint interface installed in `NavEntryComponent`\n- `NavEntry_Module` - Dagger module installed in `ViewModelComponent` that provides NavEntryScoped dependencies\n\n## Local Development\n\n### Testing Changes Locally\n\nTo test library changes immediately in the sample app, publish to Maven Local:\n\n```bash\n./gradlew publishToMavenLocal\n```\n\nThe sample app is already configured to use `mavenLocal()`, so changes will be picked up on the next build.\n\n### Using Snapshots\n\nTo use snapshot versions of the library, add the snapshot repository to your `settings.gradle.kts`:\n\n```kotlin\ndependencyResolutionManagement {\n    repositories {\n        maven(\"https://central.sonatype.com/repository/maven-snapshots/\")\n        mavenCentral()\n    }\n}\n```\n\nThen use the snapshot version in your dependencies:\n\n```kotlin\nimplementation(\"com.mercari:nav-entry-scope:\u003cversion\u003e-SNAPSHOT\")\nksp(\"com.mercari:nav-entry-scope-processor:\u003cversion\u003e-SNAPSHOT\")\n```\n\n## Contribution\n\nIf you want to submit a PR for bug fixes or documentation, please read the [CONTRIBUTING.md](CONTRIBUTING.md) and follow the instruction beforehand.\n\n## License\n\nnav-entry-scope-android is released under the [MIT License](LICENSE.txt).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmercari%2Fnav-entry-scope-android","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmercari%2Fnav-entry-scope-android","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmercari%2Fnav-entry-scope-android/lists"}