57 lines
1.6 KiB
Dart
57 lines
1.6 KiB
Dart
|
|
import 'dart:async';
|
||
|
|
import 'dart:typed_data';
|
||
|
|
|
||
|
|
import 'package:halkyon_learn_flutter/src/feature/settings/data/settings_gateway.dart';
|
||
|
|
|
||
|
|
/// A [SettingsGateway] in memory: ids are `settings-001`, `settings-002`, …,
|
||
|
|
/// in the order of creation as UUID v7s are, and [receiveRemoteRow] plays a
|
||
|
|
/// sync that brought a row.
|
||
|
|
class FakeSettingsGateway() implements SettingsGateway {
|
||
|
|
final _rows = <String, ByteData>{};
|
||
|
|
final _remoteChanges = StreamController<void>.broadcast();
|
||
|
|
var _next = 1;
|
||
|
|
|
||
|
|
/// Every payload written, in order, as (id, payload).
|
||
|
|
final writes = <(String, ByteData)>[];
|
||
|
|
|
||
|
|
/// The ids of the deleted rows, in order.
|
||
|
|
final deleted = <String>[];
|
||
|
|
|
||
|
|
/// Stores the row [id] as if another device had written it and a sync had
|
||
|
|
/// brought it here.
|
||
|
|
void receiveRemoteRow(String id, ByteData payload) {
|
||
|
|
_rows[id] = payload;
|
||
|
|
_remoteChanges.add(null);
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<List<SettingsRow>> rows() async => [
|
||
|
|
for (final MapEntry(key: id, value: payload) in _rows.entries)
|
||
|
|
(id: id, payload: payload),
|
||
|
|
];
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<String> insert(ByteData payload) async {
|
||
|
|
final id = 'settings-${'${_next++}'.padLeft(3, '0')}';
|
||
|
|
_rows[id] = payload;
|
||
|
|
writes.add((id, payload));
|
||
|
|
return id;
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<void> updatePayload(String id, ByteData payload) async {
|
||
|
|
if (_rows.containsKey(id)) {
|
||
|
|
_rows[id] = payload;
|
||
|
|
writes.add((id, payload));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<void> delete(String id) async {
|
||
|
|
_rows.remove(id);
|
||
|
|
deleted.add(id);
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Stream<void> get remoteChanges => _remoteChanges.stream;
|
||
|
|
}
|