UserView.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import 'dart:convert';
  2. import 'dart:typed_data';
  3. import 'package:autocomplete_textfield/autocomplete_textfield.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:get_it/get_it.dart';
  7. import 'package:sprintf/sprintf.dart';
  8. import 'package:ustest/ConsultationList.dart';
  9. import 'package:ustest/Services/UserService.dart';
  10. import 'package:ustest/meeting.dart';
  11. import 'Services/ConsultationService.dart';
  12. class UserView extends StatefulWidget {
  13. @override
  14. State<StatefulWidget> createState() {
  15. // TODO: implement createState
  16. return _UserView();
  17. }
  18. }
  19. class _UserView extends State<UserView> {
  20. String? selectedType = "我申请的";
  21. @override
  22. Widget build(BuildContext context) {
  23. print("build userView");
  24. var service = GetIt.instance.get<UserService>();
  25. var user = service.getCurrentUser();
  26. if (user == null) {
  27. print("user is null");
  28. _showSignInScreen(context);
  29. }
  30. var userName = user == null ? "" : user.userName;
  31. var accessToken = user == null ? "" : user.accessToken;
  32. final items = <String>["我申请的", "我组织的", "我参与的"];
  33. int selectedState = 0;
  34. if (selectedType == "我申请的") {
  35. selectedState = 0;
  36. } else if (selectedType == "我组织的") {
  37. selectedState = 1;
  38. } else {
  39. selectedState = 2;
  40. }
  41. Future<List<Consultation>> future = fetchConsultations(selectedState);
  42. GlobalKey<AutoCompleteTextFieldState<String>> typeKey = GlobalKey();
  43. GlobalKey listKey = GlobalKey();
  44. return Scaffold(
  45. body: Center(
  46. child: Column(
  47. children: [
  48. Padding(
  49. padding: EdgeInsets.all(8.0),
  50. child: Row(
  51. children: [
  52. user == null
  53. ? TextButton(
  54. child: const Text("Sign in"),
  55. onPressed: () => _showSignInScreen(context))
  56. : Text(sprintf(
  57. 'Name:%s, Token:%s', [userName, accessToken])),
  58. TextButton.icon(
  59. onPressed: () => onLogout(context),
  60. icon: Icon(Icons.logout),
  61. label: Text("注銷"))
  62. ],
  63. ),
  64. ),
  65. Row(
  66. children: [
  67. TextButton.icon(
  68. onPressed: () => onApplyConsultation(context),
  69. icon: Icon(Icons.add),
  70. label: Text("Apply")),
  71. ],
  72. ),
  73. Padding(
  74. padding: const EdgeInsets.all(8.0),
  75. child: DropdownButtonFormField<String>(
  76. key: typeKey,
  77. value: selectedType,
  78. decoration: InputDecoration(hintText: "Select one state"),
  79. items: items
  80. .map((e) => DropdownMenuItem<String>(
  81. child: Text(e),
  82. value: e,
  83. ))
  84. .toList(),
  85. onChanged: (value) {
  86. selectedType = value;
  87. },
  88. )),
  89. Row(
  90. children: [
  91. TextButton.icon(
  92. onPressed: () => setState(() {}),
  93. icon: Icon(Icons.search),
  94. label: Text("Search")),
  95. ],
  96. ),
  97. Expanded(
  98. child: Center(
  99. child: FutureBuilder<List<Consultation>>(
  100. key: listKey,
  101. future: future,
  102. builder: (context, snapshot) {
  103. if (snapshot.hasError) {
  104. return const Center(
  105. child: Text('An error has occurred!'),
  106. );
  107. } else if (snapshot.hasData) {
  108. return ConsultationList(
  109. token: accessToken, consultations: snapshot.data!);
  110. } else {
  111. return const Center(
  112. child: CircularProgressIndicator(),
  113. );
  114. }
  115. },
  116. ),
  117. )),
  118. service.Channel != null
  119. ? StreamBuilder(
  120. stream: service.Channel!.stream,
  121. builder: (context, snapshot) {
  122. var message = "";
  123. // var message =
  124. // snapshot.hasData ? '${snapshot.data}' : 'no message';
  125. if (snapshot.hasData) {
  126. var uint8Array =
  127. Uint8List.fromList(snapshot.data as List<int>);
  128. var byteData = uint8Array.buffer.asByteData();
  129. var byteDataLength = byteData.lengthInBytes;
  130. var messageConentList = Uint8List.view(
  131. uint8Array.buffer, 0, byteDataLength);
  132. var messageConentLength = messageConentList.length;
  133. var messageConent =
  134. Uint8List.fromList(messageConentList);
  135. var messageText = Utf8Decoder().convert(messageConent);
  136. //print("message text:" + messageText);
  137. Map<String, dynamic> messageObject =
  138. jsonDecode(messageText);
  139. //通知类型区分
  140. if (messageObject["NotificationType"] as int ==
  141. NotificationTypeEnum.ConnectionNotification.index) {
  142. var connectionNotification =
  143. ConnectionNotification.fromJson(messageObject);
  144. print(
  145. "connectionNotification.NotificationType:${connectionNotification.notificationType}");
  146. } else if (messageObject["NotificationType"] as int ==
  147. NotificationTypeEnum.DisconnectNotification.index) {
  148. var disconnectNotification =
  149. DisconnectNotification.fromJson(messageObject);
  150. print(
  151. "disconnectNotification.NotificationType:${disconnectNotification.notificationType}");
  152. } else if (messageObject["NotificationType"] as int ==
  153. NotificationTypeEnum
  154. .FinishNotifyRecordsMessage.index) {
  155. var disconnectNotification =
  156. FinishNotifyRecordsMessage.fromJson(
  157. messageObject);
  158. var consultationService =
  159. GetIt.instance.get<ConsultationService>();
  160. consultationService
  161. .RaiseConsultationNotificationReceived(
  162. messageObject);
  163. print(
  164. "FinishNotifyRecordsMessage.NotificationType:${disconnectNotification.notificationType}, ${disconnectNotification.codes}");
  165. }
  166. message = messageText;
  167. }
  168. return new Padding(
  169. padding: const EdgeInsets.symmetric(vertical: 24.0),
  170. child: new Text(message),
  171. );
  172. })
  173. : Text('no notification'),
  174. //MeetingPage()
  175. ],
  176. ),
  177. ),
  178. );
  179. }
  180. void _showSignInScreen(BuildContext context) {
  181. Future.delayed(Duration.zero, () {
  182. Navigator.of(context).pushNamed('/signin');
  183. });
  184. }
  185. Future<List<Consultation>> fetchConsultations(int? selectedType) async {
  186. try {
  187. var service = GetIt.instance.get<ConsultationService>();
  188. var list = await service.FindConsultationsByPageAsync('id', selectedType);
  189. return list;
  190. } catch (ex) {
  191. print('QueryExam.to ex' + ex.toString());
  192. return List.empty();
  193. }
  194. }
  195. void onLogout(context) {
  196. var service = GetIt.instance.get<UserService>();
  197. service.logout();
  198. Navigator.of(context).pushNamed('/signin');
  199. }
  200. void onApplyConsultation(context) {
  201. print("onApplyConsultation click");
  202. Navigator.of(context).pushNamed('/applyconsultation');
  203. }
  204. }
  205. enum NotificationTypeEnum {
  206. Daiding,
  207. /// <summary>
  208. /// ChatMsgNotification|1| 聊天通知
  209. /// </summary>
  210. ChatMsgNotification,
  211. /// <summary>
  212. /// UpgradeVersionNotification|2| 版本更新通知
  213. /// </summary>
  214. UpgradeVersionNotification,
  215. /// <summary>
  216. /// LogoffNotification|3| 登出通知
  217. /// </summary>
  218. LogoffNotification,
  219. /// <summary>
  220. /// DisconnectNotification| 4|与服务器断开连接通知
  221. /// </summary>
  222. DisconnectNotification,
  223. /// <summary>
  224. /// ConnectionNotification| 5| 与服务器已连接通知
  225. /// </summary>
  226. ConnectionNotification,
  227. /// <summary>
  228. ///FinishNotifyRecordsMessage| 6 | 检查记录完成消息类型
  229. /// </summary>
  230. FinishNotifyRecordsMessage,
  231. /// <summary>
  232. ///InvitedEnterRoomNotification| 7 | 邀请用户加入房间通知
  233. /// </summary>
  234. InvitedEnterRoomNotification,
  235. /// <summary>
  236. ///CancelInvitedEnterRoomNotification| 8 |取消邀请用户通知
  237. /// </summary>
  238. CancelInvitedEnterRoomNotification,
  239. placeholder9,
  240. placeholder10,
  241. placeholder11,
  242. placeholder12,
  243. placeholder13,
  244. placeholder14,
  245. placeholder15,
  246. placeholder16,
  247. placeholder17,
  248. placeholder18,
  249. placeholder19,
  250. placeholder20,
  251. placeholder21,
  252. placeholder22,
  253. placeholder23,
  254. placeholder24,
  255. /// <summary>
  256. /// InviteLiveConsultationNotification| 25 | 开始会诊的通知
  257. /// </summary>
  258. InviteLiveConsultationNotification,
  259. /// <summary>
  260. /// AcceptLiveConsultationNotification| 26 | 接受会诊的通知
  261. /// </summary>
  262. AcceptLiveConsultationNotification,
  263. /// <summary>
  264. /// RejectLiveConsultationNotification| 27 | 拒绝会诊的通知
  265. /// </summary>
  266. RejectLiveConsultationNotification,
  267. }
  268. class ConnectionNotification {
  269. NotificationTypeEnum notificationType;
  270. ConnectionNotification({
  271. this.notificationType = NotificationTypeEnum.Daiding,
  272. });
  273. factory ConnectionNotification.fromJson(Map<String, dynamic> map) {
  274. return ConnectionNotification(
  275. notificationType: NotificationTypeEnum.values
  276. .firstWhere((e) => e.index == map['NotificationType']),
  277. );
  278. }
  279. }
  280. class DisconnectNotification {
  281. NotificationTypeEnum notificationType;
  282. DisconnectNotification({
  283. this.notificationType = NotificationTypeEnum.Daiding,
  284. });
  285. factory DisconnectNotification.fromJson(Map<String, dynamic> map) {
  286. return DisconnectNotification(
  287. notificationType: NotificationTypeEnum.values
  288. .firstWhere((e) => e.index == map['NotificationType']),
  289. );
  290. }
  291. }
  292. class FinishNotifyRecordsMessage {
  293. NotificationTypeEnum notificationType;
  294. List<String>? codes;
  295. FinishNotifyRecordsMessage(
  296. {this.notificationType = NotificationTypeEnum.Daiding, this.codes});
  297. factory FinishNotifyRecordsMessage.fromJson(Map<String, dynamic> map) {
  298. return FinishNotifyRecordsMessage(
  299. notificationType: NotificationTypeEnum.values
  300. .firstWhere((e) => e.index == map['NotificationType']),
  301. codes: map['Codes'] != null ? map['Codes'].cast<String>().toList() : null,
  302. );
  303. }
  304. }