Эх сурвалжийг харах

update(measure): 使用函数节流优化颈动脉2D图像参数调整 #0010094 Review by baka

gavin.chen 2 жил өмнө
parent
commit
cc7ccdff3b

+ 0 - 2
lib/view/3d_view/carotid_player.dart

@@ -99,9 +99,7 @@ class _CarotidPlayerState extends State<CarotidPlayer> {
   }
 
   void _loadCarotidImageBytes(Object sender, String base64Url) async {
-    final oldImage = image;
     image = await getImage(base64Url);
-    oldImage?.dispose();
     setState(() {});
   }
 

+ 21 - 1
lib/view/mobile_view/widgets/throttle.dart

@@ -1,6 +1,9 @@
 import 'dart:async';
 
+// 节流函数列表
 Map<String, Timer> _funcThrottle = {};
+// 节流函数上次触发的时间
+Map<String, DateTime> _funcThrottleLastCall = {};
 Function? _lastFunc;
 
 /// 函数节流
@@ -10,14 +13,31 @@ Function? _lastFunc;
 /// [milliseconds]: 要迟延的毫秒时间
 Function throttle(Function func, String funcTag, [int milliseconds = 2000]) {
   target() {
+    // print('对 $funcTag 进行函数节流');
     String key = funcTag.toString();
     Timer? _timer = _funcThrottle[key];
     if (_timer == null) {
-      func.call();
+      // 判断是否是第一次调用
+      if (_funcThrottleLastCall[key] == null) {
+        func.call();
+        _funcThrottleLastCall[key] = DateTime.now();
+      } else {
+        // 判断是否超过了延迟时间
+        if (DateTime.now()
+                .difference(_funcThrottleLastCall[key]!)
+                .inMilliseconds >
+            milliseconds) {
+          _funcThrottleLastCall[key] = DateTime.now();
+          func.call();
+        } else {
+          _lastFunc = func;
+        }
+      }
       _timer = Timer(Duration(milliseconds: milliseconds), () {
         final lastFunc = _lastFunc;
         if (lastFunc != null) {
           _lastFunc!.call();
+          _funcThrottleLastCall[key] = DateTime.now();
           _lastFunc = null;
         }
         Timer? t = _funcThrottle.remove(key);

+ 1 - 0
lib/view/player/control_board/operate_bar.dart

@@ -6,6 +6,7 @@ import 'package:fis_ui/index.dart';
 import 'package:flutter/material.dart';
 import '../controller.dart';
 import 'package:fis_measure/interfaces/process/player/play_controller.dart';
+import 'package:fis_measure/view/mobile_view/widgets/throttle.dart' as utils;
 import 'package:get/get.dart';
 import '../events.dart';
 

+ 21 - 15
lib/view/player/control_board/tone_bar_carotid.dart

@@ -30,11 +30,13 @@ class _SharpnessCarotidToneBarState extends State<_SharpnessCarotidToneBar> {
         color: Colors.white,
       ),
       onChange: (v) {
-        measure3DViewController.sharpness = v.toInt();
-        measure3DViewController.notifyShellSetTone();
-        setState(() {
-          curValue = v;
-        });
+        utils.throttle(() {
+          measure3DViewController.sharpness = v.toInt();
+          measure3DViewController.notifyShellSetTone();
+          setState(() {
+            curValue = v;
+          });
+        }, 'onChangeSharpness', 50);
       },
     );
   }
@@ -82,11 +84,13 @@ class _ContrastCarotidToneBarState extends State<_ContrastCarotidToneBar> {
         color: Colors.white,
       ),
       onChange: (v) {
-        measure3DViewController.contrast = v.toInt();
-        measure3DViewController.notifyShellSetTone();
-        setState(() {
-          curValue = v;
-        });
+        utils.throttle(() {
+          measure3DViewController.contrast = v.toInt();
+          measure3DViewController.notifyShellSetTone();
+          setState(() {
+            curValue = v;
+          });
+        }, 'onChangeContrast', 50);
       },
     );
   }
@@ -134,11 +138,13 @@ class _BrightnessCarotidToneBarState extends State<_BrightnessCarotidToneBar> {
         color: Colors.white,
       ),
       onChange: (v) {
-        measure3DViewController.brightness = v.toInt();
-        measure3DViewController.notifyShellSetTone();
-        setState(() {
-          curValue = v;
-        });
+        utils.throttle(() {
+          measure3DViewController.brightness = v.toInt();
+          measure3DViewController.notifyShellSetTone();
+          setState(() {
+            curValue = v;
+          });
+        }, 'onChangeBrightness', 50);
       },
     );
   }