menu_user_bloc.dart 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import 'package:app_flowy/workspace/domain/i_user.dart';
  2. import 'package:flowy_log/flowy_log.dart';
  3. import 'package:flowy_sdk/protobuf/flowy-user/user_profile.pb.dart';
  4. import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart';
  5. import 'package:flowy_sdk/protobuf/flowy-workspace/workspace_create.pb.dart';
  6. import 'package:flutter_bloc/flutter_bloc.dart';
  7. import 'package:freezed_annotation/freezed_annotation.dart';
  8. import 'package:dartz/dartz.dart';
  9. part 'menu_user_bloc.freezed.dart';
  10. class MenuUserBloc extends Bloc<MenuUserEvent, MenuUserState> {
  11. final IUser iUserImpl;
  12. final IUserWatch watch;
  13. MenuUserBloc(this.iUserImpl, this.watch)
  14. : super(MenuUserState.initial(iUserImpl.user));
  15. @override
  16. Stream<MenuUserState> mapEventToState(MenuUserEvent event) async* {
  17. yield* event.map(
  18. initial: (_) async* {
  19. watch.setProfileCallback(_profileUpdated);
  20. watch.setWorkspacesCallback(_workspacesUpdated);
  21. watch.startWatching();
  22. await _initUser();
  23. },
  24. fetchWorkspaces: (_FetchWorkspaces value) async* {},
  25. );
  26. }
  27. @override
  28. Future<void> close() async {
  29. await watch.stopWatching();
  30. super.close();
  31. }
  32. Future<void> _initUser() async {
  33. final result = await iUserImpl.initUser();
  34. result.fold((l) => null, (error) => Log.error(error));
  35. }
  36. void _profileUpdated(Either<UserProfile, UserError> userOrFailed) {}
  37. void _workspacesUpdated(
  38. Either<List<Workspace>, WorkspaceError> workspacesOrFailed) {
  39. // fetch workspaces
  40. // iUserImpl.fetchWorkspaces().then((result) {
  41. // result.fold(
  42. // (workspaces) async* {
  43. // yield state.copyWith(workspaces: some(workspaces));
  44. // },
  45. // (error) async* {
  46. // yield state.copyWith(successOrFailure: right(error.msg));
  47. // },
  48. // );
  49. // });
  50. }
  51. }
  52. @freezed
  53. class MenuUserEvent with _$MenuUserEvent {
  54. const factory MenuUserEvent.initial() = _Initial;
  55. const factory MenuUserEvent.fetchWorkspaces() = _FetchWorkspaces;
  56. }
  57. @freezed
  58. class MenuUserState with _$MenuUserState {
  59. const factory MenuUserState({
  60. required UserProfile user,
  61. required Option<List<Workspace>> workspaces,
  62. required Either<Unit, String> successOrFailure,
  63. }) = _MenuUserState;
  64. factory MenuUserState.initial(UserProfile user) => MenuUserState(
  65. user: user,
  66. workspaces: none(),
  67. successOrFailure: left(unit),
  68. );
  69. }