2021-10-13 23:11:45 +08:00
|
|
|
import 'dart:async';
|
2021-10-14 14:34:22 +08:00
|
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:app_flowy/workspace/domain/i_trash.dart';
|
|
|
|
import 'package:app_flowy/workspace/infrastructure/repos/helper.dart';
|
2021-10-13 23:11:45 +08:00
|
|
|
import 'package:dartz/dartz.dart';
|
|
|
|
import 'package:flowy_sdk/dispatch/dispatch.dart';
|
2021-10-14 14:34:22 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-dart-notify/subject.pb.dart';
|
2021-11-08 10:25:10 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-workspace-infra/trash_create.pb.dart';
|
2021-10-13 23:11:45 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart';
|
2021-10-14 14:34:22 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-workspace/observable.pb.dart';
|
|
|
|
import 'package:flowy_sdk/rust_stream.dart';
|
2021-10-13 23:11:45 +08:00
|
|
|
|
|
|
|
class TrashRepo {
|
|
|
|
Future<Either<RepeatedTrash, WorkspaceError>> readTrash() {
|
|
|
|
return WorkspaceEventReadTrash().send();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Either<Unit, WorkspaceError>> putback(String trashId) {
|
|
|
|
final id = TrashIdentifier.create()..id = trashId;
|
|
|
|
|
|
|
|
return WorkspaceEventPutbackTrash(id).send();
|
|
|
|
}
|
|
|
|
|
2021-10-31 20:27:37 +08:00
|
|
|
Future<Either<Unit, WorkspaceError>> deleteViews(List<Tuple2<String, TrashType>> trashList) {
|
2021-10-31 11:41:22 +08:00
|
|
|
final items = trashList.map((trash) {
|
|
|
|
return TrashIdentifier.create()
|
2021-10-31 20:27:37 +08:00
|
|
|
..id = trash.value1
|
|
|
|
..ty = trash.value2;
|
2021-10-31 11:41:22 +08:00
|
|
|
});
|
2021-10-17 22:44:51 +08:00
|
|
|
|
2021-10-31 11:41:22 +08:00
|
|
|
final trashIdentifiers = TrashIdentifiers(items: items);
|
2021-10-17 22:44:51 +08:00
|
|
|
return WorkspaceEventDeleteTrash(trashIdentifiers).send();
|
2021-10-13 23:11:45 +08:00
|
|
|
}
|
2021-10-18 16:30:20 +08:00
|
|
|
|
|
|
|
Future<Either<Unit, WorkspaceError>> restoreAll() {
|
|
|
|
return WorkspaceEventRestoreAll().send();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Either<Unit, WorkspaceError>> deleteAll() {
|
|
|
|
return WorkspaceEventDeleteAll().send();
|
|
|
|
}
|
2021-10-13 23:11:45 +08:00
|
|
|
}
|
2021-10-14 14:34:22 +08:00
|
|
|
|
|
|
|
class TrashListenerRepo {
|
|
|
|
StreamSubscription<SubscribeObject>? _subscription;
|
|
|
|
TrashUpdatedCallback? _trashUpdated;
|
|
|
|
late WorkspaceNotificationParser _parser;
|
|
|
|
|
|
|
|
void startListening({TrashUpdatedCallback? trashUpdated}) {
|
|
|
|
_trashUpdated = trashUpdated;
|
|
|
|
_parser = WorkspaceNotificationParser(callback: _bservableCallback);
|
|
|
|
_subscription = RustStreamReceiver.listen((observable) => _parser.parse(observable));
|
|
|
|
}
|
|
|
|
|
|
|
|
void _bservableCallback(WorkspaceNotification ty, Either<Uint8List, WorkspaceError> result) {
|
|
|
|
switch (ty) {
|
|
|
|
case WorkspaceNotification.TrashUpdated:
|
|
|
|
if (_trashUpdated != null) {
|
|
|
|
result.fold(
|
|
|
|
(payload) {
|
|
|
|
final repeatedTrash = RepeatedTrash.fromBuffer(payload);
|
|
|
|
_trashUpdated!(left(repeatedTrash.items));
|
|
|
|
},
|
|
|
|
(error) => _trashUpdated!(right(error)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> close() async {
|
|
|
|
await _subscription?.cancel();
|
|
|
|
}
|
|
|
|
}
|