Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Commit

Permalink
fix: java download progress (#264)
Browse files Browse the repository at this point in the history
錯誤修復:Java 下載進度指示條
  • Loading branch information
SiongSng authored Jul 3, 2022
2 parents 3d02223 + e9d30ac commit bee1552
Showing 1 changed file with 51 additions and 48 deletions.
99 changes: 51 additions & 48 deletions lib/widget/dialog/DownloadJava.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class _DownloadJavaState extends State<DownloadJava> {
Widget build(BuildContext context) {
return AlertDialog(
title: I18nText(
"gui.tips.info",
'gui.tips.info',
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 25),
),
content: I18nText(
"launcher.java.install.not",
'launcher.java.install.not',
args: {
"java_version": widget.javaVersions.join(I18n.format('gui.separate'))
'java_version': widget.javaVersions.join(I18n.format('gui.separate'))
},
textAlign: TextAlign.center,
style: const TextStyle(
Expand All @@ -45,7 +45,7 @@ class _DownloadJavaState extends State<DownloadJava> {
actions: [
Center(
child: TextButton(
child: I18nText("launcher.java.install.auto",
child: I18nText('launcher.java.install.auto',
style: const TextStyle(fontSize: 20, color: Colors.red)),
onPressed: () {
Navigator.pop(context);
Expand All @@ -62,13 +62,13 @@ class _DownloadJavaState extends State<DownloadJava> {
),
Center(
child: TextButton(
child: I18nText("launcher.java.install.manual",
child: I18nText('launcher.java.install.manual',
style: const TextStyle(fontSize: 20, color: Colors.lightBlue)),
onPressed: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: I18nText("launcher.java.install.manual"),
title: I18nText('launcher.java.install.manual'),
content: const JavaPathWidget(),
actions: [
OkClose(
Expand All @@ -82,7 +82,7 @@ class _DownloadJavaState extends State<DownloadJava> {
builder: (context) => AlertDialog(
title: I18nText.errorInfoText(),
content: I18nText(
"launcher.java.install.manual.error"),
'launcher.java.install.manual.error'),
actions: const [OkClose()],
));
} else {
Expand Down Expand Up @@ -125,10 +125,12 @@ class _TaskState extends State<Task> {

double get downloadProgress {
if (finishList.every((b) => b)) return 1;

double p = 0.0;
downloadJavaProgress.forEach((progress) {
p += progress;
});

return p / downloadJavaProgress.length;
}

Expand Down Expand Up @@ -156,16 +158,16 @@ class _TaskState extends State<Task> {
late String execPath;

if (Platform.isWindows) {
execPath = join(dataHome.absolute.path, "jre", version.toString(),
"bin", "javaw.exe");
execPath = join(dataHome.absolute.path, 'jre', version.toString(),
'bin', 'javaw.exe');
} else if (Platform.isLinux) {
execPath = join(
dataHome.absolute.path, "jre", version.toString(), "bin", "java");
dataHome.absolute.path, 'jre', version.toString(), 'bin', 'java');
} else if (Platform.isMacOS) {
execPath = join(dataHome.absolute.path, "jre", version.toString(),
"jre.bundle", "Contents", "Home", "bin", "java");
execPath = join(dataHome.absolute.path, 'jre', version.toString(),
'jre.bundle', 'Contents', 'Home', 'bin', 'java');
}
Config.change("java_path_$version", execPath);
Config.change('java_path_$version', execPath);
if (!kTestMode) {
await chmod(execPath);
}
Expand All @@ -174,7 +176,7 @@ class _TaskState extends State<Task> {
DateTime endTime = DateTime.now();
Duration duration = endTime.difference(startTime);
logger.info(
"It took ${duration.inSeconds} Seconds to download Java $version");
'It took ${duration.inSeconds} Seconds to download Java $version');
if (mounted) {
setState(() {});
}
Expand Down Expand Up @@ -204,29 +206,30 @@ class _TaskState extends State<Task> {
Future<void> download(String url) async {
Response response = await get(Uri.parse(url));
Map data = json.decode(response.body);
Map<String, Map> files = data["files"].cast<String, Map>();
totalFiles = files.keys.length;
Map<String, Map> files = data['files'].cast<String, Map>();
DownloadInfos infos = DownloadInfos.empty();

files.keys.forEach((String file) {
if (files[file]!["type"] == "file") {
infos.add(DownloadInfo(files[file]!["downloads"]["raw"]["url"],
savePath: join(
dataHome.absolute.path, "jre", javaVersion.toString(), file),
onDownloaded: () {
for (String filePath in files.keys) {
Map file = files[filePath]!;
String type = file['type']!;

if (type == 'file') {
totalFiles++;
infos.add(DownloadInfo(file['downloads']['raw']['url'],
savePath: join(dataHome.absolute.path, 'jre',
javaVersion.toString(), filePath), onDownloaded: () {
doneFiles++;
option.sendData(doneFiles / totalFiles);
},
hashCheck: true,
sh1Hash: files[file]!["downloads"]["raw"]["sha1"]));
} else {
Directory(join(
dataHome.absolute.path, "jre", javaVersion.toString(), file))
}, hashCheck: true, sh1Hash: file['downloads']['raw']['sha1']));
} else if (type == 'directory') {
totalFiles++;
Directory(join(dataHome.absolute.path, 'jre', javaVersion.toString(),
filePath))
.createSync(recursive: true);
doneFiles++;
option.sendData(doneFiles / totalFiles);
}
});
}

if (kTestMode) {
infos.infos.clear();
Expand All @@ -235,38 +238,38 @@ class _TaskState extends State<Task> {
}

// String downloadUrl =
// "https://api.adoptium.net/v3/binary/latest/$javaVersion/ga/${Platform.isMacOS ? "mac" : Platform.operatingSystem}/x${SysInfo.processors[0].architecture.name.toLowerCase()}/jdk/hotspot/normal/eclipse?project=jdk";
// 'https://api.adoptium.net/v3/binary/latest/$javaVersion/ga/${Platform.isMacOS ? 'mac' : Platform.operatingSystem}/x${SysInfo.processors[0].architecture.name.toLowerCase()}/jdk/hotspot/normal/eclipse?project=jdk';

switch (Platform.operatingSystem) {
case 'linux':
mojangJRE["linux"].keys.forEach((version) {
if (version == "minecraft-java-exe") return;
var versionMap = mojangJRE["linux"][version][0];
if (versionMap["version"]["name"].contains(javaVersion.toString())) {
future = download(versionMap["manifest"]["url"]);
mojangJRE['linux'].keys.forEach((version) {
if (version == 'minecraft-java-exe') return;
var versionMap = mojangJRE['linux'][version][0];
if (versionMap['version']['name'].contains(javaVersion.toString())) {
future = download(versionMap['manifest']['url']);
return;
}
});
break;
case 'macos':
mojangJRE["mac-os"].keys.forEach((version) {
if (version == "minecraft-java-exe") return;
var versionMap = mojangJRE["mac-os"][version][0];
if (versionMap["version"]["name"].contains(javaVersion.toString())) {
future = download(versionMap["manifest"]["url"]);
mojangJRE['mac-os'].keys.forEach((version) {
if (version == 'minecraft-java-exe') return;
var versionMap = mojangJRE['mac-os'][version][0];
if (versionMap['version']['name'].contains(javaVersion.toString())) {
future = download(versionMap['manifest']['url']);
return;
}
});
break;
case 'windows':
mojangJRE["windows-x${SysInfo.userSpaceBitness}"]
mojangJRE['windows-x${SysInfo.userSpaceBitness}']
.keys
.forEach((version) {
if (version == "minecraft-java-exe") return;
if (version == 'minecraft-java-exe') return;
var versionMap =
mojangJRE["windows-x${SysInfo.userSpaceBitness}"][version][0];
if (versionMap["version"]["name"].contains(javaVersion.toString())) {
future = download(versionMap["manifest"]["url"]);
mojangJRE['windows-x${SysInfo.userSpaceBitness}'][version][0];
if (versionMap['version']['name'].contains(javaVersion.toString())) {
future = download(versionMap['manifest']['url']);
return;
}
});
Expand All @@ -282,7 +285,7 @@ class _TaskState extends State<Task> {
Widget build(BuildContext context) {
if (downloadProgress == 1) {
return AlertDialog(
title: Text(I18n.format("launcher.java.install.auto.download.done"),
title: Text(I18n.format('launcher.java.install.auto.download.done'),
textAlign: TextAlign.center),
actions: [
OkClose(
Expand All @@ -293,12 +296,12 @@ class _TaskState extends State<Task> {
} else {
return AlertDialog(
title: Text(
"${I18n.format("launcher.java.install.auto.downloading")}\n",
'${I18n.format('launcher.java.install.auto.downloading')}\n',
textAlign: TextAlign.center),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("${(downloadProgress * 100).toStringAsFixed(2)}%"),
Text('${(downloadProgress * 100).toStringAsFixed(2)}%'),
LinearProgressIndicator(
value: downloadProgress,
),
Expand Down

0 comments on commit bee1552

Please sign in to comment.