2023-05-16 14:58:24 +08:00
|
|
|
import 'package:appflowy/plugins/document/application/document_data_pb_extension.dart';
|
|
|
|
import 'package:appflowy/plugins/document/application/editor_transaction_adapter.dart';
|
2023-02-26 16:27:17 +08:00
|
|
|
import 'package:appflowy/plugins/trash/application/trash_service.dart';
|
|
|
|
import 'package:appflowy/user/application/user_service.dart';
|
2023-05-16 14:58:24 +08:00
|
|
|
import 'package:appflowy/util/json_print.dart';
|
2023-02-26 16:27:17 +08:00
|
|
|
import 'package:appflowy/workspace/application/view/view_listener.dart';
|
2023-04-13 18:53:51 +08:00
|
|
|
import 'package:appflowy/workspace/application/doc/doc_listener.dart';
|
2023-02-26 16:27:17 +08:00
|
|
|
import 'package:appflowy/plugins/document/application/doc_service.dart';
|
2023-05-16 14:58:24 +08:00
|
|
|
import 'package:appflowy_backend/log.dart';
|
|
|
|
import 'package:appflowy_backend/protobuf/flowy-document2/protobuf.dart';
|
2023-02-16 10:17:08 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-user/user_profile.pbserver.dart';
|
2022-10-22 21:57:44 +08:00
|
|
|
import 'package:appflowy_editor/appflowy_editor.dart'
|
2023-05-16 14:58:24 +08:00
|
|
|
show EditorState, LogLevel;
|
2023-01-08 12:10:53 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
|
2023-04-04 08:41:16 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
|
2023-03-07 09:33:59 +08:00
|
|
|
import 'package:flutter/foundation.dart';
|
2021-09-11 21:30:58 +08:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2021-07-24 18:55:13 +08:00
|
|
|
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';
|
|
|
|
|
2022-02-23 22:17:47 +08:00
|
|
|
class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
|
|
|
DocumentBloc({
|
2021-10-31 19:48:20 +08:00
|
|
|
required this.view,
|
2023-05-16 14:58:24 +08:00
|
|
|
}) : _documentListener = DocumentListener(id: view.id),
|
|
|
|
_viewListener = ViewListener(view: view),
|
|
|
|
_documentService = DocumentService(),
|
2022-10-26 10:38:57 +08:00
|
|
|
_trashService = TrashService(),
|
|
|
|
super(DocumentState.initial()) {
|
2023-05-16 14:58:24 +08:00
|
|
|
_transactionAdapter = TransactionAdapter(
|
|
|
|
documentId: view.id,
|
|
|
|
documentService: _documentService,
|
|
|
|
);
|
|
|
|
on<DocumentEvent>(_onDocumentEvent);
|
2021-07-24 18:55:13 +08:00
|
|
|
}
|
2021-09-11 21:30:58 +08:00
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
final ViewPB view;
|
2021-11-03 22:04:45 +08:00
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
final DocumentListener _documentListener;
|
|
|
|
final ViewListener _viewListener;
|
2021-11-03 23:03:42 +08:00
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
final DocumentService _documentService;
|
|
|
|
final TrashService _trashService;
|
|
|
|
|
|
|
|
late final TransactionAdapter _transactionAdapter;
|
|
|
|
|
|
|
|
EditorState? editorState;
|
|
|
|
StreamSubscription? _subscription;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> close() async {
|
|
|
|
await _viewListener.stop();
|
|
|
|
await _subscription?.cancel();
|
|
|
|
await _documentService.closeDocument(view: view);
|
|
|
|
editorState?.cancelSubscription();
|
2021-10-19 13:56:11 +08:00
|
|
|
return super.close();
|
|
|
|
}
|
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
Future<void> _onDocumentEvent(
|
|
|
|
DocumentEvent event,
|
|
|
|
Emitter<DocumentState> emit,
|
|
|
|
) async {
|
|
|
|
await event.map(
|
|
|
|
initial: (Initial value) async {
|
|
|
|
final state = await _fetchDocumentState();
|
|
|
|
await _subscribe(state);
|
|
|
|
emit(state);
|
2022-10-22 21:57:44 +08:00
|
|
|
},
|
2023-05-16 14:58:24 +08:00
|
|
|
deleted: (Deleted value) async {
|
|
|
|
emit(state.copyWith(isDeleted: true));
|
|
|
|
},
|
|
|
|
restore: (Restore value) async {
|
|
|
|
emit(state.copyWith(isDeleted: false));
|
|
|
|
},
|
|
|
|
deletePermanently: (DeletePermanently value) async {
|
|
|
|
final result = await _trashService.deleteViews([view.id]);
|
|
|
|
emit(state.copyWith(forceClose: result.swap().isLeft()));
|
|
|
|
},
|
|
|
|
restorePage: (RestorePage value) async {
|
|
|
|
final result = await _trashService.putback(view.id);
|
|
|
|
emit(state.copyWith(isDeleted: result.swap().isRight()));
|
2022-10-22 21:57:44 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
Future<void> _subscribe(DocumentState state) async {
|
|
|
|
_onViewChanged();
|
|
|
|
_onDocumentChanged();
|
|
|
|
|
|
|
|
// create the editor state
|
|
|
|
await state.loadingState.whenOrNull(
|
|
|
|
finish: (data) async => data.map((r) {
|
|
|
|
_initAppFlowyEditorState(r);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// subscribe to the view(document page) change
|
|
|
|
void _onViewChanged() {
|
|
|
|
_viewListener.start(
|
|
|
|
onViewDeleted: (r) =>
|
|
|
|
r.swap().map((r) => add(const DocumentEvent.deleted())),
|
|
|
|
onViewRestored: (r) =>
|
|
|
|
r.swap().map((r) => add(const DocumentEvent.restore())),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// subscribe to the document content change
|
|
|
|
void _onDocumentChanged() {
|
|
|
|
_documentListener.start(
|
|
|
|
didReceiveUpdate: (docEvent) {
|
|
|
|
// todo: integrate the document change to the editor
|
|
|
|
// prettyPrintJson(docEvent.toProto3Json());
|
2022-05-05 21:15:01 +08:00
|
|
|
},
|
|
|
|
);
|
2021-10-20 22:19:01 +08:00
|
|
|
}
|
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
/// Fetch document
|
|
|
|
Future<DocumentState> _fetchDocumentState() async {
|
|
|
|
final result = await UserBackendService.getCurrentUserProfile().then(
|
|
|
|
(value) async => value.andThen(
|
|
|
|
// open the document
|
|
|
|
await _documentService.openDocument(view: view),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return state.copyWith(
|
|
|
|
loadingState: DocumentLoadingState.finish(result),
|
2023-04-13 18:53:51 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-23 16:13:12 +08:00
|
|
|
Future<void> _initAppFlowyEditorState(DocumentDataPB data) async {
|
2023-05-16 14:58:24 +08:00
|
|
|
if (kDebugMode) {
|
|
|
|
prettyPrintJson(data.toProto3Json());
|
|
|
|
}
|
|
|
|
|
|
|
|
final document = data.toDocument();
|
|
|
|
if (document == null) {
|
|
|
|
assert(false, 'document is null');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-23 19:55:43 +08:00
|
|
|
final editorState = EditorState(document: document);
|
2023-03-07 09:33:59 +08:00
|
|
|
this.editorState = editorState;
|
|
|
|
|
2023-05-16 14:58:24 +08:00
|
|
|
// subscribe to the document change from the editor
|
|
|
|
_subscription = editorState.transactionStream.listen((transaction) async {
|
|
|
|
await _transactionAdapter.apply(transaction, editorState);
|
2022-10-22 21:57:44 +08:00
|
|
|
});
|
2023-05-16 14:58:24 +08:00
|
|
|
|
|
|
|
// output the log from the editor when debug mode
|
2023-03-07 09:33:59 +08:00
|
|
|
if (kDebugMode) {
|
2023-05-16 14:58:24 +08:00
|
|
|
editorState.logConfiguration
|
|
|
|
..level = LogLevel.all
|
|
|
|
..handler = (log) {
|
|
|
|
Log.debug(log);
|
|
|
|
};
|
2023-03-07 09:33:59 +08:00
|
|
|
}
|
2021-10-20 22:19:01 +08:00
|
|
|
}
|
2021-07-24 18:55:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
2022-02-23 22:17:47 +08:00
|
|
|
class DocumentEvent with _$DocumentEvent {
|
|
|
|
const factory DocumentEvent.initial() = Initial;
|
|
|
|
const factory DocumentEvent.deleted() = Deleted;
|
|
|
|
const factory DocumentEvent.restore() = Restore;
|
|
|
|
const factory DocumentEvent.restorePage() = RestorePage;
|
|
|
|
const factory DocumentEvent.deletePermanently() = DeletePermanently;
|
2021-07-24 18:55:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
2022-02-23 22:17:47 +08:00
|
|
|
class DocumentState with _$DocumentState {
|
|
|
|
const factory DocumentState({
|
|
|
|
required DocumentLoadingState loadingState,
|
2021-10-31 17:24:55 +08:00
|
|
|
required bool isDeleted,
|
2021-10-31 20:27:37 +08:00
|
|
|
required bool forceClose,
|
2023-02-16 10:17:08 +08:00
|
|
|
UserProfilePB? userProfilePB,
|
2022-02-23 22:17:47 +08:00
|
|
|
}) = _DocumentState;
|
2021-10-19 13:56:11 +08:00
|
|
|
|
2022-02-23 22:17:47 +08:00
|
|
|
factory DocumentState.initial() => const DocumentState(
|
|
|
|
loadingState: _Loading(),
|
2021-10-31 17:24:55 +08:00
|
|
|
isDeleted: false,
|
2021-10-31 20:27:37 +08:00
|
|
|
forceClose: false,
|
2023-02-16 10:17:08 +08:00
|
|
|
userProfilePB: null,
|
2021-10-31 17:24:55 +08:00
|
|
|
);
|
2021-10-19 13:56:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
2022-02-23 22:17:47 +08:00
|
|
|
class DocumentLoadingState with _$DocumentLoadingState {
|
|
|
|
const factory DocumentLoadingState.loading() = _Loading;
|
2022-08-09 10:35:27 +08:00
|
|
|
const factory DocumentLoadingState.finish(
|
2023-05-23 16:13:12 +08:00
|
|
|
Either<FlowyError, DocumentDataPB> successOrFail,
|
2023-04-10 15:10:42 +08:00
|
|
|
) = _Finish;
|
2021-07-24 18:55:13 +08:00
|
|
|
}
|