From b354fb79c87ba7ed4dd0fa8eaabf0b76809c7515 Mon Sep 17 00:00:00 2001 From: appflowy Date: Tue, 4 Jan 2022 22:44:03 +0800 Subject: [PATCH] migrate to flutter_bloc 8.0 --- .../lib/startup/tasks/application_widget.dart | 19 ++-- .../lib/user/application/sign_in_bloc.dart | 44 ++++----- .../lib/user/application/sign_up_bloc.dart | 51 +++++----- .../lib/user/application/splash_bloc.dart | 19 ++-- .../user/infrastructure/i_splash_impl.dart | 4 +- .../workspace/application/app/app_bloc.dart | 89 ++++++++--------- .../workspace/application/doc/doc_bloc.dart | 60 ++++++------ .../edit_pannel/edit_pannel_bloc.dart | 25 +++-- .../workspace/application/home/home_bloc.dart | 40 ++++---- .../application/home/home_listen_bloc.dart | 31 +++--- .../workspace/application/menu/menu_bloc.dart | 69 ++++++------- .../application/menu/menu_user_bloc.dart | 26 +++-- .../application/trash/trash_bloc.dart | 48 ++++----- .../workspace/application/view/view_bloc.dart | 97 ++++++++++--------- .../application/workspace/welcome_bloc.dart | 59 ++++++----- frontend/app_flowy/pubspec.lock | 4 +- frontend/app_flowy/pubspec.yaml | 2 +- 17 files changed, 328 insertions(+), 359 deletions(-) diff --git a/frontend/app_flowy/lib/startup/tasks/application_widget.dart b/frontend/app_flowy/lib/startup/tasks/application_widget.dart index e9541ca068..4caa86844b 100644 --- a/frontend/app_flowy/lib/startup/tasks/application_widget.dart +++ b/frontend/app_flowy/lib/startup/tasks/application_widget.dart @@ -18,14 +18,17 @@ class AppWidgetTask extends LaunchTask { Future initialize(LaunchContext context) { final widget = context.getIt().create(); final app = ApplicationWidget(child: widget); - Bloc.observer = ApplicationBlocObserver(); - - runApp( - EasyLocalization( - supportedLocales: const [Locale('en'), Locale('zh_CN'), Locale('it_IT')], - path: 'assets/translations', - fallbackLocale: const Locale('en'), - child: app), + BlocOverrides.runZoned( + () { + runApp( + EasyLocalization( + supportedLocales: const [Locale('en'), Locale('zh_CN'), Locale('it_IT')], + path: 'assets/translations', + fallbackLocale: const Locale('en'), + child: app), + ); + }, + blocObserver: ApplicationBlocObserver(), ); return Future(() => {}); diff --git a/frontend/app_flowy/lib/user/application/sign_in_bloc.dart b/frontend/app_flowy/lib/user/application/sign_in_bloc.dart index 0b8ded99be..e384a64a60 100644 --- a/frontend/app_flowy/lib/user/application/sign_in_bloc.dart +++ b/frontend/app_flowy/lib/user/application/sign_in_bloc.dart @@ -9,35 +9,33 @@ part 'sign_in_bloc.freezed.dart'; class SignInBloc extends Bloc { final IAuth authManager; - SignInBloc(this.authManager) : super(SignInState.initial()); - - @override - Stream mapEventToState( - SignInEvent event, - ) async* { - yield* event.map( - signedInWithUserEmailAndPassword: (e) async* { - yield* _performActionOnSignIn( - state, - ); - }, - emailChanged: (EmailChanged value) async* { - yield state.copyWith(email: value.email, emailError: none(), successOrFail: none()); - }, - passwordChanged: (PasswordChanged value) async* { - yield state.copyWith(password: value.password, passwordError: none(), successOrFail: none()); - }, - ); + SignInBloc(this.authManager) : super(SignInState.initial()) { + on((event, emit) async { + await event.map( + signedInWithUserEmailAndPassword: (e) async { + await _performActionOnSignIn( + state, + emit, + ); + }, + emailChanged: (EmailChanged value) async { + emit(state.copyWith(email: value.email, emailError: none(), successOrFail: none())); + }, + passwordChanged: (PasswordChanged value) async { + emit(state.copyWith(password: value.password, passwordError: none(), successOrFail: none())); + }, + ); + }); } - Stream _performActionOnSignIn(SignInState state) async* { - yield state.copyWith(isSubmitting: true, emailError: none(), passwordError: none(), successOrFail: none()); + Future _performActionOnSignIn(SignInState state, Emitter emit) async { + emit(state.copyWith(isSubmitting: true, emailError: none(), passwordError: none(), successOrFail: none())); final result = await authManager.signIn(state.email, state.password); - yield result.fold( + emit(result.fold( (userProfile) => state.copyWith(isSubmitting: false, successOrFail: some(left(userProfile))), (error) => stateFromCode(error), - ); + )); } SignInState stateFromCode(FlowyError error) { diff --git a/frontend/app_flowy/lib/user/application/sign_up_bloc.dart b/frontend/app_flowy/lib/user/application/sign_up_bloc.dart index 56e1390f4b..dd8e599017 100644 --- a/frontend/app_flowy/lib/user/application/sign_up_bloc.dart +++ b/frontend/app_flowy/lib/user/application/sign_up_bloc.dart @@ -11,62 +11,59 @@ part 'sign_up_bloc.freezed.dart'; class SignUpBloc extends Bloc { final IAuth authManager; - SignUpBloc(this.authManager) : super(SignUpState.initial()); - - @override - Stream mapEventToState( - SignUpEvent event, - ) async* { - yield* event.map(signUpWithUserEmailAndPassword: (e) async* { - yield* _performActionOnSignUp(); - }, emailChanged: (EmailChanged value) async* { - yield state.copyWith(email: value.email, emailError: none(), successOrFail: none()); - }, passwordChanged: (PasswordChanged value) async* { - yield state.copyWith(password: value.password, passwordError: none(), successOrFail: none()); - }, repeatPasswordChanged: (RepeatPasswordChanged value) async* { - yield state.copyWith(repeatedPassword: value.password, repeatPasswordError: none(), successOrFail: none()); + SignUpBloc(this.authManager) : super(SignUpState.initial()) { + on((event, emit) async { + await event.map(signUpWithUserEmailAndPassword: (e) async { + await _performActionOnSignUp(emit); + }, emailChanged: (EmailChanged value) async { + emit(state.copyWith(email: value.email, emailError: none(), successOrFail: none())); + }, passwordChanged: (PasswordChanged value) async { + emit(state.copyWith(password: value.password, passwordError: none(), successOrFail: none())); + }, repeatPasswordChanged: (RepeatPasswordChanged value) async { + emit(state.copyWith(repeatedPassword: value.password, repeatPasswordError: none(), successOrFail: none())); + }); }); } - Stream _performActionOnSignUp() async* { - yield state.copyWith( + Future _performActionOnSignUp(Emitter emit) async { + emit(state.copyWith( isSubmitting: true, successOrFail: none(), - ); + )); final password = state.password; final repeatedPassword = state.repeatedPassword; if (password == null) { - yield state.copyWith( + emit(state.copyWith( isSubmitting: false, passwordError: some(LocaleKeys.signUp_emptyPasswordError.tr()), - ); + )); return; } if (repeatedPassword == null) { - yield state.copyWith( + emit(state.copyWith( isSubmitting: false, repeatPasswordError: some(LocaleKeys.signUp_repeatPasswordEmptyError.tr()), - ); + )); return; } if (password != repeatedPassword) { - yield state.copyWith( + emit(state.copyWith( isSubmitting: false, repeatPasswordError: some(LocaleKeys.signUp_unmatchedPasswordError.tr()), - ); + )); return; } - yield state.copyWith( + emit(state.copyWith( passwordError: none(), repeatPasswordError: none(), - ); + )); final result = await authManager.signUp(state.email, state.password, state.email); - yield result.fold( + emit(result.fold( (profile) => state.copyWith( isSubmitting: false, successOrFail: some(left(profile)), @@ -75,7 +72,7 @@ class SignUpBloc extends Bloc { repeatPasswordError: none(), ), (error) => stateFromCode(error), - ); + )); } SignUpState stateFromCode(FlowyError error) { diff --git a/frontend/app_flowy/lib/user/application/splash_bloc.dart b/frontend/app_flowy/lib/user/application/splash_bloc.dart index e93d3a7435..00dd66a635 100644 --- a/frontend/app_flowy/lib/user/application/splash_bloc.dart +++ b/frontend/app_flowy/lib/user/application/splash_bloc.dart @@ -7,16 +7,15 @@ part 'splash_bloc.freezed.dart'; class SplashBloc extends Bloc { final ISplashUser authImpl; - SplashBloc(this.authImpl) : super(SplashState.initial()); - - @override - Stream mapEventToState(SplashEvent event) async* { - yield* event.map( - getUser: (val) async* { - final authState = await authImpl.currentUserProfile(); - yield state.copyWith(auth: authState); - }, - ); + SplashBloc(this.authImpl) : super(SplashState.initial()) { + on((event, emit) async { + await event.map( + getUser: (val) async { + final authState = await authImpl.currentUserProfile(); + emit(state.copyWith(auth: authState)); + }, + ); + }); } } diff --git a/frontend/app_flowy/lib/user/infrastructure/i_splash_impl.dart b/frontend/app_flowy/lib/user/infrastructure/i_splash_impl.dart index b6ac8494c3..fe74c4bd8b 100644 --- a/frontend/app_flowy/lib/user/infrastructure/i_splash_impl.dart +++ b/frontend/app_flowy/lib/user/infrastructure/i_splash_impl.dart @@ -24,8 +24,8 @@ class SplashUserImpl implements ISplashUser { (userProfile) { return AuthState.authenticated(userProfile); }, - (FlowyError) { - return AuthState.unauthenticated(FlowyError); + (error) { + return AuthState.unauthenticated(error); }, ); }); diff --git a/frontend/app_flowy/lib/workspace/application/app/app_bloc.dart b/frontend/app_flowy/lib/workspace/application/app/app_bloc.dart index 9eef178422..58df458cd9 100644 --- a/frontend/app_flowy/lib/workspace/application/app/app_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/app/app_bloc.dart @@ -12,46 +12,43 @@ part 'app_bloc.freezed.dart'; class AppBloc extends Bloc { final IApp appManager; final IAppListenr listener; - AppBloc({required App app, required this.appManager, required this.listener}) : super(AppState.initial(app)); - - @override - Stream mapEventToState( - AppEvent event, - ) async* { - yield* event.map(initial: (e) async* { - listener.start( - viewsChangeCallback: _handleViewsChanged, - updatedCallback: (app) => add(AppEvent.appDidUpdate(app)), - ); - yield* _fetchViews(); - }, createView: (CreateView value) async* { - final viewOrFailed = await appManager.createView(name: value.name, desc: value.desc, viewType: value.viewType); - yield viewOrFailed.fold( - (view) => state.copyWith( - latestCreatedView: view, - successOrFailure: left(unit), - ), - (error) { - Log.error(error); - return state.copyWith(successOrFailure: right(error)); - }, - ); - }, didReceiveViews: (e) async* { - yield* handleDidReceiveViews(e.views); - }, delete: (e) async* { - final result = await appManager.delete(); - yield result.fold( - (unit) => state.copyWith(successOrFailure: left(unit)), - (error) => state.copyWith(successOrFailure: right(error)), - ); - }, rename: (e) async* { - final result = await appManager.rename(e.newName); - yield result.fold( - (l) => state.copyWith(successOrFailure: left(unit)), - (error) => state.copyWith(successOrFailure: right(error)), - ); - }, appDidUpdate: (e) async* { - yield state.copyWith(app: e.app); + AppBloc({required App app, required this.appManager, required this.listener}) : super(AppState.initial(app)) { + on((event, emit) async { + await event.map(initial: (e) async { + listener.start( + viewsChangeCallback: _handleViewsChanged, + updatedCallback: (app) => add(AppEvent.appDidUpdate(app)), + ); + await _fetchViews(emit); + }, createView: (CreateView value) async { + final viewOrFailed = await appManager.createView(name: value.name, desc: value.desc, viewType: value.viewType); + viewOrFailed.fold( + (view) => emit(state.copyWith( + latestCreatedView: view, + successOrFailure: left(unit), + )), + (error) { + Log.error(error); + emit(state.copyWith(successOrFailure: right(error))); + }, + ); + }, didReceiveViews: (e) async { + await handleDidReceiveViews(e.views, emit); + }, delete: (e) async { + final result = await appManager.delete(); + result.fold( + (unit) => emit(state.copyWith(successOrFailure: left(unit))), + (error) => emit(state.copyWith(successOrFailure: right(error))), + ); + }, rename: (e) async { + final result = await appManager.rename(e.newName); + result.fold( + (l) => emit(state.copyWith(successOrFailure: left(unit))), + (error) => emit(state.copyWith(successOrFailure: right(error))), + ); + }, appDidUpdate: (e) async { + emit(state.copyWith(app: e.app)); + }); }); } @@ -70,7 +67,7 @@ class AppBloc extends Bloc { ); } - Stream handleDidReceiveViews(List views) async* { + Future handleDidReceiveViews(List views, Emitter emit) async { final latestCreatedView = state.latestCreatedView; AppState newState = state.copyWith(views: views); if (latestCreatedView != null) { @@ -80,16 +77,16 @@ class AppBloc extends Bloc { } } - yield newState; + emit(newState); } - Stream _fetchViews() async* { + Future _fetchViews(Emitter emit) async { final viewsOrFailed = await appManager.getViews(); - yield viewsOrFailed.fold( - (apps) => state.copyWith(views: apps), + viewsOrFailed.fold( + (apps) => emit(state.copyWith(views: apps)), (error) { Log.error(error); - return state.copyWith(successOrFailure: right(error)); + emit(state.copyWith(successOrFailure: right(error))); }, ); } diff --git a/frontend/app_flowy/lib/workspace/application/doc/doc_bloc.dart b/frontend/app_flowy/lib/workspace/application/doc/doc_bloc.dart index d97ba433ce..9dc22118ad 100644 --- a/frontend/app_flowy/lib/workspace/application/doc/doc_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/doc/doc_bloc.dart @@ -27,31 +27,30 @@ class DocBloc extends Bloc { required this.docManager, required this.listener, required this.trasnManager, - }) : super(DocState.initial()); - - @override - Stream mapEventToState(DocEvent event) async* { - yield* event.map( - initial: _initial, - deleted: (Deleted value) async* { - yield state.copyWith(isDeleted: true); - }, - restore: (Restore value) async* { - yield state.copyWith(isDeleted: false); - }, - deletePermanently: (DeletePermanently value) async* { - final result = await trasnManager.deleteViews([Tuple2(view.id, TrashType.View)]); - yield result.fold((l) => state.copyWith(forceClose: true), (r) { - return state; - }); - }, - restorePage: (RestorePage value) async* { - final result = await trasnManager.putback(view.id); - yield result.fold((l) => state.copyWith(isDeleted: false), (r) { - return state; - }); - }, - ); + }) : super(DocState.initial()) { + on((event, emit) async { + await event.map( + initial: (Initial value) async { + await _initial(value, emit); + }, + 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 trasnManager.deleteViews([Tuple2(view.id, TrashType.View)]); + final newState = result.fold((l) => state.copyWith(forceClose: true), (r) => state); + emit(newState); + }, + restorePage: (RestorePage value) async { + final result = await trasnManager.putback(view.id); + final newState = result.fold((l) => state.copyWith(isDeleted: false), (r) => state); + emit(newState); + }, + ); + }); } @override @@ -62,11 +61,11 @@ class DocBloc extends Bloc { await _subscription?.cancel(); } - // docManager.closeDoc(); + docManager.closeDoc(); return super.close(); } - Stream _initial(Initial value) async* { + Future _initial(Initial value, Emitter emit) async { listener.deletedNotifier.addPublishListener((result) { result.fold( (view) => add(const DocEvent.deleted()), @@ -82,9 +81,8 @@ class DocBloc extends Bloc { }); listener.start(); - final result = await docManager.readDoc(); - yield result.fold( + result.fold( (doc) { document = _decodeJsonToDocument(doc.deltaJson); _subscription = document.changes.listen((event) { @@ -92,10 +90,10 @@ class DocBloc extends Bloc { final documentDelta = document.toDelta(); _composeDelta(delta, documentDelta); }); - return state.copyWith(loadState: DocLoadState.finish(left(unit))); + emit(state.copyWith(loadState: DocLoadState.finish(left(unit)))); }, (err) { - return state.copyWith(loadState: DocLoadState.finish(right(err))); + emit(state.copyWith(loadState: DocLoadState.finish(right(err)))); }, ); } diff --git a/frontend/app_flowy/lib/workspace/application/edit_pannel/edit_pannel_bloc.dart b/frontend/app_flowy/lib/workspace/application/edit_pannel/edit_pannel_bloc.dart index ca4f6652b4..e8bc319e3b 100644 --- a/frontend/app_flowy/lib/workspace/application/edit_pannel/edit_pannel_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/edit_pannel/edit_pannel_bloc.dart @@ -7,20 +7,17 @@ import 'package:flutter_bloc/flutter_bloc.dart'; part 'edit_pannel_bloc.freezed.dart'; class EditPannelBloc extends Bloc { - EditPannelBloc() : super(EditPannelState.initial()); - - @override - Stream mapEventToState( - EditPannelEvent event, - ) async* { - yield* event.map( - startEdit: (e) async* { - yield state.copyWith(isEditing: true, editContext: some(e.context)); - }, - endEdit: (value) async* { - yield state.copyWith(isEditing: false, editContext: none()); - }, - ); + EditPannelBloc() : super(EditPannelState.initial()) { + on((event, emit) async { + await event.map( + startEdit: (e) async { + emit(state.copyWith(isEditing: true, editContext: some(e.context))); + }, + endEdit: (value) async { + emit(state.copyWith(isEditing: false, editContext: none())); + }, + ); + }); } } diff --git a/frontend/app_flowy/lib/workspace/application/home/home_bloc.dart b/frontend/app_flowy/lib/workspace/application/home/home_bloc.dart index fef6b176a0..385735ed44 100644 --- a/frontend/app_flowy/lib/workspace/application/home/home_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/home/home_bloc.dart @@ -5,26 +5,23 @@ import 'package:dartz/dartz.dart'; part 'home_bloc.freezed.dart'; class HomeBloc extends Bloc { - HomeBloc() : super(HomeState.initial()); - - @override - Stream mapEventToState( - HomeEvent event, - ) async* { - yield* event.map( - showLoading: (e) async* { - yield state.copyWith(isLoading: e.isLoading); - }, - setEditPannel: (e) async* { - yield state.copyWith(editContext: some(e.editContext)); - }, - dismissEditPannel: (value) async* { - yield state.copyWith(editContext: none()); - }, - forceCollapse: (e) async* { - yield state.copyWith(forceCollapse: e.forceCollapse); - }, - ); + HomeBloc() : super(HomeState.initial()) { + on((event, emit) async { + await event.map( + showLoading: (e) async { + emit(state.copyWith(isLoading: e.isLoading)); + }, + setEditPannel: (e) async { + emit(state.copyWith(editContext: some(e.editContext))); + }, + dismissEditPannel: (value) async { + emit(state.copyWith(editContext: none())); + }, + forceCollapse: (e) async { + emit(state.copyWith(forceCollapse: e.forceCollapse)); + }, + ); + }); } @override @@ -37,8 +34,7 @@ class HomeBloc extends Bloc { class HomeEvent with _$HomeEvent { const factory HomeEvent.showLoading(bool isLoading) = _ShowLoading; const factory HomeEvent.forceCollapse(bool forceCollapse) = _ForceCollapse; - const factory HomeEvent.setEditPannel(EditPannelContext editContext) = - _ShowEditPannel; + const factory HomeEvent.setEditPannel(EditPannelContext editContext) = _ShowEditPannel; const factory HomeEvent.dismissEditPannel() = _DismissEditPannel; } diff --git a/frontend/app_flowy/lib/workspace/application/home/home_listen_bloc.dart b/frontend/app_flowy/lib/workspace/application/home/home_listen_bloc.dart index 22bed11841..6a1610d452 100644 --- a/frontend/app_flowy/lib/workspace/application/home/home_listen_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/home/home_listen_bloc.dart @@ -8,22 +8,21 @@ part 'home_listen_bloc.freezed.dart'; class HomeListenBloc extends Bloc { final IUserListener listener; - HomeListenBloc(this.listener) : super(const HomeListenState.loading()); - - @override - Stream mapEventToState( - HomeListenEvent event, - ) async* { - yield* event.map( - started: (_) async* { - listener.authDidChangedNotifier.addPublishListener(_authDidChanged); - listener.start(); - }, - stop: (_) async* {}, - unauthorized: (e) async* { - yield HomeListenState.unauthorized(e.msg); - }, - ); + HomeListenBloc(this.listener) : super(const HomeListenState.loading()) { + on((event, emit) async { + await event.map( + started: (_) async { + listener.authDidChangedNotifier.addPublishListener((result) { + _authDidChanged(result); + }); + listener.start(); + }, + stop: (_) async {}, + unauthorized: (e) async { + emit(HomeListenState.unauthorized(e.msg)); + }, + ); + }); } @override diff --git a/frontend/app_flowy/lib/workspace/application/menu/menu_bloc.dart b/frontend/app_flowy/lib/workspace/application/menu/menu_bloc.dart index 538fc32414..9f6df6f71d 100644 --- a/frontend/app_flowy/lib/workspace/application/menu/menu_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/menu/menu_bloc.dart @@ -14,34 +14,31 @@ part 'menu_bloc.freezed.dart'; class MenuBloc extends Bloc { final IWorkspace workspaceManager; final IWorkspaceListener listener; - MenuBloc({required this.workspaceManager, required this.listener}) : super(MenuState.initial()); - - @override - Stream mapEventToState( - MenuEvent event, - ) async* { - yield* event.map( - initial: (e) async* { - listener.start(addAppCallback: _handleAppsOrFail); - yield* _fetchApps(); - }, - collapse: (e) async* { - final isCollapse = state.isCollapse; - yield state.copyWith(isCollapse: !isCollapse); - }, - openPage: (e) async* { - yield* _performActionOnOpenPage(e); - }, - createApp: (CreateApp event) async* { - yield* _performActionOnCreateApp(event); - }, - didReceiveApps: (e) async* { - yield e.appsOrFail.fold( - (apps) => state.copyWith(apps: some(apps), successOrFailure: left(unit)), - (err) => state.copyWith(successOrFailure: right(err)), - ); - }, - ); + MenuBloc({required this.workspaceManager, required this.listener}) : super(MenuState.initial()) { + on((event, emit) async { + await event.map( + initial: (e) async { + listener.start(addAppCallback: _handleAppsOrFail); + await _fetchApps(emit); + }, + collapse: (e) async { + final isCollapse = state.isCollapse; + emit(state.copyWith(isCollapse: !isCollapse)); + }, + openPage: (e) async { + emit(state.copyWith(stackContext: e.context)); + }, + createApp: (CreateApp event) async { + await _performActionOnCreateApp(event, emit); + }, + didReceiveApps: (e) async { + emit(e.appsOrFail.fold( + (apps) => state.copyWith(apps: some(apps), successOrFailure: left(unit)), + (err) => state.copyWith(successOrFailure: right(err)), + )); + }, + ); + }); } @override @@ -50,31 +47,27 @@ class MenuBloc extends Bloc { return super.close(); } - Stream _performActionOnOpenPage(OpenPage e) async* { - yield state.copyWith(stackContext: e.context); - } - - Stream _performActionOnCreateApp(CreateApp event) async* { + Future _performActionOnCreateApp(CreateApp event, Emitter emit) async { final result = await workspaceManager.createApp(name: event.name, desc: event.desc); - yield result.fold( + emit(result.fold( (app) => state.copyWith(apps: some([app])), (error) { Log.error(error); return state.copyWith(successOrFailure: right(error)); }, - ); + )); } // ignore: unused_element - Stream _fetchApps() async* { + Future _fetchApps(Emitter emit) async { final appsOrFail = await workspaceManager.getApps(); - yield appsOrFail.fold( + emit(appsOrFail.fold( (apps) => state.copyWith(apps: some(apps)), (error) { Log.error(error); return state.copyWith(successOrFailure: right(error)); }, - ); + )); } void _handleAppsOrFail(Either, FlowyError> appsOrFail) { diff --git a/frontend/app_flowy/lib/workspace/application/menu/menu_user_bloc.dart b/frontend/app_flowy/lib/workspace/application/menu/menu_user_bloc.dart index 86623d40ae..d96b8ff4be 100644 --- a/frontend/app_flowy/lib/workspace/application/menu/menu_user_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/menu/menu_user_bloc.dart @@ -12,20 +12,18 @@ class MenuUserBloc extends Bloc { final IUser userManager; final IUserListener listener; - MenuUserBloc(this.userManager, this.listener) : super(MenuUserState.initial(userManager.user)); - - @override - Stream mapEventToState(MenuUserEvent event) async* { - yield* event.map( - initial: (_) async* { - listener.profileUpdatedNotifier.addPublishListener(_profileUpdated); - listener.workspaceUpdatedNotifier.addPublishListener(_workspacesUpdated); - listener.start(); - - await _initUser(); - }, - fetchWorkspaces: (_FetchWorkspaces value) async* {}, - ); + MenuUserBloc(this.userManager, this.listener) : super(MenuUserState.initial(userManager.user)) { + on((event, emit) async { + await event.map( + initial: (_) async { + listener.profileUpdatedNotifier.addPublishListener(_profileUpdated); + listener.workspaceUpdatedNotifier.addPublishListener(_workspacesUpdated); + listener.start(); + await _initUser(); + }, + fetchWorkspaces: (_FetchWorkspaces value) async {}, + ); + }); } @override diff --git a/frontend/app_flowy/lib/workspace/application/trash/trash_bloc.dart b/frontend/app_flowy/lib/workspace/application/trash/trash_bloc.dart index b4ecd9c6c9..e2d5e337ea 100644 --- a/frontend/app_flowy/lib/workspace/application/trash/trash_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/trash/trash_bloc.dart @@ -10,46 +10,38 @@ part 'trash_bloc.freezed.dart'; class TrashBloc extends Bloc { final ITrash trasnManager; final ITrashListener listener; - TrashBloc({required this.trasnManager, required this.listener}) : super(TrashState.init()); - - @override - Stream mapEventToState(TrashEvent event) async* { - yield* event.map( - initial: (e) async* { + TrashBloc({required this.trasnManager, required this.listener}) : super(TrashState.init()) { + on((event, emit) async { + await event.map(initial: (e) async { listener.start(_listenTrashUpdated); final result = await trasnManager.readTrash(); - yield result.fold( + emit(result.fold( (objects) => state.copyWith(objects: objects, successOrFailure: left(unit)), (error) => state.copyWith(successOrFailure: right(error)), - ); - }, - didReceiveTrash: (e) async* { - yield state.copyWith(objects: e.trash); - }, - putback: (e) async* { + )); + }, didReceiveTrash: (e) async { + emit(state.copyWith(objects: e.trash)); + }, putback: (e) async { final result = await trasnManager.putback(e.trashId); - yield* _handleResult(result); - }, - delete: (e) async* { + await _handleResult(result, emit); + }, delete: (e) async { final result = await trasnManager.deleteViews([Tuple2(e.trash.id, e.trash.ty)]); - yield* _handleResult(result); - }, - deleteAll: (e) async* { + await _handleResult(result, emit); + }, deleteAll: (e) async { final result = await trasnManager.deleteAll(); - yield* _handleResult(result); - }, - restoreAll: (e) async* { + await _handleResult(result, emit); + }, restoreAll: (e) async { final result = await trasnManager.restoreAll(); - yield* _handleResult(result); - }, - ); + await _handleResult(result, emit); + }); + }); } - Stream _handleResult(Either result) async* { - yield result.fold( + Future _handleResult(Either result, Emitter emit) async { + emit(result.fold( (l) => state.copyWith(successOrFailure: left(unit)), (error) => state.copyWith(successOrFailure: right(error)), - ); + )); } void _listenTrashUpdated(Either, FlowyError> trashOrFailed) { diff --git a/frontend/app_flowy/lib/workspace/application/view/view_bloc.dart b/frontend/app_flowy/lib/workspace/application/view/view_bloc.dart index 4e453692fc..eade3522d5 100644 --- a/frontend/app_flowy/lib/workspace/application/view/view_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/view/view_bloc.dart @@ -14,53 +14,56 @@ class ViewBloc extends Bloc { ViewBloc({ required this.viewManager, required this.listener, - }) : super(ViewState.init(viewManager.view)); - - @override - Stream mapEventToState(ViewEvent event) async* { - yield* event.map( - initial: (e) async* { - listener.updatedNotifier.addPublishListener((result) { - add(ViewEvent.viewDidUpdate(result)); - }); - listener.start(); - yield state; - }, - setIsEditing: (e) async* { - yield state.copyWith(isEditing: e.isEditing); - }, - viewDidUpdate: (e) async* { - yield* _handleViewDidUpdate(e.result); - }, - rename: (e) async* { - final result = await viewManager.rename(e.newName); - yield result.fold( - (l) => state.copyWith(successOrFailure: left(unit)), - (error) => state.copyWith(successOrFailure: right(error)), - ); - }, - delete: (e) async* { - final result = await viewManager.delete(); - yield result.fold( - (l) => state.copyWith(successOrFailure: left(unit)), - (error) => state.copyWith(successOrFailure: right(error)), - ); - }, - duplicate: (e) async* { - final result = await viewManager.duplicate(); - yield result.fold( - (l) => state.copyWith(successOrFailure: left(unit)), - (error) => state.copyWith(successOrFailure: right(error)), - ); - }, - ); - } - - Stream _handleViewDidUpdate(Either result) async* { - yield result.fold( - (view) => state.copyWith(view: view, successOrFailure: left(unit)), - (error) => state.copyWith(successOrFailure: right(error)), - ); + }) : super(ViewState.init(viewManager.view)) { + on((event, emit) async { + await event.map( + initial: (e) { + // TODO: Listener can be refctored to a stream. + listener.updatedNotifier.addPublishListener((result) { + // emit.forEach(stream, onData: onData) + add(ViewEvent.viewDidUpdate(result)); + }); + listener.start(); + emit(state); + }, + setIsEditing: (e) { + emit(state.copyWith(isEditing: e.isEditing)); + }, + viewDidUpdate: (e) { + e.result.fold( + (view) => emit(state.copyWith(view: view, successOrFailure: left(unit))), + (error) => emit(state.copyWith(successOrFailure: right(error))), + ); + }, + rename: (e) async { + final result = await viewManager.rename(e.newName); + emit( + result.fold( + (l) => state.copyWith(successOrFailure: left(unit)), + (error) => state.copyWith(successOrFailure: right(error)), + ), + ); + }, + delete: (e) async { + final result = await viewManager.delete(); + emit( + result.fold( + (l) => state.copyWith(successOrFailure: left(unit)), + (error) => state.copyWith(successOrFailure: right(error)), + ), + ); + }, + duplicate: (e) async { + final result = await viewManager.duplicate(); + emit( + result.fold( + (l) => state.copyWith(successOrFailure: left(unit)), + (error) => state.copyWith(successOrFailure: right(error)), + ), + ); + }, + ); + }); } @override diff --git a/frontend/app_flowy/lib/workspace/application/workspace/welcome_bloc.dart b/frontend/app_flowy/lib/workspace/application/workspace/welcome_bloc.dart index d7c9b17ee6..db4ed8c4e5 100644 --- a/frontend/app_flowy/lib/workspace/application/workspace/welcome_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/workspace/welcome_bloc.dart @@ -12,27 +12,26 @@ part 'welcome_bloc.freezed.dart'; class WelcomeBloc extends Bloc { final UserRepo repo; final IUserListener listener; - WelcomeBloc({required this.repo, required this.listener}) : super(WelcomeState.initial()); - - @override - Stream mapEventToState( - WelcomeEvent event, - ) async* { - yield* event.map(initial: (e) async* { - listener.workspaceUpdatedNotifier.addPublishListener(_workspacesUpdated); - listener.start(); - // - yield* _fetchWorkspaces(); - }, openWorkspace: (e) async* { - yield* _openWorkspace(e.workspace); - }, createWorkspace: (e) async* { - yield* _createWorkspace(e.name, e.desc); - }, workspacesReveived: (e) async* { - yield e.workspacesOrFail.fold( - (workspaces) => state.copyWith(workspaces: workspaces, successOrFailure: left(unit)), - (error) => state.copyWith(successOrFailure: right(error)), - ); - }); + WelcomeBloc({required this.repo, required this.listener}) : super(WelcomeState.initial()) { + on( + (event, emit) async { + await event.map(initial: (e) async { + listener.workspaceUpdatedNotifier.addPublishListener(_workspacesUpdated); + listener.start(); + // + await _fetchWorkspaces(emit); + }, openWorkspace: (e) async { + await _openWorkspace(e.workspace, emit); + }, createWorkspace: (e) async { + await _createWorkspace(e.name, e.desc, emit); + }, workspacesReveived: (e) async { + emit(e.workspacesOrFail.fold( + (workspaces) => state.copyWith(workspaces: workspaces, successOrFailure: left(unit)), + (error) => state.copyWith(successOrFailure: right(error)), + )); + }); + }, + ); } @override @@ -41,31 +40,31 @@ class WelcomeBloc extends Bloc { super.close(); } - Stream _fetchWorkspaces() async* { + Future _fetchWorkspaces(Emitter emit) async { final workspacesOrFailed = await repo.getWorkspaces(); - yield workspacesOrFailed.fold( + emit(workspacesOrFailed.fold( (workspaces) => state.copyWith(workspaces: workspaces, successOrFailure: left(unit)), (error) { Log.error(error); return state.copyWith(successOrFailure: right(error)); }, - ); + )); } - Stream _openWorkspace(Workspace workspace) async* { + Future _openWorkspace(Workspace workspace, Emitter emit) async { final result = await repo.openWorkspace(workspace.id); - yield result.fold( + emit(result.fold( (workspaces) => state.copyWith(successOrFailure: left(unit)), (error) { Log.error(error); return state.copyWith(successOrFailure: right(error)); }, - ); + )); } - Stream _createWorkspace(String name, String desc) async* { + Future _createWorkspace(String name, String desc, Emitter emit) async { final result = await repo.createWorkspace(name, desc); - yield result.fold( + emit(result.fold( (workspace) { return state.copyWith(successOrFailure: left(unit)); }, @@ -73,7 +72,7 @@ class WelcomeBloc extends Bloc { Log.error(error); return state.copyWith(successOrFailure: right(error)); }, - ); + )); } void _workspacesUpdated(Either, FlowyError> workspacesOrFail) { diff --git a/frontend/app_flowy/pubspec.lock b/frontend/app_flowy/pubspec.lock index 648f626c44..074eadceb4 100644 --- a/frontend/app_flowy/pubspec.lock +++ b/frontend/app_flowy/pubspec.lock @@ -42,7 +42,7 @@ packages: name: bloc url: "https://pub.dartlang.org" source: hosted - version: "7.2.1" + version: "8.0.2" boolean_selector: dependency: transitive description: @@ -418,7 +418,7 @@ packages: name: flutter_bloc url: "https://pub.dartlang.org" source: hosted - version: "7.3.3" + version: "8.0.0" flutter_colorpicker: dependency: "direct main" description: diff --git a/frontend/app_flowy/pubspec.yaml b/frontend/app_flowy/pubspec.yaml index 123db7902c..b4feab5cbd 100644 --- a/frontend/app_flowy/pubspec.yaml +++ b/frontend/app_flowy/pubspec.yaml @@ -50,7 +50,7 @@ dependencies: equatable: '^2.0.3' freezed_annotation: get_it: '^7.1.3' - flutter_bloc: '^7.3.1' + flutter_bloc: '8.0.0' provider: ^6.0.1 path_provider: ^2.0.1 window_size: