feat: Background download on iOS (#1442)

This commit is contained in:
Afterain authored and GitHub committed 2026-08-18 18:08:53 +08:00
1 parent 9d3221ebbb
commit 99924296ef
14 files changed
+383 -26

No files matched your search

+29 -8
View File
@@ -99,6 +99,17 @@ void init(String network, String address, String apiToken) {
_client = _Client(network, address, apiToken);
}
/// Used by LocationKeepAliveCoordinator to reconcile keep-alive
void Function()? _onTaskChanged;
void setTaskChangedListener(void Function() listener) {
_onTaskChanged = listener;
}
void _notifyTaskChanged() {
_onTaskChanged?.call();
}
Future<T> _parse<T>(
Future<Response> Function() fetch,
T Function(dynamic json)? fromJsonT,
@@ -130,15 +141,19 @@ Future<ResolveResult> resolve(ResolveTask resolveTask) async {
}
Future<String> createTask(CreateTask createTask) async {
return _parse<String>(
final result = await _parse<String>(
() => _client.dio.post("api/v1/tasks", data: createTask),
(data) => data as String);
_notifyTaskChanged();
return result;
}
Future<List<String>> createTaskBatch(CreateTaskBatch createTaskBatch) async {
return _parse<List<String>>(
final result = await _parse<List<String>>(
() => _client.dio.post("api/v1/tasks/batch", data: createTaskBatch),
(data) => (data as List).map((e) => e as String).toList());
_notifyTaskChanged();
return result;
}
Future<void> patchTask(String id, ResolveTask patchTask) async {
@@ -154,41 +169,47 @@ Future<List<Task>> getTasks(List<Status> statuses) async {
}
Future<void> pauseTask(String id) async {
return _parse(() => _client.dio.put("api/v1/tasks/$id/pause"), null);
await _parse(() => _client.dio.put("api/v1/tasks/$id/pause"), null);
_notifyTaskChanged();
}
Future<void> continueTask(String id) async {
return _parse(() => _client.dio.put("api/v1/tasks/$id/continue"), null);
await _parse(() => _client.dio.put("api/v1/tasks/$id/continue"), null);
_notifyTaskChanged();
}
Future<void> pauseAllTasks(List<String>? ids) async {
return _parse(
await _parse(
() => _client.dio.put("api/v1/tasks/pause", queryParameters: {
"id": ids,
}),
null);
_notifyTaskChanged();
}
Future<void> continueAllTasks(List<String>? ids) async {
return _parse(
await _parse(
() => _client.dio.put("api/v1/tasks/continue", queryParameters: {
"id": ids,
}),
null);
_notifyTaskChanged();
}
Future<void> deleteTask(String id, bool force) async {
return _parse(
await _parse(
() => _client.dio.delete("api/v1/tasks/$id?force=$force"), null);
_notifyTaskChanged();
}
Future<void> deleteTasks(List<String>? ids, bool force) async {
return _parse(
await _parse(
() => _client.dio.delete("api/v1/tasks", queryParameters: {
"id": ids,
"force": force,
}),
null);
_notifyTaskChanged();
}
Future<DownloaderConfig> getConfig() async {
@@ -113,6 +113,7 @@ class ExtraConfig {
bool notifyWhenNewVersion;
bool autoStartTasks;
bool desktopNotification;
bool backgroundLocationKeepAlive;
List<DownloadCategory> downloadCategories;
ExtraConfigBt bt = ExtraConfigBt();
@@ -127,6 +128,7 @@ class ExtraConfig {
this.notifyWhenNewVersion = true,
this.autoStartTasks = false,
this.desktopNotification = true,
this.backgroundLocationKeepAlive = false,
this.downloadCategories = const [],
});
@@ -111,6 +111,8 @@ ExtraConfig _$ExtraConfigFromJson(Map<String, dynamic> json) => ExtraConfig(
notifyWhenNewVersion: json['notifyWhenNewVersion'] as bool? ?? true,
autoStartTasks: json['autoStartTasks'] as bool? ?? false,
desktopNotification: json['desktopNotification'] as bool? ?? true,
backgroundLocationKeepAlive:
json['backgroundLocationKeepAlive'] as bool? ?? false,
downloadCategories: (json['downloadCategories'] as List<dynamic>?)
?.map((e) => DownloadCategory.fromJson(e as Map<String, dynamic>))
.toList() ??
@@ -130,6 +132,7 @@ Map<String, dynamic> _$ExtraConfigToJson(ExtraConfig instance) =>
'notifyWhenNewVersion': instance.notifyWhenNewVersion,
'autoStartTasks': instance.autoStartTasks,
'desktopNotification': instance.desktopNotification,
'backgroundLocationKeepAlive': instance.backgroundLocationKeepAlive,
'downloadCategories':
instance.downloadCategories.map((e) => e.toJson()).toList(),
'bt': instance.bt.toJson(),