import 'dart:async';
import 'package:get/get.dart';
import 'package:tone_snap/routes/app_routes.dart';

class SplashController extends GetxController {
  Timer? _timer;
  int _timeCount = 1;

  @override
  void onInit() {
    super.onInit();
    _startTimer();
  }

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

  /// 开始定时器
  void _startTimer() {
    _timer = Timer.periodic(const Duration(seconds: 1), (Timer t) {
      if (_timeCount <= 0) {
        _openInitial();
        return;
      }
      _timeCount--;
    });
  }

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

  /// 打开初始页面
  void _openInitial() {
    _stopTimer();
    Get.offNamed(AppRoutes.initial);
  }
}
