menu_user_bloc.dart 2.2 KB

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