diff --git a/packages/neon_framework/lib/neon.dart b/packages/neon_framework/lib/neon.dart index 280389ccbd3..381e0f2a75d 100644 --- a/packages/neon_framework/lib/neon.dart +++ b/packages/neon_framework/lib/neon.dart @@ -34,7 +34,7 @@ Future runNeon({ final binding = bindingOverride ?? WidgetsFlutterBinding.ensureInitialized(); FlutterNativeSplash.preserve(widgetsBinding: binding); - await NeonPlatform.setup(); + await NeonPlatform.instance.init(); await NeonStorage().init(); final packageInfo = await PackageInfo.fromPlatform(); diff --git a/packages/neon_framework/lib/neon_framework.dart b/packages/neon_framework/lib/neon_framework.dart new file mode 100644 index 00000000000..3af0b91cebb --- /dev/null +++ b/packages/neon_framework/lib/neon_framework.dart @@ -0,0 +1,2 @@ +export 'package:neon_framework/src/platform/android.dart'; +export 'package:neon_framework/src/platform/linux.dart'; diff --git a/packages/neon_framework/lib/src/platform/android.dart b/packages/neon_framework/lib/src/platform/android.dart index 5a03abb9504..c12fc3475b3 100644 --- a/packages/neon_framework/lib/src/platform/android.dart +++ b/packages/neon_framework/lib/src/platform/android.dart @@ -2,20 +2,23 @@ import 'dart:typed_data'; import 'package:flutter_file_dialog/flutter_file_dialog.dart'; import 'package:meta/meta.dart'; -import 'package:neon_framework/src/platform/linux.dart'; import 'package:neon_framework/src/platform/platform.dart'; /// Android specific platform information. /// /// See: /// * [NeonPlatform] to initialize and acquire an instance -/// * [LinuxNeonPlatform] for the Linux implementation +/// * `LinuxNeonPlatform` for the Linux implementation @immutable -@internal class AndroidNeonPlatform implements NeonPlatform { /// Creates a new Android Neon platform. const AndroidNeonPlatform(); + /// Registers this platform. + static void registerWith() { + NeonPlatform.instance = const AndroidNeonPlatform(); + } + @override bool get canUseCamera => true; diff --git a/packages/neon_framework/lib/src/platform/linux.dart b/packages/neon_framework/lib/src/platform/linux.dart index f45369092e5..b219818d1f9 100644 --- a/packages/neon_framework/lib/src/platform/linux.dart +++ b/packages/neon_framework/lib/src/platform/linux.dart @@ -2,7 +2,6 @@ import 'dart:typed_data'; import 'package:file_picker/file_picker.dart'; import 'package:meta/meta.dart'; -import 'package:neon_framework/src/platform/android.dart'; import 'package:neon_framework/src/platform/platform.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; import 'package:universal_io/io.dart'; @@ -11,13 +10,17 @@ import 'package:universal_io/io.dart'; /// /// See: /// * [NeonPlatform] to initialize and acquire an instance -/// * [AndroidNeonPlatform] for the Android implementation +/// * `AndroidNeonPlatform` for the Android implementation @immutable -@internal class LinuxNeonPlatform implements NeonPlatform { /// Creates a new Linux Neon platform. const LinuxNeonPlatform(); + /// Registers this platform. + static void registerWith() { + NeonPlatform.instance = const LinuxNeonPlatform(); + } + @override bool get canUseWebView => false; diff --git a/packages/neon_framework/lib/src/platform/platform.dart b/packages/neon_framework/lib/src/platform/platform.dart index 9ad8fd8cac8..13531314222 100644 --- a/packages/neon_framework/lib/src/platform/platform.dart +++ b/packages/neon_framework/lib/src/platform/platform.dart @@ -2,55 +2,27 @@ import 'dart:async'; import 'dart:typed_data'; import 'package:meta/meta.dart'; -import 'package:neon_framework/src/platform/android.dart'; -import 'package:neon_framework/src/platform/linux.dart'; -import 'package:universal_io/io.dart'; /// Implements platform specific functionality and exposes the availability of certain features. /// -/// [NeonPlatform.setup] mus be called and completed before acquiring the [instance]. -/// /// See: -/// * [AndroidNeonPlatform] for the Android implementation -/// * [LinuxNeonPlatform] for the Linux implementation +/// * `AndroidNeonPlatform` for the Android implementation +/// * `LinuxNeonPlatform` for the Linux implementation @immutable abstract interface class NeonPlatform { - /// Initializes the platform with the given mocked [platform]. - @visibleForTesting - factory NeonPlatform.mocked(NeonPlatform platform) => _platform = platform; + static NeonPlatform? _instance; - static NeonPlatform? _platform; - - /// Infers and configures the platform automatically. - /// - /// Required to be called before accessing [NeonPlatform.instance]. - static Future setup() async { - if (_platform != null) { - return; - } - - if (Platform.isAndroid) { - _platform = const AndroidNeonPlatform(); - } else if (Platform.isLinux) { - _platform = const LinuxNeonPlatform(); - } else { - throw UnimplementedError('No implementation for platform ${Platform.operatingSystem} found'); - } - - await _platform!.init(); - } + static set instance(NeonPlatform instance) => _instance = instance; /// Gets the current instance of [NeonPlatform]. - /// - /// Make sure [NeonPlatform.setup] has been called before accessing the instance. static NeonPlatform get instance { - if (_platform == null) { + if (_instance == null) { throw StateError( 'NeonPlatform has not been set up yet. Please make sure NeonPlatform.setup() has been called before and completed.', ); } - return _platform!; + return _instance!; } /// Whether this platform supports web views. @@ -78,7 +50,7 @@ abstract interface class NeonPlatform { /// The support depends on `https://pub.dev/packages/unifiedpush`. abstract final bool canUsePushNotifications; - /// Wether this platform can use native sharing. + /// Whether this platform can use native sharing. /// /// The support depends on `https://pub.dev/packages/share_plus`. abstract final bool canUseSharing; diff --git a/packages/neon_framework/pubspec.yaml b/packages/neon_framework/pubspec.yaml index 9a8cdcca26f..d4d4fbb27a6 100644 --- a/packages/neon_framework/pubspec.yaml +++ b/packages/neon_framework/pubspec.yaml @@ -78,3 +78,9 @@ flutter: assets: - assets/ - assets/icons/server/ + plugin: + platforms: + linux: + dartPluginClass: LinuxNeonPlatform + android: + dartPluginClass: AndroidNeonPlatform diff --git a/packages/neon_framework/test/neon_platform_test.dart b/packages/neon_framework/test/neon_platform_test.dart index 8cea08532c4..f07ec3e892b 100644 --- a/packages/neon_framework/test/neon_platform_test.dart +++ b/packages/neon_framework/test/neon_platform_test.dart @@ -5,7 +5,7 @@ void main() { test('NeonPlatform', () async { expect(() => NeonPlatform.instance, throwsA(isA())); - await NeonPlatform.setup(); + await NeonPlatform.instance.init(); expect(NeonPlatform.instance, isA()); }); diff --git a/packages/neon_framework/test/user_status_bloc_test.dart b/packages/neon_framework/test/user_status_bloc_test.dart index d55ff25f6d3..0c525d664f9 100644 --- a/packages/neon_framework/test/user_status_bloc_test.dart +++ b/packages/neon_framework/test/user_status_bloc_test.dart @@ -134,7 +134,7 @@ void main() { setUpAll(() { final platform = MockNeonPlatform(); when(() => platform.canUseWindowManager).thenReturn(false); - NeonPlatform.mocked(platform); + NeonPlatform.instance = platform; final storage = MockNeonStorage(); when(() => storage.requestCache).thenReturn(null);