2021-07-22 17:06:53 +08:00
|
|
|
import 'package:app_flowy/workspace/domain/i_app.dart';
|
2021-09-12 23:04:34 +08:00
|
|
|
import 'package:flowy_log/flowy_log.dart';
|
2021-07-21 15:43:05 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart';
|
2021-07-21 22:41:44 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pb.dart';
|
2021-07-21 15:43:05 +08:00
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import 'package:dartz/dartz.dart';
|
|
|
|
|
|
|
|
part 'app_bloc.freezed.dart';
|
|
|
|
|
|
|
|
class AppBloc extends Bloc<AppEvent, AppState> {
|
|
|
|
final IApp iAppImpl;
|
|
|
|
AppBloc(this.iAppImpl) : super(AppState.initial());
|
|
|
|
|
|
|
|
@override
|
|
|
|
Stream<AppState> mapEventToState(
|
|
|
|
AppEvent event,
|
2021-07-21 22:41:44 +08:00
|
|
|
) async* {
|
|
|
|
yield* event.map(
|
2021-07-22 17:06:53 +08:00
|
|
|
initial: (e) async* {
|
|
|
|
yield* _fetchViews();
|
2021-07-21 22:41:44 +08:00
|
|
|
},
|
2021-07-22 17:06:53 +08:00
|
|
|
createView: (CreateView value) async* {
|
2021-09-11 20:09:46 +08:00
|
|
|
final viewOrFailed = await iAppImpl.createView(
|
2021-07-22 17:06:53 +08:00
|
|
|
name: value.name, desc: value.desc, viewType: value.viewType);
|
2021-09-11 20:09:46 +08:00
|
|
|
yield viewOrFailed.fold((view) => state, (error) {
|
|
|
|
Log.error(error);
|
|
|
|
return state.copyWith(successOrFailure: right(error));
|
|
|
|
});
|
2021-07-22 17:06:53 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream<AppState> _fetchViews() async* {
|
|
|
|
final viewsOrFailed = await iAppImpl.getViews();
|
|
|
|
yield viewsOrFailed.fold(
|
2021-07-29 08:28:18 +08:00
|
|
|
(apps) => state.copyWith(views: apps),
|
2021-09-05 13:50:23 +08:00
|
|
|
(error) {
|
|
|
|
Log.error(error);
|
|
|
|
return state.copyWith(successOrFailure: right(error));
|
|
|
|
},
|
2021-07-21 22:41:44 +08:00
|
|
|
);
|
|
|
|
}
|
2021-07-21 15:43:05 +08:00
|
|
|
}
|
2021-07-22 11:23:15 +08:00
|
|
|
|
|
|
|
@freezed
|
|
|
|
abstract class AppEvent with _$AppEvent {
|
2021-07-22 17:06:53 +08:00
|
|
|
const factory AppEvent.initial() = Initial;
|
|
|
|
const factory AppEvent.createView(
|
|
|
|
String name, String desc, ViewType viewType) = CreateView;
|
2021-07-22 11:23:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
|
|
|
abstract class AppState implements _$AppState {
|
|
|
|
const factory AppState({
|
|
|
|
required bool isLoading,
|
2021-07-29 08:28:18 +08:00
|
|
|
required List<View>? views,
|
2021-07-22 11:23:15 +08:00
|
|
|
required Either<Unit, WorkspaceError> successOrFailure,
|
|
|
|
}) = _AppState;
|
|
|
|
|
|
|
|
factory AppState.initial() => AppState(
|
|
|
|
isLoading: false,
|
2021-07-29 08:28:18 +08:00
|
|
|
views: null,
|
2021-07-22 11:23:15 +08:00
|
|
|
successOrFailure: left(unit),
|
|
|
|
);
|
|
|
|
}
|