diff --git a/README.md b/README.md index 8eda249..558ad8e 100644 --- a/README.md +++ b/README.md @@ -99,3 +99,4 @@ The `StartUserStoryConfiguration` has its own parameters, as specified below: | introductionFallbackScreen | The widget that will be shown when the introduction is skipped. | | introductionScrollPhysics | The scrollPhysics for the introduction. | | showIntroduction | Whether or not the introduction should be shown. | +| useKillswitch | Whether or not the killswitch should be used. This will only work when you use the splashScreen and you need to have a active internet connection| diff --git a/example/pubspec.lock b/example/pubspec.lock index 6822619..ae41956 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -261,6 +261,22 @@ packages: url: "https://pub.dev" source: hosted version: "13.0.1" + http: + dependency: transitive + description: + name: http + sha256: a2bbf9d017fcced29139daa8ed2bba4ece450ab222871df93ca9eec6f80c34ba + url: "https://pub.dev" + source: hosted + version: "1.2.0" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b" + url: "https://pub.dev" + source: hosted + version: "4.0.2" lints: dependency: transitive description: @@ -317,6 +333,22 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.0" + package_info_plus: + dependency: transitive + description: + name: package_info_plus + sha256: "88bc797f44a94814f2213db1c9bd5badebafdfb8290ca9f78d4b9ee2a3db4d79" + url: "https://pub.dev" + source: hosted + version: "5.0.1" + package_info_plus_platform_interface: + dependency: transitive + description: + name: package_info_plus_platform_interface + sha256: "9bc8ba46813a4cc42c66ab781470711781940780fd8beddd0c3da62506d3a6c6" + url: "https://pub.dev" + source: hosted + version: "2.0.1" path: dependency: transitive description: diff --git a/lib/src/models/start_configuration.dart b/lib/src/models/start_configuration.dart index 1fa22d0..739869d 100644 --- a/lib/src/models/start_configuration.dart +++ b/lib/src/models/start_configuration.dart @@ -11,6 +11,7 @@ class StartUserStoryConfiguration { this.introductionFallbackScreen, this.introductionScrollPhysics, this.showIntroduction = true, + this.useKillswitch = false, }); final Widget Function( BuildContext context, @@ -24,4 +25,5 @@ class StartUserStoryConfiguration { final IntroductionService? introductionService; final ScrollPhysics? introductionScrollPhysics; final bool? showIntroduction; + final bool? useKillswitch; } diff --git a/lib/src/services/killswitch_service.dart b/lib/src/services/killswitch_service.dart new file mode 100644 index 0000000..6430556 --- /dev/null +++ b/lib/src/services/killswitch_service.dart @@ -0,0 +1,29 @@ +import 'dart:convert'; + +import 'package:http/http.dart' as http; +import 'package:package_info_plus/package_info_plus.dart'; + +class KillswitchService { + Future isKillswitchActive() async { + PackageInfo packageInfo = await PackageInfo.fromPlatform(); + String appName = packageInfo.appName; + var response = await http + .get( + Uri.parse('https://active-obelugnnza-uc.a.run.app/?appName=$appName'), + ) + .timeout( + const Duration(seconds: 5), + onTimeout: () => http.Response('false', 500), + ) + .onError( + (error, stackTrace) => http.Response('false', 500), + ); + + var decoded = jsonDecode(response.body); + + if (decoded == true) { + return true; + } + return false; + } +} diff --git a/lib/src/user_stories/flutter_start_userstory_go_router.dart b/lib/src/user_stories/flutter_start_userstory_go_router.dart index dfb8d9c..03a2fc7 100644 --- a/lib/src/user_stories/flutter_start_userstory_go_router.dart +++ b/lib/src/user_stories/flutter_start_userstory_go_router.dart @@ -8,6 +8,7 @@ import 'package:flutter_introduction_shared_preferences/flutter_introduction_sha import 'package:flutter_start/src/go_router.dart'; import 'package:flutter_start/src/models/start_configuration.dart'; import 'package:flutter_start/src/routes.dart'; +import 'package:flutter_start/src/services/killswitch_service.dart'; import 'package:go_router/go_router.dart'; @@ -18,16 +19,24 @@ List getStartStoryRoutes( GoRoute( path: StartUserStoryRoutes.splashScreen, pageBuilder: (context, state) { + var go = context.go; return buildScreenWithoutTransition( context: context, state: state, child: configuration.splashScreenBuilder?.call( context, - () { - if (configuration.showIntroduction == false) { - return context.go(StartUserStoryRoutes.home); + () async { + if (configuration.useKillswitch == true) { + var isActive = + await KillswitchService().isKillswitchActive(); + if (!isActive) { + return; + } } - return context.go(StartUserStoryRoutes.introduction); + if (configuration.showIntroduction == false) { + return go(StartUserStoryRoutes.home); + } + return go(StartUserStoryRoutes.introduction); }, ) ?? const Scaffold( diff --git a/lib/src/user_stories/flutter_start_userstory_navigator.dart b/lib/src/user_stories/flutter_start_userstory_navigator.dart index bdb84a8..c4da210 100644 --- a/lib/src/user_stories/flutter_start_userstory_navigator.dart +++ b/lib/src/user_stories/flutter_start_userstory_navigator.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_introduction_shared_preferences/flutter_introduction_shared_preferences.dart'; import 'package:flutter_start/flutter_start.dart'; +import 'package:flutter_start/src/services/killswitch_service.dart'; Widget startNavigatorUserStory( StartUserStoryConfiguration configuration, BuildContext context) { @@ -14,17 +15,25 @@ Widget _splashScreen( StartUserStoryConfiguration configuration, BuildContext context, ) { + var navigator = Navigator.of(context); return configuration.splashScreenBuilder?.call( context, - () { + () async { + bool isActive; + if (configuration.useKillswitch == true) { + isActive = await KillswitchService().isKillswitchActive(); + if (!isActive) { + return () {}; + } + } if (configuration.showIntroduction == false) { - return Navigator.of(context).pushReplacement( + return navigator.pushReplacement( MaterialPageRoute( builder: (context) => _home(configuration, context), ), ); } - return Navigator.of(context).pushReplacement( + return navigator.pushReplacement( MaterialPageRoute( builder: (context) => _introduction(configuration, context), ), diff --git a/pubspec.lock b/pubspec.lock index 41d5f5f..4f8c917 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -254,6 +254,22 @@ packages: url: "https://pub.dev" source: hosted version: "13.0.1" + http: + dependency: "direct main" + description: + name: http + sha256: a2bbf9d017fcced29139daa8ed2bba4ece450ab222871df93ca9eec6f80c34ba + url: "https://pub.dev" + source: hosted + version: "1.2.0" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b" + url: "https://pub.dev" + source: hosted + version: "4.0.2" lints: dependency: transitive description: @@ -310,6 +326,22 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.0" + package_info_plus: + dependency: "direct main" + description: + name: package_info_plus + sha256: "88bc797f44a94814f2213db1c9bd5badebafdfb8290ca9f78d4b9ee2a3db4d79" + url: "https://pub.dev" + source: hosted + version: "5.0.1" + package_info_plus_platform_interface: + dependency: transitive + description: + name: package_info_plus_platform_interface + sha256: "9bc8ba46813a4cc42c66ab781470711781940780fd8beddd0c3da62506d3a6c6" + url: "https://pub.dev" + source: hosted + version: "2.0.1" path: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 8e4d680..3f3f5ce 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: flutter_start description: "Flutter_start is a package that allows you to jumpstart your application with a splashScreen, introduction and a home." publish_to: "none" -version: 1.0.0 +version: 1.1.0 environment: sdk: ">=3.2.5 <4.0.0" @@ -21,6 +21,8 @@ dependencies: ref: 2.0.0 path: packages/flutter_introduction_shared_preferences go_router: ^13.0.1 + http: ^1.2.0 + package_info_plus: ^5.0.1 dev_dependencies: flutter_test: