diff --git a/packages/neon/neon/lib/src/platform/android.dart b/packages/neon/neon/lib/src/platform/android.dart index 90dbf997..20450859 100644 --- a/packages/neon/neon/lib/src/platform/android.dart +++ b/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:permission_handler/permission_handler.dart'; +@immutable @internal -class AndroidNeonPlatform extends NeonPlatform { - AndroidNeonPlatform() - : super( - canUseWebView: true, - canUseQuickActions: true, - canUseSystemTray: false, - canUseWindowManager: false, - canUseCamera: true, - canUsePushNotifications: true, - getApplicationCachePath: () async => (await getTemporaryDirectory()).absolute.path, - getUserAccessibleAppDataPath: () async { - if (!await Permission.storage.request().isGranted) { - throw MissingPermissionException(Permission.storage); - } - return p.join((await getExternalStorageDirectory())!.path); - }, - ); +class AndroidNeonPlatform implements NeonPlatform { + const AndroidNeonPlatform(); + + @override + bool get canUseCamera => true; + + @override + bool get canUsePushNotifications => true; + + @override + bool get canUseQuickActions => true; + + @override + bool get canUseSystemTray => false; + + @override + bool get canUseWebView => true; + + @override + bool get canUseWindowManager => false; + + @override + Future getApplicationCachePath() async { + final tempDir = await getTemporaryDirectory(); + return tempDir.absolute.path; + } + + @override + Future getUserAccessibleAppDataPath() async { + if (!await Permission.storage.request().isGranted) { + throw MissingPermissionException(Permission.storage); + } + + return p.join((await getExternalStorageDirectory())!.path); + } + + @override + void init() {} } diff --git a/packages/neon/neon/lib/src/platform/linux.dart b/packages/neon/neon/lib/src/platform/linux.dart index 49fb669c..0ec74e83 100644 --- a/packages/neon/neon/lib/src/platform/linux.dart +++ b/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:xdg_directories/xdg_directories.dart' as xdg; +@immutable @internal -class LinuxNeonPlatform extends NeonPlatform { - LinuxNeonPlatform() - : super( - canUseWebView: false, - canUseQuickActions: false, - canUseSystemTray: true, - canUseWindowManager: true, - canUseCamera: false, - canUsePushNotifications: false, - getApplicationCachePath: () async => p.join( - xdg.cacheHome.absolute.path, - 'de.provokateurin.neon', - ), - getUserAccessibleAppDataPath: () async => p.join(Platform.environment['HOME']!, 'Neon'), - init: () async { - sqfliteFfiInit(); - databaseFactory = databaseFactoryFfi; - }, - ); +class LinuxNeonPlatform implements NeonPlatform { + const LinuxNeonPlatform(); + + @override + bool get canUseWebView => false; + + @override + bool get canUseQuickActions => false; + + @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, + 'de.provokateurin.neon', + ); + + @override + String getUserAccessibleAppDataPath() => p.join(Platform.environment['HOME']!, 'Neon'); + + @override + void init() { + sqfliteFfiInit(); + databaseFactory = databaseFactoryFfi; + } } diff --git a/packages/neon/neon/lib/src/platform/platform.dart b/packages/neon/neon/lib/src/platform/platform.dart index e41f67ba..af6a06fb 100644 --- a/packages/neon/neon/lib/src/platform/platform.dart +++ b/packages/neon/neon/lib/src/platform/platform.dart @@ -1,53 +1,41 @@ +import 'dart:async'; import 'dart:io'; +import 'package:meta/meta.dart'; import 'package:neon/src/platform/android.dart'; import 'package:neon/src/platform/linux.dart'; Future getNeonPlatform() async { - NeonPlatform? platform; + final NeonPlatform platform; if (Platform.isAndroid) { - platform = AndroidNeonPlatform(); - } - if (Platform.isLinux) { - platform = LinuxNeonPlatform(); - } - - if (platform == null) { + platform = const AndroidNeonPlatform(); + } else if (Platform.isLinux) { + platform = const LinuxNeonPlatform(); + } else { throw UnimplementedError('No implementation for platform ${Platform.operatingSystem} found'); } - await platform.init?.call(); + await platform.init.call(); return platform; } -abstract class NeonPlatform { - NeonPlatform({ - required this.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; +@immutable +abstract interface class NeonPlatform { + abstract 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 Function() getApplicationCachePath; + FutureOr getApplicationCachePath(); - final Future Function() getUserAccessibleAppDataPath; + FutureOr getUserAccessibleAppDataPath(); - final Future Function()? init; + FutureOr init(); }