mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-16 21:33:41 +00:00
feat: Background download on iOS (#1442)
This commit is contained in:
1 parent
9d3221ebbb
commit
99924296ef
14 files changed
+383
-26
No files matched your search
@@ -0,0 +1,32 @@
|
||||
//for ios
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class LocationKeepAlive {
|
||||
static const _channel = MethodChannel('gopeed/location_keep_alive');
|
||||
|
||||
static Future<bool> requestPermission() async {
|
||||
if (!Platform.isIOS) return false;
|
||||
|
||||
try {
|
||||
final result = await _channel.invokeMethod<bool>('requestPermission');
|
||||
|
||||
return result ?? false;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> start() async {
|
||||
if (!Platform.isIOS) return;
|
||||
|
||||
await _channel.invokeMethod('start');
|
||||
}
|
||||
|
||||
static Future<void> stop() async {
|
||||
if (!Platform.isIOS) return;
|
||||
|
||||
await _channel.invokeMethod('stop');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import 'dart:async';
|
||||
import 'package:get/get.dart';
|
||||
import '../../../api/api.dart' as api;
|
||||
import '../../../api/model/task.dart';
|
||||
import '../modules/app/controllers/app_controller.dart';
|
||||
import 'location_keep_alive.dart';
|
||||
|
||||
class LocationKeepAliveCoordinator extends GetxService {
|
||||
Timer? _pollTimer;
|
||||
bool _reconciling = false;
|
||||
|
||||
/// Overridable for testing, defaults to reading from AppController.
|
||||
bool Function() keepAliveEnabledProvider = () => Get.find<AppController>()
|
||||
.downloaderConfig
|
||||
.value
|
||||
.extra
|
||||
.backgroundLocationKeepAlive;
|
||||
|
||||
/// Overridable for testing so polling tests don't need to wait 10s.
|
||||
Duration pollInterval = const Duration(seconds: 10);
|
||||
|
||||
bool get _keepAliveEnabled => keepAliveEnabledProvider();
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
api.setTaskChangedListener(() {
|
||||
reconcile();
|
||||
});
|
||||
}
|
||||
|
||||
Future<int> _fetchGlobalRunningCount() async {
|
||||
try {
|
||||
final tasks = await api.getTasks([Status.running]);
|
||||
return tasks.length;
|
||||
} catch (_) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> reconcile() async {
|
||||
if (_reconciling) return;
|
||||
_reconciling = true;
|
||||
try {
|
||||
if (!_keepAliveEnabled) {
|
||||
await LocationKeepAlive.stop();
|
||||
return;
|
||||
}
|
||||
final count = await _fetchGlobalRunningCount();
|
||||
if (count < 0) return;
|
||||
if (count > 0) {
|
||||
await LocationKeepAlive.start();
|
||||
} else {
|
||||
await LocationKeepAlive.stop();
|
||||
}
|
||||
} finally {
|
||||
_reconciling = false;
|
||||
}
|
||||
}
|
||||
|
||||
void startPolling() {
|
||||
_pollTimer?.cancel();
|
||||
_pollTimer = Timer.periodic(pollInterval, (_) => reconcile());
|
||||
}
|
||||
|
||||
void stopPolling() {
|
||||
_pollTimer?.cancel();
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
stopPolling();
|
||||
super.onClose();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user