2021-10-20 22:19:01 +08:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2021-10-31 19:48:20 +08:00
|
|
|
import 'package:app_flowy/workspace/domain/i_trash.dart';
|
2021-10-31 17:24:55 +08:00
|
|
|
import 'package:app_flowy/workspace/domain/i_view.dart';
|
2021-10-31 20:27:37 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-workspace/trash_create.pb.dart';
|
2021-10-31 19:48:20 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pb.dart';
|
2021-10-24 18:54:41 +08:00
|
|
|
import 'package:flutter_quill/flutter_quill.dart';
|
2021-10-20 22:19:01 +08:00
|
|
|
import 'package:flowy_log/flowy_log.dart';
|
2021-09-11 21:30:58 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2021-07-24 18:55:13 +08:00
|
|
|
import 'package:app_flowy/workspace/domain/i_doc.dart';
|
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
2021-10-19 13:56:11 +08:00
|
|
|
import 'package:dartz/dartz.dart';
|
2021-10-20 22:19:01 +08:00
|
|
|
import 'dart:async';
|
2021-07-24 18:55:13 +08:00
|
|
|
part 'doc_bloc.freezed.dart';
|
|
|
|
|
|
|
|
class DocBloc extends Bloc<DocEvent, DocState> {
|
2021-10-31 19:48:20 +08:00
|
|
|
final View view;
|
2021-10-19 13:56:11 +08:00
|
|
|
final IDoc docManager;
|
2021-10-31 17:24:55 +08:00
|
|
|
final IViewListener listener;
|
2021-10-31 19:48:20 +08:00
|
|
|
final ITrash trasnManager;
|
2021-10-20 22:19:01 +08:00
|
|
|
late Document document;
|
|
|
|
late StreamSubscription _subscription;
|
2021-07-24 18:55:13 +08:00
|
|
|
|
2021-10-31 19:48:20 +08:00
|
|
|
DocBloc({
|
|
|
|
required this.view,
|
|
|
|
required this.docManager,
|
|
|
|
required this.listener,
|
|
|
|
required this.trasnManager,
|
|
|
|
}) : super(DocState.initial());
|
2021-07-24 18:55:13 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
Stream<DocState> mapEventToState(DocEvent event) async* {
|
2021-10-31 17:24:55 +08:00
|
|
|
yield* event.map(
|
|
|
|
initial: _initial,
|
|
|
|
deleted: (Deleted value) async* {
|
|
|
|
yield state.copyWith(isDeleted: true);
|
|
|
|
},
|
|
|
|
restore: (Restore value) async* {
|
|
|
|
yield state.copyWith(isDeleted: false);
|
|
|
|
},
|
2021-10-31 19:48:20 +08:00
|
|
|
deletePermanently: (DeletePermanently value) async* {
|
2021-10-31 20:27:37 +08:00
|
|
|
final result = await trasnManager.deleteViews([Tuple2(view.id, TrashType.View)]);
|
|
|
|
yield result.fold((l) => state.copyWith(forceClose: true), (r) {
|
|
|
|
return state;
|
|
|
|
});
|
2021-10-31 19:48:20 +08:00
|
|
|
},
|
|
|
|
restorePage: (RestorePage value) async* {
|
|
|
|
final result = await trasnManager.putback(view.id);
|
|
|
|
yield result.fold((l) => state.copyWith(isDeleted: false), (r) {
|
|
|
|
return state;
|
|
|
|
});
|
|
|
|
},
|
2021-10-31 17:24:55 +08:00
|
|
|
);
|
2021-07-24 18:55:13 +08:00
|
|
|
}
|
2021-09-11 21:30:58 +08:00
|
|
|
|
2021-10-19 13:56:11 +08:00
|
|
|
@override
|
|
|
|
Future<void> close() async {
|
2021-10-31 17:24:55 +08:00
|
|
|
await listener.stop();
|
2021-10-20 22:19:01 +08:00
|
|
|
await _subscription.cancel();
|
2021-10-19 13:56:11 +08:00
|
|
|
docManager.closeDoc();
|
|
|
|
return super.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream<DocState> _initial(Initial value) async* {
|
2021-10-31 17:24:55 +08:00
|
|
|
listener.deletedNotifier.addPublishListener((result) {
|
|
|
|
result.fold(
|
|
|
|
(view) => add(const DocEvent.deleted()),
|
|
|
|
(error) {},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
listener.restoredNotifier.addPublishListener((result) {
|
|
|
|
result.fold(
|
|
|
|
(view) => add(const DocEvent.restore()),
|
|
|
|
(error) {},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
listener.start();
|
|
|
|
|
2021-10-19 13:56:11 +08:00
|
|
|
final result = await docManager.readDoc();
|
|
|
|
yield result.fold(
|
2021-09-11 21:30:58 +08:00
|
|
|
(doc) {
|
2021-10-20 22:19:01 +08:00
|
|
|
document = _decodeJsonToDocument(doc.data);
|
|
|
|
_subscription = document.changes.listen((event) {
|
|
|
|
final delta = event.item2;
|
|
|
|
final documentDelta = document.toDelta();
|
|
|
|
_composeDelta(delta, documentDelta);
|
|
|
|
});
|
|
|
|
return state.copyWith(loadState: DocLoadState.finish(left(unit)));
|
2021-09-11 21:30:58 +08:00
|
|
|
},
|
2021-10-19 13:56:11 +08:00
|
|
|
(err) {
|
2021-10-20 22:19:01 +08:00
|
|
|
return state.copyWith(loadState: DocLoadState.finish(right(err)));
|
2021-09-11 21:30:58 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-10 11:40:45 +08:00
|
|
|
// Document _decodeListToDocument(Uint8List data) {
|
|
|
|
// final json = jsonDecode(utf8.decode(data));
|
|
|
|
// final document = Document.fromJson(json);
|
|
|
|
// return document;
|
|
|
|
// }
|
|
|
|
|
2021-10-20 22:19:01 +08:00
|
|
|
void _composeDelta(Delta composedDelta, Delta documentDelta) async {
|
|
|
|
final json = jsonEncode(composedDelta.toJson());
|
|
|
|
Log.debug("Send json: $json");
|
|
|
|
final result = await docManager.composeDelta(json: json);
|
|
|
|
|
|
|
|
result.fold((rustDoc) {
|
|
|
|
// final json = utf8.decode(doc.data);
|
|
|
|
final rustDelta = Delta.fromJson(jsonDecode(rustDoc.data));
|
|
|
|
if (documentDelta != rustDelta) {
|
|
|
|
Log.error("Receive : $rustDelta");
|
|
|
|
Log.error("Expected : $documentDelta");
|
|
|
|
}
|
|
|
|
}, (r) => null);
|
|
|
|
}
|
|
|
|
|
|
|
|
Document _decodeJsonToDocument(String data) {
|
|
|
|
final json = jsonDecode(data);
|
|
|
|
final document = Document.fromJson(json);
|
|
|
|
return document;
|
|
|
|
}
|
2021-07-24 18:55:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
2021-09-11 21:30:58 +08:00
|
|
|
class DocEvent with _$DocEvent {
|
2021-10-19 13:56:11 +08:00
|
|
|
const factory DocEvent.initial() = Initial;
|
2021-10-31 17:24:55 +08:00
|
|
|
const factory DocEvent.deleted() = Deleted;
|
|
|
|
const factory DocEvent.restore() = Restore;
|
2021-10-31 19:48:20 +08:00
|
|
|
const factory DocEvent.restorePage() = RestorePage;
|
|
|
|
const factory DocEvent.deletePermanently() = DeletePermanently;
|
2021-07-24 18:55:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
2021-09-11 21:30:58 +08:00
|
|
|
class DocState with _$DocState {
|
2021-10-20 22:19:01 +08:00
|
|
|
const factory DocState({
|
|
|
|
required DocLoadState loadState,
|
2021-10-31 17:24:55 +08:00
|
|
|
required bool isDeleted,
|
2021-10-31 20:27:37 +08:00
|
|
|
required bool forceClose,
|
2021-10-20 22:19:01 +08:00
|
|
|
}) = _DocState;
|
2021-10-19 13:56:11 +08:00
|
|
|
|
2021-10-31 17:24:55 +08:00
|
|
|
factory DocState.initial() => const DocState(
|
|
|
|
loadState: _Loading(),
|
|
|
|
isDeleted: false,
|
2021-10-31 20:27:37 +08:00
|
|
|
forceClose: false,
|
2021-10-31 17:24:55 +08:00
|
|
|
);
|
2021-10-19 13:56:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
|
|
|
class DocLoadState with _$DocLoadState {
|
|
|
|
const factory DocLoadState.loading() = _Loading;
|
2021-10-20 22:19:01 +08:00
|
|
|
const factory DocLoadState.finish(Either<Unit, WorkspaceError> successOrFail) = _Finish;
|
2021-07-24 18:55:13 +08:00
|
|
|
}
|