Skip to content

Commit

Permalink
feat: support filtering games offline
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauritz8 committed Aug 5, 2024
1 parent d418bd8 commit 09e1251
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/src/model/game/game_filter.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:chessground/chessground.dart';
import 'package:dartchess/dartchess.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:lichess_mobile/src/model/common/perf.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/game/game_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class UserGameHistory extends _$UserGameHistory {
? ref.withClient(
(client) => GameRepository(client).getUserGames(id, filter: filter),
)
: storage.page(userId: id, max: kNumberOfRecentGames).then(
: storage.page(userId: id, filter: filter).then(
(value) => value
// we can assume that `youAre` is not null either for logged
// in users or for anonymous users
Expand Down
37 changes: 23 additions & 14 deletions lib/src/model/game/game_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:lichess_mobile/src/db/database.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/game/archived_game.dart';
import 'package:lichess_mobile/src/model/game/game_filter.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:sqflite/sqflite.dart';

Expand Down Expand Up @@ -44,6 +45,7 @@ class GameStorage {
UserId? userId,
DateTime? until,
int max = 20,
GameFilterState filter = const GameFilterState(),
}) async {
final list = await _db.query(
kGameStorageTable,
Expand All @@ -59,20 +61,27 @@ class GameStorage {
limit: max,
);

return list.map((e) {
final raw = e['data']! as String;
final json = jsonDecode(raw);
if (json is! Map<String, dynamic>) {
throw const FormatException(
'[GameStorage] cannot fetch game: expected an object',
);
}
return (
userId: UserId(e['userId']! as String),
lastModified: DateTime.parse(e['lastModified']! as String),
game: ArchivedGame.fromJson(json),
);
}).toIList();
return list
.map((e) {
final raw = e['data']! as String;
final json = jsonDecode(raw);
if (json is! Map<String, dynamic>) {
throw const FormatException(
'[GameStorage] cannot fetch game: expected an object',
);
}
return (
userId: UserId(e['userId']! as String),
lastModified: DateTime.parse(e['lastModified']! as String),
game: ArchivedGame.fromJson(json),
);
})
.where(
(e) =>
filter.perfs.isEmpty || filter.perfs.contains(e.game.meta.perf),
)
.where((e) => filter.side == null || filter.side == e.game.youAre)
.toIList();
}

Future<ArchivedGame?> fetch({
Expand Down
2 changes: 1 addition & 1 deletion lib/src/view/user/game_history_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:chessground/chessground.dart';
import 'package:dartchess/dartchess.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand Down
2 changes: 2 additions & 0 deletions test/model/game/mock_game_storage.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/game/archived_game.dart';
import 'package:lichess_mobile/src/model/game/game_filter.dart';
import 'package:lichess_mobile/src/model/game/game_storage.dart';

class MockGameStorage implements GameStorage {
Expand All @@ -19,6 +20,7 @@ class MockGameStorage implements GameStorage {
UserId? userId,
DateTime? until,
int max = 10,
GameFilterState filter = const GameFilterState(),
}) {
return Future.value(IList());
}
Expand Down

0 comments on commit 09e1251

Please sign in to comment.