import 'dart:async';

import 'package:get/get.dart';
import 'package:wallpaperx/config/applovin.dart';
import 'package:wallpaperx/routes/app_pages.dart';

class SplashController extends GetxController {
  Timer? _timer;

  @override
  void onInit() {
    super.onInit();
    ApplovinUtil().initializeInterstitialAds(onGoHomeTap: toIndex);
    _startTimer();
  }

  @override
  void onClose() {
    _stopTimer();
    super.onClose();
  }

  /// 开始定时器
  void _startTimer() {
    _timer = Timer.periodic(const Duration(seconds: 10), (Timer t) {
      toIndex();
    });
  }

  /// 停止定时器
  void _stopTimer() {
    _timer?.cancel();
    _timer = null;
  }

  /// 打开首页
  void toIndex() {
    _stopTimer();
    Get.offAndToNamed(AppPages.home);
  }
}
