Merge pull request 'JSON Import für Testdaten & Funktion zum Löschen aller Daten' (#33) from feature/31-json-import-fuer-testdaten into development
Reviewed-on: #33 Reviewed-by: gelbeinhalb <spam@yannick-weigert.de>
This commit was merged in pull request #33.
This commit is contained in:
180
assets/schema.json
Normal file
180
assets/schema.json
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"games": {
|
||||||
|
"type": "array",
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"createdAt": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"players": {
|
||||||
|
"type": [
|
||||||
|
"array",
|
||||||
|
"null"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"createdAt": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"id",
|
||||||
|
"createdAt",
|
||||||
|
"name"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"group": {
|
||||||
|
"type": [
|
||||||
|
"object",
|
||||||
|
"null"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"createdAt": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"members": {
|
||||||
|
"type": "array",
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"createdAt": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"id",
|
||||||
|
"createdAt",
|
||||||
|
"name"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"id",
|
||||||
|
"createdAt",
|
||||||
|
"name",
|
||||||
|
"members"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"winner": {
|
||||||
|
"type": ["string","null"]
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"id",
|
||||||
|
"createdAt",
|
||||||
|
"name",
|
||||||
|
"winner"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"groups": {
|
||||||
|
"type": "array",
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"createdAt": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"members": {
|
||||||
|
"type": "array",
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"createdAt": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"id",
|
||||||
|
"createdAt",
|
||||||
|
"name"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"id",
|
||||||
|
"createdAt",
|
||||||
|
"name",
|
||||||
|
"members"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"players": {
|
||||||
|
"type": "array",
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"type": [
|
||||||
|
"object",
|
||||||
|
"null"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"createdAt": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"id",
|
||||||
|
"createdAt",
|
||||||
|
"name"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,2 +1,24 @@
|
|||||||
/// Button types used for styling the [CustomWidthButton]
|
/// Button types used for styling the [CustomWidthButton]
|
||||||
enum ButtonType { primary, secondary, tertiary }
|
enum ButtonType { primary, secondary, tertiary }
|
||||||
|
|
||||||
|
/// Result types for import operations in the [SettingsView]
|
||||||
|
/// - [ImportResult.success]: The import operation was successful.
|
||||||
|
/// - [ImportResult.canceled]: The import operation was canceled by the user.
|
||||||
|
/// - [ImportResult.fileReadError]: There was an error reading the selected file.
|
||||||
|
/// - [ImportResult.invalidSchema]: The JSON schema of the imported data is invalid.
|
||||||
|
/// - [ImportResult.formatException]: A format exception occurred during import.
|
||||||
|
/// - [ImportResult.unknownException]: An exception occurred during import.
|
||||||
|
enum ImportResult {
|
||||||
|
success,
|
||||||
|
canceled,
|
||||||
|
fileReadError,
|
||||||
|
invalidSchema,
|
||||||
|
formatException,
|
||||||
|
unknownException,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Result types for export operations in the [SettingsView]
|
||||||
|
/// - [ExportResult.success]: The export operation was successful.
|
||||||
|
/// - [ExportResult.canceled]: The export operation was canceled by the user.
|
||||||
|
/// - [ExportResult.unknownException]: An exception occurred during export.
|
||||||
|
enum ExportResult { success, canceled, unknownException }
|
||||||
|
|||||||
@@ -60,23 +60,157 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
|
|||||||
/// Also adds associated players and group if they exist.
|
/// Also adds associated players and group if they exist.
|
||||||
Future<void> addGame({required Game game}) async {
|
Future<void> addGame({required Game game}) async {
|
||||||
await db.transaction(() async {
|
await db.transaction(() async {
|
||||||
for (final p in game.players ?? []) {
|
|
||||||
await db.playerDao.addPlayer(player: p);
|
|
||||||
await db.playerGameDao.addPlayerToGame(gameId: game.id, playerId: p.id);
|
|
||||||
}
|
|
||||||
if (game.group != null) {
|
|
||||||
await db.groupDao.addGroup(group: game.group!);
|
|
||||||
await db.groupGameDao.addGroupToGame(game.id, game.group!.id);
|
|
||||||
}
|
|
||||||
await into(gameTable).insert(
|
await into(gameTable).insert(
|
||||||
GameTableCompanion.insert(
|
GameTableCompanion.insert(
|
||||||
id: game.id,
|
id: game.id,
|
||||||
name: game.name,
|
name: game.name,
|
||||||
winnerId: game.winner,
|
winnerId: Value(game.winner),
|
||||||
createdAt: game.createdAt,
|
createdAt: game.createdAt,
|
||||||
),
|
),
|
||||||
mode: InsertMode.insertOrReplace,
|
mode: InsertMode.insertOrReplace,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (game.players != null) {
|
||||||
|
await db.playerDao.addPlayers(players: game.players!);
|
||||||
|
for (final p in game.players ?? []) {
|
||||||
|
await db.playerGameDao.addPlayerToGame(
|
||||||
|
gameId: game.id,
|
||||||
|
playerId: p.id,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (game.group != null) {
|
||||||
|
await db.groupDao.addGroup(group: game.group!);
|
||||||
|
await db.groupGameDao.addGroupToGame(game.id, game.group!.id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> addGames({required List<Game> games}) async {
|
||||||
|
if (games.isEmpty) return;
|
||||||
|
await db.transaction(() async {
|
||||||
|
// Add all games in batch
|
||||||
|
await db.batch(
|
||||||
|
(b) => b.insertAll(
|
||||||
|
gameTable,
|
||||||
|
games
|
||||||
|
.map(
|
||||||
|
(game) => GameTableCompanion.insert(
|
||||||
|
id: game.id,
|
||||||
|
name: game.name,
|
||||||
|
createdAt: game.createdAt,
|
||||||
|
winnerId: Value(game.winner),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList(),
|
||||||
|
mode: InsertMode.insertOrReplace,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Add all groups of the games in batch
|
||||||
|
await db.batch(
|
||||||
|
(b) => b.insertAll(
|
||||||
|
db.groupTable,
|
||||||
|
games
|
||||||
|
.where((game) => game.group != null)
|
||||||
|
.map(
|
||||||
|
(game) => GroupTableCompanion.insert(
|
||||||
|
id: game.group!.id,
|
||||||
|
name: game.group!.name,
|
||||||
|
createdAt: game.group!.createdAt,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList(),
|
||||||
|
mode: InsertMode.insertOrReplace,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Add all players of the games in batch (unique)
|
||||||
|
final uniquePlayers = <String, Player>{};
|
||||||
|
for (final game in games) {
|
||||||
|
if (game.players != null) {
|
||||||
|
for (final p in game.players!) {
|
||||||
|
uniquePlayers[p.id] = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Also include members of groups
|
||||||
|
if (game.group != null) {
|
||||||
|
for (final m in game.group!.members) {
|
||||||
|
uniquePlayers[m.id] = m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uniquePlayers.isNotEmpty) {
|
||||||
|
await db.batch(
|
||||||
|
(b) => b.insertAll(
|
||||||
|
db.playerTable,
|
||||||
|
uniquePlayers.values
|
||||||
|
.map(
|
||||||
|
(p) => PlayerTableCompanion.insert(
|
||||||
|
id: p.id,
|
||||||
|
name: p.name,
|
||||||
|
createdAt: p.createdAt,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList(),
|
||||||
|
mode: InsertMode.insertOrReplace,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add all player-game associations in batch
|
||||||
|
await db.batch((b) {
|
||||||
|
for (final game in games) {
|
||||||
|
if (game.players != null) {
|
||||||
|
for (final p in game.players ?? []) {
|
||||||
|
b.insert(
|
||||||
|
db.playerGameTable,
|
||||||
|
PlayerGameTableCompanion.insert(
|
||||||
|
gameId: game.id,
|
||||||
|
playerId: p.id,
|
||||||
|
),
|
||||||
|
mode: InsertMode.insertOrReplace,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add all player-group associations in batch
|
||||||
|
await db.batch((b) {
|
||||||
|
for (final game in games) {
|
||||||
|
if (game.group != null) {
|
||||||
|
for (final m in game.group!.members) {
|
||||||
|
b.insert(
|
||||||
|
db.playerGroupTable,
|
||||||
|
PlayerGroupTableCompanion.insert(
|
||||||
|
playerId: m.id,
|
||||||
|
groupId: game.group!.id,
|
||||||
|
),
|
||||||
|
mode: InsertMode.insertOrReplace,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add all group-game associations in batch
|
||||||
|
await db.batch((b) {
|
||||||
|
for (final game in games) {
|
||||||
|
if (game.group != null) {
|
||||||
|
b.insert(
|
||||||
|
db.groupGameTable,
|
||||||
|
GroupGameTableCompanion.insert(
|
||||||
|
gameId: game.id,
|
||||||
|
groupId: game.group!.id,
|
||||||
|
),
|
||||||
|
mode: InsertMode.insertOrReplace,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,4 +238,12 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
|
|||||||
final result = await query.getSingleOrNull();
|
final result = await query.getSingleOrNull();
|
||||||
return result != null;
|
return result != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deletes all games from the database.
|
||||||
|
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||||
|
Future<bool> deleteAllGames() async {
|
||||||
|
final query = delete(gameTable);
|
||||||
|
final rowsAffected = await query.go();
|
||||||
|
return rowsAffected > 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,5 @@ part of 'game_dao.dart';
|
|||||||
|
|
||||||
// ignore_for_file: type=lint
|
// ignore_for_file: type=lint
|
||||||
mixin _$GameDaoMixin on DatabaseAccessor<AppDatabase> {
|
mixin _$GameDaoMixin on DatabaseAccessor<AppDatabase> {
|
||||||
$PlayerTableTable get playerTable => attachedDatabase.playerTable;
|
|
||||||
$GameTableTable get gameTable => attachedDatabase.gameTable;
|
$GameTableTable get gameTable => attachedDatabase.gameTable;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,6 +57,10 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
|||||||
name: group.name,
|
name: group.name,
|
||||||
createdAt: group.createdAt,
|
createdAt: group.createdAt,
|
||||||
),
|
),
|
||||||
|
mode: InsertMode.insertOrReplace,
|
||||||
|
);
|
||||||
|
await Future.wait(
|
||||||
|
group.members.map((player) => db.playerDao.addPlayer(player: player)),
|
||||||
);
|
);
|
||||||
await db.batch(
|
await db.batch(
|
||||||
(b) => b.insertAll(
|
(b) => b.insertAll(
|
||||||
@@ -69,17 +73,94 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
.toList(),
|
.toList(),
|
||||||
|
mode: InsertMode.insertOrReplace,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
await Future.wait(
|
|
||||||
group.members.map((player) => db.playerDao.addPlayer(player: player)),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds multiple groups to the database.
|
||||||
|
/// Also adds the group's members to the [PlayerGroupTable].
|
||||||
|
Future<void> addGroups({required List<Group> groups}) async {
|
||||||
|
if (groups.isEmpty) return;
|
||||||
|
await db.transaction(() async {
|
||||||
|
// Deduplicate groups by id - keep first occurrence
|
||||||
|
final Map<String, Group> uniqueGroups = {};
|
||||||
|
for (final g in groups) {
|
||||||
|
uniqueGroups.putIfAbsent(g.id, () => g);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert unique groups in batch
|
||||||
|
await db.batch(
|
||||||
|
(b) => b.insertAll(
|
||||||
|
groupTable,
|
||||||
|
uniqueGroups.values
|
||||||
|
.map(
|
||||||
|
(group) => GroupTableCompanion.insert(
|
||||||
|
id: group.id,
|
||||||
|
name: group.name,
|
||||||
|
createdAt: group.createdAt,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList(),
|
||||||
|
mode: InsertMode.insertOrReplace,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Collect unique players from all groups
|
||||||
|
final uniquePlayers = <String, Player>{};
|
||||||
|
for (final g in uniqueGroups.values) {
|
||||||
|
for (final m in g.members) {
|
||||||
|
uniquePlayers[m.id] = m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uniquePlayers.isNotEmpty) {
|
||||||
|
await db.batch(
|
||||||
|
(b) => b.insertAll(
|
||||||
|
db.playerTable,
|
||||||
|
uniquePlayers.values
|
||||||
|
.map(
|
||||||
|
(p) => PlayerTableCompanion.insert(
|
||||||
|
id: p.id,
|
||||||
|
name: p.name,
|
||||||
|
createdAt: p.createdAt,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList(),
|
||||||
|
mode: InsertMode.insertOrReplace,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare all player-group associations in one list (unique pairs)
|
||||||
|
final Set<String> seenPairs = {};
|
||||||
|
final List<PlayerGroupTableCompanion> pgRows = [];
|
||||||
|
for (final g in uniqueGroups.values) {
|
||||||
|
for (final m in g.members) {
|
||||||
|
final key = '${m.id}|${g.id}';
|
||||||
|
if (!seenPairs.contains(key)) {
|
||||||
|
seenPairs.add(key);
|
||||||
|
pgRows.add(
|
||||||
|
PlayerGroupTableCompanion.insert(playerId: m.id, groupId: g.id),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pgRows.isNotEmpty) {
|
||||||
|
await db.batch((b) {
|
||||||
|
for (final pg in pgRows) {
|
||||||
|
b.insert(db.playerGroupTable, pg, mode: InsertMode.insertOrReplace);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// Deletes the group with the given [id] from the database.
|
/// Deletes the group with the given [id] from the database.
|
||||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||||
Future<bool> deleteGroup({required String groupId}) async {
|
Future<bool> deleteGroup({required String groupId}) async {
|
||||||
@@ -117,4 +198,12 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
|||||||
final result = await query.getSingleOrNull();
|
final result = await query.getSingleOrNull();
|
||||||
return result != null;
|
return result != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deletes all groups from the database.
|
||||||
|
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||||
|
Future<bool> deleteAllGroups() async {
|
||||||
|
final query = delete(groupTable);
|
||||||
|
final rowsAffected = await query.go();
|
||||||
|
return rowsAffected > 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ part of 'group_game_dao.dart';
|
|||||||
// ignore_for_file: type=lint
|
// ignore_for_file: type=lint
|
||||||
mixin _$GroupGameDaoMixin on DatabaseAccessor<AppDatabase> {
|
mixin _$GroupGameDaoMixin on DatabaseAccessor<AppDatabase> {
|
||||||
$GroupTableTable get groupTable => attachedDatabase.groupTable;
|
$GroupTableTable get groupTable => attachedDatabase.groupTable;
|
||||||
$PlayerTableTable get playerTable => attachedDatabase.playerTable;
|
|
||||||
$GameTableTable get gameTable => attachedDatabase.gameTable;
|
$GameTableTable get gameTable => attachedDatabase.gameTable;
|
||||||
$GroupGameTableTable get groupGameTable => attachedDatabase.groupGameTable;
|
$GroupGameTableTable get groupGameTable => attachedDatabase.groupGameTable;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,12 +42,36 @@ class PlayerDao extends DatabaseAccessor<AppDatabase> with _$PlayerDaoMixin {
|
|||||||
name: player.name,
|
name: player.name,
|
||||||
createdAt: player.createdAt,
|
createdAt: player.createdAt,
|
||||||
),
|
),
|
||||||
|
mode: InsertMode.insertOrReplace,
|
||||||
);
|
);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds multiple [players] to the database in a batch operation.
|
||||||
|
Future<bool> addPlayers({required List<Player> players}) async {
|
||||||
|
if (players.isEmpty) return false;
|
||||||
|
|
||||||
|
await db.batch(
|
||||||
|
(b) => b.insertAll(
|
||||||
|
playerTable,
|
||||||
|
players
|
||||||
|
.map(
|
||||||
|
(player) => PlayerTableCompanion.insert(
|
||||||
|
id: player.id,
|
||||||
|
name: player.name,
|
||||||
|
createdAt: player.createdAt,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList(),
|
||||||
|
mode: InsertMode.insertOrReplace,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/// Deletes the player with the given [id] from the database.
|
/// Deletes the player with the given [id] from the database.
|
||||||
/// Returns `true` if the player was deleted, `false` if the player did not exist.
|
/// Returns `true` if the player was deleted, `false` if the player did not exist.
|
||||||
Future<bool> deletePlayer({required String playerId}) async {
|
Future<bool> deletePlayer({required String playerId}) async {
|
||||||
@@ -82,4 +106,12 @@ class PlayerDao extends DatabaseAccessor<AppDatabase> with _$PlayerDaoMixin {
|
|||||||
.getSingle();
|
.getSingle();
|
||||||
return count ?? 0;
|
return count ?? 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deletes all players from the database.
|
||||||
|
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||||
|
Future<bool> deleteAllPlayers() async {
|
||||||
|
final query = delete(playerTable);
|
||||||
|
final rowsAffected = await query.go();
|
||||||
|
return rowsAffected > 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,15 @@ class AppDatabase extends _$AppDatabase {
|
|||||||
@override
|
@override
|
||||||
int get schemaVersion => 1;
|
int get schemaVersion => 1;
|
||||||
|
|
||||||
|
@override
|
||||||
|
MigrationStrategy get migration {
|
||||||
|
return MigrationStrategy(
|
||||||
|
beforeOpen: (details) async {
|
||||||
|
await customStatement('PRAGMA foreign_keys = ON');
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
static QueryExecutor _openConnection() {
|
static QueryExecutor _openConnection() {
|
||||||
return driftDatabase(
|
return driftDatabase(
|
||||||
name: 'gametracker_db',
|
name: 'gametracker_db',
|
||||||
|
|||||||
@@ -552,12 +552,9 @@ class $GameTableTable extends GameTable
|
|||||||
late final GeneratedColumn<String> winnerId = GeneratedColumn<String>(
|
late final GeneratedColumn<String> winnerId = GeneratedColumn<String>(
|
||||||
'winner_id',
|
'winner_id',
|
||||||
aliasedName,
|
aliasedName,
|
||||||
false,
|
true,
|
||||||
type: DriftSqlType.string,
|
type: DriftSqlType.string,
|
||||||
requiredDuringInsert: true,
|
requiredDuringInsert: false,
|
||||||
defaultConstraints: GeneratedColumn.constraintIsAlways(
|
|
||||||
'REFERENCES player_table (id) ON DELETE CASCADE',
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
static const VerificationMeta _createdAtMeta = const VerificationMeta(
|
static const VerificationMeta _createdAtMeta = const VerificationMeta(
|
||||||
'createdAt',
|
'createdAt',
|
||||||
@@ -602,8 +599,6 @@ class $GameTableTable extends GameTable
|
|||||||
_winnerIdMeta,
|
_winnerIdMeta,
|
||||||
winnerId.isAcceptableOrUnknown(data['winner_id']!, _winnerIdMeta),
|
winnerId.isAcceptableOrUnknown(data['winner_id']!, _winnerIdMeta),
|
||||||
);
|
);
|
||||||
} else if (isInserting) {
|
|
||||||
context.missing(_winnerIdMeta);
|
|
||||||
}
|
}
|
||||||
if (data.containsKey('created_at')) {
|
if (data.containsKey('created_at')) {
|
||||||
context.handle(
|
context.handle(
|
||||||
@@ -633,7 +628,7 @@ class $GameTableTable extends GameTable
|
|||||||
winnerId: attachedDatabase.typeMapping.read(
|
winnerId: attachedDatabase.typeMapping.read(
|
||||||
DriftSqlType.string,
|
DriftSqlType.string,
|
||||||
data['${effectivePrefix}winner_id'],
|
data['${effectivePrefix}winner_id'],
|
||||||
)!,
|
),
|
||||||
createdAt: attachedDatabase.typeMapping.read(
|
createdAt: attachedDatabase.typeMapping.read(
|
||||||
DriftSqlType.dateTime,
|
DriftSqlType.dateTime,
|
||||||
data['${effectivePrefix}created_at'],
|
data['${effectivePrefix}created_at'],
|
||||||
@@ -650,12 +645,12 @@ class $GameTableTable extends GameTable
|
|||||||
class GameTableData extends DataClass implements Insertable<GameTableData> {
|
class GameTableData extends DataClass implements Insertable<GameTableData> {
|
||||||
final String id;
|
final String id;
|
||||||
final String name;
|
final String name;
|
||||||
final String winnerId;
|
final String? winnerId;
|
||||||
final DateTime createdAt;
|
final DateTime createdAt;
|
||||||
const GameTableData({
|
const GameTableData({
|
||||||
required this.id,
|
required this.id,
|
||||||
required this.name,
|
required this.name,
|
||||||
required this.winnerId,
|
this.winnerId,
|
||||||
required this.createdAt,
|
required this.createdAt,
|
||||||
});
|
});
|
||||||
@override
|
@override
|
||||||
@@ -663,7 +658,9 @@ class GameTableData extends DataClass implements Insertable<GameTableData> {
|
|||||||
final map = <String, Expression>{};
|
final map = <String, Expression>{};
|
||||||
map['id'] = Variable<String>(id);
|
map['id'] = Variable<String>(id);
|
||||||
map['name'] = Variable<String>(name);
|
map['name'] = Variable<String>(name);
|
||||||
map['winner_id'] = Variable<String>(winnerId);
|
if (!nullToAbsent || winnerId != null) {
|
||||||
|
map['winner_id'] = Variable<String>(winnerId);
|
||||||
|
}
|
||||||
map['created_at'] = Variable<DateTime>(createdAt);
|
map['created_at'] = Variable<DateTime>(createdAt);
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
@@ -672,7 +669,9 @@ class GameTableData extends DataClass implements Insertable<GameTableData> {
|
|||||||
return GameTableCompanion(
|
return GameTableCompanion(
|
||||||
id: Value(id),
|
id: Value(id),
|
||||||
name: Value(name),
|
name: Value(name),
|
||||||
winnerId: Value(winnerId),
|
winnerId: winnerId == null && nullToAbsent
|
||||||
|
? const Value.absent()
|
||||||
|
: Value(winnerId),
|
||||||
createdAt: Value(createdAt),
|
createdAt: Value(createdAt),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -685,7 +684,7 @@ class GameTableData extends DataClass implements Insertable<GameTableData> {
|
|||||||
return GameTableData(
|
return GameTableData(
|
||||||
id: serializer.fromJson<String>(json['id']),
|
id: serializer.fromJson<String>(json['id']),
|
||||||
name: serializer.fromJson<String>(json['name']),
|
name: serializer.fromJson<String>(json['name']),
|
||||||
winnerId: serializer.fromJson<String>(json['winnerId']),
|
winnerId: serializer.fromJson<String?>(json['winnerId']),
|
||||||
createdAt: serializer.fromJson<DateTime>(json['createdAt']),
|
createdAt: serializer.fromJson<DateTime>(json['createdAt']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -695,7 +694,7 @@ class GameTableData extends DataClass implements Insertable<GameTableData> {
|
|||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'id': serializer.toJson<String>(id),
|
'id': serializer.toJson<String>(id),
|
||||||
'name': serializer.toJson<String>(name),
|
'name': serializer.toJson<String>(name),
|
||||||
'winnerId': serializer.toJson<String>(winnerId),
|
'winnerId': serializer.toJson<String?>(winnerId),
|
||||||
'createdAt': serializer.toJson<DateTime>(createdAt),
|
'createdAt': serializer.toJson<DateTime>(createdAt),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -703,12 +702,12 @@ class GameTableData extends DataClass implements Insertable<GameTableData> {
|
|||||||
GameTableData copyWith({
|
GameTableData copyWith({
|
||||||
String? id,
|
String? id,
|
||||||
String? name,
|
String? name,
|
||||||
String? winnerId,
|
Value<String?> winnerId = const Value.absent(),
|
||||||
DateTime? createdAt,
|
DateTime? createdAt,
|
||||||
}) => GameTableData(
|
}) => GameTableData(
|
||||||
id: id ?? this.id,
|
id: id ?? this.id,
|
||||||
name: name ?? this.name,
|
name: name ?? this.name,
|
||||||
winnerId: winnerId ?? this.winnerId,
|
winnerId: winnerId.present ? winnerId.value : this.winnerId,
|
||||||
createdAt: createdAt ?? this.createdAt,
|
createdAt: createdAt ?? this.createdAt,
|
||||||
);
|
);
|
||||||
GameTableData copyWithCompanion(GameTableCompanion data) {
|
GameTableData copyWithCompanion(GameTableCompanion data) {
|
||||||
@@ -746,7 +745,7 @@ class GameTableData extends DataClass implements Insertable<GameTableData> {
|
|||||||
class GameTableCompanion extends UpdateCompanion<GameTableData> {
|
class GameTableCompanion extends UpdateCompanion<GameTableData> {
|
||||||
final Value<String> id;
|
final Value<String> id;
|
||||||
final Value<String> name;
|
final Value<String> name;
|
||||||
final Value<String> winnerId;
|
final Value<String?> winnerId;
|
||||||
final Value<DateTime> createdAt;
|
final Value<DateTime> createdAt;
|
||||||
final Value<int> rowid;
|
final Value<int> rowid;
|
||||||
const GameTableCompanion({
|
const GameTableCompanion({
|
||||||
@@ -759,12 +758,11 @@ class GameTableCompanion extends UpdateCompanion<GameTableData> {
|
|||||||
GameTableCompanion.insert({
|
GameTableCompanion.insert({
|
||||||
required String id,
|
required String id,
|
||||||
required String name,
|
required String name,
|
||||||
required String winnerId,
|
this.winnerId = const Value.absent(),
|
||||||
required DateTime createdAt,
|
required DateTime createdAt,
|
||||||
this.rowid = const Value.absent(),
|
this.rowid = const Value.absent(),
|
||||||
}) : id = Value(id),
|
}) : id = Value(id),
|
||||||
name = Value(name),
|
name = Value(name),
|
||||||
winnerId = Value(winnerId),
|
|
||||||
createdAt = Value(createdAt);
|
createdAt = Value(createdAt);
|
||||||
static Insertable<GameTableData> custom({
|
static Insertable<GameTableData> custom({
|
||||||
Expression<String>? id,
|
Expression<String>? id,
|
||||||
@@ -785,7 +783,7 @@ class GameTableCompanion extends UpdateCompanion<GameTableData> {
|
|||||||
GameTableCompanion copyWith({
|
GameTableCompanion copyWith({
|
||||||
Value<String>? id,
|
Value<String>? id,
|
||||||
Value<String>? name,
|
Value<String>? name,
|
||||||
Value<String>? winnerId,
|
Value<String?>? winnerId,
|
||||||
Value<DateTime>? createdAt,
|
Value<DateTime>? createdAt,
|
||||||
Value<int>? rowid,
|
Value<int>? rowid,
|
||||||
}) {
|
}) {
|
||||||
@@ -1538,13 +1536,6 @@ abstract class _$AppDatabase extends GeneratedDatabase {
|
|||||||
];
|
];
|
||||||
@override
|
@override
|
||||||
StreamQueryUpdateRules get streamUpdateRules => const StreamQueryUpdateRules([
|
StreamQueryUpdateRules get streamUpdateRules => const StreamQueryUpdateRules([
|
||||||
WritePropagation(
|
|
||||||
on: TableUpdateQuery.onTableName(
|
|
||||||
'player_table',
|
|
||||||
limitUpdateKind: UpdateKind.delete,
|
|
||||||
),
|
|
||||||
result: [TableUpdate('game_table', kind: UpdateKind.delete)],
|
|
||||||
),
|
|
||||||
WritePropagation(
|
WritePropagation(
|
||||||
on: TableUpdateQuery.onTableName(
|
on: TableUpdateQuery.onTableName(
|
||||||
'player_table',
|
'player_table',
|
||||||
@@ -1609,24 +1600,6 @@ final class $$PlayerTableTableReferences
|
|||||||
extends BaseReferences<_$AppDatabase, $PlayerTableTable, PlayerTableData> {
|
extends BaseReferences<_$AppDatabase, $PlayerTableTable, PlayerTableData> {
|
||||||
$$PlayerTableTableReferences(super.$_db, super.$_table, super.$_typedResult);
|
$$PlayerTableTableReferences(super.$_db, super.$_table, super.$_typedResult);
|
||||||
|
|
||||||
static MultiTypedResultKey<$GameTableTable, List<GameTableData>>
|
|
||||||
_gameTableRefsTable(_$AppDatabase db) => MultiTypedResultKey.fromTable(
|
|
||||||
db.gameTable,
|
|
||||||
aliasName: $_aliasNameGenerator(db.playerTable.id, db.gameTable.winnerId),
|
|
||||||
);
|
|
||||||
|
|
||||||
$$GameTableTableProcessedTableManager get gameTableRefs {
|
|
||||||
final manager = $$GameTableTableTableManager(
|
|
||||||
$_db,
|
|
||||||
$_db.gameTable,
|
|
||||||
).filter((f) => f.winnerId.id.sqlEquals($_itemColumn<String>('id')!));
|
|
||||||
|
|
||||||
final cache = $_typedResult.readTableOrNull(_gameTableRefsTable($_db));
|
|
||||||
return ProcessedTableManager(
|
|
||||||
manager.$state.copyWith(prefetchedData: cache),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
static MultiTypedResultKey<$PlayerGroupTableTable, List<PlayerGroupTableData>>
|
static MultiTypedResultKey<$PlayerGroupTableTable, List<PlayerGroupTableData>>
|
||||||
_playerGroupTableRefsTable(_$AppDatabase db) => MultiTypedResultKey.fromTable(
|
_playerGroupTableRefsTable(_$AppDatabase db) => MultiTypedResultKey.fromTable(
|
||||||
db.playerGroupTable,
|
db.playerGroupTable,
|
||||||
@@ -1698,31 +1671,6 @@ class $$PlayerTableTableFilterComposer
|
|||||||
builder: (column) => ColumnFilters(column),
|
builder: (column) => ColumnFilters(column),
|
||||||
);
|
);
|
||||||
|
|
||||||
Expression<bool> gameTableRefs(
|
|
||||||
Expression<bool> Function($$GameTableTableFilterComposer f) f,
|
|
||||||
) {
|
|
||||||
final $$GameTableTableFilterComposer composer = $composerBuilder(
|
|
||||||
composer: this,
|
|
||||||
getCurrentColumn: (t) => t.id,
|
|
||||||
referencedTable: $db.gameTable,
|
|
||||||
getReferencedColumn: (t) => t.winnerId,
|
|
||||||
builder:
|
|
||||||
(
|
|
||||||
joinBuilder, {
|
|
||||||
$addJoinBuilderToRootComposer,
|
|
||||||
$removeJoinBuilderFromRootComposer,
|
|
||||||
}) => $$GameTableTableFilterComposer(
|
|
||||||
$db: $db,
|
|
||||||
$table: $db.gameTable,
|
|
||||||
$addJoinBuilderToRootComposer: $addJoinBuilderToRootComposer,
|
|
||||||
joinBuilder: joinBuilder,
|
|
||||||
$removeJoinBuilderFromRootComposer:
|
|
||||||
$removeJoinBuilderFromRootComposer,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return f(composer);
|
|
||||||
}
|
|
||||||
|
|
||||||
Expression<bool> playerGroupTableRefs(
|
Expression<bool> playerGroupTableRefs(
|
||||||
Expression<bool> Function($$PlayerGroupTableTableFilterComposer f) f,
|
Expression<bool> Function($$PlayerGroupTableTableFilterComposer f) f,
|
||||||
) {
|
) {
|
||||||
@@ -1817,31 +1765,6 @@ class $$PlayerTableTableAnnotationComposer
|
|||||||
GeneratedColumn<DateTime> get createdAt =>
|
GeneratedColumn<DateTime> get createdAt =>
|
||||||
$composableBuilder(column: $table.createdAt, builder: (column) => column);
|
$composableBuilder(column: $table.createdAt, builder: (column) => column);
|
||||||
|
|
||||||
Expression<T> gameTableRefs<T extends Object>(
|
|
||||||
Expression<T> Function($$GameTableTableAnnotationComposer a) f,
|
|
||||||
) {
|
|
||||||
final $$GameTableTableAnnotationComposer composer = $composerBuilder(
|
|
||||||
composer: this,
|
|
||||||
getCurrentColumn: (t) => t.id,
|
|
||||||
referencedTable: $db.gameTable,
|
|
||||||
getReferencedColumn: (t) => t.winnerId,
|
|
||||||
builder:
|
|
||||||
(
|
|
||||||
joinBuilder, {
|
|
||||||
$addJoinBuilderToRootComposer,
|
|
||||||
$removeJoinBuilderFromRootComposer,
|
|
||||||
}) => $$GameTableTableAnnotationComposer(
|
|
||||||
$db: $db,
|
|
||||||
$table: $db.gameTable,
|
|
||||||
$addJoinBuilderToRootComposer: $addJoinBuilderToRootComposer,
|
|
||||||
joinBuilder: joinBuilder,
|
|
||||||
$removeJoinBuilderFromRootComposer:
|
|
||||||
$removeJoinBuilderFromRootComposer,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return f(composer);
|
|
||||||
}
|
|
||||||
|
|
||||||
Expression<T> playerGroupTableRefs<T extends Object>(
|
Expression<T> playerGroupTableRefs<T extends Object>(
|
||||||
Expression<T> Function($$PlayerGroupTableTableAnnotationComposer a) f,
|
Expression<T> Function($$PlayerGroupTableTableAnnotationComposer a) f,
|
||||||
) {
|
) {
|
||||||
@@ -1907,7 +1830,6 @@ class $$PlayerTableTableTableManager
|
|||||||
(PlayerTableData, $$PlayerTableTableReferences),
|
(PlayerTableData, $$PlayerTableTableReferences),
|
||||||
PlayerTableData,
|
PlayerTableData,
|
||||||
PrefetchHooks Function({
|
PrefetchHooks Function({
|
||||||
bool gameTableRefs,
|
|
||||||
bool playerGroupTableRefs,
|
bool playerGroupTableRefs,
|
||||||
bool playerGameTableRefs,
|
bool playerGameTableRefs,
|
||||||
})
|
})
|
||||||
@@ -1956,42 +1878,16 @@ class $$PlayerTableTableTableManager
|
|||||||
)
|
)
|
||||||
.toList(),
|
.toList(),
|
||||||
prefetchHooksCallback:
|
prefetchHooksCallback:
|
||||||
({
|
({playerGroupTableRefs = false, playerGameTableRefs = false}) {
|
||||||
gameTableRefs = false,
|
|
||||||
playerGroupTableRefs = false,
|
|
||||||
playerGameTableRefs = false,
|
|
||||||
}) {
|
|
||||||
return PrefetchHooks(
|
return PrefetchHooks(
|
||||||
db: db,
|
db: db,
|
||||||
explicitlyWatchedTables: [
|
explicitlyWatchedTables: [
|
||||||
if (gameTableRefs) db.gameTable,
|
|
||||||
if (playerGroupTableRefs) db.playerGroupTable,
|
if (playerGroupTableRefs) db.playerGroupTable,
|
||||||
if (playerGameTableRefs) db.playerGameTable,
|
if (playerGameTableRefs) db.playerGameTable,
|
||||||
],
|
],
|
||||||
addJoins: null,
|
addJoins: null,
|
||||||
getPrefetchedDataCallback: (items) async {
|
getPrefetchedDataCallback: (items) async {
|
||||||
return [
|
return [
|
||||||
if (gameTableRefs)
|
|
||||||
await $_getPrefetchedData<
|
|
||||||
PlayerTableData,
|
|
||||||
$PlayerTableTable,
|
|
||||||
GameTableData
|
|
||||||
>(
|
|
||||||
currentTable: table,
|
|
||||||
referencedTable: $$PlayerTableTableReferences
|
|
||||||
._gameTableRefsTable(db),
|
|
||||||
managerFromTypedResult: (p0) =>
|
|
||||||
$$PlayerTableTableReferences(
|
|
||||||
db,
|
|
||||||
table,
|
|
||||||
p0,
|
|
||||||
).gameTableRefs,
|
|
||||||
referencedItemsForCurrentItem:
|
|
||||||
(item, referencedItems) => referencedItems.where(
|
|
||||||
(e) => e.winnerId == item.id,
|
|
||||||
),
|
|
||||||
typedResults: items,
|
|
||||||
),
|
|
||||||
if (playerGroupTableRefs)
|
if (playerGroupTableRefs)
|
||||||
await $_getPrefetchedData<
|
await $_getPrefetchedData<
|
||||||
PlayerTableData,
|
PlayerTableData,
|
||||||
@@ -2055,7 +1951,6 @@ typedef $$PlayerTableTableProcessedTableManager =
|
|||||||
(PlayerTableData, $$PlayerTableTableReferences),
|
(PlayerTableData, $$PlayerTableTableReferences),
|
||||||
PlayerTableData,
|
PlayerTableData,
|
||||||
PrefetchHooks Function({
|
PrefetchHooks Function({
|
||||||
bool gameTableRefs,
|
|
||||||
bool playerGroupTableRefs,
|
bool playerGroupTableRefs,
|
||||||
bool playerGameTableRefs,
|
bool playerGameTableRefs,
|
||||||
})
|
})
|
||||||
@@ -2436,7 +2331,7 @@ typedef $$GameTableTableCreateCompanionBuilder =
|
|||||||
GameTableCompanion Function({
|
GameTableCompanion Function({
|
||||||
required String id,
|
required String id,
|
||||||
required String name,
|
required String name,
|
||||||
required String winnerId,
|
Value<String?> winnerId,
|
||||||
required DateTime createdAt,
|
required DateTime createdAt,
|
||||||
Value<int> rowid,
|
Value<int> rowid,
|
||||||
});
|
});
|
||||||
@@ -2444,7 +2339,7 @@ typedef $$GameTableTableUpdateCompanionBuilder =
|
|||||||
GameTableCompanion Function({
|
GameTableCompanion Function({
|
||||||
Value<String> id,
|
Value<String> id,
|
||||||
Value<String> name,
|
Value<String> name,
|
||||||
Value<String> winnerId,
|
Value<String?> winnerId,
|
||||||
Value<DateTime> createdAt,
|
Value<DateTime> createdAt,
|
||||||
Value<int> rowid,
|
Value<int> rowid,
|
||||||
});
|
});
|
||||||
@@ -2453,25 +2348,6 @@ final class $$GameTableTableReferences
|
|||||||
extends BaseReferences<_$AppDatabase, $GameTableTable, GameTableData> {
|
extends BaseReferences<_$AppDatabase, $GameTableTable, GameTableData> {
|
||||||
$$GameTableTableReferences(super.$_db, super.$_table, super.$_typedResult);
|
$$GameTableTableReferences(super.$_db, super.$_table, super.$_typedResult);
|
||||||
|
|
||||||
static $PlayerTableTable _winnerIdTable(_$AppDatabase db) =>
|
|
||||||
db.playerTable.createAlias(
|
|
||||||
$_aliasNameGenerator(db.gameTable.winnerId, db.playerTable.id),
|
|
||||||
);
|
|
||||||
|
|
||||||
$$PlayerTableTableProcessedTableManager get winnerId {
|
|
||||||
final $_column = $_itemColumn<String>('winner_id')!;
|
|
||||||
|
|
||||||
final manager = $$PlayerTableTableTableManager(
|
|
||||||
$_db,
|
|
||||||
$_db.playerTable,
|
|
||||||
).filter((f) => f.id.sqlEquals($_column));
|
|
||||||
final item = $_typedResult.readTableOrNull(_winnerIdTable($_db));
|
|
||||||
if (item == null) return manager;
|
|
||||||
return ProcessedTableManager(
|
|
||||||
manager.$state.copyWith(prefetchedData: [item]),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
static MultiTypedResultKey<$PlayerGameTableTable, List<PlayerGameTableData>>
|
static MultiTypedResultKey<$PlayerGameTableTable, List<PlayerGameTableData>>
|
||||||
_playerGameTableRefsTable(_$AppDatabase db) => MultiTypedResultKey.fromTable(
|
_playerGameTableRefsTable(_$AppDatabase db) => MultiTypedResultKey.fromTable(
|
||||||
db.playerGameTable,
|
db.playerGameTable,
|
||||||
@@ -2530,34 +2406,16 @@ class $$GameTableTableFilterComposer
|
|||||||
builder: (column) => ColumnFilters(column),
|
builder: (column) => ColumnFilters(column),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
ColumnFilters<String> get winnerId => $composableBuilder(
|
||||||
|
column: $table.winnerId,
|
||||||
|
builder: (column) => ColumnFilters(column),
|
||||||
|
);
|
||||||
|
|
||||||
ColumnFilters<DateTime> get createdAt => $composableBuilder(
|
ColumnFilters<DateTime> get createdAt => $composableBuilder(
|
||||||
column: $table.createdAt,
|
column: $table.createdAt,
|
||||||
builder: (column) => ColumnFilters(column),
|
builder: (column) => ColumnFilters(column),
|
||||||
);
|
);
|
||||||
|
|
||||||
$$PlayerTableTableFilterComposer get winnerId {
|
|
||||||
final $$PlayerTableTableFilterComposer composer = $composerBuilder(
|
|
||||||
composer: this,
|
|
||||||
getCurrentColumn: (t) => t.winnerId,
|
|
||||||
referencedTable: $db.playerTable,
|
|
||||||
getReferencedColumn: (t) => t.id,
|
|
||||||
builder:
|
|
||||||
(
|
|
||||||
joinBuilder, {
|
|
||||||
$addJoinBuilderToRootComposer,
|
|
||||||
$removeJoinBuilderFromRootComposer,
|
|
||||||
}) => $$PlayerTableTableFilterComposer(
|
|
||||||
$db: $db,
|
|
||||||
$table: $db.playerTable,
|
|
||||||
$addJoinBuilderToRootComposer: $addJoinBuilderToRootComposer,
|
|
||||||
joinBuilder: joinBuilder,
|
|
||||||
$removeJoinBuilderFromRootComposer:
|
|
||||||
$removeJoinBuilderFromRootComposer,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return composer;
|
|
||||||
}
|
|
||||||
|
|
||||||
Expression<bool> playerGameTableRefs(
|
Expression<bool> playerGameTableRefs(
|
||||||
Expression<bool> Function($$PlayerGameTableTableFilterComposer f) f,
|
Expression<bool> Function($$PlayerGameTableTableFilterComposer f) f,
|
||||||
) {
|
) {
|
||||||
@@ -2628,33 +2486,15 @@ class $$GameTableTableOrderingComposer
|
|||||||
builder: (column) => ColumnOrderings(column),
|
builder: (column) => ColumnOrderings(column),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
ColumnOrderings<String> get winnerId => $composableBuilder(
|
||||||
|
column: $table.winnerId,
|
||||||
|
builder: (column) => ColumnOrderings(column),
|
||||||
|
);
|
||||||
|
|
||||||
ColumnOrderings<DateTime> get createdAt => $composableBuilder(
|
ColumnOrderings<DateTime> get createdAt => $composableBuilder(
|
||||||
column: $table.createdAt,
|
column: $table.createdAt,
|
||||||
builder: (column) => ColumnOrderings(column),
|
builder: (column) => ColumnOrderings(column),
|
||||||
);
|
);
|
||||||
|
|
||||||
$$PlayerTableTableOrderingComposer get winnerId {
|
|
||||||
final $$PlayerTableTableOrderingComposer composer = $composerBuilder(
|
|
||||||
composer: this,
|
|
||||||
getCurrentColumn: (t) => t.winnerId,
|
|
||||||
referencedTable: $db.playerTable,
|
|
||||||
getReferencedColumn: (t) => t.id,
|
|
||||||
builder:
|
|
||||||
(
|
|
||||||
joinBuilder, {
|
|
||||||
$addJoinBuilderToRootComposer,
|
|
||||||
$removeJoinBuilderFromRootComposer,
|
|
||||||
}) => $$PlayerTableTableOrderingComposer(
|
|
||||||
$db: $db,
|
|
||||||
$table: $db.playerTable,
|
|
||||||
$addJoinBuilderToRootComposer: $addJoinBuilderToRootComposer,
|
|
||||||
joinBuilder: joinBuilder,
|
|
||||||
$removeJoinBuilderFromRootComposer:
|
|
||||||
$removeJoinBuilderFromRootComposer,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return composer;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class $$GameTableTableAnnotationComposer
|
class $$GameTableTableAnnotationComposer
|
||||||
@@ -2672,32 +2512,12 @@ class $$GameTableTableAnnotationComposer
|
|||||||
GeneratedColumn<String> get name =>
|
GeneratedColumn<String> get name =>
|
||||||
$composableBuilder(column: $table.name, builder: (column) => column);
|
$composableBuilder(column: $table.name, builder: (column) => column);
|
||||||
|
|
||||||
|
GeneratedColumn<String> get winnerId =>
|
||||||
|
$composableBuilder(column: $table.winnerId, builder: (column) => column);
|
||||||
|
|
||||||
GeneratedColumn<DateTime> get createdAt =>
|
GeneratedColumn<DateTime> get createdAt =>
|
||||||
$composableBuilder(column: $table.createdAt, builder: (column) => column);
|
$composableBuilder(column: $table.createdAt, builder: (column) => column);
|
||||||
|
|
||||||
$$PlayerTableTableAnnotationComposer get winnerId {
|
|
||||||
final $$PlayerTableTableAnnotationComposer composer = $composerBuilder(
|
|
||||||
composer: this,
|
|
||||||
getCurrentColumn: (t) => t.winnerId,
|
|
||||||
referencedTable: $db.playerTable,
|
|
||||||
getReferencedColumn: (t) => t.id,
|
|
||||||
builder:
|
|
||||||
(
|
|
||||||
joinBuilder, {
|
|
||||||
$addJoinBuilderToRootComposer,
|
|
||||||
$removeJoinBuilderFromRootComposer,
|
|
||||||
}) => $$PlayerTableTableAnnotationComposer(
|
|
||||||
$db: $db,
|
|
||||||
$table: $db.playerTable,
|
|
||||||
$addJoinBuilderToRootComposer: $addJoinBuilderToRootComposer,
|
|
||||||
joinBuilder: joinBuilder,
|
|
||||||
$removeJoinBuilderFromRootComposer:
|
|
||||||
$removeJoinBuilderFromRootComposer,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return composer;
|
|
||||||
}
|
|
||||||
|
|
||||||
Expression<T> playerGameTableRefs<T extends Object>(
|
Expression<T> playerGameTableRefs<T extends Object>(
|
||||||
Expression<T> Function($$PlayerGameTableTableAnnotationComposer a) f,
|
Expression<T> Function($$PlayerGameTableTableAnnotationComposer a) f,
|
||||||
) {
|
) {
|
||||||
@@ -2763,7 +2583,6 @@ class $$GameTableTableTableManager
|
|||||||
(GameTableData, $$GameTableTableReferences),
|
(GameTableData, $$GameTableTableReferences),
|
||||||
GameTableData,
|
GameTableData,
|
||||||
PrefetchHooks Function({
|
PrefetchHooks Function({
|
||||||
bool winnerId,
|
|
||||||
bool playerGameTableRefs,
|
bool playerGameTableRefs,
|
||||||
bool groupGameTableRefs,
|
bool groupGameTableRefs,
|
||||||
})
|
})
|
||||||
@@ -2783,7 +2602,7 @@ class $$GameTableTableTableManager
|
|||||||
({
|
({
|
||||||
Value<String> id = const Value.absent(),
|
Value<String> id = const Value.absent(),
|
||||||
Value<String> name = const Value.absent(),
|
Value<String> name = const Value.absent(),
|
||||||
Value<String> winnerId = const Value.absent(),
|
Value<String?> winnerId = const Value.absent(),
|
||||||
Value<DateTime> createdAt = const Value.absent(),
|
Value<DateTime> createdAt = const Value.absent(),
|
||||||
Value<int> rowid = const Value.absent(),
|
Value<int> rowid = const Value.absent(),
|
||||||
}) => GameTableCompanion(
|
}) => GameTableCompanion(
|
||||||
@@ -2797,7 +2616,7 @@ class $$GameTableTableTableManager
|
|||||||
({
|
({
|
||||||
required String id,
|
required String id,
|
||||||
required String name,
|
required String name,
|
||||||
required String winnerId,
|
Value<String?> winnerId = const Value.absent(),
|
||||||
required DateTime createdAt,
|
required DateTime createdAt,
|
||||||
Value<int> rowid = const Value.absent(),
|
Value<int> rowid = const Value.absent(),
|
||||||
}) => GameTableCompanion.insert(
|
}) => GameTableCompanion.insert(
|
||||||
@@ -2816,49 +2635,14 @@ class $$GameTableTableTableManager
|
|||||||
)
|
)
|
||||||
.toList(),
|
.toList(),
|
||||||
prefetchHooksCallback:
|
prefetchHooksCallback:
|
||||||
({
|
({playerGameTableRefs = false, groupGameTableRefs = false}) {
|
||||||
winnerId = false,
|
|
||||||
playerGameTableRefs = false,
|
|
||||||
groupGameTableRefs = false,
|
|
||||||
}) {
|
|
||||||
return PrefetchHooks(
|
return PrefetchHooks(
|
||||||
db: db,
|
db: db,
|
||||||
explicitlyWatchedTables: [
|
explicitlyWatchedTables: [
|
||||||
if (playerGameTableRefs) db.playerGameTable,
|
if (playerGameTableRefs) db.playerGameTable,
|
||||||
if (groupGameTableRefs) db.groupGameTable,
|
if (groupGameTableRefs) db.groupGameTable,
|
||||||
],
|
],
|
||||||
addJoins:
|
addJoins: null,
|
||||||
<
|
|
||||||
T extends TableManagerState<
|
|
||||||
dynamic,
|
|
||||||
dynamic,
|
|
||||||
dynamic,
|
|
||||||
dynamic,
|
|
||||||
dynamic,
|
|
||||||
dynamic,
|
|
||||||
dynamic,
|
|
||||||
dynamic,
|
|
||||||
dynamic,
|
|
||||||
dynamic,
|
|
||||||
dynamic
|
|
||||||
>
|
|
||||||
>(state) {
|
|
||||||
if (winnerId) {
|
|
||||||
state =
|
|
||||||
state.withJoin(
|
|
||||||
currentTable: table,
|
|
||||||
currentColumn: table.winnerId,
|
|
||||||
referencedTable: $$GameTableTableReferences
|
|
||||||
._winnerIdTable(db),
|
|
||||||
referencedColumn: $$GameTableTableReferences
|
|
||||||
._winnerIdTable(db)
|
|
||||||
.id,
|
|
||||||
)
|
|
||||||
as T;
|
|
||||||
}
|
|
||||||
|
|
||||||
return state;
|
|
||||||
},
|
|
||||||
getPrefetchedDataCallback: (items) async {
|
getPrefetchedDataCallback: (items) async {
|
||||||
return [
|
return [
|
||||||
if (playerGameTableRefs)
|
if (playerGameTableRefs)
|
||||||
@@ -2924,7 +2708,6 @@ typedef $$GameTableTableProcessedTableManager =
|
|||||||
(GameTableData, $$GameTableTableReferences),
|
(GameTableData, $$GameTableTableReferences),
|
||||||
GameTableData,
|
GameTableData,
|
||||||
PrefetchHooks Function({
|
PrefetchHooks Function({
|
||||||
bool winnerId,
|
|
||||||
bool playerGameTableRefs,
|
bool playerGameTableRefs,
|
||||||
bool groupGameTableRefs,
|
bool groupGameTableRefs,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
import 'package:drift/drift.dart';
|
import 'package:drift/drift.dart';
|
||||||
import 'package:game_tracker/data/db/tables/player_table.dart';
|
|
||||||
|
|
||||||
class GameTable extends Table {
|
class GameTable extends Table {
|
||||||
TextColumn get id => text()();
|
TextColumn get id => text()();
|
||||||
TextColumn get name => text()();
|
TextColumn get name => text()();
|
||||||
TextColumn get winnerId =>
|
late final winnerId = text().nullable()();
|
||||||
text().references(PlayerTable, #id, onDelete: KeyAction.cascade)();
|
|
||||||
DateTimeColumn get createdAt => dateTime()();
|
DateTimeColumn get createdAt => dateTime()();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -5,11 +5,11 @@ import 'package:uuid/uuid.dart';
|
|||||||
|
|
||||||
class Game {
|
class Game {
|
||||||
final String id;
|
final String id;
|
||||||
|
final DateTime createdAt;
|
||||||
final String name;
|
final String name;
|
||||||
final List<Player>? players;
|
final List<Player>? players;
|
||||||
final Group? group;
|
final Group? group;
|
||||||
final String winner;
|
final String? winner;
|
||||||
final DateTime createdAt;
|
|
||||||
|
|
||||||
Game({
|
Game({
|
||||||
String? id,
|
String? id,
|
||||||
@@ -25,4 +25,27 @@ class Game {
|
|||||||
String toString() {
|
String toString() {
|
||||||
return 'Game{\n\tid: $id,\n\tname: $name,\n\tplayers: $players,\n\tgroup: $group,\n\twinner: $winner\n}';
|
return 'Game{\n\tid: $id,\n\tname: $name,\n\tplayers: $players,\n\tgroup: $group,\n\twinner: $winner\n}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a Game instance from a JSON object.
|
||||||
|
Game.fromJson(Map<String, dynamic> json)
|
||||||
|
: id = json['id'],
|
||||||
|
name = json['name'],
|
||||||
|
createdAt = DateTime.parse(json['createdAt']),
|
||||||
|
players = json['players'] != null
|
||||||
|
? (json['players'] as List)
|
||||||
|
.map((playerJson) => Player.fromJson(playerJson))
|
||||||
|
.toList()
|
||||||
|
: null,
|
||||||
|
group = json['group'] != null ? Group.fromJson(json['group']) : null,
|
||||||
|
winner = json['winner'] ?? '';
|
||||||
|
|
||||||
|
/// Converts the Game instance to a JSON object.
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'id': id,
|
||||||
|
'createdAt': createdAt.toIso8601String(),
|
||||||
|
'name': name,
|
||||||
|
'players': players?.map((player) => player.toJson()).toList(),
|
||||||
|
'group': group?.toJson(),
|
||||||
|
'winner': winner,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import 'package:uuid/uuid.dart';
|
|||||||
|
|
||||||
class Group {
|
class Group {
|
||||||
final String id;
|
final String id;
|
||||||
|
final DateTime createdAt;
|
||||||
final String name;
|
final String name;
|
||||||
final List<Player> members;
|
final List<Player> members;
|
||||||
final DateTime createdAt;
|
|
||||||
|
|
||||||
Group({
|
Group({
|
||||||
String? id,
|
String? id,
|
||||||
@@ -20,4 +20,21 @@ class Group {
|
|||||||
String toString() {
|
String toString() {
|
||||||
return 'Group{id: $id, name: $name,members: $members}';
|
return 'Group{id: $id, name: $name,members: $members}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a Group instance from a JSON object.
|
||||||
|
Group.fromJson(Map<String, dynamic> json)
|
||||||
|
: id = json['id'],
|
||||||
|
createdAt = DateTime.parse(json['createdAt']),
|
||||||
|
name = json['name'],
|
||||||
|
members = (json['members'] as List)
|
||||||
|
.map((memberJson) => Player.fromJson(memberJson))
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
/// Converts the Group instance to a JSON object.
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'id': id,
|
||||||
|
'createdAt': createdAt.toIso8601String(),
|
||||||
|
'name': name,
|
||||||
|
'members': members.map((member) => member.toJson()).toList(),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ import 'package:uuid/uuid.dart';
|
|||||||
|
|
||||||
class Player {
|
class Player {
|
||||||
final String id;
|
final String id;
|
||||||
final String name;
|
|
||||||
final DateTime createdAt;
|
final DateTime createdAt;
|
||||||
|
final String name;
|
||||||
|
|
||||||
Player({String? id, DateTime? createdAt, required this.name})
|
Player({String? id, DateTime? createdAt, required this.name})
|
||||||
: id = id ?? const Uuid().v4(),
|
: id = id ?? const Uuid().v4(),
|
||||||
@@ -14,4 +14,17 @@ class Player {
|
|||||||
String toString() {
|
String toString() {
|
||||||
return 'Player{id: $id,name: $name}';
|
return 'Player{id: $id,name: $name}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a Player instance from a JSON object.
|
||||||
|
Player.fromJson(Map<String, dynamic> json)
|
||||||
|
: id = json['id'],
|
||||||
|
createdAt = DateTime.parse(json['createdAt']),
|
||||||
|
name = json['name'];
|
||||||
|
|
||||||
|
/// Converts the Player instance to a JSON object.
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'id': id,
|
||||||
|
'createdAt': createdAt.toIso8601String(),
|
||||||
|
'name': name,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,12 +17,7 @@ class CustomNavigationBar extends StatefulWidget {
|
|||||||
class _CustomNavigationBarState extends State<CustomNavigationBar>
|
class _CustomNavigationBarState extends State<CustomNavigationBar>
|
||||||
with SingleTickerProviderStateMixin {
|
with SingleTickerProviderStateMixin {
|
||||||
int currentIndex = 0;
|
int currentIndex = 0;
|
||||||
final List<Widget> tabs = [
|
int tabKeyCount = 0;
|
||||||
const HomeView(),
|
|
||||||
const GameHistoryView(),
|
|
||||||
const GroupsView(),
|
|
||||||
const StatisticsView(),
|
|
||||||
];
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -31,6 +26,22 @@ class _CustomNavigationBarState extends State<CustomNavigationBar>
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
// Pretty ugly but works
|
||||||
|
final List<Widget> tabs = [
|
||||||
|
KeyedSubtree(key: ValueKey('home_$tabKeyCount'), child: const HomeView()),
|
||||||
|
KeyedSubtree(
|
||||||
|
key: ValueKey('games_$tabKeyCount'),
|
||||||
|
child: const GameHistoryView(),
|
||||||
|
),
|
||||||
|
KeyedSubtree(
|
||||||
|
key: ValueKey('groups_$tabKeyCount'),
|
||||||
|
child: const GroupsView(),
|
||||||
|
),
|
||||||
|
KeyedSubtree(
|
||||||
|
key: ValueKey('stats_$tabKeyCount'),
|
||||||
|
child: const StatisticsView(),
|
||||||
|
),
|
||||||
|
];
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
centerTitle: true,
|
centerTitle: true,
|
||||||
@@ -42,10 +53,15 @@ class _CustomNavigationBarState extends State<CustomNavigationBar>
|
|||||||
scrolledUnderElevation: 0,
|
scrolledUnderElevation: 0,
|
||||||
actions: [
|
actions: [
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: () => Navigator.push(
|
onPressed: () async {
|
||||||
context,
|
await Navigator.push(
|
||||||
MaterialPageRoute(builder: (_) => const SettingsView()),
|
context,
|
||||||
),
|
MaterialPageRoute(builder: (_) => const SettingsView()),
|
||||||
|
);
|
||||||
|
setState(() {
|
||||||
|
tabKeyCount++;
|
||||||
|
});
|
||||||
|
},
|
||||||
icon: const Icon(Icons.settings),
|
icon: const Icon(Icons.settings),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -1,13 +1,191 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:game_tracker/core/custom_theme.dart';
|
||||||
|
import 'package:game_tracker/core/enums.dart';
|
||||||
|
import 'package:game_tracker/presentation/widgets/tiles/settings_list_tile.dart';
|
||||||
|
import 'package:game_tracker/services/data_transfer_service.dart';
|
||||||
|
|
||||||
class SettingsView extends StatelessWidget {
|
class SettingsView extends StatefulWidget {
|
||||||
const SettingsView({super.key});
|
const SettingsView({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<SettingsView> createState() => _SettingsViewState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _SettingsViewState extends State<SettingsView> {
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(title: const Text('Einstellungen')),
|
appBar: AppBar(backgroundColor: CustomTheme.backgroundColor),
|
||||||
body: const Center(child: Text('Settings View')),
|
backgroundColor: CustomTheme.backgroundColor,
|
||||||
|
body: LayoutBuilder(
|
||||||
|
builder: (BuildContext context, BoxConstraints constraints) =>
|
||||||
|
SingleChildScrollView(
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.start,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
const Padding(
|
||||||
|
padding: EdgeInsets.fromLTRB(24, 0, 24, 10),
|
||||||
|
child: Text(
|
||||||
|
textAlign: TextAlign.start,
|
||||||
|
'Menu',
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 28,
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const Padding(
|
||||||
|
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 10),
|
||||||
|
child: Text(
|
||||||
|
textAlign: TextAlign.start,
|
||||||
|
'Settings',
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 22,
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SettingsListTile(
|
||||||
|
title: 'Export data',
|
||||||
|
icon: Icons.upload_outlined,
|
||||||
|
suffixWidget: const Icon(Icons.arrow_forward_ios, size: 16),
|
||||||
|
onPressed: () async {
|
||||||
|
final String json =
|
||||||
|
await DataTransferService.getAppDataAsJson(context);
|
||||||
|
final result = await DataTransferService.exportData(
|
||||||
|
json,
|
||||||
|
'game_tracker-data',
|
||||||
|
);
|
||||||
|
if (!context.mounted) return;
|
||||||
|
showExportSnackBar(context: context, result: result);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
SettingsListTile(
|
||||||
|
title: 'Import data',
|
||||||
|
icon: Icons.download_outlined,
|
||||||
|
suffixWidget: const Icon(Icons.arrow_forward_ios, size: 16),
|
||||||
|
onPressed: () async {
|
||||||
|
final result = await DataTransferService.importData(
|
||||||
|
context,
|
||||||
|
);
|
||||||
|
if (!context.mounted) return;
|
||||||
|
showImportSnackBar(context: context, result: result);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
SettingsListTile(
|
||||||
|
title: 'Delete all data',
|
||||||
|
icon: Icons.download_outlined,
|
||||||
|
suffixWidget: const Icon(Icons.arrow_forward_ios, size: 16),
|
||||||
|
onPressed: () {
|
||||||
|
showDialog<bool>(
|
||||||
|
context: context,
|
||||||
|
builder: (context) => AlertDialog(
|
||||||
|
title: const Text('Delete all data?'),
|
||||||
|
content: const Text('This can\'t be undone'),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.of(context).pop(false),
|
||||||
|
child: const Text('Abbrechen'),
|
||||||
|
),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.of(context).pop(true),
|
||||||
|
child: const Text('Löschen'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
).then((confirmed) {
|
||||||
|
if (confirmed == true && context.mounted) {
|
||||||
|
DataTransferService.deleteAllData(context);
|
||||||
|
showSnackbar(
|
||||||
|
context: context,
|
||||||
|
message: 'Daten erfolgreich gelöscht',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Displays a snackbar based on the import result.
|
||||||
|
///
|
||||||
|
/// [context] The BuildContext to show the snackbar in.
|
||||||
|
/// [result] The result of the import operation.
|
||||||
|
void showImportSnackBar({
|
||||||
|
required BuildContext context,
|
||||||
|
required ImportResult result,
|
||||||
|
}) {
|
||||||
|
switch (result) {
|
||||||
|
case ImportResult.success:
|
||||||
|
showSnackbar(context: context, message: 'Data successfully imported');
|
||||||
|
case ImportResult.invalidSchema:
|
||||||
|
showSnackbar(context: context, message: 'Invalid Schema');
|
||||||
|
case ImportResult.fileReadError:
|
||||||
|
showSnackbar(context: context, message: 'Error reading file');
|
||||||
|
case ImportResult.canceled:
|
||||||
|
showSnackbar(context: context, message: 'Import canceled');
|
||||||
|
case ImportResult.formatException:
|
||||||
|
showSnackbar(
|
||||||
|
context: context,
|
||||||
|
message: 'Format Exception (see console)',
|
||||||
|
);
|
||||||
|
case ImportResult.unknownException:
|
||||||
|
showSnackbar(
|
||||||
|
context: context,
|
||||||
|
message: 'Unknown Exception (see console)',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Displays a snackbar based on the export result.
|
||||||
|
///
|
||||||
|
/// [context] The BuildContext to show the snackbar in.
|
||||||
|
/// [result] The result of the export operation.
|
||||||
|
void showExportSnackBar({
|
||||||
|
required BuildContext context,
|
||||||
|
required ExportResult result,
|
||||||
|
}) {
|
||||||
|
switch (result) {
|
||||||
|
case ExportResult.success:
|
||||||
|
showSnackbar(context: context, message: 'Data successfully exported');
|
||||||
|
case ExportResult.canceled:
|
||||||
|
showSnackbar(context: context, message: 'Export canceled');
|
||||||
|
case ExportResult.unknownException:
|
||||||
|
showSnackbar(
|
||||||
|
context: context,
|
||||||
|
message: 'Unknown Exception (see console)',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Displays a snackbar with the given message and optional action.
|
||||||
|
///
|
||||||
|
/// [context] The BuildContext to show the snackbar in.
|
||||||
|
/// [message] The message to display in the snackbar.
|
||||||
|
/// [duration] The duration for which the snackbar is displayed.
|
||||||
|
/// [action] An optional callback function to execute when the action button is pressed.
|
||||||
|
void showSnackbar({
|
||||||
|
required BuildContext context,
|
||||||
|
required String message,
|
||||||
|
Duration duration = const Duration(seconds: 3),
|
||||||
|
VoidCallback? action,
|
||||||
|
}) {
|
||||||
|
final messenger = ScaffoldMessenger.of(context);
|
||||||
|
messenger.hideCurrentSnackBar();
|
||||||
|
messenger.showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(message, style: const TextStyle(color: Colors.white)),
|
||||||
|
backgroundColor: CustomTheme.onBoxColor,
|
||||||
|
duration: duration,
|
||||||
|
action: action != null
|
||||||
|
? SnackBarAction(label: 'Rückgängig', onPressed: action)
|
||||||
|
: null,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
64
lib/presentation/widgets/tiles/settings_list_tile.dart
Normal file
64
lib/presentation/widgets/tiles/settings_list_tile.dart
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:game_tracker/core/custom_theme.dart';
|
||||||
|
|
||||||
|
class SettingsListTile extends StatelessWidget {
|
||||||
|
final VoidCallback? onPressed;
|
||||||
|
final IconData icon;
|
||||||
|
final String title;
|
||||||
|
final Widget? suffixWidget;
|
||||||
|
const SettingsListTile({
|
||||||
|
super.key,
|
||||||
|
required this.title,
|
||||||
|
required this.icon,
|
||||||
|
this.suffixWidget,
|
||||||
|
this.onPressed,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||||
|
child: Center(
|
||||||
|
child: SizedBox(
|
||||||
|
width: MediaQuery.of(context).size.width * 0.95,
|
||||||
|
child: GestureDetector(
|
||||||
|
onTap: onPressed ?? () {},
|
||||||
|
child: Container(
|
||||||
|
margin: EdgeInsets.zero,
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 14),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: CustomTheme.boxColor,
|
||||||
|
border: Border.all(color: CustomTheme.boxBorder),
|
||||||
|
borderRadius: BorderRadius.circular(12),
|
||||||
|
),
|
||||||
|
child: Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
|
children: [
|
||||||
|
Row(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
Container(
|
||||||
|
padding: const EdgeInsets.all(8),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: CustomTheme.primaryColor,
|
||||||
|
shape: BoxShape.circle,
|
||||||
|
),
|
||||||
|
child: Icon(icon, size: 24),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 16),
|
||||||
|
Text(title, style: const TextStyle(fontSize: 18)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
if (suffixWidget != null)
|
||||||
|
suffixWidget!
|
||||||
|
else
|
||||||
|
const SizedBox.shrink(),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
161
lib/services/data_transfer_service.dart
Normal file
161
lib/services/data_transfer_service.dart
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:file_picker/file_picker.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:game_tracker/core/enums.dart';
|
||||||
|
import 'package:game_tracker/data/db/database.dart';
|
||||||
|
import 'package:game_tracker/data/dto/game.dart';
|
||||||
|
import 'package:game_tracker/data/dto/group.dart';
|
||||||
|
import 'package:game_tracker/data/dto/player.dart';
|
||||||
|
import 'package:json_schema/json_schema.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
class DataTransferService {
|
||||||
|
/// Deletes all data from the database.
|
||||||
|
static Future<void> deleteAllData(BuildContext context) async {
|
||||||
|
final db = Provider.of<AppDatabase>(context, listen: false);
|
||||||
|
await db.gameDao.deleteAllGames();
|
||||||
|
await db.groupDao.deleteAllGroups();
|
||||||
|
await db.playerDao.deleteAllPlayers();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Retrieves all application data and converts it to a JSON string.
|
||||||
|
/// Returns the JSON string representation of the data.
|
||||||
|
static Future<String> getAppDataAsJson(BuildContext context) async {
|
||||||
|
final db = Provider.of<AppDatabase>(context, listen: false);
|
||||||
|
final games = await db.gameDao.getAllGames();
|
||||||
|
final groups = await db.groupDao.getAllGroups();
|
||||||
|
final players = await db.playerDao.getAllPlayers();
|
||||||
|
|
||||||
|
// Construct a JSON representation of the data
|
||||||
|
final Map<String, dynamic> jsonMap = {
|
||||||
|
'games': games.map((game) => game.toJson()).toList(),
|
||||||
|
'groups': groups.map((group) => group.toJson()).toList(),
|
||||||
|
'players': players.map((player) => player.toJson()).toList(),
|
||||||
|
};
|
||||||
|
|
||||||
|
return json.encode(jsonMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Exports the given JSON string to a file with the specified name.
|
||||||
|
/// Returns an [ExportResult] indicating the outcome.
|
||||||
|
///
|
||||||
|
/// [jsonString] The JSON string to be exported.
|
||||||
|
/// [fileName] The desired name for the exported file (without extension).
|
||||||
|
static Future<ExportResult> exportData(
|
||||||
|
String jsonString,
|
||||||
|
String fileName,
|
||||||
|
) async {
|
||||||
|
try {
|
||||||
|
final bytes = Uint8List.fromList(utf8.encode(jsonString));
|
||||||
|
final path = await FilePicker.platform.saveFile(
|
||||||
|
fileName: '$fileName.json',
|
||||||
|
bytes: bytes,
|
||||||
|
);
|
||||||
|
if (path == null) {
|
||||||
|
return ExportResult.canceled;
|
||||||
|
} else {
|
||||||
|
return ExportResult.success;
|
||||||
|
}
|
||||||
|
} catch (e, stack) {
|
||||||
|
print('[exportData] $e');
|
||||||
|
print(stack);
|
||||||
|
return ExportResult.unknownException;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Imports data from a selected JSON file into the database.
|
||||||
|
static Future<ImportResult> importData(BuildContext context) async {
|
||||||
|
final db = Provider.of<AppDatabase>(context, listen: false);
|
||||||
|
|
||||||
|
final path = await FilePicker.platform.pickFiles(
|
||||||
|
type: FileType.custom,
|
||||||
|
allowedExtensions: ['json'],
|
||||||
|
);
|
||||||
|
|
||||||
|
if (path == null) {
|
||||||
|
return ImportResult.canceled;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
final jsonString = await _readFileContent(path.files.single);
|
||||||
|
if (jsonString == null) {
|
||||||
|
return ImportResult.fileReadError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (await _validateJsonSchema(jsonString)) {
|
||||||
|
final Map<String, dynamic> jsonData =
|
||||||
|
json.decode(jsonString) as Map<String, dynamic>;
|
||||||
|
|
||||||
|
final List<dynamic>? gamesJson = jsonData['games'] as List<dynamic>?;
|
||||||
|
final List<dynamic>? groupsJson = jsonData['groups'] as List<dynamic>?;
|
||||||
|
final List<dynamic>? playersJson =
|
||||||
|
jsonData['players'] as List<dynamic>?;
|
||||||
|
|
||||||
|
final List<Game> importedGames =
|
||||||
|
gamesJson
|
||||||
|
?.map((g) => Game.fromJson(g as Map<String, dynamic>))
|
||||||
|
.toList() ??
|
||||||
|
[];
|
||||||
|
final List<Group> importedGroups =
|
||||||
|
groupsJson
|
||||||
|
?.map((g) => Group.fromJson(g as Map<String, dynamic>))
|
||||||
|
.toList() ??
|
||||||
|
[];
|
||||||
|
final List<Player> importedPlayers =
|
||||||
|
playersJson
|
||||||
|
?.map((p) => Player.fromJson(p as Map<String, dynamic>))
|
||||||
|
.toList() ??
|
||||||
|
[];
|
||||||
|
|
||||||
|
await db.playerDao.addPlayers(players: importedPlayers);
|
||||||
|
await db.groupDao.addGroups(groups: importedGroups);
|
||||||
|
await db.gameDao.addGames(games: importedGames);
|
||||||
|
} else {
|
||||||
|
return ImportResult.invalidSchema;
|
||||||
|
}
|
||||||
|
return ImportResult.success;
|
||||||
|
} on FormatException catch (e, stack) {
|
||||||
|
print('[importData] FormatException');
|
||||||
|
print('[importData] $e');
|
||||||
|
print(stack);
|
||||||
|
return ImportResult.formatException;
|
||||||
|
} on Exception catch (e, stack) {
|
||||||
|
print('[importData] Exception');
|
||||||
|
print('[importData] $e');
|
||||||
|
print(stack);
|
||||||
|
return ImportResult.unknownException;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Helper method to read file content from either bytes or path
|
||||||
|
static Future<String?> _readFileContent(PlatformFile file) async {
|
||||||
|
if (file.bytes != null) return utf8.decode(file.bytes!);
|
||||||
|
if (file.path != null) return await File(file.path!).readAsString();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validates the given JSON string against the predefined schema.
|
||||||
|
static Future<bool> _validateJsonSchema(String jsonString) async {
|
||||||
|
final String schemaString;
|
||||||
|
|
||||||
|
schemaString = await rootBundle.loadString('assets/schema.json');
|
||||||
|
|
||||||
|
try {
|
||||||
|
final schema = JsonSchema.create(json.decode(schemaString));
|
||||||
|
final jsonData = json.decode(jsonString);
|
||||||
|
final result = schema.validate(jsonData);
|
||||||
|
|
||||||
|
if (result.isValid) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
} catch (e, stack) {
|
||||||
|
print('[validateJsonSchema] $e');
|
||||||
|
print(stack);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,6 +20,9 @@ dependencies:
|
|||||||
provider: ^6.1.5
|
provider: ^6.1.5
|
||||||
skeletonizer: ^2.1.0+1
|
skeletonizer: ^2.1.0+1
|
||||||
uuid: ^4.5.2
|
uuid: ^4.5.2
|
||||||
|
file_picker: ^10.3.6
|
||||||
|
json_schema: ^5.2.2
|
||||||
|
file_saver: ^0.3.1
|
||||||
clock: ^1.1.2
|
clock: ^1.1.2
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
@@ -31,3 +34,5 @@ dev_dependencies:
|
|||||||
|
|
||||||
flutter:
|
flutter:
|
||||||
uses-material-design: true
|
uses-material-design: true
|
||||||
|
assets:
|
||||||
|
- assets/schema.json
|
||||||
|
|||||||
@@ -102,11 +102,9 @@ void main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('Adding and fetching multiple games works correctly', () async {
|
test('Adding and fetching multiple games works correctly', () async {
|
||||||
// TODO: Use upcoming addGames() method
|
await database.gameDao.addGames(
|
||||||
await database.gameDao.addGame(game: testGame1);
|
games: [testGame1, testGame2, testGameOnlyGroup, testGameOnlyPlayers],
|
||||||
await database.gameDao.addGame(game: testGame2);
|
);
|
||||||
await database.gameDao.addGame(game: testGameOnlyGroup);
|
|
||||||
await database.gameDao.addGame(game: testGameOnlyPlayers);
|
|
||||||
|
|
||||||
final allGames = await database.gameDao.getAllGames();
|
final allGames = await database.gameDao.getAllGames();
|
||||||
expect(allGames.length, 4);
|
expect(allGames.length, 4);
|
||||||
|
|||||||
@@ -81,11 +81,9 @@ void main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('Adding and fetching multiple groups works correctly', () async {
|
test('Adding and fetching multiple groups works correctly', () async {
|
||||||
// TODO: Use upcoming addGroups() method
|
await database.groupDao.addGroups(
|
||||||
await database.groupDao.addGroup(group: testGroup1);
|
groups: [testGroup1, testGroup2, testGroup3, testGroup4],
|
||||||
await database.groupDao.addGroup(group: testGroup2);
|
);
|
||||||
await database.groupDao.addGroup(group: testGroup3);
|
|
||||||
await database.groupDao.addGroup(group: testGroup4);
|
|
||||||
|
|
||||||
final allGroups = await database.groupDao.getAllGroups();
|
final allGroups = await database.groupDao.getAllGroups();
|
||||||
expect(allGroups.length, 2);
|
expect(allGroups.length, 2);
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ void main() {
|
|||||||
|
|
||||||
expect(gameHasPlayers, false);
|
expect(gameHasPlayers, false);
|
||||||
|
|
||||||
database.playerGameDao.addPlayerToGame(
|
await database.playerGameDao.addPlayerToGame(
|
||||||
gameId: testGameOnlyGroup.id,
|
gameId: testGameOnlyGroup.id,
|
||||||
playerId: testPlayer1.id,
|
playerId: testPlayer1.id,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -56,11 +56,9 @@ void main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('Adding and fetching multiple players works correctly', () async {
|
test('Adding and fetching multiple players works correctly', () async {
|
||||||
// TODO: Use upcoming addPlayers() method
|
await database.playerDao.addPlayers(
|
||||||
await database.playerDao.addPlayer(player: testPlayer1);
|
players: [testPlayer1, testPlayer2, testPlayer3, testPlayer4],
|
||||||
await database.playerDao.addPlayer(player: testPlayer2);
|
);
|
||||||
await database.playerDao.addPlayer(player: testPlayer3);
|
|
||||||
await database.playerDao.addPlayer(player: testPlayer4);
|
|
||||||
|
|
||||||
final allPlayers = await database.playerDao.getAllPlayers();
|
final allPlayers = await database.playerDao.getAllPlayers();
|
||||||
expect(allPlayers.length, 4);
|
expect(allPlayers.length, 4);
|
||||||
|
|||||||
Reference in New Issue
Block a user