Skip to content

Commit

Permalink
Force to run the splash initialization when user wants to open a link…
Browse files Browse the repository at this point in the history
… (such as the lobby deep link)
  • Loading branch information
imaNNeo committed Nov 13, 2024
1 parent a3f2627 commit ce28e46
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 71 deletions.
2 changes: 1 addition & 1 deletion lib/data/remote/nakama_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class NakamaDataSource {
session: _currentSession,
id: 'get_waiting_match',
);
if (matchId == null) {
if (matchId == null || matchId.isBlank) {
throw Exception('Failed to get match id');
}
return matchId;
Expand Down
6 changes: 6 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flappy_dash/presentation/bloc/leaderboard/leaderboard_cubit.dart
import 'package:flappy_dash/presentation/bloc/multiplayer/multiplayer_cubit.dart';
import 'package:flappy_dash/presentation/bloc/multiplayer/ping/ping_cubit.dart';
import 'package:flappy_dash/presentation/pages/debug/debug_panel.dart';
import 'package:flappy_dash/presentation/pages/splash/cubit/splash_cubit.dart';
import 'package:flappy_dash/service_locator.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -67,6 +68,11 @@ class MyApp extends StatelessWidget {
),
lazy: false,
),
BlocProvider(
create: (context) => SplashCubit(
getIt.get<GameRepository>(),
),
),
],
child: MaterialApp.router(
routerConfig: AppRoutes.router,
Expand Down
76 changes: 44 additions & 32 deletions lib/presentation/app_routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import 'package:flappy_dash/presentation/pages/home/home_page.dart';
import 'package:flappy_dash/presentation/pages/multiplayer/game/multiplayer_game_page.dart';
import 'package:flappy_dash/presentation/pages/multiplayer/lobby/multiplayer_lobby_page.dart';
import 'package:flappy_dash/presentation/pages/singleplayer/singleplayer_game_page.dart';
import 'package:flappy_dash/presentation/pages/splash/cubit/splash_cubit.dart';
import 'package:flappy_dash/presentation/pages/splash/splash_page.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';

import 'pages/multiplayer/result/match_result_page.dart';
Expand All @@ -13,44 +15,54 @@ class AppRoutes {
routes: [
GoRoute(
path: '/splash',
builder: (context, state) => const SplashPage(),
builder: (context, state) {
final redirectTo = state.uri.queryParameters['redirectTo'];
return SplashPage(redirectTo: redirectTo);
},
),
GoRoute(
path: '/',
builder: (context, state) => const HomePage(),
routes: [
GoRoute(
path: 'single_player',
builder: (context, state) => const SinglePlayerGamePage(),
),
GoRoute(
path: 'lobby/:matchId',
builder: (context, state) {
return MultiPlayerLobbyPage(
matchId: state.pathParameters['matchId']!,
);
},
),
GoRoute(
path: 'multi_player/:matchId',
builder: (context, state) {
return MultiPlayerGamePage(
matchId: state.pathParameters['matchId']!,
);
},
routes: [
GoRoute(
path: 'result',
path: '/',
builder: (context, state) => const HomePage(),
routes: [
GoRoute(
path: 'single_player',
builder: (context, state) => const SinglePlayerGamePage(),
),
GoRoute(
path: 'lobby/:matchId',
builder: (context, state) {
return MultiPlayerLobbyPage(
matchId: state.pathParameters['matchId']!,
);
},
),
GoRoute(
path: 'multi_player/:matchId',
builder: (context, state) {
return MatchResultPage(
return MultiPlayerGamePage(
matchId: state.pathParameters['matchId']!,
);
},
),
]
),
]
),
routes: [
GoRoute(
path: 'result',
builder: (context, state) {
return MatchResultPage(
matchId: state.pathParameters['matchId']!,
);
},
),
]),
]),
],
redirect: (context, state) {
final splashCubit = context.read<SplashCubit>();
final splashInitialized = splashCubit.state.isSplashInitialized;
if (!splashInitialized && state.uri.path != '/splash') {
final encodedPath = Uri.encodeComponent(state.uri.toString());
return '/splash?redirectTo=$encodedPath';
}
return null;
},
);
}
17 changes: 8 additions & 9 deletions lib/presentation/pages/home/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (BuildContext context) =>
HomeCubit(
getIt.get<MultiplayerRepository>(),
),
create: (BuildContext context) => HomeCubit(
getIt.get<MultiplayerRepository>(),
),
child: const HomePageContent(),
);
}
Expand Down Expand Up @@ -83,9 +82,9 @@ class HomePageContent extends StatelessWidget {
),
const SizedBox(height: 18),
MultiPlayerButton(
onPressed: () {
context.read<HomeCubit>().onMultiPlayerButtonPressed();
},
onPressed: context
.read<HomeCubit>()
.onMultiPlayerButtonPressed,
showLoading: state.multiPlayerMatchIdLoading,
),
const SizedBox(height: 8),
Expand Down Expand Up @@ -114,8 +113,8 @@ class HomePageContent extends StatelessWidget {
),
Expanded(
child: Container(
height: 0,
)),
height: 0,
)),
const ProfileOverlay(),
const SizedBox(
width: PresentationConstants.defaultPadding,
Expand Down
8 changes: 5 additions & 3 deletions lib/presentation/pages/splash/cubit/splash_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ class SplashCubit extends Cubit<SplashState> {
if (difference < _splashDuration) {
await Future.delayed(_splashDuration - difference);
}

emit(state.copyWith(
isSplashInitialized: true,
));
_openHomePage();
} catch (e, stack) {
print('error: $e, $stack');
}
}

void _openHomePage() async {
emit(state.copyWith(openHomePage: true));
emit(state.copyWith(openHomePage: false));
emit(state.copyWith(openTheNextPage: true));
emit(state.copyWith(openTheNextPage: false));
}
}
15 changes: 10 additions & 5 deletions lib/presentation/pages/splash/cubit/splash_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@ part of 'splash_cubit.dart';
class SplashState with EquatableMixin {
const SplashState({
this.isLoading = false,
this.openHomePage = false,
this.openTheNextPage = false,
this.isSplashInitialized = false,
});

final bool isLoading;
final bool openHomePage;
final bool openTheNextPage;
final bool isSplashInitialized;

SplashState copyWith({
bool? isLoading,
bool? openHomePage,
bool? openTheNextPage,
bool? isSplashInitialized,
}) =>
SplashState(
isLoading: isLoading ?? this.isLoading,
openHomePage: openHomePage ?? this.openHomePage,
openTheNextPage: openTheNextPage ?? this.openTheNextPage,
isSplashInitialized: isSplashInitialized ?? this.isSplashInitialized,
);

@override
List<Object> get props => [
isLoading,
openHomePage,
openTheNextPage,
isSplashInitialized,
];
}
33 changes: 12 additions & 21 deletions lib/presentation/pages/splash/splash_page.dart
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
import 'package:flappy_dash/domain/repositories/game_repository.dart';
import 'package:flappy_dash/service_locator.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';

import 'cubit/splash_cubit.dart';

class SplashPage extends StatelessWidget {
const SplashPage({super.key});
class SplashPage extends StatefulWidget {
const SplashPage({super.key, this.redirectTo});

@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => SplashCubit(getIt.get<GameRepository>()),
child: const SplashPageContent(),
);
}
}

class SplashPageContent extends StatefulWidget {
const SplashPageContent({super.key});
final String? redirectTo;

@override
State<SplashPageContent> createState() => _SplashPageContentState();
State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageContentState extends State<SplashPageContent> {

class _SplashPageState extends State<SplashPage> {
@override
void initState() {
context.read<SplashCubit>().onPageOpen();
Expand All @@ -37,8 +24,12 @@ class _SplashPageContentState extends State<SplashPageContent> {
Widget build(BuildContext context) {
return BlocConsumer<SplashCubit, SplashState>(
listener: (context, state) {
if (state.openHomePage) {
GoRouter.of(context).replace('/');
if (state.openTheNextPage) {
if (widget.redirectTo != null) {
GoRouter.of(context).replace(widget.redirectTo!);
} else {
GoRouter.of(context).replace('/');
}
}
},
builder: (context, state) {
Expand All @@ -59,7 +50,7 @@ class _SplashPageContentState extends State<SplashPageContent> {
Text(
'Flappy Dash',
style:
TextStyle(fontSize: 28, color: Color(0xFF25165F)),
TextStyle(fontSize: 28, color: Color(0xFF25165F)),
),
],
),
Expand Down

0 comments on commit ce28e46

Please sign in to comment.