teller / com.levibostian.teller.repository / OnlineRepository

OnlineRepository

abstract class OnlineRepository<CACHE : Any, GET_DATA_REQUIREMENTS : GetDataRequirements, FETCH_RESPONSE : Any> : Listener

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

Using OnlineRepository is quite simple:

  1. Subclass OnlineRepository for each of your data types
  2. Call observe to begin observing the current state of the cached data.
  3. Set requirements with an object used to begin querying cached data and fetching fresh data if the data does not exist or is old.

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

Types

FetchResponse

class FetchResponse<FETCH_RESPONSE : Any>

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

GetDataRequirements

interface GetDataRequirements

Data object that are the requirements to fetch fresh data or get cached data on device.

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.

Constructors

<init>

OnlineRepository()

Properties

maxAge

abstract var maxAge: AgeOfData

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

requirements

var requirements: GET_DATA_REQUIREMENTS?

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

Functions

dispose

fun dispose(): Unit

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

fetchFreshData

abstract fun fetchFreshData(requirements: GET_DATA_REQUIREMENTS): Single<FetchResponse<FETCH_RESPONSE>>

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

isDataEmpty

abstract fun isDataEmpty(cache: CACHE, requirements: GET_DATA_REQUIREMENTS): Boolean

DataType determines if cacheData is empty or not. Because cacheData can be of Any type, the DataType must determine when cacheData is empty or not.

observe

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

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

observeCachedData

abstract fun observeCachedData(requirements: GET_DATA_REQUIREMENTS): Observable<CACHE>

Get existing cached data saved on the device if it exists. If no data exists, return an empty data set in the Observable and return true in isDataEmpty. 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 cached data.

refreshBegin

open fun refreshBegin(): Unit

refreshComplete

open fun <RefreshResultDataType : Any> refreshComplete(response: FetchResponse<RefreshResultDataType>): Unit

saveData

abstract fun saveData(data: FETCH_RESPONSE, requirements: GET_DATA_REQUIREMENTS): Unit

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