Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

Commit

Permalink
✨ Add Download History Save 、Open Directory And Open File
Browse files Browse the repository at this point in the history
  • Loading branch information
uiYzzi committed Jan 26, 2024
1 parent 67273bb commit 1d9ecbc
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 62 deletions.
2 changes: 2 additions & 0 deletions lib/generated/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class MessageLookup extends MessageLookupByLibrary {
MessageLookupByLibrary.simpleMessage("Navigation Mode"),
"newDownload": MessageLookupByLibrary.simpleMessage("New"),
"no": MessageLookupByLibrary.simpleMessage("No"),
"openDirectory": MessageLookupByLibrary.simpleMessage("Open Directory"),
"openFile": MessageLookupByLibrary.simpleMessage("Open File"),
"path": MessageLookupByLibrary.simpleMessage("Path"),
"pauseAllTasks":
MessageLookupByLibrary.simpleMessage("Pause All Tasks"),
Expand Down
2 changes: 2 additions & 0 deletions lib/generated/intl/messages_zh_CN.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class MessageLookup extends MessageLookupByLibrary {
"navigationMode": MessageLookupByLibrary.simpleMessage("导航模式"),
"newDownload": MessageLookupByLibrary.simpleMessage("新建"),
"no": MessageLookupByLibrary.simpleMessage("不是"),
"openDirectory": MessageLookupByLibrary.simpleMessage("打开目录"),
"openFile": MessageLookupByLibrary.simpleMessage("打开文件"),
"path": MessageLookupByLibrary.simpleMessage("存储路径"),
"pauseAllTasks": MessageLookupByLibrary.simpleMessage("暂停所有任务"),
"pauseThisTasks": MessageLookupByLibrary.simpleMessage("暂停本任务"),
Expand Down
20 changes: 20 additions & 0 deletions lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,7 @@
"delete":"Delete",
"deleteFile":"Delete files when removing tasks",
"showWindow":"Show Window",
"exitApp":"Exit App"
"exitApp":"Exit App",
"openDirectory":"Open Directory",
"openFile":"Open File"
}
4 changes: 3 additions & 1 deletion lib/l10n/intl_zh_CN.arb
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,7 @@
"delete":"移除",
"deleteFile":"移除任务时删除文件",
"showWindow":"显示窗口",
"exitApp":"退出程序"
"exitApp":"退出程序",
"openDirectory":"打开目录",
"openFile":"打开文件"
}
4 changes: 4 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ class _MyHomePageState extends State<MyHomePage>
void initState() {
windowManager.addListener(this);
trayManager.addListener(this);
Provider.of<StoppedListModel>(context, listen: false)
.loadHistoryListFromJson();
super.initState();
}

Expand Down Expand Up @@ -366,6 +368,8 @@ class _MyHomePageState extends State<MyHomePage>
child: Text(S.of(context).yes),
onPressed: () async {
Navigator.pop(context);
Provider.of<StoppedListModel>(context, listen: false)
.saveHistoryListToJson();
if (Global.rememberWindowSize) {
await windowManager.getSize().then((size) {
Global.prefs.setDouble('WindowWidth', size.width);
Expand Down
26 changes: 26 additions & 0 deletions lib/model/download_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,30 @@ class DownloadItem {
required this.totalLength,
required this.uploadSpeed,
});

factory DownloadItem.fromJson(Map<String, dynamic> json) {
return DownloadItem(
completedLength: json['completedLength'],
path: json['path'],
connections: json['connections'],
downloadSpeed: json['downloadSpeed'],
gid: json['gid'],
status: json['status'],
totalLength: json['totalLength'],
uploadSpeed: json['uploadSpeed'],
);
}

Map<String, dynamic> toJson() {
return {
'completedLength': completedLength,
'path': path,
'connections': connections,
'downloadSpeed': downloadSpeed,
'gid': gid,
'status': status,
'totalLength': totalLength,
'uploadSpeed': uploadSpeed,
};
}
}
67 changes: 65 additions & 2 deletions lib/model/stopped_list_model.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,76 @@
import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:yolx/model/download_item.dart';
import 'package:yolx/utils/file_utils.dart';
import 'package:yolx/utils/log.dart';

class StoppedListModel extends ChangeNotifier {
List<DownloadItem> _downloadList = [];
List<DownloadItem> _historyList = [];

List<DownloadItem> get downloadList {
List<DownloadItem> combinedList = List<DownloadItem>.from(_downloadList);
List<String> downloadGids = _downloadList.map((item) => item.gid).toList();
List<DownloadItem> uniqueHistoryItems =
_historyList.where((item) => !downloadGids.contains(item.gid)).toList();
combinedList.addAll(uniqueHistoryItems);
return combinedList;
}

void removeAllFromHistoryList() {
_historyList.clear();
saveHistoryListToJson();
notifyListeners();
}

List<DownloadItem> get downloadList => _downloadList;
void removeFromHistoryList(String gid) {
_historyList.removeWhere((item) => item.gid == gid);
saveHistoryListToJson();
notifyListeners();
}

Future<void> saveHistoryListToJson() async {
updateHistoryList();
final historyListFile = await getLocalFile('historyList.json');
List<Map<String, dynamic>> jsonList =
_historyList.map((item) => item.toJson()).toList();
String jsonString = json.encode(jsonList);
await historyListFile.writeAsString(jsonString);
}

Future<void> loadHistoryListFromJson() async {
try {
final historyListFile = await getLocalFile('historyList.json');
String jsonString = await historyListFile.readAsString();
List<dynamic> jsonData = json.decode(jsonString);
_historyList =
jsonData.map((itemJson) => DownloadItem.fromJson(itemJson)).toList();
notifyListeners();
} catch (e) {
Log.e(e);
}
}

void updateHistoryList() {
var completedItems =
_downloadList.where((item) => item.status == "complete").toList();
for (var item in completedItems) {
item.status = "history";
}

List<String> historyIds = _historyList.map((item) => item.gid).toList();
_historyList.insertAll(
0, completedItems.where((item) => !historyIds.contains(item.gid)));
}

void updateDownloadList(List<DownloadItem> newList) {
_downloadList = newList;
if (_downloadList.length != newList.length) {
_downloadList = newList;
saveHistoryListToJson();
} else {
_downloadList = newList;
}
notifyListeners();
}
}
35 changes: 16 additions & 19 deletions lib/screens/downloading.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:yolx/common/global.dart';
import 'package:yolx/generated/l10n.dart';
import 'package:yolx/model/download_item.dart';
import 'package:yolx/model/downloading_list_model.dart';
import 'package:yolx/model/stopped_list_model.dart';
import 'package:yolx/utils/common_utils.dart';
import 'package:yolx/widgets/download_file_card.dart';
import 'dart:async';
// ignore: library_prefixes
Expand Down Expand Up @@ -32,24 +34,6 @@ class _DownloadingPageState extends State<DownloadingPage> with PageMixin {
);
}

List<DownloadItem> parseDownloadList(dynamic responseData) {
List<DownloadItem> downloadList = [];
for (var itemData in responseData) {
var downloadItem = DownloadItem(
completedLength: int.parse(itemData["completedLength"]),
path: itemData["files"][0]["path"],
connections: itemData["connections"],
downloadSpeed: int.parse(itemData["downloadSpeed"]),
gid: itemData["gid"],
status: itemData["status"],
totalLength: int.parse(itemData["totalLength"]),
uploadSpeed: int.parse(itemData["uploadSpeed"]),
);
downloadList.add(downloadItem);
}
return downloadList;
}

void updateList() async {
if (!mounted) {
return;
Expand All @@ -62,7 +46,20 @@ class _DownloadingPageState extends State<DownloadingPage> with PageMixin {
var downloadListModel =
// ignore: use_build_context_synchronously
Provider.of<DownloadingListModel>(context, listen: false);
downloadListModel.updateDownloadList(parseDownloadList(res));
List<DownloadItem> downloadList = parseDownloadList(res);
if (downloadList.length == downloadListModel.downloadList.length) {
downloadListModel.updateDownloadList(parseDownloadList(res));
} else {
downloadListModel.updateDownloadList(parseDownloadList(res));
var stoppedRes = await Aria2Http.tellStopped(Global.rpcUrl);
if (stoppedRes == null) {
return;
}
var stoppedListModel =
// ignore: use_build_context_synchronously
Provider.of<StoppedListModel>(context, listen: false);
stoppedListModel.updateDownloadList(parseDownloadList(stoppedRes));
}
}
}

Expand Down
25 changes: 5 additions & 20 deletions lib/screens/stopped.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'package:fluent_ui/fluent_ui.dart';
import 'package:provider/provider.dart';
import 'package:yolx/common/global.dart';
import 'package:yolx/generated/l10n.dart';
import 'package:yolx/model/download_item.dart';
import 'package:yolx/model/stopped_list_model.dart';
import 'package:yolx/utils/common_utils.dart';
import 'package:yolx/widgets/download_file_card.dart';
import 'dart:async';
import 'package:yolx/utils/ariar2_http_utils.dart' as Aria2Http;
Expand All @@ -23,24 +23,6 @@ class _StoppedPageState extends State<StoppedPage> with PageMixin {
// ignore: prefer_typing_uninitialized_variables
var time;

List<DownloadItem> parseDownloadList(dynamic responseData) {
List<DownloadItem> downloadList = [];
for (var itemData in responseData) {
var downloadItem = DownloadItem(
completedLength: int.parse(itemData["completedLength"]),
path: itemData["files"][0]["path"],
connections: itemData["connections"],
downloadSpeed: int.parse(itemData["downloadSpeed"]),
gid: itemData["gid"],
status: itemData["status"],
totalLength: int.parse(itemData["totalLength"]),
uploadSpeed: int.parse(itemData["uploadSpeed"]),
);
downloadList.add(downloadItem);
}
return downloadList;
}

void updateList() async {
if (!mounted) {
return;
Expand Down Expand Up @@ -75,7 +57,8 @@ class _StoppedPageState extends State<StoppedPage> with PageMixin {
@override
Widget build(BuildContext context) {
assert(debugCheckHasFluentTheme(context));
var downloadList = Provider.of<StoppedListModel>(context).downloadList;
var downloadListModel = Provider.of<StoppedListModel>(context);
var downloadList = downloadListModel.downloadList;
return Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
Expand All @@ -95,6 +78,8 @@ class _StoppedPageState extends State<StoppedPage> with PageMixin {
icon: const Icon(FluentIcons.delete, size: 18.0),
onPressed: () async {
await Aria2Http.purgeDownloadResult(Global.rpcUrl);
// ignore: use_build_context_synchronously
downloadListModel.removeAllFromHistoryList();
},
),
),
Expand Down
20 changes: 1 addition & 19 deletions lib/screens/waiting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'package:fluent_ui/fluent_ui.dart';
import 'package:provider/provider.dart';
import 'package:yolx/common/global.dart';
import 'package:yolx/generated/l10n.dart';
import 'package:yolx/model/download_item.dart';
import 'package:yolx/model/waiting_list_model.dart';
import 'package:yolx/utils/common_utils.dart';
import 'package:yolx/widgets/download_file_card.dart';
import 'dart:async';
import 'package:yolx/utils/ariar2_http_utils.dart' as Aria2Http;
Expand All @@ -23,24 +23,6 @@ class _WaitingPageState extends State<WaitingPage> with PageMixin {
// ignore: prefer_typing_uninitialized_variables
var time;

List<DownloadItem> parseDownloadList(dynamic responseData) {
List<DownloadItem> downloadList = [];
for (var itemData in responseData) {
var downloadItem = DownloadItem(
completedLength: int.parse(itemData["completedLength"]),
path: itemData["files"][0]["path"],
connections: itemData["connections"],
downloadSpeed: int.parse(itemData["downloadSpeed"]),
gid: itemData["gid"],
status: itemData["status"],
totalLength: int.parse(itemData["totalLength"]),
uploadSpeed: int.parse(itemData["uploadSpeed"]),
);
downloadList.add(downloadItem);
}
return downloadList;
}

void updateList() async {
if (!mounted) {
return;
Expand Down
19 changes: 19 additions & 0 deletions lib/utils/common_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';
import 'dart:math';

import 'package:yolx/common/const.dart';
import 'package:yolx/model/download_item.dart';

permission777(filePath) {
Process.runSync('chmod', ['-R', '777', filePath]);
Expand All @@ -12,3 +13,21 @@ String formatFileSize(int size) {
final int index = (log(size) / log(1024)).floor();
return '${(size / pow(1024, index)).toStringAsFixed(2)} ${sizeUnits[index]}';
}

List<DownloadItem> parseDownloadList(dynamic responseData) {
List<DownloadItem> downloadList = [];
for (var itemData in responseData) {
var downloadItem = DownloadItem(
completedLength: int.parse(itemData["completedLength"]),
path: itemData["files"][0]["path"],
connections: itemData["connections"],
downloadSpeed: int.parse(itemData["downloadSpeed"]),
gid: itemData["gid"],
status: itemData["status"],
totalLength: int.parse(itemData["totalLength"]),
uploadSpeed: int.parse(itemData["uploadSpeed"]),
);
downloadList.add(downloadItem);
}
return downloadList;
}
6 changes: 6 additions & 0 deletions lib/utils/file_utils.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import 'dart:io';

import 'package:path_provider/path_provider.dart';
import 'package:yolx/common/global.dart';
import 'package:yolx/generated/l10n.dart';

Future<File> getLocalFile(String filename) async {
final directory = await getApplicationCacheDirectory();
return File('${directory.path}${Global.pathSeparator}$filename');
}

getPlugAssetsDir(String plugName) async {
if (Platform.isWindows || Platform.isLinux) {
String plugDir =
Expand Down
Loading

0 comments on commit 1d9ecbc

Please sign in to comment.