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. 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: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<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: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;
}
}

50
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<NeonPlatform> 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<String> Function() getApplicationCachePath;
FutureOr<String> getApplicationCachePath();
final Future<String> Function() getUserAccessibleAppDataPath;
FutureOr<String> getUserAccessibleAppDataPath();
final Future Function()? init;
FutureOr init();
}

Loading…
Cancel
Save