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

Commit

Permalink
檔案下載進度顯示
Browse files Browse the repository at this point in the history
  • Loading branch information
SiongSng committed Sep 12, 2021
1 parent c7f4c04 commit 2fedd14
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
49 changes: 41 additions & 8 deletions lib/Utility/Updater.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import 'dart:io';
import 'package:archive/archive.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:http/http.dart';
import 'package:http/http.dart' as http;
import 'package:path/path.dart';
import 'package:rpmlauncher/LauncherInfo.dart';
import 'package:rpmlauncher/Utility/i18n.dart';
import 'package:rpmlauncher/Widget/OkClose.dart';
import 'package:rpmlauncher/path.dart';

enum UpdateChannels { stable, dev }
Expand Down Expand Up @@ -107,9 +109,9 @@ class Updater {
}
}

static Future<void> update(VersionInfo info) async {
static Future<void> download(VersionInfo info, BuildContext context) async {
Directory updateDir = Directory(join(dataHome.absolute.path, "update"));

late StateSetter setState;
String operatingSystem = Platform.operatingSystem;
String downloadUrl;

Expand All @@ -135,12 +137,43 @@ class Updater {
default:
throw Exception("Unknown operating system");
}
Dio dio = Dio();
double progress = 0;
File updateFile = File(join(updateDir.absolute.path, "update.zip"));
await dio.download(downloadUrl, updateFile.absolute.path,
onReceiveProgress: (count, total) {
print((count / total * 100).toStringAsFixed(2) + "%");
});

Future<bool> downloading() async {
Dio dio = Dio();
await dio.download(downloadUrl, updateFile.absolute.path,
onReceiveProgress: (count, total) {
setState(() {
progress = count / total;
});
});
return true;
}

showDialog(
context: context,
builder: (context) => FutureBuilder(
future: downloading(),
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return AlertDialog(
title: Text("下載完成"),
actions: [OkClose()],
);
} else {
return StatefulBuilder(builder: (context, _setState) {
setState = _setState;
return AlertDialog(
title: Text("下載檔案中..."),
content: LinearProgressIndicator(
value: progress,
),
);
});
}
}));

Archive archive = ZipDecoder().decodeBytes(updateFile.readAsBytesSync());

for (ArchiveFile file in archive) {
Expand Down Expand Up @@ -192,7 +225,7 @@ class VersionInfo {

return VersionInfo(
downloadUrl: DownloadUrl.fromJson(json['download_url']),
changelog: changelogs.join(" \n"),
changelog: changelogs.reversed.toList().join(" \n"),
type: Updater.getChannelFromString(json['type']),
versionCode: version_code,
version: version,
Expand Down
5 changes: 3 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class _HomePageState extends State<HomePage> {
selectable: true,
styleSheet: MarkdownStyleSheet(
textAlign: WrapAlignment.center,
textScaleFactor: 1.5,
h1Align: WrapAlignment.center,
unorderedListAlign: WrapAlignment.center,
),
Expand All @@ -213,8 +214,8 @@ class _HomePageState extends State<HomePage> {
child: Text("不要更新")),
TextButton(
onPressed: () {
// Navigator.pop(context);
Updater.update(info);
Navigator.pop(context);
Updater.download(info, context);
},
child: Text("更新"))
]);
Expand Down

0 comments on commit 2fedd14

Please sign in to comment.