Browse Source

[flutter]: remove edit doc bloc

appflowy 3 years ago
parent
commit
ce5cccd670

+ 38 - 18
app_flowy/lib/workspace/application/doc/doc_bloc.dart

@@ -2,33 +2,47 @@ import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart';
 import 'package:flutter_bloc/flutter_bloc.dart';
 import 'package:app_flowy/workspace/domain/i_doc.dart';
 import 'package:freezed_annotation/freezed_annotation.dart';
+import 'package:dartz/dartz.dart';
+
 part 'doc_bloc.freezed.dart';
 
 class DocBloc extends Bloc<DocEvent, DocState> {
-  final IDoc iDocImpl;
+  final IDoc docManager;
 
-  DocBloc({
-    required this.iDocImpl,
-  }) : super(const DocState.loading());
+  DocBloc({required this.docManager}) : super(DocState.initial());
 
   @override
   Stream<DocState> mapEventToState(DocEvent event) async* {
     yield* event.map(
-      loadDoc: (_) async* {
-        yield* _readDoc();
-      },
+      initial: _initial,
     );
   }
 
-  Stream<DocState> _readDoc() async* {
-    final docOrFail = await iDocImpl.readDoc();
-    yield docOrFail.fold(
+  @override
+  Future<void> close() async {
+    docManager.closeDoc();
+
+    await state.doc.fold(() => null, (doc) async {
+      await doc.close();
+    });
+    return super.close();
+  }
+
+  Stream<DocState> _initial(Initial value) async* {
+    final result = await docManager.readDoc();
+    yield result.fold(
       (doc) {
-        final flowyDoc = FlowyDoc(doc: doc, iDocImpl: iDocImpl);
-        return DocState.loadDoc(flowyDoc);
+        final flowyDoc = FlowyDoc(doc: doc, iDocImpl: docManager);
+        return state.copyWith(
+          doc: some(flowyDoc),
+          loadState: DocLoadState.finish(left(flowyDoc)),
+        );
       },
-      (error) {
-        return DocState.loadFail(error);
+      (err) {
+        return state.copyWith(
+          doc: none(),
+          loadState: DocLoadState.finish(right(err)),
+        );
       },
     );
   }
@@ -48,12 +62,18 @@ class DocBloc extends Bloc<DocEvent, DocState> {
 
 @freezed
 class DocEvent with _$DocEvent {
-  const factory DocEvent.loadDoc() = LoadDoc;
+  const factory DocEvent.initial() = Initial;
 }
 
 @freezed
 class DocState with _$DocState {
-  const factory DocState.loading() = Loading;
-  const factory DocState.loadDoc(FlowyDoc doc) = LoadedDoc;
-  const factory DocState.loadFail(WorkspaceError error) = LoadFail;
+  const factory DocState({required Option<FlowyDoc> doc, required DocLoadState loadState}) = _DocState;
+
+  factory DocState.initial() => DocState(doc: none(), loadState: const _Loading());
+}
+
+@freezed
+class DocLoadState with _$DocLoadState {
+  const factory DocLoadState.loading() = _Loading;
+  const factory DocLoadState.finish(Either<FlowyDoc, WorkspaceError> successOrFail) = _Finish;
 }

+ 257 - 227
app_flowy/lib/workspace/application/doc/doc_bloc.freezed.dart

@@ -16,8 +16,8 @@ final _privateConstructorUsedError = UnsupportedError(
 class _$DocEventTearOff {
   const _$DocEventTearOff();
 
-  LoadDoc loadDoc() {
-    return const LoadDoc();
+  Initial initial() {
+    return const Initial();
   }
 }
 
@@ -28,23 +28,23 @@ const $DocEvent = _$DocEventTearOff();
 mixin _$DocEvent {
   @optionalTypeArgs
   TResult when<TResult extends Object?>({
-    required TResult Function() loadDoc,
+    required TResult Function() initial,
   }) =>
       throw _privateConstructorUsedError;
   @optionalTypeArgs
   TResult maybeWhen<TResult extends Object?>({
-    TResult Function()? loadDoc,
+    TResult Function()? initial,
     required TResult orElse(),
   }) =>
       throw _privateConstructorUsedError;
   @optionalTypeArgs
   TResult map<TResult extends Object?>({
-    required TResult Function(LoadDoc value) loadDoc,
+    required TResult Function(Initial value) initial,
   }) =>
       throw _privateConstructorUsedError;
   @optionalTypeArgs
   TResult maybeMap<TResult extends Object?>({
-    TResult Function(LoadDoc value)? loadDoc,
+    TResult Function(Initial value)? initial,
     required TResult orElse(),
   }) =>
       throw _privateConstructorUsedError;
@@ -66,34 +66,34 @@ class _$DocEventCopyWithImpl<$Res> implements $DocEventCopyWith<$Res> {
 }
 
 /// @nodoc
-abstract class $LoadDocCopyWith<$Res> {
-  factory $LoadDocCopyWith(LoadDoc value, $Res Function(LoadDoc) then) =
-      _$LoadDocCopyWithImpl<$Res>;
+abstract class $InitialCopyWith<$Res> {
+  factory $InitialCopyWith(Initial value, $Res Function(Initial) then) =
+      _$InitialCopyWithImpl<$Res>;
 }
 
 /// @nodoc
-class _$LoadDocCopyWithImpl<$Res> extends _$DocEventCopyWithImpl<$Res>
-    implements $LoadDocCopyWith<$Res> {
-  _$LoadDocCopyWithImpl(LoadDoc _value, $Res Function(LoadDoc) _then)
-      : super(_value, (v) => _then(v as LoadDoc));
+class _$InitialCopyWithImpl<$Res> extends _$DocEventCopyWithImpl<$Res>
+    implements $InitialCopyWith<$Res> {
+  _$InitialCopyWithImpl(Initial _value, $Res Function(Initial) _then)
+      : super(_value, (v) => _then(v as Initial));
 
   @override
-  LoadDoc get _value => super._value as LoadDoc;
+  Initial get _value => super._value as Initial;
 }
 
 /// @nodoc
 
-class _$LoadDoc implements LoadDoc {
-  const _$LoadDoc();
+class _$Initial implements Initial {
+  const _$Initial();
 
   @override
   String toString() {
-    return 'DocEvent.loadDoc()';
+    return 'DocEvent.initial()';
   }
 
   @override
   bool operator ==(dynamic other) {
-    return identical(this, other) || (other is LoadDoc);
+    return identical(this, other) || (other is Initial);
   }
 
   @override
@@ -102,19 +102,19 @@ class _$LoadDoc implements LoadDoc {
   @override
   @optionalTypeArgs
   TResult when<TResult extends Object?>({
-    required TResult Function() loadDoc,
+    required TResult Function() initial,
   }) {
-    return loadDoc();
+    return initial();
   }
 
   @override
   @optionalTypeArgs
   TResult maybeWhen<TResult extends Object?>({
-    TResult Function()? loadDoc,
+    TResult Function()? initial,
     required TResult orElse(),
   }) {
-    if (loadDoc != null) {
-      return loadDoc();
+    if (initial != null) {
+      return initial();
     }
     return orElse();
   }
@@ -122,45 +122,37 @@ class _$LoadDoc implements LoadDoc {
   @override
   @optionalTypeArgs
   TResult map<TResult extends Object?>({
-    required TResult Function(LoadDoc value) loadDoc,
+    required TResult Function(Initial value) initial,
   }) {
-    return loadDoc(this);
+    return initial(this);
   }
 
   @override
   @optionalTypeArgs
   TResult maybeMap<TResult extends Object?>({
-    TResult Function(LoadDoc value)? loadDoc,
+    TResult Function(Initial value)? initial,
     required TResult orElse(),
   }) {
-    if (loadDoc != null) {
-      return loadDoc(this);
+    if (initial != null) {
+      return initial(this);
     }
     return orElse();
   }
 }
 
-abstract class LoadDoc implements DocEvent {
-  const factory LoadDoc() = _$LoadDoc;
+abstract class Initial implements DocEvent {
+  const factory Initial() = _$Initial;
 }
 
 /// @nodoc
 class _$DocStateTearOff {
   const _$DocStateTearOff();
 
-  Loading loading() {
-    return const Loading();
-  }
-
-  LoadedDoc loadDoc(FlowyDoc doc) {
-    return LoadedDoc(
-      doc,
-    );
-  }
-
-  LoadFail loadFail(WorkspaceError error) {
-    return LoadFail(
-      error,
+  _DocState call(
+      {required Option<FlowyDoc> doc, required DocLoadState loadState}) {
+    return _DocState(
+      doc: doc,
+      loadState: loadState,
     );
   }
 }
@@ -170,35 +162,11 @@ const $DocState = _$DocStateTearOff();
 
 /// @nodoc
 mixin _$DocState {
-  @optionalTypeArgs
-  TResult when<TResult extends Object?>({
-    required TResult Function() loading,
-    required TResult Function(FlowyDoc doc) loadDoc,
-    required TResult Function(WorkspaceError error) loadFail,
-  }) =>
-      throw _privateConstructorUsedError;
-  @optionalTypeArgs
-  TResult maybeWhen<TResult extends Object?>({
-    TResult Function()? loading,
-    TResult Function(FlowyDoc doc)? loadDoc,
-    TResult Function(WorkspaceError error)? loadFail,
-    required TResult orElse(),
-  }) =>
-      throw _privateConstructorUsedError;
-  @optionalTypeArgs
-  TResult map<TResult extends Object?>({
-    required TResult Function(Loading value) loading,
-    required TResult Function(LoadedDoc value) loadDoc,
-    required TResult Function(LoadFail value) loadFail,
-  }) =>
-      throw _privateConstructorUsedError;
-  @optionalTypeArgs
-  TResult maybeMap<TResult extends Object?>({
-    TResult Function(Loading value)? loading,
-    TResult Function(LoadedDoc value)? loadDoc,
-    TResult Function(LoadFail value)? loadFail,
-    required TResult orElse(),
-  }) =>
+  Option<FlowyDoc> get doc => throw _privateConstructorUsedError;
+  DocLoadState get loadState => throw _privateConstructorUsedError;
+
+  @JsonKey(ignore: true)
+  $DocStateCopyWith<DocState> get copyWith =>
       throw _privateConstructorUsedError;
 }
 
@@ -206,6 +174,9 @@ mixin _$DocState {
 abstract class $DocStateCopyWith<$Res> {
   factory $DocStateCopyWith(DocState value, $Res Function(DocState) then) =
       _$DocStateCopyWithImpl<$Res>;
+  $Res call({Option<FlowyDoc> doc, DocLoadState loadState});
+
+  $DocLoadStateCopyWith<$Res> get loadState;
 }
 
 /// @nodoc
@@ -215,174 +186,241 @@ class _$DocStateCopyWithImpl<$Res> implements $DocStateCopyWith<$Res> {
   final DocState _value;
   // ignore: unused_field
   final $Res Function(DocState) _then;
+
+  @override
+  $Res call({
+    Object? doc = freezed,
+    Object? loadState = freezed,
+  }) {
+    return _then(_value.copyWith(
+      doc: doc == freezed
+          ? _value.doc
+          : doc // ignore: cast_nullable_to_non_nullable
+              as Option<FlowyDoc>,
+      loadState: loadState == freezed
+          ? _value.loadState
+          : loadState // ignore: cast_nullable_to_non_nullable
+              as DocLoadState,
+    ));
+  }
+
+  @override
+  $DocLoadStateCopyWith<$Res> get loadState {
+    return $DocLoadStateCopyWith<$Res>(_value.loadState, (value) {
+      return _then(_value.copyWith(loadState: value));
+    });
+  }
 }
 
 /// @nodoc
-abstract class $LoadingCopyWith<$Res> {
-  factory $LoadingCopyWith(Loading value, $Res Function(Loading) then) =
-      _$LoadingCopyWithImpl<$Res>;
+abstract class _$DocStateCopyWith<$Res> implements $DocStateCopyWith<$Res> {
+  factory _$DocStateCopyWith(_DocState value, $Res Function(_DocState) then) =
+      __$DocStateCopyWithImpl<$Res>;
+  @override
+  $Res call({Option<FlowyDoc> doc, DocLoadState loadState});
+
+  @override
+  $DocLoadStateCopyWith<$Res> get loadState;
 }
 
 /// @nodoc
-class _$LoadingCopyWithImpl<$Res> extends _$DocStateCopyWithImpl<$Res>
-    implements $LoadingCopyWith<$Res> {
-  _$LoadingCopyWithImpl(Loading _value, $Res Function(Loading) _then)
-      : super(_value, (v) => _then(v as Loading));
+class __$DocStateCopyWithImpl<$Res> extends _$DocStateCopyWithImpl<$Res>
+    implements _$DocStateCopyWith<$Res> {
+  __$DocStateCopyWithImpl(_DocState _value, $Res Function(_DocState) _then)
+      : super(_value, (v) => _then(v as _DocState));
 
   @override
-  Loading get _value => super._value as Loading;
+  _DocState get _value => super._value as _DocState;
+
+  @override
+  $Res call({
+    Object? doc = freezed,
+    Object? loadState = freezed,
+  }) {
+    return _then(_DocState(
+      doc: doc == freezed
+          ? _value.doc
+          : doc // ignore: cast_nullable_to_non_nullable
+              as Option<FlowyDoc>,
+      loadState: loadState == freezed
+          ? _value.loadState
+          : loadState // ignore: cast_nullable_to_non_nullable
+              as DocLoadState,
+    ));
+  }
 }
 
 /// @nodoc
 
-class _$Loading implements Loading {
-  const _$Loading();
+class _$_DocState implements _DocState {
+  const _$_DocState({required this.doc, required this.loadState});
+
+  @override
+  final Option<FlowyDoc> doc;
+  @override
+  final DocLoadState loadState;
 
   @override
   String toString() {
-    return 'DocState.loading()';
+    return 'DocState(doc: $doc, loadState: $loadState)';
   }
 
   @override
   bool operator ==(dynamic other) {
-    return identical(this, other) || (other is Loading);
+    return identical(this, other) ||
+        (other is _DocState &&
+            (identical(other.doc, doc) ||
+                const DeepCollectionEquality().equals(other.doc, doc)) &&
+            (identical(other.loadState, loadState) ||
+                const DeepCollectionEquality()
+                    .equals(other.loadState, loadState)));
   }
 
   @override
-  int get hashCode => runtimeType.hashCode;
+  int get hashCode =>
+      runtimeType.hashCode ^
+      const DeepCollectionEquality().hash(doc) ^
+      const DeepCollectionEquality().hash(loadState);
 
+  @JsonKey(ignore: true)
+  @override
+  _$DocStateCopyWith<_DocState> get copyWith =>
+      __$DocStateCopyWithImpl<_DocState>(this, _$identity);
+}
+
+abstract class _DocState implements DocState {
+  const factory _DocState(
+      {required Option<FlowyDoc> doc,
+      required DocLoadState loadState}) = _$_DocState;
+
+  @override
+  Option<FlowyDoc> get doc => throw _privateConstructorUsedError;
   @override
+  DocLoadState get loadState => throw _privateConstructorUsedError;
+  @override
+  @JsonKey(ignore: true)
+  _$DocStateCopyWith<_DocState> get copyWith =>
+      throw _privateConstructorUsedError;
+}
+
+/// @nodoc
+class _$DocLoadStateTearOff {
+  const _$DocLoadStateTearOff();
+
+  _Loading loading() {
+    return const _Loading();
+  }
+
+  _Finish finish(Either<FlowyDoc, WorkspaceError> successOrFail) {
+    return _Finish(
+      successOrFail,
+    );
+  }
+}
+
+/// @nodoc
+const $DocLoadState = _$DocLoadStateTearOff();
+
+/// @nodoc
+mixin _$DocLoadState {
   @optionalTypeArgs
   TResult when<TResult extends Object?>({
     required TResult Function() loading,
-    required TResult Function(FlowyDoc doc) loadDoc,
-    required TResult Function(WorkspaceError error) loadFail,
-  }) {
-    return loading();
-  }
-
-  @override
+    required TResult Function(Either<FlowyDoc, WorkspaceError> successOrFail)
+        finish,
+  }) =>
+      throw _privateConstructorUsedError;
   @optionalTypeArgs
   TResult maybeWhen<TResult extends Object?>({
     TResult Function()? loading,
-    TResult Function(FlowyDoc doc)? loadDoc,
-    TResult Function(WorkspaceError error)? loadFail,
+    TResult Function(Either<FlowyDoc, WorkspaceError> successOrFail)? finish,
     required TResult orElse(),
-  }) {
-    if (loading != null) {
-      return loading();
-    }
-    return orElse();
-  }
-
-  @override
+  }) =>
+      throw _privateConstructorUsedError;
   @optionalTypeArgs
   TResult map<TResult extends Object?>({
-    required TResult Function(Loading value) loading,
-    required TResult Function(LoadedDoc value) loadDoc,
-    required TResult Function(LoadFail value) loadFail,
-  }) {
-    return loading(this);
-  }
-
-  @override
+    required TResult Function(_Loading value) loading,
+    required TResult Function(_Finish value) finish,
+  }) =>
+      throw _privateConstructorUsedError;
   @optionalTypeArgs
   TResult maybeMap<TResult extends Object?>({
-    TResult Function(Loading value)? loading,
-    TResult Function(LoadedDoc value)? loadDoc,
-    TResult Function(LoadFail value)? loadFail,
+    TResult Function(_Loading value)? loading,
+    TResult Function(_Finish value)? finish,
     required TResult orElse(),
-  }) {
-    if (loading != null) {
-      return loading(this);
-    }
-    return orElse();
-  }
+  }) =>
+      throw _privateConstructorUsedError;
 }
 
-abstract class Loading implements DocState {
-  const factory Loading() = _$Loading;
+/// @nodoc
+abstract class $DocLoadStateCopyWith<$Res> {
+  factory $DocLoadStateCopyWith(
+          DocLoadState value, $Res Function(DocLoadState) then) =
+      _$DocLoadStateCopyWithImpl<$Res>;
 }
 
 /// @nodoc
-abstract class $LoadedDocCopyWith<$Res> {
-  factory $LoadedDocCopyWith(LoadedDoc value, $Res Function(LoadedDoc) then) =
-      _$LoadedDocCopyWithImpl<$Res>;
-  $Res call({FlowyDoc doc});
+class _$DocLoadStateCopyWithImpl<$Res> implements $DocLoadStateCopyWith<$Res> {
+  _$DocLoadStateCopyWithImpl(this._value, this._then);
+
+  final DocLoadState _value;
+  // ignore: unused_field
+  final $Res Function(DocLoadState) _then;
 }
 
 /// @nodoc
-class _$LoadedDocCopyWithImpl<$Res> extends _$DocStateCopyWithImpl<$Res>
-    implements $LoadedDocCopyWith<$Res> {
-  _$LoadedDocCopyWithImpl(LoadedDoc _value, $Res Function(LoadedDoc) _then)
-      : super(_value, (v) => _then(v as LoadedDoc));
+abstract class _$LoadingCopyWith<$Res> {
+  factory _$LoadingCopyWith(_Loading value, $Res Function(_Loading) then) =
+      __$LoadingCopyWithImpl<$Res>;
+}
 
-  @override
-  LoadedDoc get _value => super._value as LoadedDoc;
+/// @nodoc
+class __$LoadingCopyWithImpl<$Res> extends _$DocLoadStateCopyWithImpl<$Res>
+    implements _$LoadingCopyWith<$Res> {
+  __$LoadingCopyWithImpl(_Loading _value, $Res Function(_Loading) _then)
+      : super(_value, (v) => _then(v as _Loading));
 
   @override
-  $Res call({
-    Object? doc = freezed,
-  }) {
-    return _then(LoadedDoc(
-      doc == freezed
-          ? _value.doc
-          : doc // ignore: cast_nullable_to_non_nullable
-              as FlowyDoc,
-    ));
-  }
+  _Loading get _value => super._value as _Loading;
 }
 
 /// @nodoc
 
-class _$LoadedDoc implements LoadedDoc {
-  const _$LoadedDoc(this.doc);
-
-  @override
-  final FlowyDoc doc;
+class _$_Loading implements _Loading {
+  const _$_Loading();
 
   @override
   String toString() {
-    return 'DocState.loadDoc(doc: $doc)';
+    return 'DocLoadState.loading()';
   }
 
   @override
   bool operator ==(dynamic other) {
-    return identical(this, other) ||
-        (other is LoadedDoc &&
-            (identical(other.doc, doc) ||
-                const DeepCollectionEquality().equals(other.doc, doc)));
+    return identical(this, other) || (other is _Loading);
   }
 
   @override
-  int get hashCode =>
-      runtimeType.hashCode ^ const DeepCollectionEquality().hash(doc);
-
-  @JsonKey(ignore: true)
-  @override
-  $LoadedDocCopyWith<LoadedDoc> get copyWith =>
-      _$LoadedDocCopyWithImpl<LoadedDoc>(this, _$identity);
+  int get hashCode => runtimeType.hashCode;
 
   @override
   @optionalTypeArgs
   TResult when<TResult extends Object?>({
     required TResult Function() loading,
-    required TResult Function(FlowyDoc doc) loadDoc,
-    required TResult Function(WorkspaceError error) loadFail,
+    required TResult Function(Either<FlowyDoc, WorkspaceError> successOrFail)
+        finish,
   }) {
-    return loadDoc(doc);
+    return loading();
   }
 
   @override
   @optionalTypeArgs
   TResult maybeWhen<TResult extends Object?>({
     TResult Function()? loading,
-    TResult Function(FlowyDoc doc)? loadDoc,
-    TResult Function(WorkspaceError error)? loadFail,
+    TResult Function(Either<FlowyDoc, WorkspaceError> successOrFail)? finish,
     required TResult orElse(),
   }) {
-    if (loadDoc != null) {
-      return loadDoc(doc);
+    if (loading != null) {
+      return loading();
     }
     return orElse();
   }
@@ -390,116 +428,109 @@ class _$LoadedDoc implements LoadedDoc {
   @override
   @optionalTypeArgs
   TResult map<TResult extends Object?>({
-    required TResult Function(Loading value) loading,
-    required TResult Function(LoadedDoc value) loadDoc,
-    required TResult Function(LoadFail value) loadFail,
+    required TResult Function(_Loading value) loading,
+    required TResult Function(_Finish value) finish,
   }) {
-    return loadDoc(this);
+    return loading(this);
   }
 
   @override
   @optionalTypeArgs
   TResult maybeMap<TResult extends Object?>({
-    TResult Function(Loading value)? loading,
-    TResult Function(LoadedDoc value)? loadDoc,
-    TResult Function(LoadFail value)? loadFail,
+    TResult Function(_Loading value)? loading,
+    TResult Function(_Finish value)? finish,
     required TResult orElse(),
   }) {
-    if (loadDoc != null) {
-      return loadDoc(this);
+    if (loading != null) {
+      return loading(this);
     }
     return orElse();
   }
 }
 
-abstract class LoadedDoc implements DocState {
-  const factory LoadedDoc(FlowyDoc doc) = _$LoadedDoc;
-
-  FlowyDoc get doc => throw _privateConstructorUsedError;
-  @JsonKey(ignore: true)
-  $LoadedDocCopyWith<LoadedDoc> get copyWith =>
-      throw _privateConstructorUsedError;
+abstract class _Loading implements DocLoadState {
+  const factory _Loading() = _$_Loading;
 }
 
 /// @nodoc
-abstract class $LoadFailCopyWith<$Res> {
-  factory $LoadFailCopyWith(LoadFail value, $Res Function(LoadFail) then) =
-      _$LoadFailCopyWithImpl<$Res>;
-  $Res call({WorkspaceError error});
+abstract class _$FinishCopyWith<$Res> {
+  factory _$FinishCopyWith(_Finish value, $Res Function(_Finish) then) =
+      __$FinishCopyWithImpl<$Res>;
+  $Res call({Either<FlowyDoc, WorkspaceError> successOrFail});
 }
 
 /// @nodoc
-class _$LoadFailCopyWithImpl<$Res> extends _$DocStateCopyWithImpl<$Res>
-    implements $LoadFailCopyWith<$Res> {
-  _$LoadFailCopyWithImpl(LoadFail _value, $Res Function(LoadFail) _then)
-      : super(_value, (v) => _then(v as LoadFail));
+class __$FinishCopyWithImpl<$Res> extends _$DocLoadStateCopyWithImpl<$Res>
+    implements _$FinishCopyWith<$Res> {
+  __$FinishCopyWithImpl(_Finish _value, $Res Function(_Finish) _then)
+      : super(_value, (v) => _then(v as _Finish));
 
   @override
-  LoadFail get _value => super._value as LoadFail;
+  _Finish get _value => super._value as _Finish;
 
   @override
   $Res call({
-    Object? error = freezed,
+    Object? successOrFail = freezed,
   }) {
-    return _then(LoadFail(
-      error == freezed
-          ? _value.error
-          : error // ignore: cast_nullable_to_non_nullable
-              as WorkspaceError,
+    return _then(_Finish(
+      successOrFail == freezed
+          ? _value.successOrFail
+          : successOrFail // ignore: cast_nullable_to_non_nullable
+              as Either<FlowyDoc, WorkspaceError>,
     ));
   }
 }
 
 /// @nodoc
 
-class _$LoadFail implements LoadFail {
-  const _$LoadFail(this.error);
+class _$_Finish implements _Finish {
+  const _$_Finish(this.successOrFail);
 
   @override
-  final WorkspaceError error;
+  final Either<FlowyDoc, WorkspaceError> successOrFail;
 
   @override
   String toString() {
-    return 'DocState.loadFail(error: $error)';
+    return 'DocLoadState.finish(successOrFail: $successOrFail)';
   }
 
   @override
   bool operator ==(dynamic other) {
     return identical(this, other) ||
-        (other is LoadFail &&
-            (identical(other.error, error) ||
-                const DeepCollectionEquality().equals(other.error, error)));
+        (other is _Finish &&
+            (identical(other.successOrFail, successOrFail) ||
+                const DeepCollectionEquality()
+                    .equals(other.successOrFail, successOrFail)));
   }
 
   @override
   int get hashCode =>
-      runtimeType.hashCode ^ const DeepCollectionEquality().hash(error);
+      runtimeType.hashCode ^ const DeepCollectionEquality().hash(successOrFail);
 
   @JsonKey(ignore: true)
   @override
-  $LoadFailCopyWith<LoadFail> get copyWith =>
-      _$LoadFailCopyWithImpl<LoadFail>(this, _$identity);
+  _$FinishCopyWith<_Finish> get copyWith =>
+      __$FinishCopyWithImpl<_Finish>(this, _$identity);
 
   @override
   @optionalTypeArgs
   TResult when<TResult extends Object?>({
     required TResult Function() loading,
-    required TResult Function(FlowyDoc doc) loadDoc,
-    required TResult Function(WorkspaceError error) loadFail,
+    required TResult Function(Either<FlowyDoc, WorkspaceError> successOrFail)
+        finish,
   }) {
-    return loadFail(error);
+    return finish(successOrFail);
   }
 
   @override
   @optionalTypeArgs
   TResult maybeWhen<TResult extends Object?>({
     TResult Function()? loading,
-    TResult Function(FlowyDoc doc)? loadDoc,
-    TResult Function(WorkspaceError error)? loadFail,
+    TResult Function(Either<FlowyDoc, WorkspaceError> successOrFail)? finish,
     required TResult orElse(),
   }) {
-    if (loadFail != null) {
-      return loadFail(error);
+    if (finish != null) {
+      return finish(successOrFail);
     }
     return orElse();
   }
@@ -507,33 +538,32 @@ class _$LoadFail implements LoadFail {
   @override
   @optionalTypeArgs
   TResult map<TResult extends Object?>({
-    required TResult Function(Loading value) loading,
-    required TResult Function(LoadedDoc value) loadDoc,
-    required TResult Function(LoadFail value) loadFail,
+    required TResult Function(_Loading value) loading,
+    required TResult Function(_Finish value) finish,
   }) {
-    return loadFail(this);
+    return finish(this);
   }
 
   @override
   @optionalTypeArgs
   TResult maybeMap<TResult extends Object?>({
-    TResult Function(Loading value)? loading,
-    TResult Function(LoadedDoc value)? loadDoc,
-    TResult Function(LoadFail value)? loadFail,
+    TResult Function(_Loading value)? loading,
+    TResult Function(_Finish value)? finish,
     required TResult orElse(),
   }) {
-    if (loadFail != null) {
-      return loadFail(this);
+    if (finish != null) {
+      return finish(this);
     }
     return orElse();
   }
 }
 
-abstract class LoadFail implements DocState {
-  const factory LoadFail(WorkspaceError error) = _$LoadFail;
+abstract class _Finish implements DocLoadState {
+  const factory _Finish(Either<FlowyDoc, WorkspaceError> successOrFail) =
+      _$_Finish;
 
-  WorkspaceError get error => throw _privateConstructorUsedError;
-  @JsonKey(ignore: true)
-  $LoadFailCopyWith<LoadFail> get copyWith =>
+  Either<FlowyDoc, WorkspaceError> get successOrFail =>
       throw _privateConstructorUsedError;
+  @JsonKey(ignore: true)
+  _$FinishCopyWith<_Finish> get copyWith => throw _privateConstructorUsedError;
 }

+ 0 - 38
app_flowy/lib/workspace/application/doc/doc_edit_bloc.dart

@@ -1,38 +0,0 @@
-import 'package:app_flowy/workspace/domain/i_doc.dart';
-import 'package:freezed_annotation/freezed_annotation.dart';
-import 'package:flutter_bloc/flutter_bloc.dart';
-
-part 'doc_edit_bloc.freezed.dart';
-
-class DocEditBloc extends Bloc<DocEditEvent, DocEditState> {
-  final IDoc iDocImpl;
-
-  DocEditBloc(this.iDocImpl) : super(DocEditState.initial());
-
-  @override
-  Stream<DocEditState> mapEventToState(DocEditEvent event) async* {
-    yield* event.map(
-      initial: (e) async* {},
-      close: (Close value) async* {
-        iDocImpl.closeDoc();
-      },
-    );
-  }
-}
-
-@freezed
-class DocEditEvent with _$DocEditEvent {
-  const factory DocEditEvent.initial() = Initial;
-  const factory DocEditEvent.close() = Close;
-}
-
-@freezed
-class DocEditState with _$DocEditState {
-  const factory DocEditState({
-    required bool isSaving,
-  }) = _DocEditState;
-
-  factory DocEditState.initial() => const DocEditState(
-        isSaving: false,
-      );
-}

+ 0 - 369
app_flowy/lib/workspace/application/doc/doc_edit_bloc.freezed.dart

@@ -1,369 +0,0 @@
-// GENERATED CODE - DO NOT MODIFY BY HAND
-// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target
-
-part of 'doc_edit_bloc.dart';
-
-// **************************************************************************
-// FreezedGenerator
-// **************************************************************************
-
-T _$identity<T>(T value) => value;
-
-final _privateConstructorUsedError = UnsupportedError(
-    'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more informations: https://github.com/rrousselGit/freezed#custom-getters-and-methods');
-
-/// @nodoc
-class _$DocEditEventTearOff {
-  const _$DocEditEventTearOff();
-
-  Initial initial() {
-    return const Initial();
-  }
-
-  Close close() {
-    return const Close();
-  }
-}
-
-/// @nodoc
-const $DocEditEvent = _$DocEditEventTearOff();
-
-/// @nodoc
-mixin _$DocEditEvent {
-  @optionalTypeArgs
-  TResult when<TResult extends Object?>({
-    required TResult Function() initial,
-    required TResult Function() close,
-  }) =>
-      throw _privateConstructorUsedError;
-  @optionalTypeArgs
-  TResult maybeWhen<TResult extends Object?>({
-    TResult Function()? initial,
-    TResult Function()? close,
-    required TResult orElse(),
-  }) =>
-      throw _privateConstructorUsedError;
-  @optionalTypeArgs
-  TResult map<TResult extends Object?>({
-    required TResult Function(Initial value) initial,
-    required TResult Function(Close value) close,
-  }) =>
-      throw _privateConstructorUsedError;
-  @optionalTypeArgs
-  TResult maybeMap<TResult extends Object?>({
-    TResult Function(Initial value)? initial,
-    TResult Function(Close value)? close,
-    required TResult orElse(),
-  }) =>
-      throw _privateConstructorUsedError;
-}
-
-/// @nodoc
-abstract class $DocEditEventCopyWith<$Res> {
-  factory $DocEditEventCopyWith(
-          DocEditEvent value, $Res Function(DocEditEvent) then) =
-      _$DocEditEventCopyWithImpl<$Res>;
-}
-
-/// @nodoc
-class _$DocEditEventCopyWithImpl<$Res> implements $DocEditEventCopyWith<$Res> {
-  _$DocEditEventCopyWithImpl(this._value, this._then);
-
-  final DocEditEvent _value;
-  // ignore: unused_field
-  final $Res Function(DocEditEvent) _then;
-}
-
-/// @nodoc
-abstract class $InitialCopyWith<$Res> {
-  factory $InitialCopyWith(Initial value, $Res Function(Initial) then) =
-      _$InitialCopyWithImpl<$Res>;
-}
-
-/// @nodoc
-class _$InitialCopyWithImpl<$Res> extends _$DocEditEventCopyWithImpl<$Res>
-    implements $InitialCopyWith<$Res> {
-  _$InitialCopyWithImpl(Initial _value, $Res Function(Initial) _then)
-      : super(_value, (v) => _then(v as Initial));
-
-  @override
-  Initial get _value => super._value as Initial;
-}
-
-/// @nodoc
-
-class _$Initial implements Initial {
-  const _$Initial();
-
-  @override
-  String toString() {
-    return 'DocEditEvent.initial()';
-  }
-
-  @override
-  bool operator ==(dynamic other) {
-    return identical(this, other) || (other is Initial);
-  }
-
-  @override
-  int get hashCode => runtimeType.hashCode;
-
-  @override
-  @optionalTypeArgs
-  TResult when<TResult extends Object?>({
-    required TResult Function() initial,
-    required TResult Function() close,
-  }) {
-    return initial();
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult maybeWhen<TResult extends Object?>({
-    TResult Function()? initial,
-    TResult Function()? close,
-    required TResult orElse(),
-  }) {
-    if (initial != null) {
-      return initial();
-    }
-    return orElse();
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult map<TResult extends Object?>({
-    required TResult Function(Initial value) initial,
-    required TResult Function(Close value) close,
-  }) {
-    return initial(this);
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult maybeMap<TResult extends Object?>({
-    TResult Function(Initial value)? initial,
-    TResult Function(Close value)? close,
-    required TResult orElse(),
-  }) {
-    if (initial != null) {
-      return initial(this);
-    }
-    return orElse();
-  }
-}
-
-abstract class Initial implements DocEditEvent {
-  const factory Initial() = _$Initial;
-}
-
-/// @nodoc
-abstract class $CloseCopyWith<$Res> {
-  factory $CloseCopyWith(Close value, $Res Function(Close) then) =
-      _$CloseCopyWithImpl<$Res>;
-}
-
-/// @nodoc
-class _$CloseCopyWithImpl<$Res> extends _$DocEditEventCopyWithImpl<$Res>
-    implements $CloseCopyWith<$Res> {
-  _$CloseCopyWithImpl(Close _value, $Res Function(Close) _then)
-      : super(_value, (v) => _then(v as Close));
-
-  @override
-  Close get _value => super._value as Close;
-}
-
-/// @nodoc
-
-class _$Close implements Close {
-  const _$Close();
-
-  @override
-  String toString() {
-    return 'DocEditEvent.close()';
-  }
-
-  @override
-  bool operator ==(dynamic other) {
-    return identical(this, other) || (other is Close);
-  }
-
-  @override
-  int get hashCode => runtimeType.hashCode;
-
-  @override
-  @optionalTypeArgs
-  TResult when<TResult extends Object?>({
-    required TResult Function() initial,
-    required TResult Function() close,
-  }) {
-    return close();
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult maybeWhen<TResult extends Object?>({
-    TResult Function()? initial,
-    TResult Function()? close,
-    required TResult orElse(),
-  }) {
-    if (close != null) {
-      return close();
-    }
-    return orElse();
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult map<TResult extends Object?>({
-    required TResult Function(Initial value) initial,
-    required TResult Function(Close value) close,
-  }) {
-    return close(this);
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult maybeMap<TResult extends Object?>({
-    TResult Function(Initial value)? initial,
-    TResult Function(Close value)? close,
-    required TResult orElse(),
-  }) {
-    if (close != null) {
-      return close(this);
-    }
-    return orElse();
-  }
-}
-
-abstract class Close implements DocEditEvent {
-  const factory Close() = _$Close;
-}
-
-/// @nodoc
-class _$DocEditStateTearOff {
-  const _$DocEditStateTearOff();
-
-  _DocEditState call({required bool isSaving}) {
-    return _DocEditState(
-      isSaving: isSaving,
-    );
-  }
-}
-
-/// @nodoc
-const $DocEditState = _$DocEditStateTearOff();
-
-/// @nodoc
-mixin _$DocEditState {
-  bool get isSaving => throw _privateConstructorUsedError;
-
-  @JsonKey(ignore: true)
-  $DocEditStateCopyWith<DocEditState> get copyWith =>
-      throw _privateConstructorUsedError;
-}
-
-/// @nodoc
-abstract class $DocEditStateCopyWith<$Res> {
-  factory $DocEditStateCopyWith(
-          DocEditState value, $Res Function(DocEditState) then) =
-      _$DocEditStateCopyWithImpl<$Res>;
-  $Res call({bool isSaving});
-}
-
-/// @nodoc
-class _$DocEditStateCopyWithImpl<$Res> implements $DocEditStateCopyWith<$Res> {
-  _$DocEditStateCopyWithImpl(this._value, this._then);
-
-  final DocEditState _value;
-  // ignore: unused_field
-  final $Res Function(DocEditState) _then;
-
-  @override
-  $Res call({
-    Object? isSaving = freezed,
-  }) {
-    return _then(_value.copyWith(
-      isSaving: isSaving == freezed
-          ? _value.isSaving
-          : isSaving // ignore: cast_nullable_to_non_nullable
-              as bool,
-    ));
-  }
-}
-
-/// @nodoc
-abstract class _$DocEditStateCopyWith<$Res>
-    implements $DocEditStateCopyWith<$Res> {
-  factory _$DocEditStateCopyWith(
-          _DocEditState value, $Res Function(_DocEditState) then) =
-      __$DocEditStateCopyWithImpl<$Res>;
-  @override
-  $Res call({bool isSaving});
-}
-
-/// @nodoc
-class __$DocEditStateCopyWithImpl<$Res> extends _$DocEditStateCopyWithImpl<$Res>
-    implements _$DocEditStateCopyWith<$Res> {
-  __$DocEditStateCopyWithImpl(
-      _DocEditState _value, $Res Function(_DocEditState) _then)
-      : super(_value, (v) => _then(v as _DocEditState));
-
-  @override
-  _DocEditState get _value => super._value as _DocEditState;
-
-  @override
-  $Res call({
-    Object? isSaving = freezed,
-  }) {
-    return _then(_DocEditState(
-      isSaving: isSaving == freezed
-          ? _value.isSaving
-          : isSaving // ignore: cast_nullable_to_non_nullable
-              as bool,
-    ));
-  }
-}
-
-/// @nodoc
-
-class _$_DocEditState implements _DocEditState {
-  const _$_DocEditState({required this.isSaving});
-
-  @override
-  final bool isSaving;
-
-  @override
-  String toString() {
-    return 'DocEditState(isSaving: $isSaving)';
-  }
-
-  @override
-  bool operator ==(dynamic other) {
-    return identical(this, other) ||
-        (other is _DocEditState &&
-            (identical(other.isSaving, isSaving) ||
-                const DeepCollectionEquality()
-                    .equals(other.isSaving, isSaving)));
-  }
-
-  @override
-  int get hashCode =>
-      runtimeType.hashCode ^ const DeepCollectionEquality().hash(isSaving);
-
-  @JsonKey(ignore: true)
-  @override
-  _$DocEditStateCopyWith<_DocEditState> get copyWith =>
-      __$DocEditStateCopyWithImpl<_DocEditState>(this, _$identity);
-}
-
-abstract class _DocEditState implements DocEditState {
-  const factory _DocEditState({required bool isSaving}) = _$_DocEditState;
-
-  @override
-  bool get isSaving => throw _privateConstructorUsedError;
-  @override
-  @JsonKey(ignore: true)
-  _$DocEditStateCopyWith<_DocEditState> get copyWith =>
-      throw _privateConstructorUsedError;
-}

+ 1 - 3
app_flowy/lib/workspace/infrastructure/deps_resolver.dart

@@ -1,6 +1,5 @@
 import 'package:app_flowy/workspace/application/app/app_bloc.dart';
 import 'package:app_flowy/workspace/application/doc/doc_bloc.dart';
-import 'package:app_flowy/workspace/application/doc/doc_edit_bloc.dart';
 import 'package:app_flowy/workspace/application/menu/menu_bloc.dart';
 import 'package:app_flowy/workspace/application/menu/menu_user_bloc.dart';
 import 'package:app_flowy/workspace/application/trash/trash_bloc.dart';
@@ -86,8 +85,7 @@ class HomeDepsResolver {
     );
 
     // Doc
-    getIt.registerFactoryParam<DocBloc, String, void>((docId, _) => DocBloc(iDocImpl: getIt<IDoc>(param1: docId)));
-    getIt.registerFactoryParam<DocEditBloc, String, void>((docId, _) => DocEditBloc(getIt<IDoc>(param1: docId)));
+    getIt.registerFactoryParam<DocBloc, String, void>((docId, _) => DocBloc(docManager: getIt<IDoc>(param1: docId)));
 
     // trash
     getIt.registerLazySingleton<TrashRepo>(() => TrashRepo());

+ 42 - 28
app_flowy/lib/workspace/presentation/stack_page/doc/doc_page.dart

@@ -1,55 +1,69 @@
 import 'dart:io';
 import 'package:app_flowy/startup/startup.dart';
-import 'package:app_flowy/workspace/application/doc/doc_edit_bloc.dart';
+import 'package:app_flowy/workspace/application/doc/doc_bloc.dart';
 import 'package:app_flowy/workspace/domain/i_doc.dart';
 import 'package:editor/flutter_quill.dart';
+import 'package:flowy_infra_ui/style_widget/progress_indicator.dart';
+import 'package:flowy_infra_ui/widget/error_page.dart';
+import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pb.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter_bloc/flutter_bloc.dart';
 
 class DocPage extends StatefulWidget {
-  final QuillController controller;
-  final DocEditBloc editBloc;
-  final FlowyDoc doc;
-
-  DocPage({Key? key, required this.doc})
-      : controller = QuillController(
-          document: doc.document,
-          selection: const TextSelection.collapsed(offset: 0),
-        ),
-        editBloc = getIt<DocEditBloc>(param1: doc.id),
-        super(key: key);
+  final View view;
+
+  const DocPage({Key? key, required this.view}) : super(key: key);
 
   @override
   State<DocPage> createState() => _DocPageState();
 }
 
 class _DocPageState extends State<DocPage> {
+  late DocBloc docBloc;
   final FocusNode _focusNode = FocusNode();
 
+  @override
+  void initState() {
+    docBloc = getIt<DocBloc>(param1: super.widget.view.id)..add(const DocEvent.initial());
+    super.initState();
+  }
+
   @override
   Widget build(BuildContext context) {
-    return BlocProvider.value(
-      value: widget.editBloc,
-      child: BlocBuilder<DocEditBloc, DocEditState>(
-        builder: (ctx, state) {
-          return Column(
-            mainAxisAlignment: MainAxisAlignment.spaceBetween,
-            children: [
-              _renderEditor(widget.controller),
-              _renderToolbar(widget.controller),
-            ],
-          );
-        },
-      ),
+    return MultiBlocProvider(
+      providers: [
+        BlocProvider<DocBloc>.value(value: docBloc),
+      ],
+      child: BlocBuilder<DocBloc, DocState>(builder: (context, state) {
+        return state.loadState.map(
+          loading: (_) => const FlowyProgressIndicator(),
+          finish: (result) => result.successOrFail.fold(
+            (doc) => _renderDoc(context, doc),
+            (err) => FlowyErrorPage(err.toString()),
+          ),
+        );
+      }),
     );
   }
 
   @override
   Future<void> dispose() async {
-    widget.editBloc.add(const DocEditEvent.close());
-    widget.editBloc.close();
+    docBloc.close();
     super.dispose();
-    await widget.doc.close();
+  }
+
+  Widget _renderDoc(BuildContext context, FlowyDoc doc) {
+    QuillController controller = QuillController(
+      document: doc.document,
+      selection: const TextSelection.collapsed(offset: 0),
+    );
+    return Column(
+      mainAxisAlignment: MainAxisAlignment.spaceBetween,
+      children: [
+        _renderEditor(controller),
+        _renderToolbar(controller),
+      ],
+    );
   }
 
   Widget _renderEditor(QuillController controller) {

+ 1 - 24
app_flowy/lib/workspace/presentation/stack_page/doc/doc_stack_page.dart

@@ -1,14 +1,8 @@
-import 'package:app_flowy/startup/startup.dart';
-import 'package:app_flowy/workspace/application/doc/doc_bloc.dart';
 import 'package:app_flowy/workspace/domain/page_stack/page_stack.dart';
 import 'package:app_flowy/workspace/domain/view_ext.dart';
 import 'package:flowy_infra_ui/style_widget/text.dart';
-import 'package:flowy_log/flowy_log.dart';
-import 'package:flowy_infra_ui/widget/error_page.dart';
 import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pb.dart';
 import 'package:flutter/material.dart';
-import 'package:flutter_bloc/flutter_bloc.dart';
-import 'package:flowy_infra_ui/style_widget/progress_indicator.dart';
 
 import 'doc_page.dart';
 
@@ -41,8 +35,6 @@ class DocStackContext extends HomeStackContext {
   List<NavigationItem> makeNavigationItems() {
     return [
       this,
-      this,
-      this,
     ];
   }
 }
@@ -58,22 +50,7 @@ class DocStackPage extends StatefulWidget {
 class _DocStackPageState extends State<DocStackPage> {
   @override
   Widget build(BuildContext context) {
-    return MultiBlocProvider(
-      providers: [
-        BlocProvider<DocBloc>(
-            create: (context) => getIt<DocBloc>(param1: widget.view.id)..add(const DocEvent.loadDoc())),
-      ],
-      child: BlocBuilder<DocBloc, DocState>(builder: (context, state) {
-        return state.map(
-          loading: (_) => const FlowyProgressIndicator(),
-          loadDoc: (s) => DocPage(doc: s.doc),
-          loadFail: (s) {
-            Log.error("$s");
-            return FlowyErrorPage(s.error.toString());
-          },
-        );
-      }),
-    );
+    return DocPage(view: widget.view);
   }
 
   @override