doc_bloc.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import 'dart:convert';
  2. import 'package:app_flowy/workspace/domain/i_view.dart';
  3. import 'package:flutter_quill/flutter_quill.dart';
  4. import 'package:flowy_log/flowy_log.dart';
  5. import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart';
  6. import 'package:flutter_bloc/flutter_bloc.dart';
  7. import 'package:app_flowy/workspace/domain/i_doc.dart';
  8. import 'package:freezed_annotation/freezed_annotation.dart';
  9. import 'package:dartz/dartz.dart';
  10. import 'dart:async';
  11. part 'doc_bloc.freezed.dart';
  12. class DocBloc extends Bloc<DocEvent, DocState> {
  13. final IDoc docManager;
  14. final IViewListener listener;
  15. late Document document;
  16. late StreamSubscription _subscription;
  17. DocBloc({required this.docManager, required this.listener}) : super(DocState.initial());
  18. @override
  19. Stream<DocState> mapEventToState(DocEvent event) async* {
  20. yield* event.map(
  21. initial: _initial,
  22. deleted: (Deleted value) async* {
  23. yield state.copyWith(isDeleted: true);
  24. },
  25. restore: (Restore value) async* {
  26. yield state.copyWith(isDeleted: false);
  27. },
  28. );
  29. }
  30. @override
  31. Future<void> close() async {
  32. await listener.stop();
  33. await _subscription.cancel();
  34. docManager.closeDoc();
  35. return super.close();
  36. }
  37. Stream<DocState> _initial(Initial value) async* {
  38. listener.deletedNotifier.addPublishListener((result) {
  39. result.fold(
  40. (view) => add(const DocEvent.deleted()),
  41. (error) {},
  42. );
  43. });
  44. listener.restoredNotifier.addPublishListener((result) {
  45. result.fold(
  46. (view) => add(const DocEvent.restore()),
  47. (error) {},
  48. );
  49. });
  50. listener.start();
  51. final result = await docManager.readDoc();
  52. yield result.fold(
  53. (doc) {
  54. document = _decodeJsonToDocument(doc.data);
  55. _subscription = document.changes.listen((event) {
  56. final delta = event.item2;
  57. final documentDelta = document.toDelta();
  58. _composeDelta(delta, documentDelta);
  59. });
  60. return state.copyWith(loadState: DocLoadState.finish(left(unit)));
  61. },
  62. (err) {
  63. return state.copyWith(loadState: DocLoadState.finish(right(err)));
  64. },
  65. );
  66. }
  67. // Document _decodeListToDocument(Uint8List data) {
  68. // final json = jsonDecode(utf8.decode(data));
  69. // final document = Document.fromJson(json);
  70. // return document;
  71. // }
  72. void _composeDelta(Delta composedDelta, Delta documentDelta) async {
  73. final json = jsonEncode(composedDelta.toJson());
  74. Log.debug("Send json: $json");
  75. final result = await docManager.composeDelta(json: json);
  76. result.fold((rustDoc) {
  77. // final json = utf8.decode(doc.data);
  78. final rustDelta = Delta.fromJson(jsonDecode(rustDoc.data));
  79. if (documentDelta != rustDelta) {
  80. Log.error("Receive : $rustDelta");
  81. Log.error("Expected : $documentDelta");
  82. }
  83. }, (r) => null);
  84. }
  85. Document _decodeJsonToDocument(String data) {
  86. final json = jsonDecode(data);
  87. final document = Document.fromJson(json);
  88. return document;
  89. }
  90. }
  91. @freezed
  92. class DocEvent with _$DocEvent {
  93. const factory DocEvent.initial() = Initial;
  94. const factory DocEvent.deleted() = Deleted;
  95. const factory DocEvent.restore() = Restore;
  96. }
  97. @freezed
  98. class DocState with _$DocState {
  99. const factory DocState({
  100. required DocLoadState loadState,
  101. required bool isDeleted,
  102. }) = _DocState;
  103. factory DocState.initial() => const DocState(
  104. loadState: _Loading(),
  105. isDeleted: false,
  106. );
  107. }
  108. @freezed
  109. class DocLoadState with _$DocLoadState {
  110. const factory DocLoadState.loading() = _Loading;
  111. const factory DocLoadState.finish(Either<Unit, WorkspaceError> successOrFail) = _Finish;
  112. }