基础组件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

54 lines
1.5 KiB

  1. import 'dart:async';
  2. import 'package:zhiying_base_widget/pages/feedback_page/bloc/feedback_repository.dart';
  3. import 'package:zhiying_comm/zhiying_comm.dart';
  4. import 'feedback_event.dart';
  5. import 'feedback_state.dart';
  6. import 'package:flutter_bloc/flutter_bloc.dart';
  7. class FeedbackBloc extends Bloc<FeedbackEvent, FeedbackState> {
  8. // FeedbackBloc() : super(FeedbackInitial());
  9. FeedBackRepository repository;
  10. FeedbackBloc(this.repository) : super(FeedbackInitial());
  11. @override
  12. Stream<FeedbackState> mapEventToState(
  13. FeedbackEvent event,
  14. ) async* {
  15. /// 初始化
  16. if (event is FeedbackInitEvent) {
  17. yield* _mapInitToState(event);
  18. }
  19. /// 提交反馈
  20. if (event is FeedbackSubmitEvent) {
  21. yield* _mapSaveToState(event);
  22. }
  23. }
  24. /// 提交反馈
  25. Stream<FeedbackState> _mapSaveToState(FeedbackSubmitEvent event) async* {
  26. var result = await repository.saveFeedback(event?.model);
  27. if (!EmptyUtil.isEmpty(result)) {
  28. yield FeedbackSaveSuccessState();
  29. } else {
  30. yield FeedbackSaveErrorState();
  31. }
  32. }
  33. /// 初始化
  34. Stream<FeedbackState> _mapInitToState(FeedbackInitEvent event) async* {
  35. var cache = await repository.fetchCachedData(event.model);
  36. if (!EmptyUtil.isEmpty(cache)) {
  37. yield FeedbackLoadedState(model: cache);
  38. }
  39. var result = await repository.fetchData(event.model);
  40. if (!EmptyUtil.isEmpty(result)) {
  41. yield FeedbackLoadedState(model: result);
  42. } else {
  43. yield FeedbackErrorState();
  44. }
  45. }
  46. }