controller.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:electronic_assistant/base/base_controller.dart';
  4. import 'package:electronic_assistant/module/talk/mindmap/mind_util.dart';
  5. import 'package:electronic_assistant/resource/assets.gen.dart';
  6. import 'package:flutter/rendering.dart';
  7. import 'package:flutter/widgets.dart';
  8. import 'package:get/get.dart';
  9. import 'package:get/get_core/src/get_main.dart';
  10. import 'package:webview_flutter/webview_flutter.dart';
  11. import '../../../data/bean/talks.dart';
  12. import '../../../data/bean/template_bean.dart';
  13. import '../controller.dart';
  14. class MindMapController extends BaseController {
  15. final String? talkId;
  16. MindMapController(this.talkId);
  17. TalkController get talkController => Get.find<TalkController>(tag: talkId);
  18. List<TemplateBean>? get templateList => talkController.templateList.value;
  19. int? get templateSelectId => talkController.templateSelectId.value;
  20. RxBool get isShowMindFullScreen => talkController.isShowMindFullScreen;
  21. StreamSubscription? _talkSummaryListener;
  22. StreamSubscription? _talkStatusListener;
  23. StreamSubscription? _talkBeanListener;
  24. GlobalKey repaintKey = GlobalKey();
  25. bool _isWebViewLoaded = false;
  26. @override
  27. void onReady() async {
  28. super.onReady();
  29. _talkBeanListener = talkController.talkBean.listen((bean) {
  30. _dealTalkSummaryUpdate(bean?.summary.value);
  31. });
  32. _talkStatusListener =
  33. talkController.talkBean.value?.status.listen((status) {
  34. if (status == TalkStatus.analysisSuccess) {
  35. _dealTalkSummaryUpdate(talkController.talkBean.value?.summary.value);
  36. }
  37. });
  38. _dealTalkSummaryUpdate(talkController.talkBean.value?.summary.value);
  39. _talkSummaryListener =
  40. talkController.talkBean.value?.summary.listen((summary) {
  41. _dealTalkSummaryUpdate(summary);
  42. });
  43. }
  44. void _loadWebMindData(String summary) {
  45. talkController.webViewController.loadFlutterAsset(Assets.html.index);
  46. talkController.webViewController.setNavigationDelegate(
  47. NavigationDelegate(
  48. onPageFinished: (String url) async {
  49. _isWebViewLoaded = true;
  50. _callUpdateMind(summary);
  51. },
  52. onNavigationRequest: (NavigationRequest request) {
  53. return NavigationDecision.navigate;
  54. },
  55. ),
  56. );
  57. }
  58. void _callUpdateMind(String summary) {
  59. talkController.webViewController
  60. .callHandler(MindUtil.functionToJsUpdateMind, args: [summary],
  61. handler: (value) {
  62. debugPrint('_dealTalkSummaryUpdate mindMap update success: $value');
  63. });
  64. }
  65. void _updateMindData(String summary) {
  66. if (_isWebViewLoaded) {
  67. _callUpdateMind(summary);
  68. } else {
  69. _loadWebMindData(summary);
  70. }
  71. }
  72. void _dealTalkSummaryUpdate(String? summary) {
  73. if (summary == null ||
  74. summary.isEmpty ||
  75. talkController.talkBean.value?.status.value !=
  76. TalkStatus.analysisSuccess) {
  77. return;
  78. }
  79. _updateMindData(summary);
  80. }
  81. void fullScreenClick() {
  82. talkController.isShowMindFullScreen.value = true;
  83. }
  84. void exitFullScreenClick() {
  85. talkController.onExitMindFullScreen();
  86. }
  87. void selectTemplate(TemplateBean bean) {
  88. talkController.selectTemplate(bean);
  89. }
  90. @override
  91. void onClose() {
  92. super.onClose();
  93. _talkBeanListener?.cancel();
  94. _talkStatusListener?.cancel();
  95. _talkSummaryListener?.cancel();
  96. }
  97. }