Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rh-id/a-provider
This is a simple pure java service locator for Android
https://github.com/rh-id/a-provider
android android-lib android-library provider service-locator
Last synced: 30 days ago
JSON representation
This is a simple pure java service locator for Android
- Host: GitHub
- URL: https://github.com/rh-id/a-provider
- Owner: rh-id
- License: mit
- Created: 2021-09-16T01:17:47.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-02-24T11:58:59.000Z (10 months ago)
- Last Synced: 2024-11-07T21:42:44.316Z (about 1 month ago)
- Topics: android, android-lib, android-library, provider, service-locator
- Language: Java
- Homepage:
- Size: 153 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-indo-projects - a-provider - Service Locator library for Android. (Android)
- awesome-indonesia-repo - a-provider - Service Locator library for Android. (Android)
README
# a-provider
![JitPack](https://img.shields.io/jitpack/v/github/rh-id/a-provider)
![Downloads](https://jitpack.io/v/rh-id/a-provider/week.svg)
![Downloads](https://jitpack.io/v/rh-id/a-provider/month.svg)
![Android CI](https://github.com/rh-id/a-provider/actions/workflows/gradlew-build.yml/badge.svg)This is a simple Service Locator for Android projects that doesn't rely on annotations or "magic"
## Example Usage
This project support jitpack, in order to use this, you need to add jitpack to your project root build.gradle:
```
allprojects {
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
jcenter() // Warning: this repository is going to shut down soon
}
}
```Include this to your module dependency (module build.gradle)
```
dependencies {
implementation 'com.github.rh-id:a-provider:v0.0.1'
}
```Then you could proceed writing code,
First create root module as a root of the provider to provide services.```
public class RootModule implements ProviderModule{
@Override
void provides(ProviderRegistry providerRegistry, Provider provider){
// Register your services/components here or other ProviderModule
providerRegistry.register(IService.class, () -> new ServiceImpl());
providerRegistry.registerModule(new ProviderModuleA());
// You could use registerLazy to lazy-load your services
providerRegistry.registerLazy(IServiceA.class, ServiceAImpl::new);
// You could use registerAsync to initialize your services in background thread
providerRegistry.registerAsync(IServiceB.class,
() -> new ServiceBImpl(provider.get(IServiceA.class)));
// use registerFactory to load new instances everytime Provider.get() is invoked
providerRegistry.registerFactory(MyPojo.class, () -> {
MyPojo myPojo = new MyPojo();
myPojo.setAge(99);
myPojo.setName("Foo");
return myPojo;
});
// OR use registerPool to load new instances everytime Provider.get() is invoked.
providerRegistry.registerPool(MyPojo.class, () -> {
MyPojo myPojo = new MyPojo();
myPojo.setAge(99);
myPojo.setName("Foo");
return myPojo;
});
}@Override
void dispose(Provider provider){
// do something when this module is going to be disposed
}
}
```Initialize on your application for global access (example only)
```
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// store the instance in static value for global access
Provider provider = Provider.createProvider(this, new RootModule());
// example retrieve value
IServiceA iServiceA = provider.get(IServiceA.class);
MyPojo myPojo = provider.get(MyPojo.class);
}
}
```
If you need to handle dispose event you could implement `ProviderDisposable` to your component/services
```
public class ServiceAImpl implements IServiceA, ProviderDisposable {
@Override
public void dispose(Context context){
// anything to dispose, this will be called on Provide.dispose
}
}
```
```
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Provider provider = Provider.createProvider(this, new RootModule());
IServiceA iServiceA = provider.get(IServiceA.class);
provider.dispose(); // ServiceAImpl.dispose(Context) will be called
}
}
```## Skipping same type
For integration testing purposes you could turn on `skipSameType`. This will make `providerRegistry`
ignore duplicate type during registrationExample production RootModule:
```
public class RootModule implements ProviderModule{
@Override
void provides(ProviderRegistry providerRegistry, Provider provider){
providerRegistry.register(IService.class, () -> new ServiceImpl());
}
}
```Example test RootModule to be used:
```
public class TestRootModule extends RootModule{
@Override
void provides(ProviderRegistry providerRegistry, Provider provider){
// register IService.class with test instance
providerRegistry.register(IService.class, () -> new TestServiceImpl());providerRegistry.setSkipSameType(true); // enable
// since skip is true, the IService.class from parent will not be registered again
super.provides(providerRegistry, provider);
providerRegistry.setSkipSameType(false); // disable skip after done
}
}
```The configuration `providerRegistry.setSkipSameType(true);` can be useful on some circumstances such
as multiple android app flavors or configuration## Example Projects
- https://github.com/rh-id/a-news-provider
- https://github.com/rh-id/a-flash-deck
- https://github.com/rh-id/a-medic-log
- https://github.com/rh-id/a-personal-stuff
## Support this project
Consider donation to support this project