teller-android / com.levibostian.teller.repository / OnlineRepository

OnlineRepository

abstract class OnlineRepository<CACHE, GET_CACHE_REQUIREMENTS : GetCacheRequirements, FETCH_RESPONSE>

Teller repository that manages a cache that is obtained from a network fetch request.

Using OnlineRepository is quite simple:

  1. Subclass OnlineRepository for each of your cache types.
  2. Call observe to begin observing the current state of the cache.
  3. Set requirements with an object used to begin querying cache and fetching fresh cache if the cache does not exist or is old.
  4. Call dispose when you are done using the OnlineRepository.

OnlineRepository is thread safe. Actions called upon for OnlineRepository can be performed on any thread.

Types

FetchResponse

class FetchResponse<FETCH_RESPONSE>

When a OnlineRepository.fetchFreshCache task is performed, Teller needs to know if the fetch request is considered to be a success or a failure.

GetCacheRequirements

interface GetCacheRequirements

Used by Teller to determine what chunk of cache to fetch and query.

RefreshResult

class RefreshResult

Result object of a call to OnlineRepository.refresh. Understand if a refresh call was successful, not successful, or skipped for some reason.

Testing

object Testing

Used for testing purposes to initialize the state of a OnlineRepository subclass instance.

Constructors

<init>

OnlineRepository()

Properties

maxAgeOfCache

abstract var maxAgeOfCache: Age

Used to set how old cache can be on the device before it is considered too old and new cache should be fetched.

requirements

var requirements: GET_CACHE_REQUIREMENTS?

Requirements needed to be able to load cache from the device and to fetch new cache from the network.

Functions

dispose

fun dispose(): Unit

Dispose of the OnlineRepository to shut down observing of the cache and stops refresh tasks if they have begun.

fetchFreshCache

abstract fun fetchFreshCache(requirements: GET_CACHE_REQUIREMENTS): Single<FetchResponse<FETCH_RESPONSE>>

Repository does what it needs in order to fetch fresh cache. Probably call network API.

isCacheEmpty

abstract fun isCacheEmpty(cache: CACHE, requirements: GET_CACHE_REQUIREMENTS): Boolean

Used to determine if cache is empty or not.

observe

fun observe(): Observable<OnlineCacheState<CACHE>>

How to begin observing the state of the cache for this OnlineRepository.

observeCache

abstract fun observeCache(requirements: GET_CACHE_REQUIREMENTS): Observable<CACHE>

Get existing cache saved on the device if it exists. If no cache exists, return an empty response set in the Observable and return true in isCacheEmpty. Do not return nil or an Observable with nil as a value as this will cause an exception.

refresh

fun refresh(force: Boolean): Single<RefreshResult>

Manually perform a refresh of the cache.

saveCache

abstract fun saveCache(cache: FETCH_RESPONSE, requirements: GET_CACHE_REQUIREMENTS): Unit

Save the new cache cache to whatever storage method OnlineRepository chooses.