This repository has been archived by the owner on Dec 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add Download History Save 、Open Directory And Open File
- Loading branch information
Showing
14 changed files
with
221 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.