{"id":13428706,"url":"https://github.com/InflationX/ViewPump","last_synced_at":"2025-03-16T01:32:55.758Z","repository":{"id":40601767,"uuid":"78296126","full_name":"InflationX/ViewPump","owner":"InflationX","description":"View Inflation you can intercept.","archived":false,"fork":false,"pushed_at":"2023-07-27T18:33:03.000Z","size":592,"stargazers_count":897,"open_issues_count":9,"forks_count":61,"subscribers_count":27,"default_branch":"main","last_synced_at":"2024-10-27T05:37:13.445Z","etag":null,"topics":["android","inflation","interceptor","java","post-inflation-interceptors","pre-inflation-interceptors"],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/InflationX.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-01-07T18:27:58.000Z","updated_at":"2024-10-21T09:16:26.000Z","dependencies_parsed_at":"2024-01-14T02:37:32.800Z","dependency_job_id":"15ce0cad-5833-4843-a345-16ceb381feba","html_url":"https://github.com/InflationX/ViewPump","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InflationX%2FViewPump","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InflationX%2FViewPump/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InflationX%2FViewPump/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InflationX%2FViewPump/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/InflationX","download_url":"https://codeload.github.com/InflationX/ViewPump/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243814905,"owners_count":20352037,"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","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","inflation","interceptor","java","post-inflation-interceptors","pre-inflation-interceptors"],"created_at":"2024-07-31T01:01:03.214Z","updated_at":"2025-03-16T01:32:55.442Z","avatar_url":"https://github.com/InflationX.png","language":"Kotlin","readme":"ViewPump\n========\n\nView inflation you can intercept.\n\nViewPump installs a custom `LayoutInflater` via a `ContextThemeWrapper` and provides an API of pre/post-inflation interceptors.\n\n## Getting started\n\n### Dependency\n\nInclude the dependency [Download (.aar)](http://search.maven.org/remotecontent?filepath=io/github/inflationx/viewpump/2.1.0/viewpump-2.1.0.aar) :\n\n```groovy\ndependencies {\n    implementation 'io.github.inflationx:viewpump:2.1.0'\n}\n```\n\n### Usage\n\nDefine your interceptor. Below is a somewhat arbitrary example of a post-inflation interceptor that prefixes the text in a TextView.\n\n```java\npublic class TextUpdatingInterceptor implements Interceptor {\n    @Override\n    public InflateResult intercept(Chain chain) {\n        InflateResult result = chain.proceed(chain.request());\n        if (result.view() instanceof TextView) {\n            // Do something to result.view()\n            // You have access to result.context() and result.attrs()\n            TextView textView = (TextView) result.view();\n            textView.setText(\"[Prefix] \" + textView.getText());\n        }\n        return result;\n    }\n}\n```\n\nBelow is an example of a pre-inflation interceptor that returns a CustomTextView when a TextView was defined in the layout's XML.\n\n```java\npublic class CustomTextViewInterceptor implements Interceptor {\n    @Override\n    public InflateResult intercept(Chain chain) {\n        InflateRequest request = chain.request();\n        if (request.name().endsWith(\"TextView\")) {\n            CustomTextView view = new CustomTextView(request.context(), request.attrs());\n            return InflateResult.builder()\n                    .view(view)\n                    .name(view.getClass().getName())\n                    .context(request.context())\n                    .attrs(request.attrs())\n                    .build();\n        } else {\n            return chain.proceed(request);\n        }\n    }\n}\n```\n\n## Installation\n\nCreate a `ViewPump` instance via `ViewPump.builder()` and add your interceptors. The order of the interceptors is important since they form the interceptor chain of requests and results.\n\nAn interceptor may choose to return a programmatically instantiated view rather than letting the default inflation occur, in which case interceptors added after it will be skipped. For this reason, it is better to add your post-inflation interceptors before the pre-inflation interceptors\n\n```java\nViewPump viewPump = ViewPump.builder()\n    .addInterceptor(new TextUpdatingInterceptor())\n    .addInterceptor(new CustomTextViewInterceptor())\n    .build()\n```\n\nOnce the instance is created (via dependency injection or otherwise), provide it to your relevant context via wrapping the `Activity` context:\n\n```java\n@Override\nprotected void attachBaseContext(Context newBase) {\n    super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase, viewPump));\n}\n```\n\n_You're good to go!_\n\nTo see more ideas for potential use cases, check out the [Recipes](https://github.com/InflationX/ViewPump/wiki/Recipes) wiki page.\n\n## Testing\n\nIf you need to test views in isolation (i.e. not under the indirect umbrella of an `Activity`), you need to set `factory2` manually in order for `ViewPump` to work.\n\n```kotlin\nval context = ViewPumpContextWrapper.wrap(textContext, viewPump)\nLayoutInflater.from(context).factory2 = FakeFactory2() // Can be a stub that just returns null\n\n// Now inflate your view\nval view = LayoutInflater.from(context).inflate(R.layout.view, null)\n```\n\nOtherwise, no `factory2` instance will be set in the underlying `LayoutInflater` and subsequently `ViewPump` will never be called during inflation.\n\n## Collaborators\n\n- [@jbarr21](https://github.com/jbarr21)\n- [@chrisjenx](https://github.com/chrisjenx)\n\n## Licence\n\n    Copyright 2017 InflationX\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n        http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n","funding_links":[],"categories":["Libraries","Kotlin"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FInflationX%2FViewPump","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FInflationX%2FViewPump","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FInflationX%2FViewPump/lists"}