From c162e8b2947e2fa86e55ab12fd8071221ab119ce Mon Sep 17 00:00:00 2001 From: Hans Kokx Date: Tue, 29 Apr 2025 11:31:42 +0200 Subject: [PATCH] Added examples for feature flags Signed-off-by: Hans Kokx --- example/lib/config.dart | 1 + example/lib/main.dart | 97 ++++++++++++++++++++++++++++++++--------- 2 files changed, 78 insertions(+), 20 deletions(-) diff --git a/example/lib/config.dart b/example/lib/config.dart index 2868ef5..fc094c0 100644 --- a/example/lib/config.dart +++ b/example/lib/config.dart @@ -1,5 +1,6 @@ enum Feature { logging(true), + authentication(true), ; final bool enabledAtStartup; diff --git a/example/lib/main.dart b/example/lib/main.dart index 1a76e5a..5e03abd 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -102,6 +102,7 @@ class _HomeScreenState extends State { maxCrossAxisExtent: 300, padding: const EdgeInsets.all(16), children: [ + // Theme Card( child: Padding( padding: const EdgeInsets.all(8.0), @@ -216,6 +217,10 @@ class _HomeScreenState extends State { ), ); } + + Arcane.log( + "Setting ${Arcane.theme.currentThemeMode.name} theme color to ${themeColors[index]}", + ); }, child: Container( color: themeColors[index], @@ -238,6 +243,8 @@ class _HomeScreenState extends State { ), ), ), + + // Authentication Card( child: Padding( padding: const EdgeInsets.all(8.0), @@ -250,26 +257,28 @@ class _HomeScreenState extends State { style: Theme.of(context).textTheme.headlineSmall, ), ElevatedButton( + onPressed: Feature.authentication.enabled + ? () async { + if (isSignedIn) { + await Arcane.auth.logOut( + onLoggedOut: () async { + setState(() {}); + }, + ); + } else { + await Arcane.auth.login( + input: ( + email: "email", + password: "password", + ), + onLoggedIn: () async { + setState(() {}); + }, + ); + } + } + : null, child: Text(isSignedIn ? "Sign out" : "Sign in"), - onPressed: () async { - if (isSignedIn) { - await Arcane.auth.logOut( - onLoggedOut: () async { - setState(() {}); - }, - ); - } else { - await Arcane.auth.login( - input: ( - email: "email", - password: "password", - ), - onLoggedIn: () async { - setState(() {}); - }, - ); - } - }, ), Center( child: Text("Status: ${Arcane.auth.status.name}"), @@ -278,9 +287,50 @@ class _HomeScreenState extends State { ), ), ), + + // Feature flags + Card( + child: Padding( + padding: const EdgeInsets.all(8.0), + child: Column( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Text( + "Feature Flags", + style: Theme.of(context).textTheme.headlineSmall, + ), + Expanded( + child: ListView.builder( + itemCount: Feature.values.length, + itemBuilder: (context, i) { + final Feature feature = Feature.values[i]; + return Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text(feature.name), + Switch( + value: feature.enabled, + onChanged: (_) { + feature.enabled + ? feature.disable() + : feature.enable(); + }, + ), + ], + ); + }, + ), + ), + ], + ), + ), + ), ], ), ), + + // Logging Padding( padding: const EdgeInsets.all(16.0), child: SizedBox( @@ -299,6 +349,13 @@ class _HomeScreenState extends State { fontStyle: FontStyle.italic, ), ), + if (Feature.logging.disabled) + Text( + "Logging feature is disabled.", + style: Theme.of(context).textTheme.labelSmall?.copyWith( + fontWeight: FontWeight.bold, + ), + ), Expanded( child: ListView.builder( itemCount: latestLogs.length, @@ -320,7 +377,7 @@ class _HomeScreenState extends State { super.initState(); _subscription = Arcane.logger.logStream.listen((message) { setState(() { - latestLogs.insert(0, message); + if (Feature.logging.enabled) latestLogs.insert(0, message); }); }); }