import 'package:flutter/material.dart';
import 'package:get/get.dart';

/// 全局监听应用是否进入后台 或者 前台
class AppStateController extends GetxController with WidgetsBindingObserver {
  // 响应式变量，存储当前生命周期状态
  final appLifecycleState = AppLifecycleState.resumed.obs;

  // 便捷方法：判断应用是否在前台
  bool get isAppInForeground => appLifecycleState.value == AppLifecycleState.resumed;

  @override
  void onInit() {
    super.onInit();
    WidgetsBinding.instance.addObserver(this); // 注册生命周期监听
  }

  @override
  void onClose() {
    WidgetsBinding.instance.removeObserver(this); // 移除监听
    super.onClose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    appLifecycleState.value = state; // 更新状态
    // if (isAppInForeground) {
    //   WidgetsBinding.instance.addPostFrameCallback((_) {
    //     Get.find<UpgradeLogic>().initiateVersionCheck(Get.context!);
    //   });
    // }
  }
}
