2020-06-12 02:04:41 +00:00
import ' dart:convert ' ;
import ' in_app_webview_controller.dart ' ;
import ' types.dart ' ;
2020-06-21 23:17:35 +00:00
///Class that provides access to the JavaScript [Web Storage API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API): `window.sessionStorage` and `window.localStorage`.
///It used by [InAppWebViewController.webStorage].
2020-06-12 02:04:41 +00:00
class WebStorage {
2020-06-21 23:17:35 +00:00
///Represents `window.localStorage`.
2020-06-12 02:04:41 +00:00
LocalStorage localStorage ;
2020-06-21 23:17:35 +00:00
///Represents `window.sessionStorage`.
2020-06-12 02:04:41 +00:00
SessionStorage sessionStorage ;
2021-01-28 16:10:15 +00:00
WebStorage ( { required this . localStorage , required this . sessionStorage } ) ;
2020-06-12 02:04:41 +00:00
}
2020-06-21 23:17:35 +00:00
///Class that represents a single web storage item of the JavaScript `window.sessionStorage` and `window.localStorage` objects.
2020-06-12 02:04:41 +00:00
class WebStorageItem {
2020-06-21 23:17:35 +00:00
///Item key.
2021-01-28 16:10:15 +00:00
String ? key ;
2020-06-24 22:18:01 +00:00
2020-06-21 23:17:35 +00:00
///Item value.
2020-06-12 02:04:41 +00:00
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 ( ) ;
}
}
2020-06-21 23:17:35 +00:00
///Class that provides methods to manage the JavaScript [Storage](https://developer.mozilla.org/en-US/docs/Web/API/Storage) object.
///It is used by [LocalStorage] and [SessionStorage].
2020-06-12 02:04:41 +00:00
class Storage {
2021-01-28 16:10:15 +00:00
late InAppWebViewController _controller ;
2020-06-21 23:17:35 +00:00
///The web storage type: `window.sessionStorage` or `window.localStorage`.
2020-06-12 02:04:41 +00:00
WebStorageType webStorageType ;
Storage ( InAppWebViewController controller , this . webStorageType ) {
this . _controller = controller ;
}
2020-06-21 23:17:35 +00:00
///Returns an integer representing the number of data items stored in the Storage object.
2021-01-28 16:10:15 +00:00
Future < int ? > length ( ) async {
2020-06-12 02:04:41 +00:00
var result = await _controller . evaluateJavascript ( source : """
window . $webStorageType . length ;
""" );
return result ! = null ? int . parse ( json . decode ( result ) ) : null ;
}
2020-06-21 23:17:35 +00:00
///When passed a [key] name and [value], will add that key to the storage, or update that key's value if it already exists.
2021-01-28 16:10:15 +00:00
Future < void > setItem ( { required String key , required dynamic value } ) async {
2020-06-12 02:04:41 +00:00
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
""" );
}
2020-06-21 23:17:35 +00:00
///When passed a [key] name, will return that key's value, or `null` if the key does not exist, in the given Storage object.
2021-01-28 16:10:15 +00:00
Future < dynamic > getItem ( { required String key } ) async {
2020-06-12 02:04:41 +00:00
var itemValue = await _controller . evaluateJavascript ( source : """
window . $webStorageType . getItem ( " $ key " ) ;
""" );
if ( itemValue = = null ) {
return null ;
}
try {
return json . decode ( itemValue ) ;
} catch ( e ) { }
return itemValue ;
}
2020-06-21 23:17:35 +00:00
///When passed a [key] name, will remove that key from the given Storage object if it exists.
2021-01-28 16:10:15 +00:00
Future < void > removeItem ( { required String key } ) async {
2020-06-12 02:04:41 +00:00
await _controller . evaluateJavascript ( source : """
window . $webStorageType . removeItem ( " $ key " ) ;
""" );
}
2020-06-21 23:17:35 +00:00
///Returns the list of all items from the given Storage object.
2020-06-12 02:04:41 +00:00
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 ;
}
2020-06-21 23:17:35 +00:00
///Clears all keys stored in a given Storage object.
2020-06-12 02:04:41 +00:00
Future < void > clear ( ) async {
await _controller . evaluateJavascript ( source : """
window . $webStorageType . clear ( ) ;
""" );
}
2020-06-21 23:17:35 +00:00
///When passed a number [index], returns the name of the nth key in a given Storage object.
///The order of keys is user-agent defined, so you should not rely on it.
2021-01-28 16:10:15 +00:00
Future < String > key ( { required int index } ) async {
2020-06-12 02:04:41 +00:00
var result = await _controller . evaluateJavascript ( source : """
window . $webStorageType . key ( $index ) ;
""" );
return result ! = null ? json . decode ( result ) : null ;
}
}
2020-06-21 23:17:35 +00:00
///Class that provides methods to manage the JavaScript `window.localStorage` object.
///It used by [WebStorage].
2020-06-12 02:04:41 +00:00
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
}
2020-06-21 23:17:35 +00:00
///Class that provides methods to manage the JavaScript `window.sessionStorage` object.
///It used by [WebStorage].
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
}