|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:flutter/foundation.dart'; |
| 4 | +import 'package:screen_brightness/screen_brightness.dart'; |
| 5 | + |
| 6 | +import '../flet_service.dart'; |
| 7 | +import '../utils/numbers.dart'; |
| 8 | +import '../utils/platform.dart'; |
| 9 | + |
| 10 | +class ScreenBrightnessService extends FletService { |
| 11 | + final ScreenBrightness _screenBrightness = ScreenBrightness(); |
| 12 | + StreamSubscription<double>? _systemSubscription; |
| 13 | + StreamSubscription<double>? _applicationSubscription; |
| 14 | + |
| 15 | + ScreenBrightnessService({required super.control}); |
| 16 | + |
| 17 | + @override |
| 18 | + void init() { |
| 19 | + super.init(); |
| 20 | + debugPrint("ScreenBrightnessService(${control.id}).init"); |
| 21 | + control.addInvokeMethodListener(_invokeMethod); |
| 22 | + _updateListeners(); |
| 23 | + } |
| 24 | + |
| 25 | + @override |
| 26 | + void update() { |
| 27 | + _updateListeners(); |
| 28 | + } |
| 29 | + |
| 30 | + Future<dynamic> _invokeMethod(String name, dynamic args) async { |
| 31 | + if (isWebPlatform()) { |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + switch (name) { |
| 36 | + case "get_system_screen_brightness": |
| 37 | + return _screenBrightness.system; |
| 38 | + case "can_change_system_screen_brightness": |
| 39 | + return _screenBrightness.canChangeSystemBrightness; |
| 40 | + case "set_system_screen_brightness": |
| 41 | + final brightness = parseDouble(args["value"]); |
| 42 | + if (brightness == null) { |
| 43 | + throw ArgumentError.notNull("value"); |
| 44 | + } |
| 45 | + return _screenBrightness.setSystemScreenBrightness(brightness); |
| 46 | + case "get_application_screen_brightness": |
| 47 | + return _screenBrightness.application; |
| 48 | + case "set_application_screen_brightness": |
| 49 | + final brightness = parseDouble(args["value"]); |
| 50 | + if (brightness == null) { |
| 51 | + throw ArgumentError.notNull("value"); |
| 52 | + } |
| 53 | + return _screenBrightness.setApplicationScreenBrightness(brightness); |
| 54 | + case "reset_application_screen_brightness": |
| 55 | + return _screenBrightness.resetApplicationScreenBrightness(); |
| 56 | + case "is_animate": |
| 57 | + return _screenBrightness.isAnimate; |
| 58 | + case "set_animate": |
| 59 | + final isAnimate = parseBool(args["value"]); |
| 60 | + if (isAnimate == null) { |
| 61 | + throw ArgumentError.notNull("value"); |
| 62 | + } |
| 63 | + return _screenBrightness.setAnimate(isAnimate); |
| 64 | + case "is_auto_reset": |
| 65 | + return _screenBrightness.isAutoReset; |
| 66 | + case "set_auto_reset": |
| 67 | + final isAutoReset = parseBool(args["value"]); |
| 68 | + if (isAutoReset == null) { |
| 69 | + throw ArgumentError.notNull("value"); |
| 70 | + } |
| 71 | + return _screenBrightness.setAutoReset(isAutoReset); |
| 72 | + default: |
| 73 | + throw Exception("Unknown ScreenBrightness method: $name"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + void _updateListeners() { |
| 78 | + if (isWebPlatform()) { |
| 79 | + _disposeSubscriptions(); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + final listenSystem = |
| 84 | + control.getBool("on_system_screen_brightness_change") == true; |
| 85 | + final listenApplication = |
| 86 | + control.getBool("on_application_screen_brightness_change") == true; |
| 87 | + |
| 88 | + if (listenSystem && _systemSubscription == null) { |
| 89 | + _systemSubscription = _screenBrightness.onSystemScreenBrightnessChanged |
| 90 | + .listen((value) { |
| 91 | + control.triggerEvent( |
| 92 | + "system_screen_brightness_change", {"brightness": value}); |
| 93 | + }, onError: (error) { |
| 94 | + debugPrint( |
| 95 | + "ScreenBrightnessService: error listening for system changes: $error"); |
| 96 | + }); |
| 97 | + } else if (!listenSystem && _systemSubscription != null) { |
| 98 | + _systemSubscription?.cancel(); |
| 99 | + _systemSubscription = null; |
| 100 | + } |
| 101 | + |
| 102 | + if (listenApplication && _applicationSubscription == null) { |
| 103 | + _applicationSubscription = |
| 104 | + _screenBrightness.onApplicationScreenBrightnessChanged.listen( |
| 105 | + (value) { |
| 106 | + control.triggerEvent( |
| 107 | + "application_screen_brightness_change", {"brightness": value}); |
| 108 | + }, |
| 109 | + onError: (error) { |
| 110 | + debugPrint( |
| 111 | + "ScreenBrightnessService: error listening for application changes: $error"); |
| 112 | + }, |
| 113 | + ); |
| 114 | + } else if (!listenApplication && _applicationSubscription != null) { |
| 115 | + _applicationSubscription?.cancel(); |
| 116 | + _applicationSubscription = null; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + void _disposeSubscriptions() { |
| 121 | + _systemSubscription?.cancel(); |
| 122 | + _systemSubscription = null; |
| 123 | + _applicationSubscription?.cancel(); |
| 124 | + _applicationSubscription = null; |
| 125 | + } |
| 126 | + |
| 127 | + @override |
| 128 | + void dispose() { |
| 129 | + debugPrint("ScreenBrightnessService(${control.id}).dispose()"); |
| 130 | + control.removeInvokeMethodListener(_invokeMethod); |
| 131 | + _disposeSubscriptions(); |
| 132 | + super.dispose(); |
| 133 | + } |
| 134 | +} |
0 commit comments