UserView.dart 12 KB

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