Skip to content

Commit

Permalink
feat(android): Expose cacheMode property (#895)
Browse files Browse the repository at this point in the history
  • Loading branch information
afcastano authored and Titozzz committed Sep 29, 2019
1 parent 902d3d1 commit 5da5925
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,27 @@ public void setCacheEnabled(WebView view, boolean enabled) {
}
}

@ReactProp(name = "cacheMode")
public void setCacheMode(WebView view, String cacheModeString) {
Integer cacheMode;
switch (cacheModeString) {
case "LOAD_CACHE_ONLY":
cacheMode = WebSettings.LOAD_CACHE_ONLY;
break;
case "LOAD_CACHE_ELSE_NETWORK":
cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK;
break;
case "LOAD_NO_CACHE":
cacheMode = WebSettings.LOAD_NO_CACHE;
break;
case "LOAD_DEFAULT":
default:
cacheMode = WebSettings.LOAD_DEFAULT;
break;
}
view.getSettings().setCacheMode(cacheMode);
}

@ReactProp(name = "androidHardwareAccelerationDisabled")
public void setHardwareAccelerationDisabled(WebView view, boolean disabled) {
ReactContext reactContext = (ReactContext) view.getContext();
Expand Down
17 changes: 17 additions & 0 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ This document lays out the current public properties and methods for the React N
- [`allowFileAccess`](Reference.md#allowFileAccess)
- [`saveFormDataDisabled`](Reference.md#saveFormDataDisabled)
- [`cacheEnabled`](Reference.md#cacheEnabled)
- [`cacheMode`](Reference.md#cacheMode)
- [`pagingEnabled`](Reference.md#pagingEnabled)
- [`allowsLinkPreview`](Reference.md#allowsLinkPreview)
- [`sharedCookiesEnabled`](Reference.md#sharedCookiesEnabled)
Expand Down Expand Up @@ -985,6 +986,22 @@ Sets whether WebView should use browser caching.

---

### `cacheMode`

Overrides the way the cache is used. The way the cache is used is based on the navigation type. For a normal page load, the cache is checked and content is re-validated as needed. When navigating back, content is not revalidated, instead the content is just retrieved from the cache. This property allows the client to override this behavior.

Possible values are:
- `LOAD_DEFAULT` - Default cache usage mode. If the navigation type doesn't impose any specific behavior, use cached resources when they are available and not expired, otherwise load resources from the network.
- `LOAD_CACHE_ELSE_NETWORK` - Use cached resources when they are available, even if they have expired. Otherwise load resources from the network.
- `LOAD_NO_CACHE` - Don't use the cache, load from the network.
- `LOAD_CACHE_ONLY` - Don't use the network, load from the cache.

| Type | Required | Default | Platform |
| ------- | -------- | -------------| -------- |
| string | No | LOAD_DEFAULT | Android |

---

### `pagingEnabled`

If the value of this property is true, the scroll view stops on multiples of the scroll view’s bounds when the user scrolls. The default value is false.
Expand Down
16 changes: 16 additions & 0 deletions src/WebViewTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ export type DataDetectorTypes =

export type OverScrollModeType = 'always' | 'content' | 'never';

export type CacheMode = 'LOAD_DEFAULT' | 'LOAD_CACHE_ONLY' | 'LOAD_CACHE_ELSE_NETWORK' | 'LOAD_NO_CACHE';

export interface WebViewSourceUri {
/**
* The URI to load in the `WebView`. Can be a local or remote file.
Expand Down Expand Up @@ -237,6 +239,7 @@ export interface CommonNativeWebViewProps extends ViewProps {
}

export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps {
cacheMode?: CacheMode;
allowFileAccess?: boolean;
scalesPageToFit?: boolean;
allowUniversalAccessFromFileURLs?: boolean;
Expand Down Expand Up @@ -459,6 +462,19 @@ export interface AndroidWebViewProps extends WebViewSharedProps {
onNavigationStateChange?: (event: WebViewNavigation) => void;
onContentSizeChange?: (event: WebViewEvent) => void;

/**
* https://developer.android.com/reference/android/webkit/WebSettings.html#setCacheMode(int)
* Set the cacheMode. Possible values are:
*
* - `'LOAD_DEFAULT'` (default)
* - `'LOAD_CACHE_ELSE_NETWORK'`
* - `'LOAD_NO_CACHE'`
* - `'LOAD_CACHE_ONLY'`
*
* @platform android
*/
cacheMode?: CacheMode;

/**
* https://developer.android.com/reference/android/view/View#OVER_SCROLL_NEVER
* Sets the overScrollMode. Possible values are:
Expand Down

0 comments on commit 5da5925

Please sign in to comment.