| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import 'package:flutter/material.dart';
- import '../game/zentap_game.dart';
- import '../game/audio/audio_manager.dart';
- /// Manages app lifecycle events for game suspension and resumption
- class AppLifecycleManager with WidgetsBindingObserver {
- static AppLifecycleManager? _instance;
- ZenTapGame? _currentGame;
- AppLifecycleManager._();
- static AppLifecycleManager get instance {
- _instance ??= AppLifecycleManager._();
- return _instance!;
- }
- /// Initialize the lifecycle manager
- void initialize() {
- WidgetsBinding.instance.addObserver(this);
- }
- /// Dispose the lifecycle manager
- void dispose() {
- WidgetsBinding.instance.removeObserver(this);
- _currentGame = null;
- }
- /// Register the current active game instance
- void setCurrentGame(ZenTapGame? game) {
- _currentGame = game;
- }
- /// Get the current active game instance
- ZenTapGame? getCurrentGame() {
- return _currentGame;
- }
- @override
- void didChangeAppLifecycleState(AppLifecycleState state) {
- super.didChangeAppLifecycleState(state);
- // Handle app lifecycle changes for game suspension and audio management
- switch (state) {
- case AppLifecycleState.resumed:
- // App came back to foreground, resume game if there's an active game
- _currentGame?.resumeGame();
- // Resume background music if enabled
- AudioManager().resumeBackgroundMusic();
- break;
- case AppLifecycleState.inactive:
- case AppLifecycleState.paused:
- // App went to background, screen turned off
- // Suspend the game to save battery and resources
- _currentGame?.pauseGame();
- // Pause background music to save battery
- AudioManager().pauseBackgroundMusic();
- break;
- case AppLifecycleState.detached:
- case AppLifecycleState.hidden:
- // App is being closed or hidden
- // Stop all audio immediately
- AudioManager().stopBackgroundMusic();
- _currentGame?.pauseGame();
- break;
- }
- }
- }
|