freezer/lib/api/download_manager/download_manager.dart

76 lines
2.4 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'dart:isolate';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:freezer/api/definitions.dart';
import 'package:freezer/api/download_manager/download_service.dart';
import 'package:freezer/api/download_manager/service_interface.dart';
import 'package:freezer/translations.i18n.dart';
class DownloadManager {
//implements dl.DownloadManager {
SendPort? _sendPort;
Isolate? _isolate;
Future<bool> configure() {
if (Platform.isAndroid || Platform.isIOS) {
return FlutterBackgroundService().configure(
iosConfiguration: IosConfiguration(), // fuck ios
androidConfiguration: AndroidConfiguration(
onStart: _startNative,
isForegroundMode: false,
autoStart: false,
autoStartOnBoot: false,
foregroundServiceNotificationId: DownloadService.NOTIFICATION_ID,
notificationChannelId: DownloadService.NOTIFICATION_CHANNEL_ID,
initialNotificationTitle: 'Freezer'.i18n,
initialNotificationContent: 'Starting download service...'.i18n,
));
}
// will run in foreground instead, in a separate isolate
return Future.value(true);
}
Future<bool> startService() async {
if (Platform.isAndroid) {
return FlutterBackgroundService().startService();
}
final receivePort = ReceivePort();
_sendPort = receivePort.sendPort;
_isolate = await Isolate.spawn(
_startService, ServiceInterface(receivePort: receivePort));
return true;
}
void kill() {
_isolate?.kill();
}
void invoke(String method, [Map<String, dynamic>? args]) {
if (_sendPort != null) {
_sendPort!.send({
'method': method,
if (args != null) ...args,
});
return;
}
FlutterBackgroundService().invoke(method, args);
}
Future<bool> addOfflineTrack(Track track,
{bool private = true, BuildContext? context, isSingleton = false}) {
// TODO: implement addOfflineTrack
throw UnimplementedError();
}
static void _startNative(ServiceInstance service) =>
_startService(ServiceInterface(service: service));
static void _startService(ServiceInterface? service) =>
DownloadService(service).run();
}