A flexible solution for creating a repository pattern in your Android apps.
Based on RxJava and Retrofit you can either instantiate a ready-made one or build your own.
The idea behind this library is to provide a simple way to instantiate a repository pattern, providing a parameter to define the type of repository.
The following steps describe how to implement a ready-made repository pattern.
a. model returned by the endpoint call, the same class will be used for caching the response - <M>
in the code
b. any parameters required for the endpoint call - <P>
in the code. It can be Void
if no params are required.
In case of multiple parameters, then Pair
or Triple
(from Kotlin or java) can be used, or any other Tuple defined in other libraries.
The only limitation is that methods equals
and hashCode
must be overridden.
This is because this class will be used as key for the HashMap
that handles the cache.
c. type of repository chosen. Currently it can be:
- NO_CACHE: the repository simply triggers a network call for each request
- SINGLE_LEVEL_CACHE: a one-level memory cache is present and will be used until the response associated with a specific value of parameters doesn't expire
private final RepoFactory repoFactory = new RepoFactory();
private final Repository<ForecastResponse, String> weatherForecastRepo;
weatherForecastRepo = repoFactory.createRepo(RepoType.SINGLE_LEVEL_CACHE,
location -> interfaceApi.getForecast(location);
public Single<ForecastResponse> getForecast(String location) {
return weatherForecastRepo.get();
}
If any part of the architecture needs to be customised for specific needs - either the network datasource or the cache or the repository itself - the RepoFactory
class can be derived and its method overridden as required.
See RepoFactory.java for more info.
Add the JitPack repository in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency
dependencies {
implementation 'com.github.guidovezzoni:repofactory:1.0.0'
}
These features are likely to be included in future releases:
- 3 level cache
- periodical removal of expired cache
The Repository
interface doesn't give access to setCacheValidity()
so currently the only way to do that is
((SingleLevelCacheRepository) weatherForecastRepo).setCacheValidity(TimeUnit.MINUTES.toMillis(1));
I'm still figuring out the best way to do it
When parameter <P>
uses a class for which methods equals
and hashCode
have not been overridden, cache won't work properly.
Expired cache entries can build up in time if the parameters change frequently and values are not refreshed
For bugs, questions and discussions please use the GitHub Issues .
Version | Date | Issues | Notes |
---|---|---|---|
1.0.1 | 28/05/2019 | 008,009,F010 | Some Kotlin files converted, minor changes |
1.0.0 | 01/05/2019 | First Release | |
0.1.2_alpha | 01/05/2019 | Minor changes, documentation improvements | |
0.1.1_alpha | 28/04/2019 | Fixed: network call always executed | |
0.1.0_alpha | 26/04/2019 | First alpha release |
Copyright 2019 Guido Vezzoni
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.