feat(mobile-next): 导入官方 openim-flutter-demo 固定基线 3.8.3-patch.3 并完成双构建
- 上游: github.com/OpenIMSDK/openim-flutter-demo tag 3.8.3-patch.3 commit b3dfdb1e8aaeaaf6f0793e10cadd20d5c184a31f - 并行目录 mobile-next/ 原样导入(含 LICENSE),不覆盖现网 mobile/ - 锁定 Flutter 3.24.5 / Dart 3.5.4 / JDK 17 / Gradle 7.6.3 / AGP 7.3.1 - 实测 Android debug 与 release 构建均成功(见 docs/mobile-next-baseline-import.md) - 本提交可单独回退:git revert 1db1229(重写前) Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
library flutter_download_manager;
|
||||
|
||||
export 'src/downloader.dart';
|
||||
export 'src/download_request.dart';
|
||||
export 'src/download_status.dart';
|
||||
export 'src/download_task.dart';
|
||||
@@ -0,0 +1,24 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
class DownloadRequest {
|
||||
final String url;
|
||||
final String path;
|
||||
var cancelToken = CancelToken();
|
||||
var forceDownload = false;
|
||||
|
||||
DownloadRequest(
|
||||
this.url,
|
||||
this.path,
|
||||
);
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is DownloadRequest &&
|
||||
runtimeType == other.runtimeType &&
|
||||
url == other.url &&
|
||||
path == other.path;
|
||||
|
||||
@override
|
||||
int get hashCode => url.hashCode ^ path.hashCode;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
enum DownloadStatus { queued, downloading, completed, failed, paused, canceled }
|
||||
|
||||
extension DownloadStatusExtension on DownloadStatus {
|
||||
bool get isCompleted {
|
||||
switch (this) {
|
||||
case DownloadStatus.queued:
|
||||
return false;
|
||||
case DownloadStatus.downloading:
|
||||
return false;
|
||||
case DownloadStatus.paused:
|
||||
return false;
|
||||
case DownloadStatus.completed:
|
||||
return true;
|
||||
case DownloadStatus.failed:
|
||||
return true;
|
||||
|
||||
case DownloadStatus.canceled:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_download_manager/flutter_download_manager.dart';
|
||||
|
||||
class DownloadTask {
|
||||
final DownloadRequest request;
|
||||
ValueNotifier<DownloadStatus> status = ValueNotifier(DownloadStatus.queued);
|
||||
ValueNotifier<double> progress = ValueNotifier(0);
|
||||
|
||||
DownloadTask(
|
||||
this.request,
|
||||
);
|
||||
|
||||
Future<DownloadStatus> whenDownloadComplete(
|
||||
{Duration timeout = const Duration(hours: 2)}) async {
|
||||
var completer = Completer<DownloadStatus>();
|
||||
|
||||
if (status.value.isCompleted) {
|
||||
completer.complete(status.value);
|
||||
}
|
||||
|
||||
var listener;
|
||||
listener = () {
|
||||
if (status.value.isCompleted) {
|
||||
completer.complete(status.value);
|
||||
status.removeListener(listener);
|
||||
}
|
||||
};
|
||||
|
||||
status.addListener(listener);
|
||||
|
||||
return completer.future.timeout(timeout);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,361 @@
|
||||
import 'dart:async';
|
||||
import 'dart:collection';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_download_manager/flutter_download_manager.dart';
|
||||
|
||||
class DownloadManager {
|
||||
final Map<String, DownloadTask> _cache = <String, DownloadTask>{};
|
||||
final Queue<DownloadRequest> _queue = Queue();
|
||||
var dio = Dio();
|
||||
static const partialExtension = ".partial";
|
||||
static const tempExtension = ".temp";
|
||||
|
||||
static final DownloadManager _dm = new DownloadManager._internal();
|
||||
|
||||
DownloadManager._internal();
|
||||
|
||||
factory DownloadManager() {
|
||||
return _dm;
|
||||
}
|
||||
|
||||
void Function(int, int) createCallback(url, int partialFileLength) => (int received, int total) {
|
||||
getDownload(url)?.progress.value = (received + partialFileLength) / (total + partialFileLength);
|
||||
|
||||
if (total == -1) {}
|
||||
};
|
||||
|
||||
Future<void> download(String url, String savePath, cancelToken, {forceDownload = false}) async {
|
||||
late String partialFilePath;
|
||||
late File partialFile;
|
||||
try {
|
||||
var task = getDownload(url);
|
||||
|
||||
if (task == null || task.status.value == DownloadStatus.canceled) {
|
||||
return;
|
||||
}
|
||||
setStatus(task, DownloadStatus.downloading);
|
||||
|
||||
if (kDebugMode) {
|
||||
print(url);
|
||||
}
|
||||
var file = File(savePath.toString());
|
||||
partialFilePath = savePath + partialExtension;
|
||||
partialFile = File(partialFilePath);
|
||||
|
||||
var fileExist = await file.exists();
|
||||
var partialFileExist = await partialFile.exists();
|
||||
|
||||
if (fileExist) {
|
||||
if (kDebugMode) {
|
||||
print("File Exists");
|
||||
}
|
||||
setStatus(task, DownloadStatus.completed);
|
||||
} else if (partialFileExist) {
|
||||
if (kDebugMode) {
|
||||
print("Partial File Exists");
|
||||
}
|
||||
|
||||
var partialFileLength = await partialFile.length();
|
||||
|
||||
var response = await dio.download(url, partialFilePath + tempExtension,
|
||||
onReceiveProgress: createCallback(url, partialFileLength),
|
||||
options: Options(
|
||||
headers: {HttpHeaders.rangeHeader: 'bytes=$partialFileLength-'},
|
||||
),
|
||||
cancelToken: cancelToken,
|
||||
deleteOnError: false);
|
||||
|
||||
if (response.statusCode == HttpStatus.partialContent) {
|
||||
var ioSink = partialFile.openWrite(mode: FileMode.writeOnlyAppend);
|
||||
var _f = File(partialFilePath + tempExtension);
|
||||
await ioSink.addStream(_f.openRead());
|
||||
await _f.delete();
|
||||
await ioSink.close();
|
||||
await partialFile.rename(savePath);
|
||||
|
||||
setStatus(task, DownloadStatus.completed);
|
||||
}
|
||||
} else {
|
||||
var response = await dio.download(url, partialFilePath,
|
||||
onReceiveProgress: createCallback(url, 0), cancelToken: cancelToken, deleteOnError: false);
|
||||
|
||||
if (response.statusCode == HttpStatus.ok) {
|
||||
await partialFile.rename(savePath);
|
||||
setStatus(task, DownloadStatus.completed);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
var task = getDownload(url)!;
|
||||
|
||||
if (task.status.value != DownloadStatus.canceled && task.status.value != DownloadStatus.paused) {
|
||||
setStatus(task, DownloadStatus.failed);
|
||||
|
||||
if (_queue.isNotEmpty) {
|
||||
_startExecution();
|
||||
}
|
||||
rethrow;
|
||||
} else if (task.status.value == DownloadStatus.paused) {
|
||||
final ioSink = partialFile.openWrite(mode: FileMode.writeOnlyAppend);
|
||||
final f = File(partialFilePath + tempExtension);
|
||||
if (await f.exists()) {
|
||||
await ioSink.addStream(f.openRead());
|
||||
}
|
||||
await ioSink.close();
|
||||
}
|
||||
}
|
||||
|
||||
if (_queue.isNotEmpty) {
|
||||
_startExecution();
|
||||
}
|
||||
}
|
||||
|
||||
void disposeNotifiers(DownloadTask task) {}
|
||||
|
||||
void setStatus(DownloadTask? task, DownloadStatus status) {
|
||||
if (task != null) {
|
||||
task.status.value = status;
|
||||
|
||||
if (status.isCompleted) {
|
||||
disposeNotifiers(task);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<DownloadTask?> addDownload(String url, String savedDir) async {
|
||||
if (url.isNotEmpty) {
|
||||
if (savedDir.isEmpty) {
|
||||
savedDir = ".";
|
||||
}
|
||||
|
||||
var isDirectory = await Directory(savedDir).exists();
|
||||
var downloadFilename = isDirectory ? savedDir + Platform.pathSeparator + getFileNameFromUrl(url) : savedDir;
|
||||
|
||||
return _addDownloadRequest(DownloadRequest(url, downloadFilename));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<DownloadTask> _addDownloadRequest(
|
||||
DownloadRequest downloadRequest,
|
||||
) async {
|
||||
if (_cache[downloadRequest.url] != null) {
|
||||
if (!_cache[downloadRequest.url]!.status.value.isCompleted &&
|
||||
_cache[downloadRequest.url]!.request == downloadRequest) {
|
||||
return _cache[downloadRequest.url]!;
|
||||
} else {
|
||||
_queue.remove(_cache[downloadRequest.url]);
|
||||
}
|
||||
}
|
||||
|
||||
_queue.add(DownloadRequest(downloadRequest.url, downloadRequest.path));
|
||||
var task = DownloadTask(_queue.last);
|
||||
|
||||
_cache[downloadRequest.url] = task;
|
||||
|
||||
_startExecution();
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
Future<void> pauseDownload(String url) async {
|
||||
if (kDebugMode) {
|
||||
print("Pause Download");
|
||||
}
|
||||
var task = getDownload(url)!;
|
||||
setStatus(task, DownloadStatus.paused);
|
||||
task.request.cancelToken.cancel();
|
||||
|
||||
_queue.remove(task.request);
|
||||
}
|
||||
|
||||
Future<void> cancelDownload(String url) async {
|
||||
if (kDebugMode) {
|
||||
print("Cancel Download");
|
||||
}
|
||||
var task = getDownload(url)!;
|
||||
setStatus(task, DownloadStatus.canceled);
|
||||
_queue.remove(task.request);
|
||||
task.request.cancelToken.cancel();
|
||||
}
|
||||
|
||||
Future<void> resumeDownload(String url) async {
|
||||
if (kDebugMode) {
|
||||
print("Resume Download");
|
||||
}
|
||||
var task = getDownload(url)!;
|
||||
setStatus(task, DownloadStatus.downloading);
|
||||
task.request.cancelToken = CancelToken();
|
||||
_queue.add(task.request);
|
||||
|
||||
_startExecution();
|
||||
}
|
||||
|
||||
Future<void> removeDownload(String url) async {
|
||||
cancelDownload(url);
|
||||
_cache.remove(url);
|
||||
}
|
||||
|
||||
DownloadTask? getDownload(String url) {
|
||||
return _cache[url];
|
||||
}
|
||||
|
||||
Future<DownloadStatus> whenDownloadComplete(String url, {Duration timeout = const Duration(hours: 2)}) async {
|
||||
DownloadTask? task = getDownload(url);
|
||||
|
||||
if (task != null) {
|
||||
return task.whenDownloadComplete(timeout: timeout);
|
||||
} else {
|
||||
return Future.error("Not found");
|
||||
}
|
||||
}
|
||||
|
||||
List<DownloadTask> getAllDownloads() {
|
||||
return _cache.values.toList();
|
||||
}
|
||||
|
||||
Future<void> addBatchDownloads(List<String> urls, String savedDir) async {
|
||||
urls.forEach((url) {
|
||||
addDownload(url, savedDir);
|
||||
});
|
||||
}
|
||||
|
||||
List<DownloadTask?> getBatchDownloads(List<String> urls) {
|
||||
return urls.map((e) => _cache[e]).toList();
|
||||
}
|
||||
|
||||
Future<void> pauseBatchDownloads(List<String> urls) async {
|
||||
urls.forEach((element) {
|
||||
pauseDownload(element);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> cancelBatchDownloads(List<String> urls) async {
|
||||
urls.forEach((element) {
|
||||
cancelDownload(element);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> resumeBatchDownloads(List<String> urls) async {
|
||||
urls.forEach((element) {
|
||||
resumeDownload(element);
|
||||
});
|
||||
}
|
||||
|
||||
ValueNotifier<double> getBatchDownloadProgress(List<String> urls) {
|
||||
ValueNotifier<double> progress = ValueNotifier(0);
|
||||
var total = urls.length;
|
||||
|
||||
if (total == 0) {
|
||||
return progress;
|
||||
}
|
||||
|
||||
if (total == 1) {
|
||||
return getDownload(urls.first)?.progress ?? progress;
|
||||
}
|
||||
|
||||
var progressMap = Map<String, double>();
|
||||
|
||||
urls.forEach((url) {
|
||||
DownloadTask? task = getDownload(url);
|
||||
|
||||
if (task != null) {
|
||||
progressMap[url] = 0.0;
|
||||
|
||||
if (task.status.value.isCompleted) {
|
||||
progressMap[url] = 1.0;
|
||||
progress.value = progressMap.values.sum / total;
|
||||
}
|
||||
|
||||
var progressListener;
|
||||
progressListener = () {
|
||||
progressMap[url] = task.progress.value;
|
||||
progress.value = progressMap.values.sum / total;
|
||||
};
|
||||
|
||||
task.progress.addListener(progressListener);
|
||||
|
||||
var listener;
|
||||
listener = () {
|
||||
if (task.status.value.isCompleted) {
|
||||
progressMap[url] = 1.0;
|
||||
progress.value = progressMap.values.sum / total;
|
||||
task.status.removeListener(listener);
|
||||
task.progress.removeListener(progressListener);
|
||||
}
|
||||
};
|
||||
|
||||
task.status.addListener(listener);
|
||||
} else {
|
||||
total--;
|
||||
}
|
||||
});
|
||||
|
||||
return progress;
|
||||
}
|
||||
|
||||
Future<List<DownloadTask?>?> whenBatchDownloadsComplete(List<String> urls,
|
||||
{Duration timeout = const Duration(hours: 2)}) async {
|
||||
var completer = Completer<List<DownloadTask?>?>();
|
||||
|
||||
var completed = 0;
|
||||
var total = urls.length;
|
||||
|
||||
urls.forEach((url) {
|
||||
DownloadTask? task = getDownload(url);
|
||||
|
||||
if (task != null) {
|
||||
if (task.status.value.isCompleted) {
|
||||
completed++;
|
||||
|
||||
if (completed == total) {
|
||||
completer.complete(getBatchDownloads(urls));
|
||||
}
|
||||
}
|
||||
|
||||
var listener;
|
||||
listener = () {
|
||||
if (task.status.value.isCompleted) {
|
||||
completed++;
|
||||
|
||||
if (completed == total) {
|
||||
completer.complete(getBatchDownloads(urls));
|
||||
task.status.removeListener(listener);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
task.status.addListener(listener);
|
||||
} else {
|
||||
total--;
|
||||
|
||||
if (total == 0) {
|
||||
completer.complete(null);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return completer.future.timeout(timeout);
|
||||
}
|
||||
|
||||
void _startExecution() async {
|
||||
if (_queue.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (_queue.isNotEmpty) {
|
||||
var currentRequest = _queue.removeFirst();
|
||||
|
||||
download(currentRequest.url, currentRequest.path, currentRequest.cancelToken);
|
||||
|
||||
await Future.delayed(Duration(milliseconds: 500), null);
|
||||
}
|
||||
}
|
||||
|
||||
String getFileNameFromUrl(String url) {
|
||||
return url.split('/').last;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user