network_connectivity.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import 'dart:async';
  2. import 'package:connectivity_plus/connectivity_plus.dart';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:vnote_device_plugin/events/event_type.dart';
  5. import 'package:fis_common/logger/logger.dart';
  6. import 'package:fis_common/env/env.dart';
  7. /// 网络连接检测器 - 全局实例
  8. final netChecker = _init();
  9. NetworkConnectivityChecker _init() {
  10. final instance = NetworkConnectivityChecker._();
  11. bool isConnecting = true;
  12. instance.onlineChangedEvent.addListener((sender, e) {
  13. if (e) {
  14. if (!isConnecting) {
  15. // TODO: 暂时不提示
  16. // PromptBox.toast("网络已连接");
  17. }
  18. } else {
  19. // TODO: 暂时不提示
  20. // PromptBox.error("网络已断开");
  21. }
  22. isConnecting = e;
  23. });
  24. return instance;
  25. }
  26. /// 网络连接检查器
  27. class NetworkConnectivityChecker {
  28. NetworkConnectivityChecker._();
  29. static bool customNetStatus = false;
  30. static void customUpdateStatus(ConnectivityResult v) {
  31. logger.i("NetworkConnectivityChecker - CustomUpdateStatus: ${v.name}.");
  32. netChecker._status = v;
  33. netChecker.onlineChangedEvent.emit(netChecker, netChecker.isOnline);
  34. }
  35. final Connectivity _api = Connectivity();
  36. StreamSubscription<ConnectivityResult>? _subscription;
  37. ConnectivityResult _status = ConnectivityResult.none;
  38. /// 在线状态变更事件
  39. final FEventHandler<bool> onlineChangedEvent = FEventHandler<bool>();
  40. /// 连接状态
  41. ConnectivityResult get status => _status;
  42. FPlatformEnum get platform => FPlatform.current;
  43. /// 是否在线
  44. bool get isOnline {
  45. if (kIsWeb) {
  46. return true;
  47. }
  48. return status != ConnectivityResult.none;
  49. }
  50. void run() {
  51. if (_subscription != null) {
  52. return;
  53. }
  54. _subscription = _api.onConnectivityChanged.listen((event) {
  55. logger.i(
  56. "NetworkConnectivityChecker - Current net connection type: ${event.name}.");
  57. _update(event);
  58. logger.i("NetworkConnectivityChecker - IsOnline: $isOnline.");
  59. });
  60. }
  61. void stop() {
  62. _subscription?.cancel();
  63. _subscription = null;
  64. }
  65. /// 手动检查
  66. Future<bool> check() async {
  67. logger.i("NetworkConnectivityChecker - Check start.");
  68. final result = await _api.checkConnectivity();
  69. logger.i("NetworkConnectivityChecker - Check result:${result.name}.");
  70. _update(result);
  71. logger.i("NetworkConnectivityChecker - IsOnline: $isOnline.");
  72. return isOnline;
  73. }
  74. void _update(ConnectivityResult result) {
  75. if (customNetStatus) {
  76. logger.i(
  77. "NetworkConnectivityChecker - Intercept status update by customNetStatus.");
  78. return;
  79. }
  80. _status = result;
  81. onlineChangedEvent.emit(this, isOnline);
  82. }
  83. }