doc_bloc.dart 4.6 KB

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