topic_picture_bloc.dart
4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:wow_english/common/request/dao/listen_dao.dart';
import 'package:wow_english/common/request/exception.dart';
import 'package:wow_english/models/course_process_entity.dart';
import 'package:wow_english/utils/loading.dart';
part 'topic_picture_event.dart';
part 'topic_picture_state.dart';
class TopicPictureBloc extends Bloc<TopicPictureEvent, TopicPictureState> {
final PageController pageController;
final int modelCount;
int _currentPage = 0;
int _selectItem = -1;
CourseProcessEntity? _entity;
///正在评测
bool _isVoicing = false;
CourseProcessEntity? get entity => _entity;
int get currentPage => _currentPage + 1;
int get selectItem => _selectItem;
bool get isVoicing => _isVoicing;
var messageChannel = const BasicMessageChannel('com.owEnglish.voiceXs.BasicMessageChannel', StandardMessageCodec());
final audioPlayer = AudioPlayer();
TopicPictureBloc(this.pageController, this.modelCount) : super(TopicPictureInitial()) {
on<CurrentPageIndexChangeEvent>(_pageControllerChange);
on<InitMessageChannelEvent>(_initMessageChannelCall);
on<RequestDataEvent>(_requestData);
on<SelectItemEvent>(_selectItemLoad);
on<VoiceXsTestEvent>(_voiceXsTest);
on<VoiceResultEvent>(_voiceResult);
on<TopicPictureEvent>((event, emit) {
});
}
@override
Future<void> close() {
pageController.dispose();
audioPlayer.dispose();
return super.close();
}
void _requestData(RequestDataEvent event,Emitter<TopicPictureState> emitter) async {
try {
await loading(() async {
_entity = await ListenDao.process('8');
emitter(RequestDataState());
});
} catch (e) {
if (e is ApiException) {
EasyLoading.showToast(e.message??'请求失败,请检查网络连接');
}
}
}
void _pageControllerChange(CurrentPageIndexChangeEvent event,Emitter<TopicPictureState> emitter) async {
_currentPage = event.pageIndex;
final topics = _entity?.topics?[_currentPage];
if (topics?.type == 1 || topics?.type == 2) {
if (topics?.audioUrl != null) {
final urlStr = topics?.audioUrl??'';
if (urlStr.isNotEmpty) {
audioPlayer.play(UrlSource(urlStr));
}
}
} else {
audioPlayer.stop();
}
emitter(CurrentPageIndexState());
}
void _selectItemLoad(SelectItemEvent event,Emitter<TopicPictureState> emitter) async {
_selectItem = event.selectIndex;
emitter(SelectItemChangeState());
}
///messageChannel回调
Future messageResultHandler(message,Emitter<TopicPictureState> emitter) async {
EasyLoading.dismiss();
final Map args = message as Map;
final result = args['result'] as String;
if (result == '1') {
final overall = args['overall'].toString();
EasyLoading.showToast('测评成功,分数是$overall',duration: const Duration(seconds: 10));
} else {
EasyLoading.showToast('测评失败',duration: const Duration(seconds: 10));
}
if (kDebugMode) {
print('是否被移除>>>>>$emitter');
}
_isVoicing = false;
}
Future _initMessageChannelCall(InitMessageChannelEvent event,Emitter<TopicPictureState> emitter) async {
messageChannel.setMessageHandler((message) =>
messageResultHandler(message, emitter)
);
audioPlayer.onPlayerStateChanged.listen((event) {
if (event == PlayerState.stopped) {
if (kDebugMode) {
print('播放结束');
}
}
});
}
void _voiceXsTest(VoiceXsTestEvent event,Emitter<TopicPictureState> emitter) async {
EasyLoading.show(status: '录音中....');
messageChannel.send({'word':event.testWord,'type':event.type,'userId':event.userId.toString()});
_isVoicing = true;
emitter(VoiceXsTestState());
}
void _voiceResult(VoiceResultEvent event,Emitter<TopicPictureState> emitter) async {
_isVoicing = false;
emitter(VoiceXsTestState());
}
}