message_model.dart 4.3 KB

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