@LevelUpApi(contract=INTERNAL) public abstract class AbstractPagingRequestFactory extends AbstractRequestFactory
A request factory that can page through requests. This is used with endpoints that support the
Link header, as noted in LinkHeaderParser
. This will save the
next page URL in a shared preference so that it can be resumed from that point forward.
To use, subclass and implement methods to retrieve the first page (getFirstPageRequest()
) and to construct a request for an arbitrary page from a URL (getPageRequest(Uri)
).
Once a page has been loaded successfully, setNextPage(Uri)
should be called with the URL
contained in the Link header.
Modifier and Type | Class and Description |
---|---|
static interface |
AbstractPagingRequestFactory.PageCacheRetriever
Implement this to support persisting the URL that points to the next page of information.
|
Constructor and Description |
---|
AbstractPagingRequestFactory(Context context,
AccessTokenRetriever retriever,
AbstractPagingRequestFactory.PageCacheRetriever pageCacheRetriever,
String savedPageKey) |
Modifier and Type | Method and Description |
---|---|
abstract AbstractRequest |
getFirstPageRequest()
Subclasses must implement this to construct a new request for the first page.
|
AbstractRequest |
getNextPageRequest()
Gets the next page.
|
protected AbstractPagingRequestFactory.PageCacheRetriever |
getPageCacheRetriever() |
abstract AbstractRequest |
getPageRequest(Uri page)
Subclasses must implement this to construct a new request for the given next page.
|
void |
setNextPage(Uri page)
Sets the next page.
|
getAccessTokenRetriever, getContext
public AbstractPagingRequestFactory(Context context, AccessTokenRetriever retriever, AbstractPagingRequestFactory.PageCacheRetriever pageCacheRetriever, String savedPageKey)
context
- the context.retriever
- an access token retriever.pageCacheRetriever
- a page cache retriever.savedPageKey
- a key that's unique for the endpoint that this request factory is for.
This should be full namespaced to ensure its uniqueness.public abstract AbstractRequest getFirstPageRequest()
public final AbstractRequest getNextPageRequest()
Gets the next page. If no pages have been loaded before, this returns the result of
getFirstPageRequest()
, otherwise it loads a request for the next page with
getPageRequest(Uri)
.
This must not be called from the UI thread.
public abstract AbstractRequest getPageRequest(Uri page)
page
- the URL of the next page that should be loaded.public final void setNextPage(Uri page)
Sets the next page. This is the URL returned in the Link header for each page that has been successfully loaded into cache.
This may be called from the UI thread.
page
- the URL as returned by the Link header of the response or null to clear.protected AbstractPagingRequestFactory.PageCacheRetriever getPageCacheRetriever()