{"id":32972591,"url":"https://github.com/emmano/Scopes","last_synced_at":"2025-11-16T03:01:27.947Z","repository":{"id":25110130,"uuid":"28531506","full_name":"emmano/Scopes","owner":"emmano","description":"Easier creation of Dagger ObjectGraph scopes with Retrofit and Butterknife niceties","archived":false,"fork":false,"pushed_at":"2019-12-30T21:49:21.000Z","size":994,"stargazers_count":34,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-06-25T09:40:52.464Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/emmano.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-27T07:42:29.000Z","updated_at":"2023-03-11T15:41:24.000Z","dependencies_parsed_at":"2022-08-23T19:40:45.027Z","dependency_job_id":null,"html_url":"https://github.com/emmano/Scopes","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/emmano/Scopes","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emmano%2FScopes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emmano%2FScopes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emmano%2FScopes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emmano%2FScopes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/emmano","download_url":"https://codeload.github.com/emmano/Scopes/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emmano%2FScopes/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":284654194,"owners_count":27041729,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-11-16T02:00:05.974Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":[],"created_at":"2025-11-13T05:00:36.452Z","updated_at":"2025-11-16T03:01:27.942Z","avatar_url":"https://github.com/emmano.png","language":"Java","funding_links":[],"categories":["Dependency Injections"],"sub_categories":[],"readme":"[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Scopes-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/1286)\nScopes\n======\n\n###What is Scopes?\n\nHave you ever tried to set up scoped `ObjectGraphs` with Dagger and failed miserably? Scopes is a compile time annotation processor that is here to help!\n\n###What does Scopes do?\nIt allows to separate portions of your `Application` in logical \"flows\". It generates \"`BaseActivity`s\" that contain common dependencies that other `Activities` that are part of the same flow could use.\n\n###What the hell are you talking about?!\nHere is an example. Let's say that your `Application` has a login/signup flow (i.e. a screen with a login button, a another one with an \"Enter username and password\", etc). It is really likely that `Activities` that are part of this flow will have common dependencies (i.e. an `AuthenticatorService.java`,`LoginErrorDialog.java`, etc). Scopes allows you to define a `BaseActivity` that contains all these shared dependencies.\n\n\n###Ok, got it, how do I use it?\nIt all starts by defining a class that is Annotated with `@DaggerScope`; it does not need to be an `Activity`. If you decide not to annotate your `Activity`, the class annotated with `@DaggerScope` has to be in the same package as the `Activity` that extends the generated `BaseActivity` (huh?!... Look at the `app` module and you will see what I mean).\n\n```java\n@DaggerScope(baseActivityName = \"BaseLoginFlowActivity\", retrofitServices = GithubService.class,\n    restAdapterModule = RestAdapterModule.class, butterKnife = true)\npublic class LoginFlow {}\n```\n\n`baseActivityName` is the name you want to give to the parent `Activity` (it should be distinct for all `BaseActivities`)\n\n`retrofitServices` takes in an array of `Class` `Objects` that are the retrofit `interfaces` you have defined\n\n`restAdapterModule` is a `Module` that contains a provider for the `RestAdapter` to be used to create the `retrofitServices`\n\n`butterKnife` it is an optional field that tells `Scopes` to wire up `ButterKnife` on the `BaseActivity`.\n    \n**Once you build your project**, `Scopes` will generate `BaseLoginFlowActivity.java` with the following content: \n\n```java\npackage me.emmano.scopes.app.login;\n\nimport android.app.Activity;\nimport android.os.Bundle;\nimport dagger.ObjectGraph;\nimport butterknife.ButterKnife;\npublic abstract class BaseLoginFlowActivity extends Activity {\n  @javax.inject.Inject\n  protected me.emmano.scopes.app.services.GithubService githubService;\n\n  private ObjectGraph scopedObjectGraph;\n  @Override\n  protected void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(getLayout());\n    ButterKnife.inject(getActivity());\n    scopedObjectGraph = ((me.emmano.scopes.app.ScopedApplication)getApplication()).getObjectGraph().plus(new me.emmano.scopes.app.login.BaseLoginFlowActivityModule()).plus(getModules());\n    scopedObjectGraph.inject(this);\n  }\n  @Override\n  protected void onDestroy() {\n    scopedObjectGraph = null;\n    super.onDestroy();\n  }\n  protected abstract Object[] getModules();\n  protected abstract Activity getActivity();\n  protected abstract int getLayout();\n}\n```\n\nAs you can see `Scopes` creates `BaseLoginFlowActivityModule.java` that contains `@Providers` for the `retrofitServices`. This class uses the `RestAdapter` you provided to `@DaggerScope` to create the `retrofitServices`. If you did not provide a `RestAdapter`, `Scopes` assumes your `Application` `Class` has a module that will provide a `RestAdapter`. You will have to add `@ApplicationGraph` to a `public` method that returns the `Application`'s `ObjectGraph`. In any way, you have to supply a `RestAdapter` one way or another.  (more about `@ApplicationGraph` below)\n\n```java\npackage scopes;\n\nimport services.GithubService;\nimport retrofit.RestAdapter;\n@dagger.Module(injects = me.emmano.scopes.app.BaseLoginFlowActivity.class, includes = modules.RestAdapterModule.class)\npublic class BaseLoginFlowActivityModule {\n  @dagger.Provides\n  public GithubService providesGithubService(RestAdapter adapter) {\n    return adapter.create(services.GithubService.class);\n  }\n}\n```\n    \n### This is all fine and great, but what else do I have to do?\nNow, you have to make your `Activity` extends the `BaseActivity` generated by `Scopes`; in this case it will be `BaseLoginFlowActivity`. You will have to implement a couple methods, in this case:\n\n```java\nprotected abstract Object[] getModules();\nprotected abstract Activity getActivity();\nprotected abstract int getLayout();\n```\n\nHere is a basic example of a class that extends `BaseLoginFlowActivity`:\n\n```java    \npackage me.emmano.scopes.app;\n\nimport android.app.Activity;\nimport android.os.Bundle;\nimport android.util.Log;\nimport android.widget.TextView;\n\nimport java.util.List;\n\nimport butterknife.InjectView;\nimport modules.ActivityModule;\nimport retrofit.Callback;\nimport retrofit.RetrofitError;\nimport retrofit.client.Response;\nimport me.emmano.scopes.app.BaseLoginFlowActivity;\nimport services.Repo;\n\n\npublic class MainActivity extends BaseLoginFlowActivity {\n\n    @InjectView(R.id.text)\n    protected TextView textView;\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        textView.setText(\"Eureka!\");\n        githubService.starGazers(new Callback\u003cList\u003cRepo\u003e\u003e() {\n            @Override\n            public void success(List\u003cRepo\u003e repos, Response response) {\n                for (Repo repo : repos) {\n                    Log.e(MainActivity.class.getSimpleName(), repo.getLogIn());\n                }\n            }\n\n            @Override\n            public void failure(RetrofitError error) {\n\n            }\n        });\n    }\n\n    @Override\n    protected Object[] getModules() {\n        return new Object[]{new ActivityModule()};\n    }\n\n    @Override\n    protected Activity getActivity() {\n        return this;\n    }\n\n    @Override\n    protected int getLayout() {\n        return R.layout.activity_main;\n    }\n}\n```\n\nThere is one last thing for you to do. `getModules()` gives you the option to add extra dependencies to be used just in this class (namely, `MainActivity`). The most simplistic implementation of a `Module` could be as follows:\n\n```java\npackage modules;\n\nimport dagger.Module;\nimport me.emmano.scopes.app.login.MainActivity;\nimport scopes.BaseLoginFlowActivityModule;\n\n@Module(injects = MainActivity.class, addsTo = BaseLoginFlowActivityModule.class)\npublic class ActivityModule {}\n```\n    \nPlease note `addsTo`. Unfortunately, you will have to manually add the `Module` to which we are plus()sing. This will always be the `Module` generated by `Scope` that is injected on the `BaseActivity`; `BaseLoginFlowActivityModule` in this case.\n\n### What if I want Scopes to use dependencies from my Application's ObjectGraph?!\nThat is what `@ApplicationGraph` is for. Let's say you have dependencies that are common to your whole application (i.e. a `Bus`, a `RestAdapter`, etc) and you want your scoped graphs to have these dependencies; after all, this is what scopes are about. How do I do it? here is how:\n\n```java\n@ApplicationGraph(applicationModule = ApplicationModule.class)\npublic ObjectGraph getObjectGraph() {return objectGraph;}\n```\nYou can name this method whatever you like, but it must be public and reside inside your `Application` class. `ApplicationModule` is the module that contains the `@Providers` for dependencies that are common for the whole `Application`\n\n### TODO\nTons of refactoring. Kittens are currently dying due to some code on the `ScopeProcessor` class.\n\nAdd a parameter to `@DaggerScope` that allows passing an `Classes[]` to be injected on the `BaseActivity`. Right now, only `Retrofit` services can be injected. You can currently add these dependencies to your version of `ActivityModule`, add the corresponding `@Injects` and `extends` your version of `MainActivity` to get regular `Objects` other than `retrofitServices` injected. It is hacky and nasty, I know.\n\n###Installation\nJust add the dependency to your `build.gradle`:\n``` gradle\nimplementation 'me.emmano:scopes:0.1.5'\napt 'me.emmano:scopes-compiler:0.1.5@jar'\n```\n`Scopes` requires the `apt` plugin. You can add it easily by adding this to your `build.gradle`:\n```gradle\nbuildscript {\n    repositories {\n        jcenter()\n    }\n    dependencies {\n        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'\n    }\n}\n\napply plugin: 'com.android.application'\napply plugin: 'android-apt'\n```\n\nLastly, add this inside `android{}` in your `build.gradle`:\n```gradle\npackagingOptions {\n    exclude 'META-INF/services/javax.annotation.processing.Processor'\n}\n```\nFor more help setting up `Scopes` you can look at the `app` sample module.\n\nLicense\n-------\n\n    The MIT License (MIT)\n\n    Copyright (c) 2014 Emmanuel Ortiguela\n\n    Permission is hereby granted, free of charge, to any person obtaining a copy\n    of this software and associated documentation files (the \"Software\"), to deal\n    in the Software without restriction, including without limitation the rights\n    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n    copies of the Software, and to permit persons to whom the Software is\n    furnished to do so, subject to the following conditions:\n    \n    The above copyright notice and this permission notice shall be included in\n    all copies or substantial portions of the Software.\n    \n    THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n    THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femmano%2FScopes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femmano%2FScopes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femmano%2FScopes/lists"}