2021-05-23 21:11:06 +02:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
2021-12-05 13:02:22 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
2021-05-23 21:11:06 +02:00
|
|
|
/// Store simple contents across different platforms
|
|
|
|
///
|
|
|
|
/// On mobile, the contents will be persisted as a file. On web, the contents
|
|
|
|
/// will be stored in local storage
|
|
|
|
abstract class UniversalStorage {
|
|
|
|
Future<void> putBinary(String name, Uint8List content);
|
|
|
|
|
|
|
|
/// Return the content associated with [name], or null if no such association
|
|
|
|
/// exists
|
2021-07-23 22:05:57 +02:00
|
|
|
Future<Uint8List?> getBinary(String name);
|
2021-05-23 21:11:06 +02:00
|
|
|
|
|
|
|
Future<void> putString(String name, String content);
|
|
|
|
|
|
|
|
/// Return the string associated with [name], or null if no such association
|
|
|
|
/// exists
|
2021-07-23 22:05:57 +02:00
|
|
|
Future<String?> getString(String name);
|
2021-05-23 21:11:06 +02:00
|
|
|
|
|
|
|
Future<void> remove(String name);
|
|
|
|
}
|
2021-12-05 13:02:22 +01:00
|
|
|
|
|
|
|
/// UniversalStorage backed by memory, useful in unit tests
|
|
|
|
@visibleForTesting
|
|
|
|
class UniversalMemoryStorage implements UniversalStorage {
|
|
|
|
@override
|
|
|
|
putBinary(String name, Uint8List content) async {
|
|
|
|
data[name] = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
getBinary(String name) async => data[name];
|
|
|
|
|
|
|
|
@override
|
|
|
|
putString(String name, String content) async {
|
|
|
|
data[name] = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
getString(String name) async => data[name];
|
|
|
|
|
|
|
|
@override
|
|
|
|
remove(String name) async {
|
|
|
|
data.remove(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
final data = <String, dynamic>{};
|
|
|
|
}
|