瀏覽代碼

feat(android): Expose cacheMode property (#895)

Andres Castano 5 年之前
父節點
當前提交
5da59251ce
共有 3 個檔案被更改,包括 54 行新增0 行删除
  1. 21
    0
      android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java
  2. 17
    0
      docs/Reference.md
  3. 16
    0
      src/WebViewTypes.ts

+ 21
- 0
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java 查看文件

@@ -257,6 +257,27 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
257 257
     }
258 258
   }
259 259
 
260
+  @ReactProp(name = "cacheMode")
261
+  public void setCacheMode(WebView view, String cacheModeString) {
262
+    Integer cacheMode;
263
+    switch (cacheModeString) {
264
+      case "LOAD_CACHE_ONLY":
265
+        cacheMode = WebSettings.LOAD_CACHE_ONLY;
266
+        break;
267
+      case "LOAD_CACHE_ELSE_NETWORK":
268
+        cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK;
269
+        break;  
270
+      case "LOAD_NO_CACHE":
271
+        cacheMode = WebSettings.LOAD_NO_CACHE;
272
+        break;
273
+      case "LOAD_DEFAULT":
274
+      default:
275
+        cacheMode = WebSettings.LOAD_DEFAULT;
276
+        break;
277
+    }
278
+    view.getSettings().setCacheMode(cacheMode);
279
+  }
280
+
260 281
   @ReactProp(name = "androidHardwareAccelerationDisabled")
261 282
   public void setHardwareAccelerationDisabled(WebView view, boolean disabled) {
262 283
     ReactContext reactContext = (ReactContext) view.getContext();

+ 17
- 0
docs/Reference.md 查看文件

@@ -55,6 +55,7 @@ This document lays out the current public properties and methods for the React N
55 55
 - [`allowFileAccess`](Reference.md#allowFileAccess)
56 56
 - [`saveFormDataDisabled`](Reference.md#saveFormDataDisabled)
57 57
 - [`cacheEnabled`](Reference.md#cacheEnabled)
58
+- [`cacheMode`](Reference.md#cacheMode)
58 59
 - [`pagingEnabled`](Reference.md#pagingEnabled)
59 60
 - [`allowsLinkPreview`](Reference.md#allowsLinkPreview)
60 61
 - [`sharedCookiesEnabled`](Reference.md#sharedCookiesEnabled)
@@ -985,6 +986,22 @@ Sets whether WebView should use browser caching.
985 986
 
986 987
 ---
987 988
 
989
+### `cacheMode`
990
+
991
+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.
992
+
993
+Possible values are:
994
+- `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.
995
+- `LOAD_CACHE_ELSE_NETWORK` - Use cached resources when they are available, even if they have expired. Otherwise load resources from the network.
996
+- `LOAD_NO_CACHE` - Don't use the cache, load from the network.
997
+- `LOAD_CACHE_ONLY` - Don't use the network, load from the cache. 
998
+ 
999
+| Type    | Required | Default      | Platform |
1000
+| ------- | -------- | -------------| -------- |
1001
+| string  | No       | LOAD_DEFAULT | Android  |
1002
+
1003
+---
1004
+
988 1005
 ### `pagingEnabled`
989 1006
 
990 1007
 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.

+ 16
- 0
src/WebViewTypes.ts 查看文件

@@ -145,6 +145,8 @@ export type DataDetectorTypes =
145 145
 
146 146
 export type OverScrollModeType = 'always' | 'content' | 'never';
147 147
 
148
+export type CacheMode = 'LOAD_DEFAULT' | 'LOAD_CACHE_ONLY' | 'LOAD_CACHE_ELSE_NETWORK' | 'LOAD_NO_CACHE';
149
+
148 150
 export interface WebViewSourceUri {
149 151
   /**
150 152
    * The URI to load in the `WebView`. Can be a local or remote file.
@@ -237,6 +239,7 @@ export interface CommonNativeWebViewProps extends ViewProps {
237 239
 }
238 240
 
239 241
 export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps {
242
+  cacheMode?: CacheMode;
240 243
   allowFileAccess?: boolean;
241 244
   scalesPageToFit?: boolean;
242 245
   allowUniversalAccessFromFileURLs?: boolean;
@@ -459,6 +462,19 @@ export interface AndroidWebViewProps extends WebViewSharedProps {
459 462
   onNavigationStateChange?: (event: WebViewNavigation) => void;
460 463
   onContentSizeChange?: (event: WebViewEvent) => void;
461 464
 
465
+  /**
466
+   * https://developer.android.com/reference/android/webkit/WebSettings.html#setCacheMode(int)
467
+   * Set the cacheMode. Possible values are:
468
+   * 
469
+   * - `'LOAD_DEFAULT'` (default)
470
+   * - `'LOAD_CACHE_ELSE_NETWORK'`
471
+   * - `'LOAD_NO_CACHE'`
472
+   * - `'LOAD_CACHE_ONLY'`
473
+   *
474
+   * @platform android
475
+   */
476
+  cacheMode?: CacheMode;
477
+
462 478
   /**
463 479
    * https://developer.android.com/reference/android/view/View#OVER_SCROLL_NEVER
464 480
    * Sets the overScrollMode. Possible values are: