Skip to content

Commit

Permalink
Fix getting new opponent from lobby screen
Browse files Browse the repository at this point in the history
Fixes #674
  • Loading branch information
veloce committed May 13, 2024
1 parent e1d87c8 commit 30db855
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
7 changes: 3 additions & 4 deletions lib/src/model/lobby/create_game_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CreateGameService {
final socketClient = socketPool.open(Uri(path: '/lobby/socket/v5'));

// ensure the pending game connection is closed in any case
final completer = Completer<GameFullId>()..future.whenComplete(_close);
final completer = Completer<GameFullId>()..future.whenComplete(dispose);

_pendingGameConnection = socketClient.stream.listen((event) {
if (event.topic == 'redirect') {
Expand Down Expand Up @@ -93,14 +93,13 @@ class CreateGameService {
final sri = ref.read(sriProvider);
try {
await LobbyRepository(lichessClient).cancelSeek(sri: sri);
_close();
} catch (e) {
_log.warning('Failed to cancel seek: $e', e);
}
}

void _close() {
// cancel the socket subscription
/// Dispose the service.
void dispose() {
_pendingGameConnection?.cancel();
_pendingGameConnection = null;
}
Expand Down
10 changes: 8 additions & 2 deletions lib/src/view/game/game_loading_board.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import 'package:lichess_mobile/src/widgets/bottom_bar_button.dart';
import 'package:lichess_mobile/src/widgets/platform.dart';

class LobbyScreenLoadingContent extends StatelessWidget {
const LobbyScreenLoadingContent(this.seek);
const LobbyScreenLoadingContent(this.seek, this.cancelGameCreation);

final GameSeek seek;
final Future<void> Function() cancelGameCreation;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -77,7 +78,12 @@ class LobbyScreenLoadingContent extends StatelessWidget {
_BottomBar(
children: [
BottomBarButton(
onTap: () => Navigator.of(context, rootNavigator: true).pop(),
onTap: () async {
await cancelGameCreation();
if (context.mounted) {
Navigator.of(context, rootNavigator: true).pop();
}
},
label: context.l10n.cancel,
showLabel: true,
icon: CupertinoIcons.xmark,
Expand Down
17 changes: 11 additions & 6 deletions lib/src/view/game/lobby_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ part 'lobby_screen.g.dart';
class _LobbyGame extends _$LobbyGame {
@override
Future<(GameFullId, {bool fromRematch})> build(GameSeek seek) {
final service = ref.read(createGameServiceProvider);
ref.onDispose(() {
_service.cancel();
service.dispose();
});
return _service.newLobbyGame(seek).then((id) => (id, fromRematch: false));
return service.newLobbyGame(seek).then((id) => (id, fromRematch: false));
}

void rematch(GameFullId id) {
state = AsyncValue.data((id, fromRematch: true));
}

CreateGameService get _service => ref.read(createGameServiceProvider);
}

/// Screen for games created from the lobby.
Expand Down Expand Up @@ -117,15 +116,21 @@ class _LobbyScreenState extends ConsumerState<LobbyScreen> with RouteAware {
appBar: GameAppBar(seek: widget.seek),
body: PopScope(
canPop: false,
child: LobbyScreenLoadingContent(widget.seek),
child: LobbyScreenLoadingContent(
widget.seek,
() => ref.read(createGameServiceProvider).cancel(),
),
),
),
iosBuilder: (context) => CupertinoPageScaffold(
resizeToAvoidBottomInset: false,
navigationBar: GameCupertinoNavBar(seek: widget.seek),
child: PopScope(
canPop: false,
child: LobbyScreenLoadingContent(widget.seek),
child: LobbyScreenLoadingContent(
widget.seek,
() => ref.read(createGameServiceProvider).cancel(),
),
),
),
),
Expand Down

0 comments on commit 30db855

Please sign in to comment.