app_bloc.dart 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import 'package:app_flowy/home/domain/i_app.dart';
  2. import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart';
  3. import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pb.dart';
  4. import 'package:freezed_annotation/freezed_annotation.dart';
  5. import 'package:flutter_bloc/flutter_bloc.dart';
  6. import 'package:dartz/dartz.dart';
  7. part 'app_bloc.freezed.dart';
  8. class AppBloc extends Bloc<AppEvent, AppState> {
  9. final IApp iAppImpl;
  10. AppBloc(this.iAppImpl) : super(AppState.initial());
  11. @override
  12. Stream<AppState> mapEventToState(
  13. AppEvent event,
  14. ) async* {
  15. yield* event.map(
  16. initial: (e) async* {},
  17. viewsReceived: (e) async* {
  18. yield state;
  19. },
  20. );
  21. }
  22. }
  23. @freezed
  24. abstract class AppEvent with _$AppEvent {
  25. const factory AppEvent.initial() = _Initial;
  26. const factory AppEvent.viewsReceived(
  27. Either<List<View>, WorkspaceError> appsOrFail) = ViewsReceived;
  28. }
  29. @freezed
  30. abstract class AppState implements _$AppState {
  31. const factory AppState({
  32. required bool isLoading,
  33. required Option<List<View>> views,
  34. required Either<Unit, WorkspaceError> successOrFailure,
  35. }) = _AppState;
  36. factory AppState.initial() => AppState(
  37. isLoading: false,
  38. views: none(),
  39. successOrFailure: left(unit),
  40. );
  41. }