From 75be97e9cf88377de9d0e53f0fd264d13cd46892 Mon Sep 17 00:00:00 2001 From: Lucio Baglione Date: Tue, 19 Mar 2024 11:03:06 +0100 Subject: [PATCH 1/8] feat: Add PlatformWidgetBuilder. This widget abstracts the functionality to detect the platform used by the user. Related to #139. --- .../lib/src/catalyst_voices_shared.dart | 1 + .../platform_widget_builder.dart | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_widget_builder/platform_widget_builder.dart diff --git a/catalyst_voices/packages/catalyst_voices_shared/lib/src/catalyst_voices_shared.dart b/catalyst_voices/packages/catalyst_voices_shared/lib/src/catalyst_voices_shared.dart index 5723c64c28a..eed3d8f970a 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/lib/src/catalyst_voices_shared.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/lib/src/catalyst_voices_shared.dart @@ -1 +1,2 @@ export 'platform/catalyst_platform.dart'; +export 'platform_widget_builder/platform_widget_builder.dart'; diff --git a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_widget_builder/platform_widget_builder.dart b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_widget_builder/platform_widget_builder.dart new file mode 100644 index 00000000000..78832986a9e --- /dev/null +++ b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_widget_builder/platform_widget_builder.dart @@ -0,0 +1,66 @@ +import 'package:catalyst_voices_shared/src/platform/catalyst_platform.dart'; +import 'package:flutter/widgets.dart'; + +class PlatformWidgetBuilder extends StatelessWidget { + final Widget Function(BuildContext context, T? data) builder; + final T? android; + final T? desktop; + final T? fuchsia; + final T? iOS; + final T? linux; + final T? macOS; + final T? mobile; + final T? mobileWeb; + final T? web; + final T? webDesktop; + final T? windows; + final T? other; + + const PlatformWidgetBuilder({ + super.key, + required this.builder, + this.android, + this.desktop, + this.fuchsia, + this.iOS, + this.linux, + this.macOS, + this.mobile, + this.mobileWeb, + this.web, + this.webDesktop, + this.windows, + this.other, + }); + + @override + Widget build(BuildContext context) { + if (CatalystPlatform.isAndroid && android != null) { + return builder(context, android as T); + } else if (CatalystPlatform.isDesktop && desktop != null) { + return builder(context, desktop as T); + } else if (CatalystPlatform.isFuchsia && fuchsia != null) { + return builder(context, fuchsia as T); + } else if (CatalystPlatform.isIOS && iOS != null) { + return builder(context, iOS as T); + } else if (CatalystPlatform.isLinux && linux != null) { + return builder(context, linux as T); + } else if (CatalystPlatform.isMacOS && macOS != null) { + return builder(context, linux as T); + } else if (CatalystPlatform.isMobile && mobile != null) { + return builder(context, mobile as T); + } else if (CatalystPlatform.isMobileWeb && mobileWeb != null) { + return builder(context, mobileWeb as T); + } else if (CatalystPlatform.isWeb && web != null) { + return builder(context, web as T); + } else if (CatalystPlatform.isWebDesktop && webDesktop != null) { + return builder(context, webDesktop as T); + } else if (CatalystPlatform.isWindows && windows != null) { + return builder(context, windows as T); + } else if (other != null) { + return builder(context, other as T); + } else { + return builder(context, null); + } + } +} From be154a1cc69ebdea51410ef2a8d1424a2f330f16 Mon Sep 17 00:00:00 2001 From: Lucio Baglione Date: Tue, 19 Mar 2024 16:43:04 +0100 Subject: [PATCH 2/8] feat: Add basic test for PlatformWidgetBuilder. --- .../catalyst_voices_shared/pubspec.yaml | 2 ++ .../test/src/catalyst_voices_shared_test.dart | 2 +- .../platform_widget_builder_test.dart | 27 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 catalyst_voices/packages/catalyst_voices_shared/test/src/platform_widget_builder/platform_widget_builder_test.dart diff --git a/catalyst_voices/packages/catalyst_voices_shared/pubspec.yaml b/catalyst_voices/packages/catalyst_voices_shared/pubspec.yaml index 9729ed1af42..56359919b80 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/pubspec.yaml +++ b/catalyst_voices/packages/catalyst_voices_shared/pubspec.yaml @@ -15,5 +15,7 @@ dependencies: dev_dependencies: catalyst_analysis: path: ../../../catalyst_voices_packages/catalyst_analysis + flutter_test: + sdk: flutter test: ^1.24.9 diff --git a/catalyst_voices/packages/catalyst_voices_shared/test/src/catalyst_voices_shared_test.dart b/catalyst_voices/packages/catalyst_voices_shared/test/src/catalyst_voices_shared_test.dart index ab73b3a234a..f5af33b44ab 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/test/src/catalyst_voices_shared_test.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/test/src/catalyst_voices_shared_test.dart @@ -1 +1 @@ -void main() {} +export 'platform_widget_builder/platform_widget_builder_test.dart'; diff --git a/catalyst_voices/packages/catalyst_voices_shared/test/src/platform_widget_builder/platform_widget_builder_test.dart b/catalyst_voices/packages/catalyst_voices_shared/test/src/platform_widget_builder/platform_widget_builder_test.dart new file mode 100644 index 00000000000..33167596250 --- /dev/null +++ b/catalyst_voices/packages/catalyst_voices_shared/test/src/platform_widget_builder/platform_widget_builder_test.dart @@ -0,0 +1,27 @@ +import 'package:catalyst_voices_shared/catalyst_voices_shared.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + + Widget buildApp() => MaterialApp( + home: Scaffold( + body: PlatformWidgetBuilder( + other: 'other', + builder: (context, platformData) => Text(platformData!), + ), + ), + ); + + group('Test platform detection', () { + testWidgets('PlatformWidgetBuilder fallbacks to other', (tester) async { + await tester.pumpWidget(buildApp()); + // Verify the Widget renders properly + expect(find.byType(Text), findsOneWidget); + // Check the output containst platform that was tested. + expect(find.text('other'), findsOneWidget); + + }); + }); +} From f0e393f54d672b71353f31f10a5726459e24be9a Mon Sep 17 00:00:00 2001 From: Lucio Baglione Date: Tue, 19 Mar 2024 18:06:57 +0100 Subject: [PATCH 3/8] chore: Spell check. --- .../platform_widget_builder/platform_widget_builder_test.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/catalyst_voices/packages/catalyst_voices_shared/test/src/platform_widget_builder/platform_widget_builder_test.dart b/catalyst_voices/packages/catalyst_voices_shared/test/src/platform_widget_builder/platform_widget_builder_test.dart index 33167596250..bb8c8ba4b54 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/test/src/platform_widget_builder/platform_widget_builder_test.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/test/src/platform_widget_builder/platform_widget_builder_test.dart @@ -1,5 +1,4 @@ import 'package:catalyst_voices_shared/catalyst_voices_shared.dart'; -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -19,7 +18,7 @@ void main() { await tester.pumpWidget(buildApp()); // Verify the Widget renders properly expect(find.byType(Text), findsOneWidget); - // Check the output containst platform that was tested. + // Check the output contains the platform that was tested. expect(find.text('other'), findsOneWidget); }); From 71853d1919d31a25c41233a013afd3359c4069de Mon Sep 17 00:00:00 2001 From: Lucio Baglione Date: Wed, 20 Mar 2024 13:41:23 +0100 Subject: [PATCH 4/8] refactor: Switch name from `PlatformWidgetBuilder` to `PlatformAwareBuilder`. --- .../lib/src/catalyst_voices_shared.dart | 2 +- .../platform_aware_builder.dart} | 4 ++-- .../test/src/catalyst_voices_shared_test.dart | 2 +- .../platform_aware_builder_test.dart} | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) rename catalyst_voices/packages/catalyst_voices_shared/lib/src/{platform_widget_builder/platform_widget_builder.dart => platform_aware_builder/platform_aware_builder.dart} (95%) rename catalyst_voices/packages/catalyst_voices_shared/test/src/{platform_widget_builder/platform_widget_builder_test.dart => platform_aware_builder/platform_aware_builder_test.dart} (85%) diff --git a/catalyst_voices/packages/catalyst_voices_shared/lib/src/catalyst_voices_shared.dart b/catalyst_voices/packages/catalyst_voices_shared/lib/src/catalyst_voices_shared.dart index eed3d8f970a..1a8d7ac99d2 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/lib/src/catalyst_voices_shared.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/lib/src/catalyst_voices_shared.dart @@ -1,2 +1,2 @@ export 'platform/catalyst_platform.dart'; -export 'platform_widget_builder/platform_widget_builder.dart'; +export 'platform_aware_builder/platform_aware_builder.dart'; diff --git a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_widget_builder/platform_widget_builder.dart b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart similarity index 95% rename from catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_widget_builder/platform_widget_builder.dart rename to catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart index 78832986a9e..99db7369f24 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_widget_builder/platform_widget_builder.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart @@ -1,7 +1,7 @@ import 'package:catalyst_voices_shared/src/platform/catalyst_platform.dart'; import 'package:flutter/widgets.dart'; -class PlatformWidgetBuilder extends StatelessWidget { +class PlatformAwareBuilder extends StatelessWidget { final Widget Function(BuildContext context, T? data) builder; final T? android; final T? desktop; @@ -16,7 +16,7 @@ class PlatformWidgetBuilder extends StatelessWidget { final T? windows; final T? other; - const PlatformWidgetBuilder({ + const PlatformAwareBuilder({ super.key, required this.builder, this.android, diff --git a/catalyst_voices/packages/catalyst_voices_shared/test/src/catalyst_voices_shared_test.dart b/catalyst_voices/packages/catalyst_voices_shared/test/src/catalyst_voices_shared_test.dart index f5af33b44ab..7430153ff4e 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/test/src/catalyst_voices_shared_test.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/test/src/catalyst_voices_shared_test.dart @@ -1 +1 @@ -export 'platform_widget_builder/platform_widget_builder_test.dart'; +export 'platform_aware_builder/platform_aware_builder_test.dart'; diff --git a/catalyst_voices/packages/catalyst_voices_shared/test/src/platform_widget_builder/platform_widget_builder_test.dart b/catalyst_voices/packages/catalyst_voices_shared/test/src/platform_aware_builder/platform_aware_builder_test.dart similarity index 85% rename from catalyst_voices/packages/catalyst_voices_shared/test/src/platform_widget_builder/platform_widget_builder_test.dart rename to catalyst_voices/packages/catalyst_voices_shared/test/src/platform_aware_builder/platform_aware_builder_test.dart index bb8c8ba4b54..fac277aba99 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/test/src/platform_widget_builder/platform_widget_builder_test.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/test/src/platform_aware_builder/platform_aware_builder_test.dart @@ -1,4 +1,4 @@ -import 'package:catalyst_voices_shared/catalyst_voices_shared.dart'; +import 'package:catalyst_voices_shared/src/catalyst_voices_shared.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -6,7 +6,7 @@ void main() { Widget buildApp() => MaterialApp( home: Scaffold( - body: PlatformWidgetBuilder( + body: PlatformAwareBuilder( other: 'other', builder: (context, platformData) => Text(platformData!), ), From 481f7e58c3583cd888841edcf9e06d640db96047 Mon Sep 17 00:00:00 2001 From: Lucio Baglione Date: Wed, 20 Mar 2024 18:18:48 +0100 Subject: [PATCH 5/8] refactor: Use platform identifiers to select proper data to be used. --- .../lib/src/platform/io_platform.dart | 14 +++ .../lib/src/platform/stub_platform.dart | 4 + .../lib/src/platform/web_platform.dart | 14 +++ .../platform_aware_builder.dart | 97 +++++++++---------- 4 files changed, 76 insertions(+), 53 deletions(-) diff --git a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/io_platform.dart b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/io_platform.dart index bf4faa30aa0..be90a87dae5 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/io_platform.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/io_platform.dart @@ -23,4 +23,18 @@ final class CatalystPlatform { static bool get isWebDesktop => false; static bool get isWindows => Platform.isWindows; + + static Map get identifiers => { + 'android': isAndroid, + 'desktop': isDesktop, + 'fuchsia': isFuchsia, + 'iOS': isIOS, + 'linux': isLinux, + 'macOS': isMacOS, + 'mobile': isMobile, + 'mobileWeb': isMobileWeb, + 'web': isWeb, + 'webDesktop': isWebDesktop, + 'windows': isWindows, + }; } diff --git a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/stub_platform.dart b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/stub_platform.dart index 539a794b214..ea9a2329b09 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/stub_platform.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/stub_platform.dart @@ -43,5 +43,9 @@ final class CatalystPlatform { throw UnimplementedError('Stub CatalystPlatform'); } + static Map get identifiers { + throw UnimplementedError('Stub CatalystPlatform'); + } + const CatalystPlatform._(); } diff --git a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/web_platform.dart b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/web_platform.dart index 4f74a8726f2..7f6a5ccb106 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/web_platform.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/web_platform.dart @@ -34,5 +34,19 @@ final class CatalystPlatform { return mobileIdentifiers.any(userAgent.contains); } + static Map get identifiers => { + 'android': isAndroid, + 'desktop': isDesktop, + 'fuchsia': isFuchsia, + 'iOS': isIOS, + 'linux': isLinux, + 'macOS': isMacOS, + 'mobile': isMobile, + 'mobileWeb': isMobileWeb, + 'web': isWeb, + 'webDesktop': isWebDesktop, + 'windows': isWindows, + }; + const CatalystPlatform._(); } diff --git a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart index 99db7369f24..0aadc3e8131 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart @@ -1,66 +1,57 @@ +import 'dart:developer'; +import 'dart:io'; + import 'package:catalyst_voices_shared/src/platform/catalyst_platform.dart'; import 'package:flutter/widgets.dart'; class PlatformAwareBuilder extends StatelessWidget { final Widget Function(BuildContext context, T? data) builder; - final T? android; - final T? desktop; - final T? fuchsia; - final T? iOS; - final T? linux; - final T? macOS; - final T? mobile; - final T? mobileWeb; - final T? web; - final T? webDesktop; - final T? windows; - final T? other; + final Map _platformData; - const PlatformAwareBuilder({ + PlatformAwareBuilder({ super.key, required this.builder, - this.android, - this.desktop, - this.fuchsia, - this.iOS, - this.linux, - this.macOS, - this.mobile, - this.mobileWeb, - this.web, - this.webDesktop, - this.windows, - this.other, - }); + T? android, + T? desktop, + T? fuchsia, + T? iOS, + T? linux, + T? macOS, + T? mobile, + T? mobileWeb, + T? isWeb, + T? webDesktop, + T? windows, + required T other, + }) : _platformData = { + 'android': android, + 'desktop': desktop, + 'fuchsia': fuchsia, + 'iOS': iOS, + 'linux': linux, + 'macOS': macOS, + 'mobile': mobile, + 'mobileWeb': mobileWeb, + 'web': isWeb, + 'webDesktop': webDesktop, + 'windows': windows, + 'other': other, + }; @override Widget build(BuildContext context) { - if (CatalystPlatform.isAndroid && android != null) { - return builder(context, android as T); - } else if (CatalystPlatform.isDesktop && desktop != null) { - return builder(context, desktop as T); - } else if (CatalystPlatform.isFuchsia && fuchsia != null) { - return builder(context, fuchsia as T); - } else if (CatalystPlatform.isIOS && iOS != null) { - return builder(context, iOS as T); - } else if (CatalystPlatform.isLinux && linux != null) { - return builder(context, linux as T); - } else if (CatalystPlatform.isMacOS && macOS != null) { - return builder(context, linux as T); - } else if (CatalystPlatform.isMobile && mobile != null) { - return builder(context, mobile as T); - } else if (CatalystPlatform.isMobileWeb && mobileWeb != null) { - return builder(context, mobileWeb as T); - } else if (CatalystPlatform.isWeb && web != null) { - return builder(context, web as T); - } else if (CatalystPlatform.isWebDesktop && webDesktop != null) { - return builder(context, webDesktop as T); - } else if (CatalystPlatform.isWindows && windows != null) { - return builder(context, windows as T); - } else if (other != null) { - return builder(context, other as T); - } else { - return builder(context, null); - } + return builder(context, _getPlatformData()); + } + + T _getPlatformData() { + final currentPlatformKey = CatalystPlatform.identifiers.entries + .firstWhere( + // We select the platform only if the platform-specific data + // is also present. + (entry) => entry.value && (_platformData[entry.key] != null), + orElse: () => const MapEntry('other', true), + ) + .key; + return _platformData[currentPlatformKey]!; } } From cb794be30f115b28d801c15e08078e99b2532586 Mon Sep 17 00:00:00 2001 From: Lucio Baglione Date: Wed, 20 Mar 2024 19:12:37 +0100 Subject: [PATCH 6/8] refactor: Use `PlatformKey` enum for platform identifiers. --- .../lib/src/platform/io_platform.dart | 26 ++++++++------- .../lib/src/platform/platform_key.dart | 14 ++++++++ .../lib/src/platform/stub_platform.dart | 4 ++- .../lib/src/platform/web_platform.dart | 27 ++++++++-------- .../platform_aware_builder.dart | 32 +++++++++---------- 5 files changed, 60 insertions(+), 43 deletions(-) create mode 100644 catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/platform_key.dart diff --git a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/io_platform.dart b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/io_platform.dart index be90a87dae5..5b3db720fea 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/io_platform.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/io_platform.dart @@ -1,5 +1,7 @@ import 'dart:io'; +import 'package:catalyst_voices_shared/src/platform/platform_key.dart'; + final class CatalystPlatform { static bool get isAndroid => Platform.isAndroid; @@ -24,17 +26,17 @@ final class CatalystPlatform { static bool get isWindows => Platform.isWindows; - static Map get identifiers => { - 'android': isAndroid, - 'desktop': isDesktop, - 'fuchsia': isFuchsia, - 'iOS': isIOS, - 'linux': isLinux, - 'macOS': isMacOS, - 'mobile': isMobile, - 'mobileWeb': isMobileWeb, - 'web': isWeb, - 'webDesktop': isWebDesktop, - 'windows': isWindows, + static Map get identifiers => { + PlatformKey.android: isAndroid, + PlatformKey.desktop: isDesktop, + PlatformKey.fuchsia: isFuchsia, + PlatformKey.iOS: isIOS, + PlatformKey.linux: isLinux, + PlatformKey.macOS: isMacOS, + PlatformKey.mobile: isMobile, + PlatformKey.mobileWeb: isMobileWeb, + PlatformKey.web: isWeb, + PlatformKey.webDesktop: isWebDesktop, + PlatformKey.windows: isWindows, }; } diff --git a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/platform_key.dart b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/platform_key.dart new file mode 100644 index 00000000000..e8b6a7890d7 --- /dev/null +++ b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/platform_key.dart @@ -0,0 +1,14 @@ +enum PlatformKey { + android, + desktop, + fuchsia, + iOS, + linux, + macOS, + mobile, + mobileWeb, + web, + webDesktop, + windows, + other, +} diff --git a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/stub_platform.dart b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/stub_platform.dart index ea9a2329b09..ab1c6929bae 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/stub_platform.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/stub_platform.dart @@ -1,3 +1,5 @@ +import 'package:catalyst_voices_shared/src/platform/platform_key.dart'; + final class CatalystPlatform { static bool get isAndroid { throw UnimplementedError('Stub CatalystPlatform'); @@ -43,7 +45,7 @@ final class CatalystPlatform { throw UnimplementedError('Stub CatalystPlatform'); } - static Map get identifiers { + static Map get identifiers { throw UnimplementedError('Stub CatalystPlatform'); } diff --git a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/web_platform.dart b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/web_platform.dart index 7f6a5ccb106..a7a9d074d79 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/web_platform.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform/web_platform.dart @@ -1,3 +1,4 @@ +import 'package:catalyst_voices_shared/src/platform/platform_key.dart'; import 'package:flutter/foundation.dart' show kIsWeb; import 'package:web/web.dart'; @@ -33,19 +34,19 @@ final class CatalystPlatform { ]; return mobileIdentifiers.any(userAgent.contains); } - - static Map get identifiers => { - 'android': isAndroid, - 'desktop': isDesktop, - 'fuchsia': isFuchsia, - 'iOS': isIOS, - 'linux': isLinux, - 'macOS': isMacOS, - 'mobile': isMobile, - 'mobileWeb': isMobileWeb, - 'web': isWeb, - 'webDesktop': isWebDesktop, - 'windows': isWindows, + + static Map get identifiers => { + PlatformKey.android: isAndroid, + PlatformKey.desktop: isDesktop, + PlatformKey.fuchsia: isFuchsia, + PlatformKey.iOS: isIOS, + PlatformKey.linux: isLinux, + PlatformKey.macOS: isMacOS, + PlatformKey.mobile: isMobile, + PlatformKey.mobileWeb: isMobileWeb, + PlatformKey.web: isWeb, + PlatformKey.webDesktop: isWebDesktop, + PlatformKey.windows: isWindows, }; const CatalystPlatform._(); diff --git a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart index 0aadc3e8131..5073c6d054a 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart @@ -1,12 +1,10 @@ -import 'dart:developer'; -import 'dart:io'; - import 'package:catalyst_voices_shared/src/platform/catalyst_platform.dart'; +import 'package:catalyst_voices_shared/src/platform/platform_key.dart'; import 'package:flutter/widgets.dart'; class PlatformAwareBuilder extends StatelessWidget { final Widget Function(BuildContext context, T? data) builder; - final Map _platformData; + final Map _platformData; PlatformAwareBuilder({ super.key, @@ -24,18 +22,18 @@ class PlatformAwareBuilder extends StatelessWidget { T? windows, required T other, }) : _platformData = { - 'android': android, - 'desktop': desktop, - 'fuchsia': fuchsia, - 'iOS': iOS, - 'linux': linux, - 'macOS': macOS, - 'mobile': mobile, - 'mobileWeb': mobileWeb, - 'web': isWeb, - 'webDesktop': webDesktop, - 'windows': windows, - 'other': other, + PlatformKey.android: android, + PlatformKey.desktop: desktop, + PlatformKey.fuchsia: fuchsia, + PlatformKey.iOS: iOS, + PlatformKey.linux: linux, + PlatformKey.macOS: macOS, + PlatformKey.mobile: mobile, + PlatformKey.mobileWeb: mobileWeb, + PlatformKey.web: isWeb, + PlatformKey.webDesktop: webDesktop, + PlatformKey.windows: windows, + PlatformKey.other: other, }; @override @@ -49,7 +47,7 @@ class PlatformAwareBuilder extends StatelessWidget { // We select the platform only if the platform-specific data // is also present. (entry) => entry.value && (_platformData[entry.key] != null), - orElse: () => const MapEntry('other', true), + orElse: () => const MapEntry(PlatformKey.other, true), ) .key; return _platformData[currentPlatformKey]!; From 0a6fa021e5a551c8c728657592175718a393e3a7 Mon Sep 17 00:00:00 2001 From: Lucio Baglione Date: Wed, 20 Mar 2024 19:37:57 +0100 Subject: [PATCH 7/8] docs: Add `PlatformAwareBuilder` documentation. --- .../platform_aware_builder.dart | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart index 5073c6d054a..21f49e0c997 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart @@ -2,6 +2,46 @@ import 'package:catalyst_voices_shared/src/platform/catalyst_platform.dart'; import 'package:catalyst_voices_shared/src/platform/platform_key.dart'; import 'package:flutter/widgets.dart'; +// A [StatelessWidget] that is aware of the current platform. +// +// This is an abstract widget that has a required argument [builder] that can +// consume platform-specific data automatically based on platform that is +// detected. +// +// The platform detection happens in [CatalystPlatform]. +// +// The widget accepts an argument for each specific platform defined in +// [PlatformKey]. The platform specific [data] is selected when two conditions +// are verified at the same time: +// - the platform is detected +// - the platform-specific argument is present +// In case those conditions are not verified the [other] argument is used (and +// because of this it is required). +// The type of the platform specific data is generic. +// +// A simple usage is to render a string based on the platform: +// +// ```dart +// PlatformWidgetBuilder( +// android: 'This is an Android platform.', +// other: 'This is an other platform.', +// builder: (context, title) => Text(title!), +// ); +// ``` +// +// or to have a specific Padding: +// +// ```dart +// PlatformWidgetBuilder( +// android: EdgeInsets.all(10.0), +// other: EdgeInsets.all(40.0), +// builder: (context, padding) => Padding( +// padding: padding, +// child: Text('This is an example.') +// ), +// ); +// ``` + class PlatformAwareBuilder extends StatelessWidget { final Widget Function(BuildContext context, T? data) builder; final Map _platformData; @@ -17,7 +57,7 @@ class PlatformAwareBuilder extends StatelessWidget { T? macOS, T? mobile, T? mobileWeb, - T? isWeb, + T? web, T? webDesktop, T? windows, required T other, @@ -30,7 +70,7 @@ class PlatformAwareBuilder extends StatelessWidget { PlatformKey.macOS: macOS, PlatformKey.mobile: mobile, PlatformKey.mobileWeb: mobileWeb, - PlatformKey.web: isWeb, + PlatformKey.web: web, PlatformKey.webDesktop: webDesktop, PlatformKey.windows: windows, PlatformKey.other: other, From fe4b6ffad2fcaae890cd2cad7d0dbaeffedb3b86 Mon Sep 17 00:00:00 2001 From: Lucio Baglione Date: Thu, 21 Mar 2024 09:12:26 +0100 Subject: [PATCH 8/8] docs: Improve `PlatformAwareBuilder` description. --- .../lib/src/platform_aware_builder/platform_aware_builder.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart index 21f49e0c997..8a444265191 100644 --- a/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart +++ b/catalyst_voices/packages/catalyst_voices_shared/lib/src/platform_aware_builder/platform_aware_builder.dart @@ -2,7 +2,8 @@ import 'package:catalyst_voices_shared/src/platform/catalyst_platform.dart'; import 'package:catalyst_voices_shared/src/platform/platform_key.dart'; import 'package:flutter/widgets.dart'; -// A [StatelessWidget] that is aware of the current platform. +// A [PlatformAwareBuilder] is a StatelessWidget that is aware of the current +// platform. // // This is an abstract widget that has a required argument [builder] that can // consume platform-specific data automatically based on platform that is