field_control_controller.dart 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import 'package:application/service/api.dart';
  2. import 'package:application/service/map_watch.dart';
  3. import 'package:application/logger.dart';
  4. import 'package:get/get.dart';
  5. import '../home_controller.dart';
  6. export 'package:application/service/map_watch.dart';
  7. extension ActiveInfoExt2 on ActiveInfo{
  8. Future<void> update(ToActionInfo info) async {
  9. final newUserList = <UserInfo>[];
  10. for (final nUser in info.userList) {
  11. late UserInfo user;
  12. final oUser = getUserById(nUser.userId);
  13. if (oUser != null) {
  14. user = oUser;
  15. await user.update(nUser);
  16. }else{
  17. user = await newUserInfo(nUser);
  18. }
  19. newUserList.add(user);
  20. }
  21. userList = newUserList;
  22. }
  23. }
  24. class FieldControlController extends GetxController{
  25. HomeController get _home => Get.find();
  26. MapWatchService? get mapWatch => MapWatchService.instance;
  27. final activeList = <ActiveInfo>[].obs;
  28. @override
  29. void onInit() {
  30. super.onInit();
  31. workFlushData();
  32. }
  33. Future<void> workFlushData()async{
  34. while(!isClosed){
  35. try{
  36. await flushData();
  37. }catch(e){
  38. error(e);
  39. }
  40. await 1.seconds.delay();
  41. }
  42. }
  43. ActiveInfo? getActiveById(int id){
  44. for (final one in activeList){
  45. if(one.id==id){
  46. return one;
  47. }
  48. }
  49. return null;
  50. }
  51. Future<void> flushData()async{
  52. final r = await ApiService.to.stub.toUserDetailQueryV2(ToUserDetailQueryRequestV2(
  53. mapId: mapWatch?.id.toInt()));
  54. final newList = <ActiveInfo>[];
  55. for(final one in r.list){
  56. late ActiveInfo info;
  57. final old = getActiveById(one.actId);
  58. if(old != null){
  59. info = old;
  60. await info.update(one);
  61. }else{
  62. info = await one.into();
  63. }
  64. newList.add(info);
  65. }
  66. activeList.value = newList;
  67. }
  68. }