Compatibility with quicksync

This commit is contained in:
Yannick Mauray 2021-10-21 23:54:27 +02:00
parent 1ed771add3
commit efd6125425
No known key found for this signature in database
GPG Key ID: 67C4AAC5E99CB909
3 changed files with 73 additions and 87 deletions

View File

@ -23,9 +23,10 @@ class Downloader extends StatefulWidget {
}
class _DownloaderState extends State<Downloader> {
final pattern = RegExp("( [0-9]+%)");
final pattern = RegExp("( [0-9.]+%)");
late final Stream<double> _progressStream;
bool _downloadFinished = false;
var controller = StreamController<double>();
@override
void initState() {
@ -33,24 +34,28 @@ class _DownloaderState extends State<Downloader> {
super.initState();
}
void parseProgress(String line) {
print(line);
var matches = pattern.allMatches(line).toList();
if (matches.isNotEmpty) {
var percent = matches[0].group(1);
if (percent != null) {
var value = double.parse(percent.replaceAll('%', '')) / 100.0;
controller.add(value);
}
}
}
Stream<double> progressStream() {
var options = [widget.operatingSystem.code, widget.version.version];
if (widget.option != null) {
options.add(widget.option!);
}
var controller = StreamController<double>();
Process.start('quickget', options).then((process) {
process.stderr.transform(utf8.decoder).forEach((line) {
var matches = pattern.allMatches(line).toList();
if (matches.isNotEmpty) {
var percent = matches[0].group(1);
if (percent != null) {
var value = double.parse(percent.replaceAll('%', '')) / 100.0;
controller.add(value);
}
}
});
process.stdout.transform(utf8.decoder).forEach(parseProgress);
process.stderr.transform(utf8.decoder).forEach(parseProgress);
process.exitCode.then((value) {
print("Process exited with exit code $value");
controller.close();
setState(() {
_downloadFinished = true;
@ -62,69 +67,63 @@ class _DownloaderState extends State<Downloader> {
@override
Widget build(BuildContext context) {
//process.then((process) {
// process.stderr.transform(utf8.decoder).forEach((e) {
// var matches = pattern.allMatches(e).toList();
// var percent = matches[0].group(1);
// if (percent != null) {}
// });
//});
return Scaffold(
appBar: AppBar(
title: Text('Downloading ${widget.operatingSystem.name} ${widget.version.version}' + (widget.option != null ? ' (${widget.option})' : '')),
automaticallyImplyLeading: false,
),
body: Column(children: [
Expanded(
child: StreamBuilder(
stream: _progressStream,
builder: (context, AsyncSnapshot<double> snapshot) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: _downloadFinished
? const Text('Download finished.')
: snapshot.hasData
? Text('Downloading...${(snapshot.data! * 100).toInt()}%')
: const Text('Waiting for download to start'),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
width: 200,
child: LinearProgressIndicator(
value: _downloadFinished
? 1
: snapshot.hasData
? snapshot.data!
: null,
body: Column(
children: [
Expanded(
child: StreamBuilder(
stream: _progressStream,
builder: (context, AsyncSnapshot<double> snapshot) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: _downloadFinished
? const Text('Download finished.')
: snapshot.hasData
? Text('Downloading...${(snapshot.data! * 100).toInt()}%')
: const Text('Waiting for download to start'),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
width: 200,
child: LinearProgressIndicator(
value: _downloadFinished
? 1
: snapshot.hasData
? snapshot.data!
: null,
),
),
),
),
],
);
},
],
);
},
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _downloadFinished
? () {
Navigator.of(context).pop();
}
: null,
child: _downloadFinished ? const Text('Dimiss') : const Text('Cancel'))
],
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _downloadFinished
? () {
Navigator.of(context).pop();
}
: null,
child: _downloadFinished ? const Text('Dimiss') : const Text('Cancel'))
],
),
),
),
]),
],
),
);
}
}

View File

@ -10,6 +10,7 @@ class MainPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
Directory.current = '/home/yannick';
return Scaffold(
body: Column(
children: [

View File

@ -1,4 +1,3 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/cupertino.dart';
@ -75,32 +74,19 @@ class _HomePageButtonsState extends State<HomePageButtons> {
onPressed: (_selectedVersion == null)
? null
: () {
Navigator.of(context).push(MaterialPageRoute(
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Downloader(
operatingSystem: _selectedOperatingSystem!,
version: _selectedVersion!,
option: _selectedOption,
)));
/*
showLoadingIndicator(text: 'Downloading');
//await Process.run('quickget', [_selectedOperatingSystem!.code, _selectedVersion!.version]);
var options = [_selectedOperatingSystem!.code, _selectedVersion!.version];
if (_selectedOption != null) {
options.add(_selectedOption!);
}
var process = await Process.start('quickget', options);
process.stderr.transform(utf8.decoder).forEach(
operatingSystem: _selectedOperatingSystem!,
version: _selectedVersion!,
option: _selectedOption,
),
),
);
var exitCode = await process.exitCode;
hideLoadingIndicator();
showDoneDialog(operatingSystem: _selectedOperatingSystem!.code, version: _selectedVersion!.version);
*/
},
),
],
);
;
}
void showLoadingIndicator({String text = ''}) {