// Author: fengshengxiong
// Date: 2024/6/26
// Description: 网络连接服务

import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:get/get.dart';
import 'package:tone_snap/ads/app_open_ad_manager.dart';
import 'package:tone_snap/ads/interstitial_ad_manager.dart';
import 'package:tone_snap/utils/log_util.dart';

class NetworkConnectivityService extends GetxService {
  StreamSubscription<List<ConnectivityResult>>? subscription;
  List<ConnectivityResult> recordResult = [];

  @override
  void onInit() {
    super.onInit();
    subscription = Connectivity().onConnectivityChanged.listen((List<ConnectivityResult> result) {
      LogUtil.d('网络连接类型变化：$result');
      if (recordResult.contains(ConnectivityResult.none) && !result.contains(ConnectivityResult.none)) {
        AppOpenAdManager().loadAd();
        InterstitialAdManager().loadAd();
      }
      recordResult = result;
    });
  }

  @override
  void onClose() {
    subscription?.cancel();
    super.onClose();
  }
}