mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-16 05:13: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
@@ -32,6 +32,8 @@ import '../../../views/outlined_button_loading.dart';
|
||||
import '../../../views/text_button_loading.dart';
|
||||
import '../../app/controllers/app_controller.dart';
|
||||
import '../controllers/setting_controller.dart';
|
||||
import '../../../services/location_keep_alive.dart';
|
||||
import '../../../services/location_keep_alive_coordinator.dart';
|
||||
|
||||
const _padding = SizedBox(height: 10);
|
||||
final _divider = const Divider().paddingOnly(left: 10, right: 10);
|
||||
@@ -521,6 +523,44 @@ class SettingView extends GetView<SettingController> {
|
||||
);
|
||||
});
|
||||
|
||||
final buildBackgroundLocationKeepAlive = !Platform.isIOS
|
||||
? () => const SizedBox.shrink()
|
||||
: _buildConfigItem(
|
||||
'backgroundLocationKeepAlive',
|
||||
() {
|
||||
return appController
|
||||
.downloaderConfig.value.extra.backgroundLocationKeepAlive
|
||||
? 'on'.tr
|
||||
: 'off'.tr;
|
||||
},
|
||||
(Key key) {
|
||||
return Container(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: Switch(
|
||||
value: appController
|
||||
.downloaderConfig.value.extra.backgroundLocationKeepAlive,
|
||||
onChanged: (bool value) async {
|
||||
if (value) {
|
||||
final authorized =
|
||||
await LocationKeepAlive.requestPermission();
|
||||
|
||||
if (!authorized) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
appController.downloaderConfig.update((val) {
|
||||
val!.extra.backgroundLocationKeepAlive = value;
|
||||
});
|
||||
|
||||
await debounceSave();
|
||||
await Get.find<LocationKeepAliveCoordinator>().reconcile();
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
// http config items start
|
||||
final httpConfig = downloaderCfg.value.protocolConfig.http;
|
||||
final buildHttpUa =
|
||||
@@ -1748,6 +1788,7 @@ class SettingView extends GetView<SettingController> {
|
||||
buildAutoStartup(),
|
||||
buildMenubarMode(),
|
||||
buildDesktopNotification(),
|
||||
buildBackgroundLocationKeepAlive(),
|
||||
]),
|
||||
)),
|
||||
Text('archives'.tr),
|
||||
|
||||
@@ -50,4 +50,5 @@ abstract class TaskListController extends GetxController {
|
||||
tasks.sort(compare);
|
||||
this.tasks.value = tasks;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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