Browse Source

refactor(neon): make NeonPlatform an interface

pull/478/head
Nikolas Rimikis 1 year ago
parent
commit
7dfcaf2021
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 48
      packages/neon/neon/lib/src/platform/android.dart
  2. 47
      packages/neon/neon/lib/src/platform/linux.dart
  3. 50
      packages/neon/neon/lib/src/platform/platform.dart

48
packages/neon/neon/lib/src/platform/android.dart

@ -5,22 +5,44 @@ import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler/permission_handler.dart';
@immutable
@internal @internal
class AndroidNeonPlatform extends NeonPlatform { class AndroidNeonPlatform implements NeonPlatform {
AndroidNeonPlatform() const AndroidNeonPlatform();
: super(
canUseWebView: true, @override
canUseQuickActions: true, bool get canUseCamera => true;
canUseSystemTray: false,
canUseWindowManager: false, @override
canUseCamera: true, bool get canUsePushNotifications => true;
canUsePushNotifications: true,
getApplicationCachePath: () async => (await getTemporaryDirectory()).absolute.path, @override
getUserAccessibleAppDataPath: () async { bool get canUseQuickActions => true;
@override
bool get canUseSystemTray => false;
@override
bool get canUseWebView => true;
@override
bool get canUseWindowManager => false;
@override
Future<String> getApplicationCachePath() async {
final tempDir = await getTemporaryDirectory();
return tempDir.absolute.path;
}
@override
Future<String> getUserAccessibleAppDataPath() async {
if (!await Permission.storage.request().isGranted) { if (!await Permission.storage.request().isGranted) {
throw MissingPermissionException(Permission.storage); throw MissingPermissionException(Permission.storage);
} }
return p.join((await getExternalStorageDirectory())!.path); return p.join((await getExternalStorageDirectory())!.path);
}, }
);
@override
void init() {}
} }

47
packages/neon/neon/lib/src/platform/linux.dart

@ -6,24 +6,41 @@ import 'package:path/path.dart' as p;
import 'package:sqflite_common_ffi/sqflite_ffi.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:xdg_directories/xdg_directories.dart' as xdg; import 'package:xdg_directories/xdg_directories.dart' as xdg;
@immutable
@internal @internal
class LinuxNeonPlatform extends NeonPlatform { class LinuxNeonPlatform implements NeonPlatform {
LinuxNeonPlatform() const LinuxNeonPlatform();
: super(
canUseWebView: false, @override
canUseQuickActions: false, bool get canUseWebView => false;
canUseSystemTray: true,
canUseWindowManager: true, @override
canUseCamera: false, bool get canUseQuickActions => false;
canUsePushNotifications: false,
getApplicationCachePath: () async => p.join( @override
bool get canUseSystemTray => true;
@override
bool get canUseWindowManager => true;
@override
bool get canUseCamera => false;
@override
bool get canUsePushNotifications => false;
@override
String getApplicationCachePath() => p.join(
xdg.cacheHome.absolute.path, xdg.cacheHome.absolute.path,
'de.provokateurin.neon', 'de.provokateurin.neon',
), );
getUserAccessibleAppDataPath: () async => p.join(Platform.environment['HOME']!, 'Neon'),
init: () async { @override
String getUserAccessibleAppDataPath() => p.join(Platform.environment['HOME']!, 'Neon');
@override
void init() {
sqfliteFfiInit(); sqfliteFfiInit();
databaseFactory = databaseFactoryFfi; databaseFactory = databaseFactoryFfi;
}, }
);
} }

50
packages/neon/neon/lib/src/platform/platform.dart

@ -1,53 +1,41 @@
import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:meta/meta.dart';
import 'package:neon/src/platform/android.dart'; import 'package:neon/src/platform/android.dart';
import 'package:neon/src/platform/linux.dart'; import 'package:neon/src/platform/linux.dart';
Future<NeonPlatform> getNeonPlatform() async { Future<NeonPlatform> getNeonPlatform() async {
NeonPlatform? platform; final NeonPlatform platform;
if (Platform.isAndroid) { if (Platform.isAndroid) {
platform = AndroidNeonPlatform(); platform = const AndroidNeonPlatform();
} } else if (Platform.isLinux) {
if (Platform.isLinux) { platform = const LinuxNeonPlatform();
platform = LinuxNeonPlatform(); } else {
}
if (platform == null) {
throw UnimplementedError('No implementation for platform ${Platform.operatingSystem} found'); throw UnimplementedError('No implementation for platform ${Platform.operatingSystem} found');
} }
await platform.init?.call(); await platform.init.call();
return platform; return platform;
} }
abstract class NeonPlatform { @immutable
NeonPlatform({ abstract interface class NeonPlatform {
required this.canUseWebView, abstract final bool canUseWebView;
required this.canUseQuickActions,
required this.canUseSystemTray,
required this.canUseWindowManager,
required this.canUseCamera,
required this.canUsePushNotifications,
required this.getApplicationCachePath,
required this.getUserAccessibleAppDataPath,
this.init,
});
final bool canUseWebView;
final bool canUseQuickActions; abstract final bool canUseQuickActions;
final bool canUseSystemTray; abstract final bool canUseSystemTray;
final bool canUseWindowManager; abstract final bool canUseWindowManager;
final bool canUseCamera; abstract final bool canUseCamera;
final bool canUsePushNotifications; abstract final bool canUsePushNotifications;
final Future<String> Function() getApplicationCachePath; FutureOr<String> getApplicationCachePath();
final Future<String> Function() getUserAccessibleAppDataPath; FutureOr<String> getUserAccessibleAppDataPath();
final Future Function()? init; FutureOr init();
} }

Loading…
Cancel
Save