feat: downloader update

This commit is contained in:
xkeyC
2025-12-06 15:31:11 +08:00
parent 8ea5373dec
commit fbf40580cf
24 changed files with 281 additions and 69 deletions

View File

@@ -33,6 +33,8 @@ class HomeDownloaderUI extends HookConsumerWidget {
const MapEntry("resume_all", FluentIcons.download): S.current.downloader_action_resume_all,
if (state.activeTasks.isNotEmpty || state.waitingTasks.isNotEmpty)
const MapEntry("cancel_all", FluentIcons.cancel): S.current.downloader_action_cancel_all,
if (state.completedTasks.isNotEmpty || state.errorTasks.isNotEmpty)
const MapEntry("clear_completed", FluentIcons.clear): S.current.downloader_action_clear_completed,
}.entries)
Padding(
padding: const EdgeInsets.only(left: 6, right: 6),
@@ -151,7 +153,7 @@ class HomeDownloaderUI extends HookConsumerWidget {
],
),
const SizedBox(width: 32),
if (type != "stopped")
if (type != "completed" && type != "error")
DropDownButton(
closeAfterClick: true,
title: Padding(
@@ -183,6 +185,26 @@ class HomeDownloaderUI extends HookConsumerWidget {
onPressed: () => model.openFolder(task),
),
],
)
else
DropDownButton(
closeAfterClick: true,
title: Padding(
padding: const EdgeInsets.all(3),
child: Text(S.current.downloader_action_options),
),
items: [
MenuFlyoutItem(
leading: const Icon(FluentIcons.chrome_close, size: 14),
text: Text(S.current.downloader_action_remove_record),
onPressed: () => model.removeTask(task.id.toInt()),
),
MenuFlyoutItem(
leading: const Icon(FluentIcons.folder_open, size: 14),
text: Text(S.current.action_open_folder),
onPressed: () => model.openFolder(task),
),
],
),
const SizedBox(width: 12),
],

View File

@@ -22,7 +22,8 @@ abstract class HomeDownloaderUIState with _$HomeDownloaderUIState {
factory HomeDownloaderUIState({
@Default([]) List<DownloadTaskInfo> activeTasks,
@Default([]) List<DownloadTaskInfo> waitingTasks,
@Default([]) List<DownloadTaskInfo> stoppedTasks,
@Default([]) List<DownloadTaskInfo> completedTasks,
@Default([]) List<DownloadTaskInfo> errorTasks,
DownloadGlobalStat? globalStat,
}) = _HomeDownloaderUIState;
}
@@ -48,7 +49,8 @@ class HomeDownloaderUIModel extends _$HomeDownloaderUIModel {
final listHeaderStatusMap = {
"active": S.current.downloader_title_downloading,
"waiting": S.current.downloader_info_waiting,
"stopped": S.current.downloader_title_ended,
"completed": S.current.downloader_title_ended,
"error": S.current.downloader_info_download_failed,
};
@override
@@ -92,6 +94,17 @@ class HomeDownloaderUIModel extends _$HomeDownloaderUIModel {
}
}
return;
case "clear_completed":
if (!downloadManagerState.isRunning) return;
try {
final allTasks = [...state.completedTasks, ...state.errorTasks];
for (var task in allTasks) {
await downloadManager.removeTask(task.id.toInt(), deleteFiles: false);
}
} catch (e) {
dPrint("DownloadsUIModel clear_completed Error: $e");
}
return;
case "settings":
_showDownloadSpeedSettings(context);
return;
@@ -99,19 +112,33 @@ class HomeDownloaderUIModel extends _$HomeDownloaderUIModel {
}
int getTasksLen() {
return state.activeTasks.length + state.waitingTasks.length + state.stoppedTasks.length;
return state.activeTasks.length + state.waitingTasks.length + state.completedTasks.length + state.errorTasks.length;
}
(DownloadTaskInfo, String, bool) getTaskAndType(int index) {
final tempList = <DownloadTaskInfo>[...state.activeTasks, ...state.waitingTasks, ...state.stoppedTasks];
final tempList = <DownloadTaskInfo>[
...state.activeTasks,
...state.waitingTasks,
...state.completedTasks,
...state.errorTasks,
];
if (index >= 0 && index < state.activeTasks.length) {
return (tempList[index], "active", index == 0);
}
if (index >= state.activeTasks.length && index < state.activeTasks.length + state.waitingTasks.length) {
return (tempList[index], "waiting", index == state.activeTasks.length);
}
if (index >= state.activeTasks.length + state.waitingTasks.length && index < tempList.length) {
return (tempList[index], "stopped", index == state.activeTasks.length + state.waitingTasks.length);
if (index >= state.activeTasks.length + state.waitingTasks.length &&
index < state.activeTasks.length + state.waitingTasks.length + state.completedTasks.length) {
return (tempList[index], "completed", index == state.activeTasks.length + state.waitingTasks.length);
}
if (index >= state.activeTasks.length + state.waitingTasks.length + state.completedTasks.length &&
index < tempList.length) {
return (
tempList[index],
"error",
index == state.activeTasks.length + state.waitingTasks.length + state.completedTasks.length,
);
}
throw Exception("Index out of range or element is null");
}
@@ -172,6 +199,11 @@ class HomeDownloaderUIModel extends _$HomeDownloaderUIModel {
}
}
Future<void> removeTask(int taskId) async {
final downloadManager = ref.read(downloadManagerProvider.notifier);
await downloadManager.removeTask(taskId, deleteFiles: false);
}
void openFolder(DownloadTaskInfo task) {
final outputFolder = task.outputFolder;
if (outputFolder.isNotEmpty) {
@@ -190,7 +222,8 @@ class HomeDownloaderUIModel extends _$HomeDownloaderUIModel {
final activeTasks = <DownloadTaskInfo>[];
final waitingTasks = <DownloadTaskInfo>[];
final stoppedTasks = <DownloadTaskInfo>[];
final completedTasks = <DownloadTaskInfo>[];
final errorTasks = <DownloadTaskInfo>[];
for (var task in allTasks) {
switch (task.status) {
@@ -202,8 +235,10 @@ class HomeDownloaderUIModel extends _$HomeDownloaderUIModel {
waitingTasks.add(task);
break;
case DownloadTaskStatus.finished:
completedTasks.add(task);
break;
case DownloadTaskStatus.error:
stoppedTasks.add(task);
errorTasks.add(task);
break;
}
}
@@ -211,11 +246,18 @@ class HomeDownloaderUIModel extends _$HomeDownloaderUIModel {
state = state.copyWith(
activeTasks: activeTasks,
waitingTasks: waitingTasks,
stoppedTasks: stoppedTasks,
completedTasks: completedTasks,
errorTasks: errorTasks,
globalStat: downloadManagerState.globalStat,
);
} else {
state = state.copyWith(activeTasks: [], waitingTasks: [], stoppedTasks: [], globalStat: null);
state = state.copyWith(
activeTasks: [],
waitingTasks: [],
completedTasks: [],
errorTasks: [],
globalStat: null,
);
}
await Future.delayed(const Duration(seconds: 1));
}

View File

@@ -14,7 +14,7 @@ T _$identity<T>(T value) => value;
/// @nodoc
mixin _$HomeDownloaderUIState {
List<DownloadTaskInfo> get activeTasks; List<DownloadTaskInfo> get waitingTasks; List<DownloadTaskInfo> get stoppedTasks; DownloadGlobalStat? get globalStat;
List<DownloadTaskInfo> get activeTasks; List<DownloadTaskInfo> get waitingTasks; List<DownloadTaskInfo> get completedTasks; List<DownloadTaskInfo> get errorTasks; DownloadGlobalStat? get globalStat;
/// Create a copy of HomeDownloaderUIState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@@ -25,16 +25,16 @@ $HomeDownloaderUIStateCopyWith<HomeDownloaderUIState> get copyWith => _$HomeDown
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is HomeDownloaderUIState&&const DeepCollectionEquality().equals(other.activeTasks, activeTasks)&&const DeepCollectionEquality().equals(other.waitingTasks, waitingTasks)&&const DeepCollectionEquality().equals(other.stoppedTasks, stoppedTasks)&&(identical(other.globalStat, globalStat) || other.globalStat == globalStat));
return identical(this, other) || (other.runtimeType == runtimeType&&other is HomeDownloaderUIState&&const DeepCollectionEquality().equals(other.activeTasks, activeTasks)&&const DeepCollectionEquality().equals(other.waitingTasks, waitingTasks)&&const DeepCollectionEquality().equals(other.completedTasks, completedTasks)&&const DeepCollectionEquality().equals(other.errorTasks, errorTasks)&&(identical(other.globalStat, globalStat) || other.globalStat == globalStat));
}
@override
int get hashCode => Object.hash(runtimeType,const DeepCollectionEquality().hash(activeTasks),const DeepCollectionEquality().hash(waitingTasks),const DeepCollectionEquality().hash(stoppedTasks),globalStat);
int get hashCode => Object.hash(runtimeType,const DeepCollectionEquality().hash(activeTasks),const DeepCollectionEquality().hash(waitingTasks),const DeepCollectionEquality().hash(completedTasks),const DeepCollectionEquality().hash(errorTasks),globalStat);
@override
String toString() {
return 'HomeDownloaderUIState(activeTasks: $activeTasks, waitingTasks: $waitingTasks, stoppedTasks: $stoppedTasks, globalStat: $globalStat)';
return 'HomeDownloaderUIState(activeTasks: $activeTasks, waitingTasks: $waitingTasks, completedTasks: $completedTasks, errorTasks: $errorTasks, globalStat: $globalStat)';
}
@@ -45,7 +45,7 @@ abstract mixin class $HomeDownloaderUIStateCopyWith<$Res> {
factory $HomeDownloaderUIStateCopyWith(HomeDownloaderUIState value, $Res Function(HomeDownloaderUIState) _then) = _$HomeDownloaderUIStateCopyWithImpl;
@useResult
$Res call({
List<DownloadTaskInfo> activeTasks, List<DownloadTaskInfo> waitingTasks, List<DownloadTaskInfo> stoppedTasks, DownloadGlobalStat? globalStat
List<DownloadTaskInfo> activeTasks, List<DownloadTaskInfo> waitingTasks, List<DownloadTaskInfo> completedTasks, List<DownloadTaskInfo> errorTasks, DownloadGlobalStat? globalStat
});
@@ -62,11 +62,12 @@ class _$HomeDownloaderUIStateCopyWithImpl<$Res>
/// Create a copy of HomeDownloaderUIState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline') @override $Res call({Object? activeTasks = null,Object? waitingTasks = null,Object? stoppedTasks = null,Object? globalStat = freezed,}) {
@pragma('vm:prefer-inline') @override $Res call({Object? activeTasks = null,Object? waitingTasks = null,Object? completedTasks = null,Object? errorTasks = null,Object? globalStat = freezed,}) {
return _then(_self.copyWith(
activeTasks: null == activeTasks ? _self.activeTasks : activeTasks // ignore: cast_nullable_to_non_nullable
as List<DownloadTaskInfo>,waitingTasks: null == waitingTasks ? _self.waitingTasks : waitingTasks // ignore: cast_nullable_to_non_nullable
as List<DownloadTaskInfo>,stoppedTasks: null == stoppedTasks ? _self.stoppedTasks : stoppedTasks // ignore: cast_nullable_to_non_nullable
as List<DownloadTaskInfo>,completedTasks: null == completedTasks ? _self.completedTasks : completedTasks // ignore: cast_nullable_to_non_nullable
as List<DownloadTaskInfo>,errorTasks: null == errorTasks ? _self.errorTasks : errorTasks // ignore: cast_nullable_to_non_nullable
as List<DownloadTaskInfo>,globalStat: freezed == globalStat ? _self.globalStat : globalStat // ignore: cast_nullable_to_non_nullable
as DownloadGlobalStat?,
));
@@ -153,10 +154,10 @@ return $default(_that);case _:
/// }
/// ```
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( List<DownloadTaskInfo> activeTasks, List<DownloadTaskInfo> waitingTasks, List<DownloadTaskInfo> stoppedTasks, DownloadGlobalStat? globalStat)? $default,{required TResult orElse(),}) {final _that = this;
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( List<DownloadTaskInfo> activeTasks, List<DownloadTaskInfo> waitingTasks, List<DownloadTaskInfo> completedTasks, List<DownloadTaskInfo> errorTasks, DownloadGlobalStat? globalStat)? $default,{required TResult orElse(),}) {final _that = this;
switch (_that) {
case _HomeDownloaderUIState() when $default != null:
return $default(_that.activeTasks,_that.waitingTasks,_that.stoppedTasks,_that.globalStat);case _:
return $default(_that.activeTasks,_that.waitingTasks,_that.completedTasks,_that.errorTasks,_that.globalStat);case _:
return orElse();
}
@@ -174,10 +175,10 @@ return $default(_that.activeTasks,_that.waitingTasks,_that.stoppedTasks,_that.gl
/// }
/// ```
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( List<DownloadTaskInfo> activeTasks, List<DownloadTaskInfo> waitingTasks, List<DownloadTaskInfo> stoppedTasks, DownloadGlobalStat? globalStat) $default,) {final _that = this;
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( List<DownloadTaskInfo> activeTasks, List<DownloadTaskInfo> waitingTasks, List<DownloadTaskInfo> completedTasks, List<DownloadTaskInfo> errorTasks, DownloadGlobalStat? globalStat) $default,) {final _that = this;
switch (_that) {
case _HomeDownloaderUIState():
return $default(_that.activeTasks,_that.waitingTasks,_that.stoppedTasks,_that.globalStat);case _:
return $default(_that.activeTasks,_that.waitingTasks,_that.completedTasks,_that.errorTasks,_that.globalStat);case _:
throw StateError('Unexpected subclass');
}
@@ -194,10 +195,10 @@ return $default(_that.activeTasks,_that.waitingTasks,_that.stoppedTasks,_that.gl
/// }
/// ```
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( List<DownloadTaskInfo> activeTasks, List<DownloadTaskInfo> waitingTasks, List<DownloadTaskInfo> stoppedTasks, DownloadGlobalStat? globalStat)? $default,) {final _that = this;
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( List<DownloadTaskInfo> activeTasks, List<DownloadTaskInfo> waitingTasks, List<DownloadTaskInfo> completedTasks, List<DownloadTaskInfo> errorTasks, DownloadGlobalStat? globalStat)? $default,) {final _that = this;
switch (_that) {
case _HomeDownloaderUIState() when $default != null:
return $default(_that.activeTasks,_that.waitingTasks,_that.stoppedTasks,_that.globalStat);case _:
return $default(_that.activeTasks,_that.waitingTasks,_that.completedTasks,_that.errorTasks,_that.globalStat);case _:
return null;
}
@@ -209,7 +210,7 @@ return $default(_that.activeTasks,_that.waitingTasks,_that.stoppedTasks,_that.gl
class _HomeDownloaderUIState implements HomeDownloaderUIState {
_HomeDownloaderUIState({final List<DownloadTaskInfo> activeTasks = const [], final List<DownloadTaskInfo> waitingTasks = const [], final List<DownloadTaskInfo> stoppedTasks = const [], this.globalStat}): _activeTasks = activeTasks,_waitingTasks = waitingTasks,_stoppedTasks = stoppedTasks;
_HomeDownloaderUIState({final List<DownloadTaskInfo> activeTasks = const [], final List<DownloadTaskInfo> waitingTasks = const [], final List<DownloadTaskInfo> completedTasks = const [], final List<DownloadTaskInfo> errorTasks = const [], this.globalStat}): _activeTasks = activeTasks,_waitingTasks = waitingTasks,_completedTasks = completedTasks,_errorTasks = errorTasks;
final List<DownloadTaskInfo> _activeTasks;
@@ -226,11 +227,18 @@ class _HomeDownloaderUIState implements HomeDownloaderUIState {
return EqualUnmodifiableListView(_waitingTasks);
}
final List<DownloadTaskInfo> _stoppedTasks;
@override@JsonKey() List<DownloadTaskInfo> get stoppedTasks {
if (_stoppedTasks is EqualUnmodifiableListView) return _stoppedTasks;
final List<DownloadTaskInfo> _completedTasks;
@override@JsonKey() List<DownloadTaskInfo> get completedTasks {
if (_completedTasks is EqualUnmodifiableListView) return _completedTasks;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(_stoppedTasks);
return EqualUnmodifiableListView(_completedTasks);
}
final List<DownloadTaskInfo> _errorTasks;
@override@JsonKey() List<DownloadTaskInfo> get errorTasks {
if (_errorTasks is EqualUnmodifiableListView) return _errorTasks;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(_errorTasks);
}
@override final DownloadGlobalStat? globalStat;
@@ -245,16 +253,16 @@ _$HomeDownloaderUIStateCopyWith<_HomeDownloaderUIState> get copyWith => __$HomeD
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is _HomeDownloaderUIState&&const DeepCollectionEquality().equals(other._activeTasks, _activeTasks)&&const DeepCollectionEquality().equals(other._waitingTasks, _waitingTasks)&&const DeepCollectionEquality().equals(other._stoppedTasks, _stoppedTasks)&&(identical(other.globalStat, globalStat) || other.globalStat == globalStat));
return identical(this, other) || (other.runtimeType == runtimeType&&other is _HomeDownloaderUIState&&const DeepCollectionEquality().equals(other._activeTasks, _activeTasks)&&const DeepCollectionEquality().equals(other._waitingTasks, _waitingTasks)&&const DeepCollectionEquality().equals(other._completedTasks, _completedTasks)&&const DeepCollectionEquality().equals(other._errorTasks, _errorTasks)&&(identical(other.globalStat, globalStat) || other.globalStat == globalStat));
}
@override
int get hashCode => Object.hash(runtimeType,const DeepCollectionEquality().hash(_activeTasks),const DeepCollectionEquality().hash(_waitingTasks),const DeepCollectionEquality().hash(_stoppedTasks),globalStat);
int get hashCode => Object.hash(runtimeType,const DeepCollectionEquality().hash(_activeTasks),const DeepCollectionEquality().hash(_waitingTasks),const DeepCollectionEquality().hash(_completedTasks),const DeepCollectionEquality().hash(_errorTasks),globalStat);
@override
String toString() {
return 'HomeDownloaderUIState(activeTasks: $activeTasks, waitingTasks: $waitingTasks, stoppedTasks: $stoppedTasks, globalStat: $globalStat)';
return 'HomeDownloaderUIState(activeTasks: $activeTasks, waitingTasks: $waitingTasks, completedTasks: $completedTasks, errorTasks: $errorTasks, globalStat: $globalStat)';
}
@@ -265,7 +273,7 @@ abstract mixin class _$HomeDownloaderUIStateCopyWith<$Res> implements $HomeDownl
factory _$HomeDownloaderUIStateCopyWith(_HomeDownloaderUIState value, $Res Function(_HomeDownloaderUIState) _then) = __$HomeDownloaderUIStateCopyWithImpl;
@override @useResult
$Res call({
List<DownloadTaskInfo> activeTasks, List<DownloadTaskInfo> waitingTasks, List<DownloadTaskInfo> stoppedTasks, DownloadGlobalStat? globalStat
List<DownloadTaskInfo> activeTasks, List<DownloadTaskInfo> waitingTasks, List<DownloadTaskInfo> completedTasks, List<DownloadTaskInfo> errorTasks, DownloadGlobalStat? globalStat
});
@@ -282,11 +290,12 @@ class __$HomeDownloaderUIStateCopyWithImpl<$Res>
/// Create a copy of HomeDownloaderUIState
/// with the given fields replaced by the non-null parameter values.
@override @pragma('vm:prefer-inline') $Res call({Object? activeTasks = null,Object? waitingTasks = null,Object? stoppedTasks = null,Object? globalStat = freezed,}) {
@override @pragma('vm:prefer-inline') $Res call({Object? activeTasks = null,Object? waitingTasks = null,Object? completedTasks = null,Object? errorTasks = null,Object? globalStat = freezed,}) {
return _then(_HomeDownloaderUIState(
activeTasks: null == activeTasks ? _self._activeTasks : activeTasks // ignore: cast_nullable_to_non_nullable
as List<DownloadTaskInfo>,waitingTasks: null == waitingTasks ? _self._waitingTasks : waitingTasks // ignore: cast_nullable_to_non_nullable
as List<DownloadTaskInfo>,stoppedTasks: null == stoppedTasks ? _self._stoppedTasks : stoppedTasks // ignore: cast_nullable_to_non_nullable
as List<DownloadTaskInfo>,completedTasks: null == completedTasks ? _self._completedTasks : completedTasks // ignore: cast_nullable_to_non_nullable
as List<DownloadTaskInfo>,errorTasks: null == errorTasks ? _self._errorTasks : errorTasks // ignore: cast_nullable_to_non_nullable
as List<DownloadTaskInfo>,globalStat: freezed == globalStat ? _self.globalStat : globalStat // ignore: cast_nullable_to_non_nullable
as DownloadGlobalStat?,
));

View File

@@ -42,7 +42,7 @@ final class HomeDownloaderUIModelProvider
}
String _$homeDownloaderUIModelHash() =>
r'bf7d095d761fff078de707562cf311c20db664d9';
r'b230746a782b511dd58b0b46def7845c01412762';
abstract class _$HomeDownloaderUIModel
extends $Notifier<HomeDownloaderUIState> {

View File

@@ -257,7 +257,7 @@ class InputMethodDialogUIModel extends _$InputMethodDialogUIModel {
}
// get torrent Data
final data = await RSHttp.get(torrentUrl!);
final taskId = await downloadManager.addTorrent(data.data!, outputFolder: _localTranslateModelDir);
final taskId = await downloadManager.addTorrent(data.data!, outputFolder: "$_localTranslateModelDir/$_localTranslateModelName");
return taskId.toString();
} catch (e) {
dPrint("[InputMethodDialogUIModel] doDownloadTranslateModel error: $e");

View File

@@ -43,7 +43,7 @@ final class InputMethodDialogUIModelProvider
}
String _$inputMethodDialogUIModelHash() =>
r'5c2989faf94d43bb814e5b80e10d68416c8241ec';
r'77bf2b02a7b7ea66e9a06be068b791b3a4295a44';
abstract class _$InputMethodDialogUIModel
extends $Notifier<InputMethodDialogUIState> {