Skip to content

Commit

Permalink
internet check feature implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
fedecarroz committed Oct 3, 2024
1 parent 8dbdd10 commit d5b76f3
Show file tree
Hide file tree
Showing 18 changed files with 282 additions and 54 deletions.
5 changes: 5 additions & 0 deletions assets/images/dino.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ PODS:
- Firebase/Functions (= 10.25.0)
- firebase_core
- Flutter
- connectivity_plus (0.0.1):
- Flutter
- FlutterMacOS
- Firebase/Auth (10.25.0):
- Firebase/CoreOnly
- FirebaseAuth (~> 10.25.0)
Expand Down Expand Up @@ -128,6 +131,7 @@ PODS:

DEPENDENCIES:
- cloud_functions (from `.symlinks/plugins/cloud_functions/ios`)
- connectivity_plus (from `.symlinks/plugins/connectivity_plus/darwin`)
- firebase_auth (from `.symlinks/plugins/firebase_auth/ios`)
- firebase_core (from `.symlinks/plugins/firebase_core/ios`)
- Flutter (from `Flutter`)
Expand Down Expand Up @@ -166,6 +170,8 @@ SPEC REPOS:
EXTERNAL SOURCES:
cloud_functions:
:path: ".symlinks/plugins/cloud_functions/ios"
connectivity_plus:
:path: ".symlinks/plugins/connectivity_plus/darwin"
firebase_auth:
:path: ".symlinks/plugins/firebase_auth/ios"
firebase_core:
Expand All @@ -185,6 +191,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
cloud_functions: 1a28a0b6ec4caf864692e54e1f5c1934431510c8
connectivity_plus: ddd7f30999e1faaef5967c23d5b6d503d10434db
Firebase: 0312a2352584f782ea56f66d91606891d4607f06
firebase_auth: 5f76bda1f6d8b4365d0741aef49baecee550ad35
firebase_core: a626d00494efa398e7c54f25f1454a64c8abf197
Expand Down
43 changes: 37 additions & 6 deletions lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ class App extends StatelessWidget {
scaffoldBackgroundColor: Colors.white,
),
builder: (context, child) {
return BlocListener<AuthenticationCubit, AuthenticationState>(
listener: _authListener,
return MultiBlocListener(
listeners: <BlocListener>[
BlocListener<AuthenticationCubit, AuthenticationState>(
listener: _authListener,
),
BlocListener<InternetCubit, InternetState>(
listener: _internetListener,
),
],
child: LoaderOverlay(
overlayColor: Colors.black.withOpacity(.4),
overlayWidgetBuilder: (_) {
Expand All @@ -39,24 +46,28 @@ class App extends StatelessWidget {
}

List<BlocProvider> _topLevelProviders = <BlocProvider>[
BlocProvider<InternetCubit>(
lazy: false,
create: (_) => InternetCubit(),
),
BlocProvider<AuthenticationCubit>(
lazy: false,
create: (context) => AuthenticationCubit(),
create: (_) => AuthenticationCubit(),
),
BlocProvider<QuizCubit>(
lazy: false,
create: (context) => QuizCubit(),
create: (_) => QuizCubit(),
),
BlocProvider<LeaderboardCubit>(
lazy: false,
create: (context) => LeaderboardCubit(),
create: (_) => LeaderboardCubit(),
),
];

void _authListener(
BuildContext context,
AuthenticationState state,
) async {
) {
switch (state.status) {
case AuthenticationStatus.initialAuthFailure:
context.loaderOverlay.hide();
Expand Down Expand Up @@ -120,3 +131,23 @@ void _authListener(
break;
}
}

void _internetListener(
BuildContext context,
InternetState state,
) {
if (state is InternetDisconnected) {
context.loaderOverlay.hide();
appRouter.goNamed(RouteNames.noInternetRoute.name);
FlutterNativeSplash.remove();
}

if (state is InternetConnected) {
context.read<LeaderboardCubit>().changeLeaderboard(0);
final route = context.read<AuthenticationCubit>().state.isAuthenticated
? RouteNames.leaderboardRoute
: RouteNames.welcomeRoute;

appRouter.goNamed(route.name);
}
}
1 change: 1 addition & 0 deletions lib/logic.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export 'logic/cubit.dart';
export 'logic/debug_bloc.dart';
export 'logic/input_validators.dart';
1 change: 1 addition & 0 deletions lib/logic/cubit.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export 'cubit/authentication_cubit.dart';
export 'cubit/internet_cubit.dart';
export 'cubit/leaderboard_cubit.dart';
export 'cubit/quiz_cubit.dart';
41 changes: 41 additions & 0 deletions lib/logic/cubit/internet_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:equatable/equatable.dart';

part 'internet_state.dart';

class InternetCubit extends Cubit<InternetState> {
static InternetCubit? _instance;

factory InternetCubit() {
return _instance ??= InternetCubit._internal();
}

InternetCubit._internal() : super(InternetLoading());

StreamSubscription? connectivityStreamSubscription;

StreamSubscription<List<ConnectivityResult>> monitorInternetConnection() {
final connectivityStream = Connectivity().onConnectivityChanged;
connectivityStreamSubscription?.cancel();
return connectivityStreamSubscription = connectivityStream.listen(
(connectivityResults) {
connectivityResults.first == ConnectivityResult.none
? sendInternetFailure()
: sendInternetResume();
},
);
}

void sendInternetFailure() => emit(InternetDisconnected());

void sendInternetResume() => emit(InternetConnected());

@override
Future<void> close() {
connectivityStreamSubscription?.cancel();
return super.close();
}
}
14 changes: 14 additions & 0 deletions lib/logic/cubit/internet_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
part of 'internet_cubit.dart';

sealed class InternetState extends Equatable {
const InternetState();

@override
List<Object> get props => [];
}

class InternetLoading extends InternetState {}

class InternetConnected extends InternetState {}

class InternetDisconnected extends InternetState {}
10 changes: 10 additions & 0 deletions lib/logic/debug_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'dart:io';

import 'package:bloc/bloc.dart';
import 'package:devfest_bari_2024/logic.dart';

class DebugBloc extends BlocObserver {
@override
Expand All @@ -16,6 +19,13 @@ class DebugBloc extends BlocObserver {
@override
void onError(BlocBase bloc, Object error, StackTrace stackTrace) async {
print(error);

if (error is SocketException) {
InternetCubit().sendInternetFailure();
} else {
throw await Future.error(error, stackTrace);
}

super.onError(bloc, error, stackTrace);
}

Expand Down
8 changes: 7 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:devfest_bari_2024/firebase_options.dart';
import 'package:devfest_bari_2024/logic.dart';
import 'package:devfest_bari_2024/ui.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
Expand All @@ -19,7 +20,9 @@ void main() async {
runApp(const App());
},
(error, stack) {
// TODO: implement error handling
if (kDebugMode) {
print(error);
}
},
);
}
Expand Down Expand Up @@ -48,10 +51,13 @@ Future<void> _initialization() async {
);

await _precacheAllSvg();

InternetCubit().monitorInternetConnection();
}

Future<void> _precacheAllSvg() async {
final List<String> imageUrls = [
'assets/images/dino.svg',
'assets/images/devfest_logo.svg',
'assets/images/qr_marker.svg',
'assets/images/icons/email.svg',
Expand Down
5 changes: 5 additions & 0 deletions lib/ui/navigation/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ final appRouter = GoRouter(
navigatorKey: _rootNavigatorKey,
initialLocation: RouteNames.welcomeRoute.path,
routes: <RouteBase>[
GoRoute(
name: RouteNames.noInternetRoute.name,
path: RouteNames.noInternetRoute.path,
builder: (context, state) => const NoInternetPage(),
),
GoRoute(
name: RouteNames.welcomeRoute.name,
path: RouteNames.welcomeRoute.path,
Expand Down
1 change: 1 addition & 0 deletions lib/ui/navigation/route_names.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
enum RouteNames {
noInternetRoute(path: '/no_internet'),
welcomeRoute(path: '/welcome'),
loginRoute(path: '/welcome/login'),
signUpRoute(path: '/welcome/sign_up'),
Expand Down
21 changes: 21 additions & 0 deletions lib/ui/navigation/router_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class RouterObserver extends NavigatorObserver {
Future.delayed(
const Duration(milliseconds: 0),
() {
if (route.settings.name == 'signUpRoute') {
SystemChrome.setSystemUIOverlayStyle(
const SystemUiOverlayStyle(
systemNavigationBarColor: ColorPalette.seaBlue,
),
);
}
if (route.settings.name == 'qrCodeRoute') {
SystemChrome.setSystemUIOverlayStyle(
const SystemUiOverlayStyle(
Expand Down Expand Up @@ -57,13 +64,27 @@ class RouterObserver extends NavigatorObserver {

void _setSystemUIColor(String? routeName) {
switch (routeName) {
case 'noInternetRoute':
SystemChrome.setSystemUIOverlayStyle(
const SystemUiOverlayStyle(
systemNavigationBarColor: ColorPalette.white,
),
);
break;
case 'welcomeRoute':
SystemChrome.setSystemUIOverlayStyle(
const SystemUiOverlayStyle(
systemNavigationBarColor: ColorPalette.seaBlue,
),
);
break;
case 'signUpRoute':
SystemChrome.setSystemUIOverlayStyle(
const SystemUiOverlayStyle(
systemNavigationBarColor: ColorPalette.white,
),
);
break;
case 'checkInRoute':
SystemChrome.setSystemUIOverlayStyle(
const SystemUiOverlayStyle(
Expand Down
1 change: 1 addition & 0 deletions lib/ui/pages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export 'pages/check_in_page.dart';
export 'pages/leaderboard_page.dart';
export 'pages/login_page.dart';
export 'pages/navigation_bar_page.dart';
export 'pages/no_internet_page.dart';
export 'pages/profile_page.dart';
export 'pages/qr_code_page.dart';
export 'pages/quiz_page.dart';
Expand Down
Loading

0 comments on commit d5b76f3

Please sign in to comment.