Updated folder structure
This commit is contained in:
78
lib/data/dto/game_manager.dart
Normal file
78
lib/data/dto/game_manager.dart
Normal file
@@ -0,0 +1,78 @@
|
||||
import 'package:cabo_counter/data/dto/game_session.dart';
|
||||
import 'package:cabo_counter/services/local_storage_service.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class GameManager extends ChangeNotifier {
|
||||
List<GameSession> gameList = [];
|
||||
|
||||
/// Adds a new game session to the list and sorts it by creation date.
|
||||
/// Takes a [GameSession] object as input. It then adds the session to the `gameList`,
|
||||
/// sorts the list in descending order based on the creation date, and notifies listeners of the change.
|
||||
/// It also saves the updated game sessions to local storage.
|
||||
/// Returns the index of the newly added session in the sorted list.
|
||||
int addGameSession(GameSession session) {
|
||||
session.addListener(() {
|
||||
notifyListeners(); // Propagate session changes
|
||||
});
|
||||
gameList.add(session);
|
||||
gameList.sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
||||
notifyListeners();
|
||||
LocalStorageService.saveGameSessions();
|
||||
return gameList.indexOf(session);
|
||||
}
|
||||
|
||||
/// Retrieves a game session by its id.
|
||||
/// Takes a String [id] as input. It searches the `gameList` for a session
|
||||
/// with a matching id and returns it if found.
|
||||
/// If no session is found, it returns null.
|
||||
GameSession? getGameSessionById(String id) {
|
||||
return gameList.firstWhereOrNull((session) => session.id == id);
|
||||
}
|
||||
|
||||
/// Removes a game session from the list and sorts it by creation date.
|
||||
/// Takes a [index] as input. It then removes the session at the specified index from the `gameList`,
|
||||
/// sorts the list in descending order based on the creation date, and notifies listeners of the change.
|
||||
/// It also saves the updated game sessions to local storage.
|
||||
void removeGameSessionByIndex(int index) {
|
||||
gameList[index].removeListener(notifyListeners);
|
||||
gameList.removeAt(index);
|
||||
notifyListeners();
|
||||
LocalStorageService.saveGameSessions();
|
||||
}
|
||||
|
||||
/// Removes a game session by its ID.
|
||||
/// Takes a String [id] as input. It finds the index of the game session with the matching ID
|
||||
/// in the `gameList`, and then calls `removeGameSessionByIndex` with that index.
|
||||
void removeGameSessionById(String id) {
|
||||
final int index =
|
||||
gameList.indexWhere((session) => session.id.toString() == id);
|
||||
if (index == -1) return;
|
||||
removeGameSessionByIndex(index);
|
||||
}
|
||||
|
||||
/// Retrieves a game session by its ID.
|
||||
/// Takes a String [id] as input. It finds the game session with the matching id
|
||||
bool gameExistsInGameList(String id) {
|
||||
return gameList.any((session) => session.id.toString() == id);
|
||||
}
|
||||
|
||||
/// Ends a game session if its in unlimited mode.
|
||||
/// Takes a String [id] as input. It finds the index of the game
|
||||
/// session with the matching ID marks it as finished,
|
||||
void endGame(String id) {
|
||||
final int index =
|
||||
gameList.indexWhere((session) => session.id.toString() == id);
|
||||
|
||||
// Game session not found or not in unlimited mode
|
||||
if (index == -1 || gameList[index].isPointsLimitEnabled == true) return;
|
||||
|
||||
gameList[index].roundNumber--;
|
||||
gameList[index].isGameFinished = true;
|
||||
gameList[index].setWinner();
|
||||
notifyListeners();
|
||||
LocalStorageService.saveGameSessions();
|
||||
}
|
||||
}
|
||||
|
||||
final gameManager = GameManager();
|
||||
Reference in New Issue
Block a user