diff --git a/packages/firebase_introduction_repository/lib/src/firebase_introduction_repository.dart b/packages/firebase_introduction_repository/lib/src/firebase_introduction_repository.dart index f2ebbf4..246a7f9 100644 --- a/packages/firebase_introduction_repository/lib/src/firebase_introduction_repository.dart +++ b/packages/firebase_introduction_repository/lib/src/firebase_introduction_repository.dart @@ -7,12 +7,18 @@ import "package:introduction_repository_interface/introduction_repository_interf class FirebaseIntroductionRepository implements IntroductionRepositoryInterface { + bool? _shouldShowIntroduction; + List? _introductionPages; + final _introductionCollection = FirebaseFirestore.instance .collection("flutter_introduction") .doc("flutter_introduction"); @override Future> fetchIntroductionPages() async { + if (_introductionPages != null) { + return _introductionPages!; + } try { var introductionPagesData = await _introductionCollection .collection("pages") @@ -27,6 +33,7 @@ class FirebaseIntroductionRepository introductionPagesData.docs.map((e) => e.data()).toList(); introductionPages.sort((a, b) => a.id.compareTo(b.id)); + _introductionPages = introductionPages; return introductionPages; } on Exception catch (_) { throw Exception(); @@ -51,6 +58,9 @@ class FirebaseIntroductionRepository @override Future shouldShow() async { + if (_shouldShowIntroduction != null) { + return _shouldShowIntroduction!; + } try { await FirebaseAuth.instance.signInAnonymously(); var deviceId = await _getDeviceId(); @@ -64,8 +74,10 @@ class FirebaseIntroductionRepository if (!introductionCompleted.exists) { return true; } - // ignore: avoid_dynamic_calls - return !introductionCompleted.data()!["introduction_completed"]; + _shouldShowIntroduction = + // ignore: avoid_dynamic_calls + !introductionCompleted.data()!["introduction_completed"]; + return _shouldShowIntroduction!; } on Exception catch (_) { throw Exception(); } @@ -83,4 +95,10 @@ class FirebaseIntroductionRepository } return null; } + + @override + Future prefetchIntroduction() async { + await shouldShow(); + await fetchIntroductionPages(); + } } diff --git a/packages/flutter_introduction/lib/src/widgets/button.dart b/packages/flutter_introduction/lib/src/widgets/button.dart index 2d40578..6a6bb7f 100644 --- a/packages/flutter_introduction/lib/src/widgets/button.dart +++ b/packages/flutter_introduction/lib/src/widgets/button.dart @@ -17,38 +17,33 @@ class Button extends StatelessWidget { @override Widget build(BuildContext context) { var theme = Theme.of(context); - return Expanded( - child: ConstrainedBox( - constraints: const BoxConstraints( - maxWidth: 180, - maxHeight: 32, - ), - child: showButton - ? FilledButton( - style: ButtonStyle( - backgroundColor: WidgetStateProperty.all( - introductionTheme.buttonBackgroundColor, - ), - shape: WidgetStateProperty.all( - RoundedRectangleBorder( - borderRadius: const BorderRadius.all(Radius.circular(16)), - side: BorderSide( - color: introductionTheme.buttonBorderColor, - width: 1, - ), + child: showButton + ? FilledButton( + style: ButtonStyle( + tapTargetSize: MaterialTapTargetSize.shrinkWrap, + minimumSize: WidgetStateProperty.all(const Size(160, 32)), + backgroundColor: WidgetStateProperty.all( + introductionTheme.buttonBackgroundColor, + ), + shape: WidgetStateProperty.all( + RoundedRectangleBorder( + borderRadius: const BorderRadius.all(Radius.circular(16)), + side: BorderSide( + color: introductionTheme.buttonBorderColor, + width: 1, ), ), ), - onPressed: onPressed, - child: Text( - text, - style: introductionTheme.buttonTextStyle ?? - theme.textTheme.bodyMedium, - ), - ) - : const SizedBox(), - ), + ), + onPressed: onPressed, + child: Text( + text, + style: introductionTheme.buttonTextStyle ?? + theme.textTheme.bodyMedium, + ), + ) + : const SizedBox.shrink(), ); } } diff --git a/packages/flutter_introduction/lib/src/widgets/introduction_page.dart b/packages/flutter_introduction/lib/src/widgets/introduction_page.dart index a9c972b..d6d4380 100644 --- a/packages/flutter_introduction/lib/src/widgets/introduction_page.dart +++ b/packages/flutter_introduction/lib/src/widgets/introduction_page.dart @@ -1,7 +1,5 @@ import "package:flutter/material.dart"; import "package:flutter_introduction/flutter_introduction.dart"; -import "package:flutter_introduction/src/config/introduction_theme.dart"; -import "package:flutter_introduction/src/enums/introduction_layout_style.dart"; class IntroductionPage extends StatelessWidget { const IntroductionPage({ diff --git a/packages/introduction_repository_interface/lib/src/interfaces/introduction_repository_interface.dart b/packages/introduction_repository_interface/lib/src/interfaces/introduction_repository_interface.dart index 927e5ad..7388eff 100644 --- a/packages/introduction_repository_interface/lib/src/interfaces/introduction_repository_interface.dart +++ b/packages/introduction_repository_interface/lib/src/interfaces/introduction_repository_interface.dart @@ -7,4 +7,6 @@ abstract class IntroductionRepositoryInterface { Future setCompleted({bool value = true}); Future shouldShow(); + + Future prefetchIntroduction(); } diff --git a/packages/introduction_repository_interface/lib/src/local/local_introduction_repository.dart b/packages/introduction_repository_interface/lib/src/local/local_introduction_repository.dart index 9559875..a533ff9 100644 --- a/packages/introduction_repository_interface/lib/src/local/local_introduction_repository.dart +++ b/packages/introduction_repository_interface/lib/src/local/local_introduction_repository.dart @@ -11,7 +11,11 @@ class LocalIntroductionRepository implements IntroductionRepositoryInterface { Future shouldShow() async => !_completed; @override - Future> fetchIntroductionPages() { - throw Exception(); + Future> fetchIntroductionPages() async => []; + + @override + Future prefetchIntroduction() async { + await shouldShow(); + await fetchIntroductionPages(); } } diff --git a/packages/introduction_repository_interface/lib/src/services/introduction_service.dart b/packages/introduction_repository_interface/lib/src/services/introduction_service.dart index 52dac1b..b8a87d2 100644 --- a/packages/introduction_repository_interface/lib/src/services/introduction_service.dart +++ b/packages/introduction_repository_interface/lib/src/services/introduction_service.dart @@ -16,4 +16,7 @@ class IntroductionService { Future shouldShow() async => introductionRepositoryInterface.shouldShow(); + + Future prefetchIntroduction() async => + introductionRepositoryInterface.prefetchIntroduction(); } diff --git a/packages/shared_preferences_introduction_repository/lib/src/shared_preferences_introduction_repository.dart b/packages/shared_preferences_introduction_repository/lib/src/shared_preferences_introduction_repository.dart index a7677e8..1ab9808 100644 --- a/packages/shared_preferences_introduction_repository/lib/src/shared_preferences_introduction_repository.dart +++ b/packages/shared_preferences_introduction_repository/lib/src/shared_preferences_introduction_repository.dart @@ -3,10 +3,10 @@ import "package:shared_preferences/shared_preferences.dart"; class SharedPreferencesIntroductionRepository implements IntroductionRepositoryInterface { + bool? _shouldShowIntroduction; + @override - Future> fetchIntroductionPages() async { - throw Exception(); - } + Future> fetchIntroductionPages() async => []; @override Future setCompleted({bool value = true}) async { @@ -16,8 +16,18 @@ class SharedPreferencesIntroductionRepository @override Future shouldShow() async { + if (_shouldShowIntroduction != null) { + return !_shouldShowIntroduction!; + } var sharedPrefs = await SharedPreferences.getInstance(); var shouldShow = sharedPrefs.getBool("_completedIntroduction") ?? true; - return !shouldShow; + _shouldShowIntroduction = shouldShow; + return !_shouldShowIntroduction!; + } + + @override + Future prefetchIntroduction() async { + await shouldShow(); + await fetchIntroductionPages(); } }