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> {
|
2021-10-18 16:30:20 +08:00
|
|
|
final IApp appManager;
|
2021-10-14 18:11:59 +08:00
|
|
|
final IAppListenr listener;
|
2021-10-18 16:30:20 +08:00
|
|
|
AppBloc({required this.appManager, required this.listener}) : super(AppState.initial());
|
2021-07-21 15:43:05 +08:00
|
|
|
|
|
|
|
@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* {
|
2021-10-14 18:11:59 +08:00
|
|
|
listener.start(viewsChangeCallback: _handleViewsOrFail);
|
|
|
|
|
2021-07-22 17:06:53 +08:00
|
|
|
yield* _fetchViews();
|
2021-07-21 22:41:44 +08:00
|
|
|
},
|
2021-07-22 17:06:53 +08:00
|
|
|
createView: (CreateView value) async* {
|
2021-10-18 16:30:20 +08:00
|
|
|
final viewOrFailed = await appManager.createView(name: value.name, desc: value.desc, viewType: value.viewType);
|
2021-10-29 14:43:10 +08:00
|
|
|
yield viewOrFailed.fold(
|
|
|
|
(view) => state.copyWith(
|
|
|
|
selectedView: view,
|
|
|
|
successOrFailure: left(unit),
|
|
|
|
),
|
|
|
|
(error) {
|
|
|
|
Log.error(error);
|
|
|
|
return state.copyWith(successOrFailure: right(error));
|
|
|
|
},
|
|
|
|
);
|
2021-07-22 17:06:53 +08:00
|
|
|
},
|
2021-10-14 18:11:59 +08:00
|
|
|
didReceiveViews: (e) async* {
|
|
|
|
yield state.copyWith(views: e.views);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> close() async {
|
|
|
|
await listener.stop();
|
|
|
|
return super.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _handleViewsOrFail(Either<List<View>, WorkspaceError> viewsOrFail) {
|
|
|
|
viewsOrFail.fold(
|
|
|
|
(views) => add(AppEvent.didReceiveViews(views)),
|
|
|
|
(error) {
|
|
|
|
Log.error(error);
|
|
|
|
},
|
2021-07-22 17:06:53 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream<AppState> _fetchViews() async* {
|
2021-10-18 16:30:20 +08:00
|
|
|
final viewsOrFailed = await appManager.getViews();
|
2021-07-22 17:06:53 +08:00
|
|
|
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
|
2021-10-09 10:09:31 +08:00
|
|
|
class AppEvent with _$AppEvent {
|
2021-07-22 17:06:53 +08:00
|
|
|
const factory AppEvent.initial() = Initial;
|
2021-10-11 22:57:27 +08:00
|
|
|
const factory AppEvent.createView(String name, String desc, ViewType viewType) = CreateView;
|
2021-10-14 18:11:59 +08:00
|
|
|
const factory AppEvent.didReceiveViews(List<View> views) = ReceiveViews;
|
2021-07-22 11:23:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
2021-10-09 10:09:31 +08:00
|
|
|
class AppState with _$AppState {
|
2021-07-22 11:23:15 +08:00
|
|
|
const factory AppState({
|
|
|
|
required bool isLoading,
|
2021-07-29 08:28:18 +08:00
|
|
|
required List<View>? views,
|
2021-10-29 14:43:10 +08:00
|
|
|
View? selectedView,
|
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-10-29 14:43:10 +08:00
|
|
|
selectedView: null,
|
2021-07-22 11:23:15 +08:00
|
|
|
successOrFailure: left(unit),
|
|
|
|
);
|
|
|
|
}
|