Skip to content

Commit 1f31e94

Browse files
committed
fix chat messages disappearing when a move is made
Closes #2495 Closes #2781
1 parent e8b2cb4 commit 1f31e94

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

lib/src/model/chat/chat_controller.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,20 @@ class ChatController extends AsyncNotifier<ChatState> {
111111
_subscription?.cancel();
112112
});
113113

114+
// Do NOT use ref.read() here, since we just need to get the initial messages.
115+
// The game/tournament/study controllers do NOT update their `chat` fields when a new
116+
// chat message is received, instead the ChatController itselfs listens to this socket event.
117+
// This means that with ref.watch(), whenever the game/tournament/study controller updates,
118+
// (e.g. when a new move has been made), the `ChatController` would rebuild and we'd lose
119+
// any chat messages that have been received since the last build.
114120
final initialMessages = switch (options) {
115-
GameChatOptions(:final id) => (await ref.watch(
121+
GameChatOptions(:final id) => (await ref.read(
116122
gameControllerProvider(id).future,
117123
)).game.chat?.lines,
118-
TournamentChatOptions(:final id) => (await ref.watch(
124+
TournamentChatOptions(:final id) => (await ref.read(
119125
tournamentControllerProvider(id).future,
120126
)).tournament.chat?.lines,
121-
StudyChatOptions(:final id) => (await ref.watch(
127+
StudyChatOptions(:final id) => (await ref.read(
122128
studyControllerProvider(id).future,
123129
)).study.chat?.lines,
124130
};

test/view/game/game_screen_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import 'package:lichess_mobile/src/model/settings/preferences_storage.dart';
2323
import 'package:lichess_mobile/src/network/http.dart';
2424
import 'package:lichess_mobile/src/network/socket.dart';
2525
import 'package:lichess_mobile/src/styles/lichess_icons.dart';
26+
import 'package:lichess_mobile/src/view/chat/chat_screen.dart';
2627
import 'package:lichess_mobile/src/view/game/game_screen.dart';
2728
import 'package:lichess_mobile/src/view/game/game_screen_providers.dart';
2829
import 'package:lichess_mobile/src/widgets/bottom_bar.dart';
@@ -963,6 +964,25 @@ void main() {
963964
() => mockSoundService.play(Sound.confirmation, volume: any(named: 'volume')),
964965
).called(1);
965966
});
967+
968+
testWidgets('chat messages do not disappear when game state changes', (
969+
WidgetTester tester,
970+
) async {
971+
await createTestGame(tester, pgn: 'e4 e5');
972+
sendServerSocketMessages(testGameSocketUri, [
973+
'{"t":"message","d":{"u":"Steven","t":"Hello!"}}',
974+
]);
975+
await tester.pump();
976+
977+
// Play a move to update the GameController's state.
978+
// There used to be a bug where this would make chat messages disappear.
979+
await playMove(tester, 'g1', 'f3');
980+
981+
await tester.tap(find.byType(ChatBottomBarButton));
982+
await tester.pumpAndSettle(); // wait for chat to open
983+
984+
expect(find.text('Hello!'), findsOneWidget);
985+
});
966986
});
967987

968988
group('Disabled', () {

0 commit comments

Comments
 (0)