Skip to content

Commit

Permalink
Merge pull request #1608 from nextcloud/refactor/neon_framework/platf…
Browse files Browse the repository at this point in the history
…orm-plugin
  • Loading branch information
provokateurin authored Feb 17, 2024
2 parents 2803eb3 + 853f1db commit 7975430
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 55 deletions.
2 changes: 1 addition & 1 deletion packages/neon_framework/lib/neon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Future<void> 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();
Expand Down
2 changes: 2 additions & 0 deletions packages/neon_framework/lib/neon_framework.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'package:neon_framework/src/platform/android.dart';
export 'package:neon_framework/src/platform/linux.dart';
9 changes: 6 additions & 3 deletions packages/neon_framework/lib/src/platform/android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 6 additions & 3 deletions packages/neon_framework/lib/src/platform/linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;

Expand Down
42 changes: 7 additions & 35 deletions packages/neon_framework/lib/src/platform/platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> 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.
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions packages/neon_framework/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ flutter:
assets:
- assets/
- assets/icons/server/
plugin:
platforms:
linux:
dartPluginClass: LinuxNeonPlatform
android:
dartPluginClass: AndroidNeonPlatform
12 changes: 0 additions & 12 deletions packages/neon_framework/test/neon_platform_test.dart

This file was deleted.

2 changes: 1 addition & 1 deletion packages/neon_framework/test/user_status_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7975430

Please sign in to comment.