2020-06-12 02:04:41 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
|
|
|
import 'in_app_webview_controller.dart';
|
|
|
|
import 'types.dart';
|
|
|
|
|
|
|
|
class WebStorage {
|
|
|
|
LocalStorage localStorage;
|
|
|
|
SessionStorage sessionStorage;
|
|
|
|
|
|
|
|
WebStorage({@required this.localStorage, @required this.sessionStorage});
|
|
|
|
}
|
|
|
|
|
|
|
|
class WebStorageItem {
|
|
|
|
String key;
|
|
|
|
dynamic value;
|
|
|
|
|
|
|
|
WebStorageItem({this.key, this.value});
|
|
|
|
|
|
|
|
Map<String, dynamic> toMap() {
|
|
|
|
return {
|
|
|
|
"key": key,
|
|
|
|
"value": value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() {
|
|
|
|
return this.toMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return toMap().toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Storage {
|
|
|
|
InAppWebViewController _controller;
|
|
|
|
WebStorageType webStorageType;
|
|
|
|
|
|
|
|
Storage(InAppWebViewController controller, this.webStorageType) {
|
|
|
|
assert(controller != null && this.webStorageType != null);
|
|
|
|
this._controller = controller;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> length() async {
|
|
|
|
var result = await _controller.evaluateJavascript(source: """
|
|
|
|
window.$webStorageType.length;
|
|
|
|
""");
|
|
|
|
return result != null ? int.parse(json.decode(result)) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> setItem({@required String key, @required dynamic value}) async {
|
|
|
|
var encodedValue = json.encode(value);
|
|
|
|
await _controller.evaluateJavascript(source: """
|
2020-06-21 22:09:35 +00:00
|
|
|
window.$webStorageType.setItem("$key", ${value is String ? encodedValue : "JSON.stringify($encodedValue)"});
|
2020-06-12 02:04:41 +00:00
|
|
|
""");
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<dynamic> getItem({@required String key}) async {
|
|
|
|
var itemValue = await _controller.evaluateJavascript(source: """
|
|
|
|
window.$webStorageType.getItem("$key");
|
|
|
|
""");
|
|
|
|
|
|
|
|
if (itemValue == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return json.decode(itemValue);
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
return itemValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> removeItem({@required String key}) async {
|
|
|
|
await _controller.evaluateJavascript(source: """
|
|
|
|
window.$webStorageType.removeItem("$key");
|
|
|
|
""");
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<WebStorageItem>> getItems() async {
|
|
|
|
var webStorageItems = <WebStorageItem>[];
|
|
|
|
|
2020-06-21 22:09:35 +00:00
|
|
|
List<Map<dynamic, dynamic>> items =
|
|
|
|
(await _controller.evaluateJavascript(source: """
|
2020-06-12 02:04:41 +00:00
|
|
|
(function() {
|
|
|
|
var webStorageItems = [];
|
|
|
|
for(var i = 0; i < window.$webStorageType.length; i++){
|
|
|
|
var key = window.$webStorageType.key(i);
|
|
|
|
webStorageItems.push(
|
|
|
|
{
|
|
|
|
key: key,
|
|
|
|
value: window.$webStorageType.getItem(key)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return webStorageItems;
|
|
|
|
})();
|
|
|
|
""")).cast<Map<dynamic, dynamic>>();
|
|
|
|
|
|
|
|
if (items == null) {
|
|
|
|
return webStorageItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var item in items) {
|
2020-06-21 22:09:35 +00:00
|
|
|
webStorageItems
|
|
|
|
.add(WebStorageItem(key: item["key"], value: item["value"]));
|
2020-06-12 02:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return webStorageItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> clear() async {
|
|
|
|
await _controller.evaluateJavascript(source: """
|
|
|
|
window.$webStorageType.clear();
|
|
|
|
""");
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> key({@required int index}) async {
|
|
|
|
var result = await _controller.evaluateJavascript(source: """
|
|
|
|
window.$webStorageType.key($index);
|
|
|
|
""");
|
|
|
|
return result != null ? json.decode(result) : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class LocalStorage extends Storage {
|
2020-06-21 22:09:35 +00:00
|
|
|
LocalStorage(InAppWebViewController controller)
|
|
|
|
: super(controller, WebStorageType.LOCAL_STORAGE);
|
2020-06-12 02:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class SessionStorage extends Storage {
|
2020-06-21 22:09:35 +00:00
|
|
|
SessionStorage(InAppWebViewController controller)
|
|
|
|
: super(controller, WebStorageType.SESSION_STORAGE);
|
2020-06-12 02:04:41 +00:00
|
|
|
}
|