Experimenten with database
This commit is contained in:
@@ -30,3 +30,5 @@ class AppDatabase extends _$AppDatabase {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final db = AppDatabase();
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'package:cabo_counter/data/db/database.dart';
|
||||
import 'package:cabo_counter/data/dto/game_session.dart';
|
||||
import 'package:cabo_counter/services/local_storage_service.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
@@ -11,7 +12,7 @@ class GameManager extends ChangeNotifier {
|
||||
/// 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) {
|
||||
int addGameSession(GameSession session, AppDatabase db) {
|
||||
session.addListener(() {
|
||||
notifyListeners(); // Propagate session changes
|
||||
});
|
||||
@@ -19,6 +20,7 @@ class GameManager extends ChangeNotifier {
|
||||
gameList.sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
||||
notifyListeners();
|
||||
LocalStorageService.saveGameSessions();
|
||||
db.gameSessionDao.insertGameSession(session);
|
||||
return gameList.indexOf(session);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:cabo_counter/core/custom_theme.dart';
|
||||
import 'package:cabo_counter/data/db/database.dart';
|
||||
import 'package:cabo_counter/l10n/generated/app_localizations.dart';
|
||||
import 'package:cabo_counter/presentation/views/tab_view.dart';
|
||||
import 'package:cabo_counter/services/config_service.dart';
|
||||
@@ -6,6 +7,7 @@ import 'package:cabo_counter/services/local_storage_service.dart';
|
||||
import 'package:cabo_counter/services/version_service.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
@@ -16,7 +18,12 @@ Future<void> main() async {
|
||||
// Initialize services
|
||||
await ConfigService.initConfig();
|
||||
await VersionService.init();
|
||||
runApp(const App());
|
||||
runApp(
|
||||
Provider<AppDatabase>(
|
||||
create: (_) => db,
|
||||
child: const App(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class App extends StatefulWidget {
|
||||
@@ -31,7 +38,6 @@ class _AppState extends State<App> with WidgetsBindingObserver {
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
LocalStorageService.loadGameSessions();
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -53,7 +59,7 @@ class _AppState extends State<App> with WidgetsBindingObserver {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
LocalStorageService.loadGameSessions();
|
||||
//LocalStorageService.loadGameSessions();
|
||||
|
||||
return CupertinoApp(
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:cabo_counter/core/constants.dart';
|
||||
import 'package:cabo_counter/core/custom_theme.dart';
|
||||
import 'package:cabo_counter/data/db/database.dart';
|
||||
import 'package:cabo_counter/data/dto/game_manager.dart';
|
||||
import 'package:cabo_counter/data/dto/game_session.dart';
|
||||
import 'package:cabo_counter/data/dto/player.dart';
|
||||
@@ -11,6 +12,7 @@ import 'package:cabo_counter/services/config_service.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
enum CreateStatus {
|
||||
@@ -479,7 +481,9 @@ class _CreateGameViewState extends State<CreateGameView> {
|
||||
caboPenalty: ConfigService.getCaboPenalty(),
|
||||
isPointsLimitEnabled: isPointsLimitEnabled,
|
||||
isGameFinished: false);
|
||||
gameManager.addGameSession(gameSession);
|
||||
|
||||
final db = Provider.of<AppDatabase>(context, listen: false);
|
||||
gameManager.addGameSession(gameSession, db);
|
||||
final session = gameManager.getGameSessionById(gameId) ?? gameSession;
|
||||
|
||||
Navigator.pushAndRemoveUntil(
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:cabo_counter/core/constants.dart';
|
||||
import 'package:cabo_counter/core/custom_theme.dart';
|
||||
import 'package:cabo_counter/data/db/database.dart';
|
||||
import 'package:cabo_counter/data/dto/game_manager.dart';
|
||||
import 'package:cabo_counter/data/dto/game_session.dart';
|
||||
import 'package:cabo_counter/l10n/generated/app_localizations.dart';
|
||||
@@ -7,9 +8,9 @@ import 'package:cabo_counter/presentation/views/home/active_game/active_game_vie
|
||||
import 'package:cabo_counter/presentation/views/home/create_game_view.dart';
|
||||
import 'package:cabo_counter/presentation/views/home/settings_view.dart';
|
||||
import 'package:cabo_counter/services/config_service.dart';
|
||||
import 'package:cabo_counter/services/local_storage_service.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
enum PreRatingDialogDecision { yes, no, cancel }
|
||||
@@ -31,16 +32,34 @@ class MainMenuView extends StatefulWidget {
|
||||
|
||||
class _MainMenuViewState extends State<MainMenuView> {
|
||||
bool _isLoading = true;
|
||||
late final AppDatabase db;
|
||||
|
||||
@override
|
||||
initState() {
|
||||
super.initState();
|
||||
LocalStorageService.loadGameSessions().then((_) {
|
||||
db = Provider.of<AppDatabase>(context, listen: false);
|
||||
|
||||
db.gameSessionDao.getAllGameSessions().then((gameSessions) {
|
||||
print(
|
||||
'[MainMenuView] Loaded ${gameSessions.length} game sessions from the database.');
|
||||
for (final session in gameSessions) {
|
||||
gameManager.addGameSession(session, db);
|
||||
}
|
||||
|
||||
print('[MainMenuView] Game sessions loaded successfully.');
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}).catchError((error) {
|
||||
print('[MainMenuView] Error loading game sessions: $error');
|
||||
});
|
||||
|
||||
/* LocalStorageService.loadGameSessions().then((_) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
});
|
||||
gameManager.addListener(_updateView);
|
||||
gameManager.addListener(_updateView);*/
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||
precacheImage(
|
||||
@@ -216,7 +235,16 @@ class _MainMenuViewState extends State<MainMenuView> {
|
||||
],
|
||||
),
|
||||
),
|
||||
child: const Center(child: CupertinoActivityIndicator()),
|
||||
child: Center(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const CupertinoActivityIndicator(),
|
||||
const SizedBox(height: 10),
|
||||
Text(AppLocalizations.of(context).loading_games)
|
||||
],
|
||||
)),
|
||||
),
|
||||
)));
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:cabo_counter/data/db/database.dart';
|
||||
import 'package:cabo_counter/data/dto/game_manager.dart';
|
||||
import 'package:cabo_counter/data/dto/game_session.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
@@ -173,12 +174,12 @@ class LocalStorageService {
|
||||
.toList();
|
||||
|
||||
for (GameSession s in importedList) {
|
||||
_importSession(s);
|
||||
_importSession(s, db);
|
||||
}
|
||||
} else if (await _validateJsonSchema(jsonString, false)) {
|
||||
// Checks if the JSON String is in the single game format
|
||||
final jsonData = json.decode(jsonString) as Map<String, dynamic>;
|
||||
_importSession(GameSession.fromJson(jsonData));
|
||||
_importSession(GameSession.fromJson(jsonData), db);
|
||||
} else {
|
||||
return ImportStatus.validationError;
|
||||
}
|
||||
@@ -199,13 +200,14 @@ class LocalStorageService {
|
||||
}
|
||||
|
||||
/// Imports a single game session into the gameList.
|
||||
static Future<void> _importSession(GameSession session) async {
|
||||
static Future<void> _importSession(
|
||||
GameSession session, AppDatabase db) async {
|
||||
if (gameManager.gameExistsInGameList(session.id)) {
|
||||
print(
|
||||
'[local_storage_service.dart] Die Session mit der ID ${session.id} existiert bereits. Sie wird überschrieben.');
|
||||
gameManager.removeGameSessionById(session.id);
|
||||
}
|
||||
gameManager.addGameSession(session);
|
||||
gameManager.addGameSession(session, db);
|
||||
print(
|
||||
'[local_storage_service.dart] Die Session mit der ID ${session.id} wurde erfolgreich importiert.');
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ name: cabo_counter
|
||||
description: "Mobile app for the card game Cabo"
|
||||
publish_to: 'none'
|
||||
|
||||
version: 0.5.8+685
|
||||
version: 0.5.8+688
|
||||
|
||||
environment:
|
||||
sdk: ^3.5.4
|
||||
|
||||
Reference in New Issue
Block a user