From ce5cccd670708873a8b6d2e6aab1e6b99cd8f12d Mon Sep 17 00:00:00 2001 From: appflowy Date: Tue, 19 Oct 2021 13:56:11 +0800 Subject: [PATCH] [flutter]: remove edit doc bloc --- .../workspace/application/doc/doc_bloc.dart | 56 +- .../application/doc/doc_bloc.freezed.dart | 558 +++++++++--------- .../application/doc/doc_edit_bloc.dart | 38 -- .../doc/doc_edit_bloc.freezed.dart | 369 ------------ .../infrastructure/deps_resolver.dart | 4 +- .../presentation/stack_page/doc/doc_page.dart | 68 ++- .../stack_page/doc/doc_stack_page.dart | 25 +- 7 files changed, 375 insertions(+), 743 deletions(-) delete mode 100644 app_flowy/lib/workspace/application/doc/doc_edit_bloc.dart delete mode 100644 app_flowy/lib/workspace/application/doc/doc_edit_bloc.freezed.dart diff --git a/app_flowy/lib/workspace/application/doc/doc_bloc.dart b/app_flowy/lib/workspace/application/doc/doc_bloc.dart index 58945c08d9..ce065f87a7 100644 --- a/app_flowy/lib/workspace/application/doc/doc_bloc.dart +++ b/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 { - final IDoc iDocImpl; + final IDoc docManager; - DocBloc({ - required this.iDocImpl, - }) : super(const DocState.loading()); + DocBloc({required this.docManager}) : super(DocState.initial()); @override Stream mapEventToState(DocEvent event) async* { yield* event.map( - loadDoc: (_) async* { - yield* _readDoc(); - }, + initial: _initial, ); } - Stream _readDoc() async* { - final docOrFail = await iDocImpl.readDoc(); - yield docOrFail.fold( + @override + Future close() async { + docManager.closeDoc(); + + await state.doc.fold(() => null, (doc) async { + await doc.close(); + }); + return super.close(); + } + + Stream _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 { @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 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 successOrFail) = _Finish; } diff --git a/app_flowy/lib/workspace/application/doc/doc_bloc.freezed.dart b/app_flowy/lib/workspace/application/doc/doc_bloc.freezed.dart index 976e6648c2..61e9e32369 100644 --- a/app_flowy/lib/workspace/application/doc/doc_bloc.freezed.dart +++ b/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({ - required TResult Function() loadDoc, + required TResult Function() initial, }) => throw _privateConstructorUsedError; @optionalTypeArgs TResult maybeWhen({ - TResult Function()? loadDoc, + TResult Function()? initial, required TResult orElse(), }) => throw _privateConstructorUsedError; @optionalTypeArgs TResult map({ - required TResult Function(LoadDoc value) loadDoc, + required TResult Function(Initial value) initial, }) => throw _privateConstructorUsedError; @optionalTypeArgs TResult maybeMap({ - 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({ - required TResult Function() loadDoc, + required TResult Function() initial, }) { - return loadDoc(); + return initial(); } @override @optionalTypeArgs TResult maybeWhen({ - 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({ - required TResult Function(LoadDoc value) loadDoc, + required TResult Function(Initial value) initial, }) { - return loadDoc(this); + return initial(this); } @override @optionalTypeArgs TResult maybeMap({ - 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 doc, required DocLoadState loadState}) { + return _DocState( + doc: doc, + loadState: loadState, ); } } @@ -170,35 +162,11 @@ const $DocState = _$DocStateTearOff(); /// @nodoc mixin _$DocState { - @optionalTypeArgs - TResult when({ - required TResult Function() loading, - required TResult Function(FlowyDoc doc) loadDoc, - required TResult Function(WorkspaceError error) loadFail, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult maybeWhen({ - TResult Function()? loading, - TResult Function(FlowyDoc doc)? loadDoc, - TResult Function(WorkspaceError error)? loadFail, - required TResult orElse(), - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult map({ - required TResult Function(Loading value) loading, - required TResult Function(LoadedDoc value) loadDoc, - required TResult Function(LoadFail value) loadFail, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult maybeMap({ - TResult Function(Loading value)? loading, - TResult Function(LoadedDoc value)? loadDoc, - TResult Function(LoadFail value)? loadFail, - required TResult orElse(), - }) => + Option get doc => throw _privateConstructorUsedError; + DocLoadState get loadState => throw _privateConstructorUsedError; + + @JsonKey(ignore: true) + $DocStateCopyWith 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 doc, DocLoadState loadState}); + + $DocLoadStateCopyWith<$Res> get loadState; } /// @nodoc @@ -215,37 +186,217 @@ class _$DocStateCopyWithImpl<$Res> implements $DocStateCopyWith<$Res> { final DocState _value; // ignore: unused_field final $Res Function(DocState) _then; -} - -/// @nodoc -abstract class $LoadingCopyWith<$Res> { - factory $LoadingCopyWith(Loading value, $Res Function(Loading) then) = - _$LoadingCopyWithImpl<$Res>; -} - -/// @nodoc -class _$LoadingCopyWithImpl<$Res> extends _$DocStateCopyWithImpl<$Res> - implements $LoadingCopyWith<$Res> { - _$LoadingCopyWithImpl(Loading _value, $Res Function(Loading) _then) - : super(_value, (v) => _then(v as Loading)); @override - Loading get _value => super._value as Loading; + $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, + 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 _$DocStateCopyWith<$Res> implements $DocStateCopyWith<$Res> { + factory _$DocStateCopyWith(_DocState value, $Res Function(_DocState) then) = + __$DocStateCopyWithImpl<$Res>; + @override + $Res call({Option doc, DocLoadState loadState}); + + @override + $DocLoadStateCopyWith<$Res> get loadState; +} + +/// @nodoc +class __$DocStateCopyWithImpl<$Res> extends _$DocStateCopyWithImpl<$Res> + implements _$DocStateCopyWith<$Res> { + __$DocStateCopyWithImpl(_DocState _value, $Res Function(_DocState) _then) + : super(_value, (v) => _then(v as _DocState)); + + @override + _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, + 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 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 ^ + 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 doc, + required DocLoadState loadState}) = _$_DocState; + + @override + Option 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 successOrFail) { + return _Finish( + successOrFail, + ); + } +} + +/// @nodoc +const $DocLoadState = _$DocLoadStateTearOff(); + +/// @nodoc +mixin _$DocLoadState { + @optionalTypeArgs + TResult when({ + required TResult Function() loading, + required TResult Function(Either successOrFail) + finish, + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult maybeWhen({ + TResult Function()? loading, + TResult Function(Either successOrFail)? finish, + required TResult orElse(), + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult map({ + required TResult Function(_Loading value) loading, + required TResult Function(_Finish value) finish, + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult maybeMap({ + TResult Function(_Loading value)? loading, + TResult Function(_Finish value)? finish, + required TResult orElse(), + }) => + throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $DocLoadStateCopyWith<$Res> { + factory $DocLoadStateCopyWith( + DocLoadState value, $Res Function(DocLoadState) then) = + _$DocLoadStateCopyWithImpl<$Res>; +} + +/// @nodoc +class _$DocLoadStateCopyWithImpl<$Res> implements $DocLoadStateCopyWith<$Res> { + _$DocLoadStateCopyWithImpl(this._value, this._then); + + final DocLoadState _value; + // ignore: unused_field + final $Res Function(DocLoadState) _then; +} + +/// @nodoc +abstract class _$LoadingCopyWith<$Res> { + factory _$LoadingCopyWith(_Loading value, $Res Function(_Loading) then) = + __$LoadingCopyWithImpl<$Res>; +} + +/// @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 + _Loading get _value => super._value as _Loading; +} + +/// @nodoc + +class _$_Loading implements _Loading { + const _$_Loading(); + + @override + String toString() { + return 'DocLoadState.loading()'; + } + + @override + bool operator ==(dynamic other) { + return identical(this, other) || (other is _Loading); } @override @@ -255,8 +406,8 @@ class _$Loading implements Loading { @optionalTypeArgs TResult when({ required TResult Function() loading, - required TResult Function(FlowyDoc doc) loadDoc, - required TResult Function(WorkspaceError error) loadFail, + required TResult Function(Either successOrFail) + finish, }) { return loading(); } @@ -265,8 +416,7 @@ class _$Loading implements Loading { @optionalTypeArgs TResult maybeWhen({ TResult Function()? loading, - TResult Function(FlowyDoc doc)? loadDoc, - TResult Function(WorkspaceError error)? loadFail, + TResult Function(Either successOrFail)? finish, required TResult orElse(), }) { if (loading != null) { @@ -278,9 +428,8 @@ class _$Loading implements Loading { @override @optionalTypeArgs TResult map({ - 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 loading(this); } @@ -288,9 +437,8 @@ class _$Loading implements Loading { @override @optionalTypeArgs TResult maybeMap({ - 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) { @@ -300,89 +448,89 @@ class _$Loading implements Loading { } } -abstract class Loading implements DocState { - const factory Loading() = _$Loading; +abstract class _Loading implements DocLoadState { + const factory _Loading() = _$_Loading; } /// @nodoc -abstract class $LoadedDocCopyWith<$Res> { - factory $LoadedDocCopyWith(LoadedDoc value, $Res Function(LoadedDoc) then) = - _$LoadedDocCopyWithImpl<$Res>; - $Res call({FlowyDoc doc}); +abstract class _$FinishCopyWith<$Res> { + factory _$FinishCopyWith(_Finish value, $Res Function(_Finish) then) = + __$FinishCopyWithImpl<$Res>; + $Res call({Either successOrFail}); } /// @nodoc -class _$LoadedDocCopyWithImpl<$Res> extends _$DocStateCopyWithImpl<$Res> - implements $LoadedDocCopyWith<$Res> { - _$LoadedDocCopyWithImpl(LoadedDoc _value, $Res Function(LoadedDoc) _then) - : super(_value, (v) => _then(v as LoadedDoc)); +class __$FinishCopyWithImpl<$Res> extends _$DocLoadStateCopyWithImpl<$Res> + implements _$FinishCopyWith<$Res> { + __$FinishCopyWithImpl(_Finish _value, $Res Function(_Finish) _then) + : super(_value, (v) => _then(v as _Finish)); @override - LoadedDoc get _value => super._value as LoadedDoc; + _Finish get _value => super._value as _Finish; @override $Res call({ - Object? doc = freezed, + Object? successOrFail = freezed, }) { - return _then(LoadedDoc( - doc == freezed - ? _value.doc - : doc // ignore: cast_nullable_to_non_nullable - as FlowyDoc, + return _then(_Finish( + successOrFail == freezed + ? _value.successOrFail + : successOrFail // ignore: cast_nullable_to_non_nullable + as Either, )); } } /// @nodoc -class _$LoadedDoc implements LoadedDoc { - const _$LoadedDoc(this.doc); +class _$_Finish implements _Finish { + const _$_Finish(this.successOrFail); @override - final FlowyDoc doc; + final Either successOrFail; @override String toString() { - return 'DocState.loadDoc(doc: $doc)'; + return 'DocLoadState.finish(successOrFail: $successOrFail)'; } @override bool operator ==(dynamic other) { return identical(this, other) || - (other is LoadedDoc && - (identical(other.doc, doc) || - const DeepCollectionEquality().equals(other.doc, doc))); + (other is _Finish && + (identical(other.successOrFail, successOrFail) || + const DeepCollectionEquality() + .equals(other.successOrFail, successOrFail))); } @override int get hashCode => - runtimeType.hashCode ^ const DeepCollectionEquality().hash(doc); + runtimeType.hashCode ^ const DeepCollectionEquality().hash(successOrFail); @JsonKey(ignore: true) @override - $LoadedDocCopyWith get copyWith => - _$LoadedDocCopyWithImpl(this, _$identity); + _$FinishCopyWith<_Finish> get copyWith => + __$FinishCopyWithImpl<_Finish>(this, _$identity); @override @optionalTypeArgs TResult when({ required TResult Function() loading, - required TResult Function(FlowyDoc doc) loadDoc, - required TResult Function(WorkspaceError error) loadFail, + required TResult Function(Either successOrFail) + finish, }) { - return loadDoc(doc); + return finish(successOrFail); } @override @optionalTypeArgs TResult maybeWhen({ TResult Function()? loading, - TResult Function(FlowyDoc doc)? loadDoc, - TResult Function(WorkspaceError error)? loadFail, + TResult Function(Either successOrFail)? finish, required TResult orElse(), }) { - if (loadDoc != null) { - return loadDoc(doc); + if (finish != null) { + return finish(successOrFail); } return orElse(); } @@ -390,150 +538,32 @@ class _$LoadedDoc implements LoadedDoc { @override @optionalTypeArgs TResult map({ - 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 finish(this); } @override @optionalTypeArgs TResult maybeMap({ - 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 (finish != null) { + return finish(this); } return orElse(); } } -abstract class LoadedDoc implements DocState { - const factory LoadedDoc(FlowyDoc doc) = _$LoadedDoc; +abstract class _Finish implements DocLoadState { + const factory _Finish(Either successOrFail) = + _$_Finish; - FlowyDoc get doc => throw _privateConstructorUsedError; - @JsonKey(ignore: true) - $LoadedDocCopyWith get copyWith => + Either get successOrFail => throw _privateConstructorUsedError; -} - -/// @nodoc -abstract class $LoadFailCopyWith<$Res> { - factory $LoadFailCopyWith(LoadFail value, $Res Function(LoadFail) then) = - _$LoadFailCopyWithImpl<$Res>; - $Res call({WorkspaceError error}); -} - -/// @nodoc -class _$LoadFailCopyWithImpl<$Res> extends _$DocStateCopyWithImpl<$Res> - implements $LoadFailCopyWith<$Res> { - _$LoadFailCopyWithImpl(LoadFail _value, $Res Function(LoadFail) _then) - : super(_value, (v) => _then(v as LoadFail)); - - @override - LoadFail get _value => super._value as LoadFail; - - @override - $Res call({ - Object? error = freezed, - }) { - return _then(LoadFail( - error == freezed - ? _value.error - : error // ignore: cast_nullable_to_non_nullable - as WorkspaceError, - )); - } -} - -/// @nodoc - -class _$LoadFail implements LoadFail { - const _$LoadFail(this.error); - - @override - final WorkspaceError error; - - @override - String toString() { - return 'DocState.loadFail(error: $error)'; - } - - @override - bool operator ==(dynamic other) { - return identical(this, other) || - (other is LoadFail && - (identical(other.error, error) || - const DeepCollectionEquality().equals(other.error, error))); - } - - @override - int get hashCode => - runtimeType.hashCode ^ const DeepCollectionEquality().hash(error); - @JsonKey(ignore: true) - @override - $LoadFailCopyWith get copyWith => - _$LoadFailCopyWithImpl(this, _$identity); - - @override - @optionalTypeArgs - TResult when({ - required TResult Function() loading, - required TResult Function(FlowyDoc doc) loadDoc, - required TResult Function(WorkspaceError error) loadFail, - }) { - return loadFail(error); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function()? loading, - TResult Function(FlowyDoc doc)? loadDoc, - TResult Function(WorkspaceError error)? loadFail, - required TResult orElse(), - }) { - if (loadFail != null) { - return loadFail(error); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(Loading value) loading, - required TResult Function(LoadedDoc value) loadDoc, - required TResult Function(LoadFail value) loadFail, - }) { - return loadFail(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - TResult Function(Loading value)? loading, - TResult Function(LoadedDoc value)? loadDoc, - TResult Function(LoadFail value)? loadFail, - required TResult orElse(), - }) { - if (loadFail != null) { - return loadFail(this); - } - return orElse(); - } -} - -abstract class LoadFail implements DocState { - const factory LoadFail(WorkspaceError error) = _$LoadFail; - - WorkspaceError get error => throw _privateConstructorUsedError; - @JsonKey(ignore: true) - $LoadFailCopyWith get copyWith => - throw _privateConstructorUsedError; + _$FinishCopyWith<_Finish> get copyWith => throw _privateConstructorUsedError; } diff --git a/app_flowy/lib/workspace/application/doc/doc_edit_bloc.dart b/app_flowy/lib/workspace/application/doc/doc_edit_bloc.dart deleted file mode 100644 index 2242a322bf..0000000000 --- a/app_flowy/lib/workspace/application/doc/doc_edit_bloc.dart +++ /dev/null @@ -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 { - final IDoc iDocImpl; - - DocEditBloc(this.iDocImpl) : super(DocEditState.initial()); - - @override - Stream 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, - ); -} diff --git a/app_flowy/lib/workspace/application/doc/doc_edit_bloc.freezed.dart b/app_flowy/lib/workspace/application/doc/doc_edit_bloc.freezed.dart deleted file mode 100644 index 22eab17e1c..0000000000 --- a/app_flowy/lib/workspace/application/doc/doc_edit_bloc.freezed.dart +++ /dev/null @@ -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 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({ - required TResult Function() initial, - required TResult Function() close, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult maybeWhen({ - TResult Function()? initial, - TResult Function()? close, - required TResult orElse(), - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult map({ - required TResult Function(Initial value) initial, - required TResult Function(Close value) close, - }) => - throw _privateConstructorUsedError; - @optionalTypeArgs - TResult maybeMap({ - 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({ - required TResult Function() initial, - required TResult Function() close, - }) { - return initial(); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function()? initial, - TResult Function()? close, - required TResult orElse(), - }) { - if (initial != null) { - return initial(); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(Initial value) initial, - required TResult Function(Close value) close, - }) { - return initial(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - 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({ - required TResult Function() initial, - required TResult Function() close, - }) { - return close(); - } - - @override - @optionalTypeArgs - TResult maybeWhen({ - TResult Function()? initial, - TResult Function()? close, - required TResult orElse(), - }) { - if (close != null) { - return close(); - } - return orElse(); - } - - @override - @optionalTypeArgs - TResult map({ - required TResult Function(Initial value) initial, - required TResult Function(Close value) close, - }) { - return close(this); - } - - @override - @optionalTypeArgs - TResult maybeMap({ - 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 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; -} diff --git a/app_flowy/lib/workspace/infrastructure/deps_resolver.dart b/app_flowy/lib/workspace/infrastructure/deps_resolver.dart index 2d428db358..76c16de562 100644 --- a/app_flowy/lib/workspace/infrastructure/deps_resolver.dart +++ b/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((docId, _) => DocBloc(iDocImpl: getIt(param1: docId))); - getIt.registerFactoryParam((docId, _) => DocEditBloc(getIt(param1: docId))); + getIt.registerFactoryParam((docId, _) => DocBloc(docManager: getIt(param1: docId))); // trash getIt.registerLazySingleton(() => TrashRepo()); diff --git a/app_flowy/lib/workspace/presentation/stack_page/doc/doc_page.dart b/app_flowy/lib/workspace/presentation/stack_page/doc/doc_page.dart index d12b4ad847..1b0045999a 100644 --- a/app_flowy/lib/workspace/presentation/stack_page/doc/doc_page.dart +++ b/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; + final View view; - DocPage({Key? key, required this.doc}) - : controller = QuillController( - document: doc.document, - selection: const TextSelection.collapsed(offset: 0), - ), - editBloc = getIt(param1: doc.id), - super(key: key); + const DocPage({Key? key, required this.view}) : super(key: key); @override State createState() => _DocPageState(); } class _DocPageState extends State { + late DocBloc docBloc; final FocusNode _focusNode = FocusNode(); + @override + void initState() { + docBloc = getIt(param1: super.widget.view.id)..add(const DocEvent.initial()); + super.initState(); + } + @override Widget build(BuildContext context) { - return BlocProvider.value( - value: widget.editBloc, - child: BlocBuilder( - builder: (ctx, state) { - return Column( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - _renderEditor(widget.controller), - _renderToolbar(widget.controller), - ], - ); - }, - ), + return MultiBlocProvider( + providers: [ + BlocProvider.value(value: docBloc), + ], + child: BlocBuilder(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 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) { diff --git a/app_flowy/lib/workspace/presentation/stack_page/doc/doc_stack_page.dart b/app_flowy/lib/workspace/presentation/stack_page/doc/doc_stack_page.dart index 48bee91713..788692a3e7 100644 --- a/app_flowy/lib/workspace/presentation/stack_page/doc/doc_stack_page.dart +++ b/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 makeNavigationItems() { return [ this, - this, - this, ]; } } @@ -58,22 +50,7 @@ class DocStackPage extends StatefulWidget { class _DocStackPageState extends State { @override Widget build(BuildContext context) { - return MultiBlocProvider( - providers: [ - BlocProvider( - create: (context) => getIt(param1: widget.view.id)..add(const DocEvent.loadDoc())), - ], - child: BlocBuilder(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