import 'dart:async';

import 'package:internet_connection_checker/internet_connection_checker.dart';

import '../dialog/net_error_dialog.dart';

class InternetConnectionHelper {
  InternetConnectionHelper._();

  static StreamSubscription<InternetConnectionStatus>? _subscription;

  ///当前网络是否可用
  static Future<bool> isConnected() {
    return InternetConnectionChecker.instance.hasConnection;
  }

  static void startInternetConnection() {
    _subscription?.cancel();
    _subscription = InternetConnectionChecker.instance.onStatusChange.listen(
      (InternetConnectionStatus status) {
        if (status == InternetConnectionStatus.connected) {
          //有网
          NetErrorDialog.dismiss();
        } else {
          //无网络|网络差
          NetErrorDialog.show();
        }
      },
    );
  }

  //一般不需要调用
  static void stopInternetConnection() {
    _subscription?.cancel();
  }
}
