37 lines
999 B
Dart
37 lines
999 B
Dart
import 'package:cookie_jar/cookie_jar.dart';
|
|
import 'package:hive_flutter/adapters.dart';
|
|
|
|
class HiveStorage implements Storage {
|
|
final String boxName;
|
|
final String? boxPath;
|
|
HiveStorage(this.boxName, {this.boxPath});
|
|
|
|
bool _initialized = false;
|
|
late final Box<String> _box;
|
|
|
|
Future<void>? _initFuture;
|
|
|
|
Future<void> init(bool persistSession, bool ignoreExpires) =>
|
|
_initFuture ??= _init(persistSession, ignoreExpires);
|
|
|
|
Future<void> _init(bool persistSession, bool ignoreExpires) async {
|
|
if (_initialized) return;
|
|
_initialized = true;
|
|
_box = await Hive.openBox(boxName, path: boxPath);
|
|
print('init() finished');
|
|
}
|
|
|
|
@override
|
|
Future<String?> read(String key) async {
|
|
await _initFuture;
|
|
return _box.get(key);
|
|
}
|
|
|
|
@override
|
|
Future<void> write(String key, String value) => _box.put(key, value);
|
|
@override
|
|
Future<void> delete(String key) => _box.delete(key);
|
|
@override
|
|
Future<void> deleteAll(List<String> keys) => _box.deleteAll(keys);
|
|
}
|