Browse Source

Merge pull request #479 from provokateurin/refactor/neon_platform

refactor(neon): make NeonPlatform an interface
pull/481/head
Nikolas Rimikis 1 year ago committed by GitHub
parent
commit
c627a152b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 56
      packages/neon/neon/lib/src/platform/android.dart
  2. 55
      packages/neon/neon/lib/src/platform/linux.dart
  3. 50
      packages/neon/neon/lib/src/platform/platform.dart

56
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;
if (!await Permission.storage.request().isGranted) {
throw MissingPermissionException(Permission.storage); @override
} bool get canUseSystemTray => false;
return p.join((await getExternalStorageDirectory())!.path);
}, @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) {
throw MissingPermissionException(Permission.storage);
}
return p.join((await getExternalStorageDirectory())!.path);
}
@override
void init() {}
} }

55
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
xdg.cacheHome.absolute.path, bool get canUseSystemTray => true;
'de.provokateurin.neon',
), @override
getUserAccessibleAppDataPath: () async => p.join(Platform.environment['HOME']!, 'Neon'), bool get canUseWindowManager => true;
init: () async {
sqfliteFfiInit(); @override
databaseFactory = databaseFactoryFfi; 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;
}
} }

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