message_model.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:shared_preferences/shared_preferences.dart';
  5. import 'package:sport/bean/message.dart';
  6. import 'package:sport/db/message_db.dart';
  7. import 'package:sport/services/api/inject_api.dart';
  8. class MessageModel extends ChangeNotifier with InjectApi {
  9. // Message _message;
  10. int _curId;
  11. List<MessageInstance> _messages;
  12. final StreamController<List<MessageInstance>> _queryController =
  13. StreamController.broadcast();
  14. Stream<List<MessageInstance>> get queryStream => _queryController.stream;
  15. ///事件订阅对象
  16. StreamSubscription dataSubscription;
  17. StreamSubscription periodicSubscription;
  18. init() {
  19. periodicSubscription =
  20. Stream.periodic(Duration(seconds: 10)).listen((event) async {
  21. Message data;
  22. SharedPreferences prefs = await SharedPreferences.getInstance();
  23. String token = prefs.getString("token");
  24. if(token == null || token.isEmpty == true)
  25. return;
  26. if (_curId != null) {
  27. data = (await api.getMessageForPoll(curId: _curId)).data;
  28. } else {
  29. data = (await api.getMessageForPoll()).data;
  30. }
  31. // curId 这里一定是接口返回的 本地存 sqlite的 curId 全部都是 0
  32. _curId = data.curId;
  33. _messages = data.messages;
  34. // 没有新消息还处理流程吗?
  35. if(!(data?.messages?.isNotEmpty == true))
  36. return
  37. // var list = await MessageDB().findAll(); // 清空数字
  38. // print("[list]:$list--------------------------");
  39. // var userList = await MessageDB().findAllUser();
  40. // print("[list]:$userList--------------------------");
  41. // MessageDB().deleteTable(); //
  42. // 这里本来是做了去重操作的 现在不用了 ...
  43. // if (_messages.length > 0) {
  44. // var list = await MessageDB().findLatest();
  45. // print(list);
  46. // if (list.length != 0) {
  47. // int lastCurId = list[0]["curId"];
  48. // print('[lastCurId]:$lastCurId-------------------------');
  49. // // 可能会重复插入...;
  50. // if (lastCurId != _curId) {
  51. // add(data.messages, _curId);
  52. // }
  53. // } else {
  54. //// add(data.messages, _curId);
  55. // }
  56. // }
  57. add(data.messages, _curId);
  58. });
  59. }
  60. add(List<MessageInstance> messages, int curId) async {
  61. List<MessageItem> _items = [];
  62. // 插进去是 1 未读... 0 已读...
  63. for (MessageInstance _instance in _messages) {
  64. _items.add(new MessageItem(
  65. message: _instance,
  66. curId: curId,
  67. status: 1,
  68. userId: _instance.fromUser.id));
  69. var list = await MessageDB().findHasUserId(_instance.fromUser.id);
  70. // print("[user:]$list-------------------------------");
  71. //
  72. // printWell("list", list);
  73. if (list.length == 0) {
  74. await MessageDB().insertUser(
  75. new UserTableInfo(userId: _instance.fromUser.id, isTop: 0));
  76. }
  77. }
  78. await MessageDB().insertAll(_items);
  79. // 这里就是 广播?
  80. get(messages);
  81. // print("[items]:$_items----------------------------------------------");
  82. }
  83. get(List<MessageInstance> list) {
  84. _queryController.add(list);
  85. }
  86. close() {
  87. ///关闭
  88. dataSubscription.cancel();
  89. // _queryController.close();
  90. }
  91. }
  92. // Generic Interface for all BLoCs
  93. abstract class BlocBase {
  94. void dispose();
  95. }
  96. // Generic BLoC provider
  97. class BlocProvider<T extends BlocBase> extends StatefulWidget {
  98. BlocProvider({
  99. Key key,
  100. @required this.child,
  101. @required this.bloc,
  102. }) : super(key: key);
  103. final T bloc;
  104. final Widget child;
  105. @override
  106. _BlocProviderState<T> createState() => _BlocProviderState<T>();
  107. static T of<T extends BlocBase>(BuildContext context) {
  108. final type = _typeOf<BlocProvider<T>>();
  109. BlocProvider<T> provider = context.ancestorWidgetOfExactType(type);
  110. return provider.bloc;
  111. }
  112. static Type _typeOf<T>() => T;
  113. }
  114. class _BlocProviderState<T> extends State<BlocProvider<BlocBase>> {
  115. @override
  116. void dispose() {
  117. widget.bloc.dispose();
  118. super.dispose();
  119. }
  120. @override
  121. Widget build(BuildContext context) {
  122. return widget.child;
  123. }
  124. }
  125. class MessageBloc implements BlocBase {
  126. Message _message;
  127. //
  128. // Stream to handle the counter
  129. //
  130. StreamController<Message> _counterController = StreamController<Message>();
  131. // 流的输入...
  132. StreamSink<Message> get _addOperate => _counterController.sink;
  133. Stream<Message> get outCounter => _counterController.stream;
  134. void dispose() {
  135. _counterController.close();
  136. }
  137. // 这是操作?
  138. void addOperate(data) {
  139. _addOperate.add(_message);
  140. }
  141. }