feat: killswitch

This commit is contained in:
mike doornenbal 2024-01-24 16:56:53 +01:00
parent 20eec24857
commit 1f2602ff76
8 changed files with 124 additions and 8 deletions

View file

@ -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. | | introductionFallbackScreen | The widget that will be shown when the introduction is skipped. |
| introductionScrollPhysics | The scrollPhysics for the introduction. | | introductionScrollPhysics | The scrollPhysics for the introduction. |
| showIntroduction | Whether or not the introduction should be shown. | | 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|

View file

@ -261,6 +261,22 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "13.0.1" 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: lints:
dependency: transitive dependency: transitive
description: description:
@ -317,6 +333,22 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.1.0" 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: path:
dependency: transitive dependency: transitive
description: description:

View file

@ -11,6 +11,7 @@ class StartUserStoryConfiguration {
this.introductionFallbackScreen, this.introductionFallbackScreen,
this.introductionScrollPhysics, this.introductionScrollPhysics,
this.showIntroduction = true, this.showIntroduction = true,
this.useKillswitch = false,
}); });
final Widget Function( final Widget Function(
BuildContext context, BuildContext context,
@ -24,4 +25,5 @@ class StartUserStoryConfiguration {
final IntroductionService? introductionService; final IntroductionService? introductionService;
final ScrollPhysics? introductionScrollPhysics; final ScrollPhysics? introductionScrollPhysics;
final bool? showIntroduction; final bool? showIntroduction;
final bool? useKillswitch;
} }

View file

@ -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<bool> 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;
}
}

View file

@ -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/go_router.dart';
import 'package:flutter_start/src/models/start_configuration.dart'; import 'package:flutter_start/src/models/start_configuration.dart';
import 'package:flutter_start/src/routes.dart'; import 'package:flutter_start/src/routes.dart';
import 'package:flutter_start/src/services/killswitch_service.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
@ -18,16 +19,24 @@ List<GoRoute> getStartStoryRoutes(
GoRoute( GoRoute(
path: StartUserStoryRoutes.splashScreen, path: StartUserStoryRoutes.splashScreen,
pageBuilder: (context, state) { pageBuilder: (context, state) {
var go = context.go;
return buildScreenWithoutTransition( return buildScreenWithoutTransition(
context: context, context: context,
state: state, state: state,
child: configuration.splashScreenBuilder?.call( child: configuration.splashScreenBuilder?.call(
context, context,
() { () async {
if (configuration.showIntroduction == false) { if (configuration.useKillswitch == true) {
return context.go(StartUserStoryRoutes.home); 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( const Scaffold(

View file

@ -1,6 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_introduction_shared_preferences/flutter_introduction_shared_preferences.dart'; import 'package:flutter_introduction_shared_preferences/flutter_introduction_shared_preferences.dart';
import 'package:flutter_start/flutter_start.dart'; import 'package:flutter_start/flutter_start.dart';
import 'package:flutter_start/src/services/killswitch_service.dart';
Widget startNavigatorUserStory( Widget startNavigatorUserStory(
StartUserStoryConfiguration configuration, BuildContext context) { StartUserStoryConfiguration configuration, BuildContext context) {
@ -14,17 +15,25 @@ Widget _splashScreen(
StartUserStoryConfiguration configuration, StartUserStoryConfiguration configuration,
BuildContext context, BuildContext context,
) { ) {
var navigator = Navigator.of(context);
return configuration.splashScreenBuilder?.call( return configuration.splashScreenBuilder?.call(
context, context,
() { () async {
bool isActive;
if (configuration.useKillswitch == true) {
isActive = await KillswitchService().isKillswitchActive();
if (!isActive) {
return () {};
}
}
if (configuration.showIntroduction == false) { if (configuration.showIntroduction == false) {
return Navigator.of(context).pushReplacement( return navigator.pushReplacement(
MaterialPageRoute( MaterialPageRoute(
builder: (context) => _home(configuration, context), builder: (context) => _home(configuration, context),
), ),
); );
} }
return Navigator.of(context).pushReplacement( return navigator.pushReplacement(
MaterialPageRoute( MaterialPageRoute(
builder: (context) => _introduction(configuration, context), builder: (context) => _introduction(configuration, context),
), ),

View file

@ -254,6 +254,22 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "13.0.1" 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: lints:
dependency: transitive dependency: transitive
description: description:
@ -310,6 +326,22 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.1.0" 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: path:
dependency: transitive dependency: transitive
description: description:

View file

@ -1,7 +1,7 @@
name: flutter_start name: flutter_start
description: "Flutter_start is a package that allows you to jumpstart your application with a splashScreen, introduction and a home." description: "Flutter_start is a package that allows you to jumpstart your application with a splashScreen, introduction and a home."
publish_to: "none" publish_to: "none"
version: 1.0.0 version: 1.1.0
environment: environment:
sdk: ">=3.2.5 <4.0.0" sdk: ">=3.2.5 <4.0.0"
@ -21,6 +21,8 @@ dependencies:
ref: 2.0.0 ref: 2.0.0
path: packages/flutter_introduction_shared_preferences path: packages/flutter_introduction_shared_preferences
go_router: ^13.0.1 go_router: ^13.0.1
http: ^1.2.0
package_info_plus: ^5.0.1
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: