tsacdop-podcast-app-android/lib/episodes/episode_download.dart

267 lines
8.3 KiB
Dart
Raw Normal View History

import 'dart:async';
2020-08-28 22:52:35 +02:00
import 'dart:math' as math;
import 'dart:ui';
2020-02-09 13:29:09 +01:00
2020-07-26 12:20:42 +02:00
import 'package:connectivity/connectivity.dart';
2020-02-09 13:29:09 +01:00
import 'package:flutter/material.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:fluttertoast/fluttertoast.dart';
2020-07-26 12:20:42 +02:00
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
2020-09-15 18:07:34 +02:00
import '../local_storage/key_value_storage.dart';
2020-05-06 18:50:32 +02:00
2020-07-07 17:29:21 +02:00
import '../state/audio_state.dart';
2020-07-26 12:20:42 +02:00
import '../state/download_state.dart';
import '../type/episode_task.dart';
2020-05-06 18:50:32 +02:00
import '../type/episodebrief.dart';
2020-07-26 12:20:42 +02:00
import '../util/custom_widget.dart';
import '../util/extension_helper.dart';
import '../util/general_dialog.dart';
2020-02-09 13:29:09 +01:00
class DownloadButton extends StatefulWidget {
2020-03-31 18:36:20 +02:00
final EpisodeBrief episode;
DownloadButton({this.episode, Key key}) : super(key: key);
2020-02-09 13:29:09 +01:00
@override
_DownloadButtonState createState() => _DownloadButtonState();
}
class _DownloadButtonState extends State<DownloadButton> {
2020-09-15 13:48:22 +02:00
Future<void> _requestDownload(EpisodeBrief episode) async {
final downloadUsingData = await KeyValueStorage(downloadUsingDataKey)
.getBool(defaultValue: true, reverse: true);
final permissionReady = await _checkPermmison();
final result = await Connectivity().checkConnectivity();
final usingData = result == ConnectivityResult.mobile;
var dataConfirm = true;
if (permissionReady) {
if (downloadUsingData && usingData) {
dataConfirm = await _useDataConfirm();
2020-03-31 18:36:20 +02:00
}
2020-09-15 13:48:22 +02:00
if (dataConfirm) {
2020-03-31 18:36:20 +02:00
Provider.of<DownloadState>(context, listen: false).startTask(episode);
}
}
2020-02-09 13:29:09 +01:00
}
2020-03-31 18:36:20 +02:00
void _deleteDownload(EpisodeBrief episode) async {
Provider.of<DownloadState>(context, listen: false).delTask(episode);
2020-02-09 13:29:09 +01:00
Fluttertoast.showToast(
2020-09-15 13:48:22 +02:00
msg: context.s.downloadRemovedToast,
2020-02-09 13:29:09 +01:00
gravity: ToastGravity.BOTTOM,
);
}
2020-09-15 13:48:22 +02:00
Future<void> _pauseDownload(EpisodeBrief episode) async {
2020-03-31 18:36:20 +02:00
Provider.of<DownloadState>(context, listen: false).pauseTask(episode);
2020-02-09 13:29:09 +01:00
}
2020-09-15 13:48:22 +02:00
Future<void> _resumeDownload(EpisodeBrief episode) async {
2020-03-31 18:36:20 +02:00
Provider.of<DownloadState>(context, listen: false).resumeTask(episode);
2020-02-09 13:29:09 +01:00
}
2020-09-15 13:48:22 +02:00
Future<void> _retryDownload(EpisodeBrief episode) async {
2020-03-31 18:36:20 +02:00
Provider.of<DownloadState>(context, listen: false).retryTask(episode);
2020-02-09 13:29:09 +01:00
}
Future<bool> _checkPermmison() async {
2020-07-26 12:20:42 +02:00
var permission = await Permission.storage.status;
2020-02-09 13:29:09 +01:00
if (permission != PermissionStatus.granted) {
2020-07-26 12:20:42 +02:00
var permissions = await [Permission.storage].request();
2020-04-13 17:53:21 +02:00
if (permissions[Permission.storage] == PermissionStatus.granted) {
2020-02-09 13:29:09 +01:00
return true;
} else {
return false;
}
} else {
return true;
}
}
2020-09-15 13:48:22 +02:00
Future<bool> _useDataConfirm() async {
2020-07-26 12:20:42 +02:00
var ifUseData = false;
final s = context.s;
await generalDialog(
context,
title: Text(s.cellularConfirm),
content: Text(s.cellularConfirmDes),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(
s.cancel,
style: TextStyle(color: Colors.grey[600]),
),
2020-03-31 18:36:20 +02:00
),
FlatButton(
onPressed: () {
ifUseData = true;
Navigator.of(context).pop();
},
child: Text(
s.confirm,
style: TextStyle(color: Colors.red),
),
)
],
2020-03-31 18:36:20 +02:00
);
return ifUseData;
}
Widget _buttonOnMenu(Widget widget, Function() onTap) => Material(
color: Colors.transparent,
child: InkWell(
onTap: onTap,
child: Container(
height: 50.0,
padding: EdgeInsets.symmetric(horizontal: 15.0),
child: widget),
),
);
2020-02-09 13:29:09 +01:00
@override
Widget build(BuildContext context) {
2020-03-31 18:36:20 +02:00
return Consumer<DownloadState>(builder: (_, downloader, __) {
2020-07-26 12:20:42 +02:00
var _task = Provider.of<DownloadState>(context, listen: false)
2020-03-31 18:36:20 +02:00
.episodeToTask(widget.episode);
return Row(
children: <Widget>[
_downloadButton(_task, context),
AnimatedContainer(
duration: Duration(seconds: 1),
decoration: BoxDecoration(
color: Theme.of(context).accentColor,
borderRadius: BorderRadius.all(Radius.circular(15.0))),
height: 20.0,
width: (_task.status == DownloadTaskStatus.running) ? 50.0 : 0,
alignment: Alignment.center,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
2020-08-28 22:52:35 +02:00
child: Text('${math.max(_task.progress, 0)}%',
2020-03-31 18:36:20 +02:00
style: TextStyle(color: Colors.white)),
)),
],
);
});
2020-02-09 13:29:09 +01:00
}
2020-03-31 18:36:20 +02:00
Widget _downloadButton(EpisodeTask task, BuildContext context) {
switch (task.status.value) {
case 0:
2020-09-15 13:48:22 +02:00
return _buttonOnMenu(
Center(
child: SizedBox(
height: 20,
width: 20,
child: CustomPaint(
painter: DownloadPainter(
color: Colors.grey[700],
fraction: 0,
progressColor: context.accentColor,
),
),
2020-03-31 18:36:20 +02:00
),
2020-09-15 13:48:22 +02:00
),
() => _requestDownload(task.episode));
2020-03-31 18:36:20 +02:00
break;
case 2:
return Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
if (task.progress > 0) _pauseDownload(task.episode);
},
child: Container(
height: 50.0,
alignment: Alignment.center,
2020-08-28 22:52:35 +02:00
padding: EdgeInsets.symmetric(horizontal: 15.0),
child: TweenAnimationBuilder(
duration: Duration(milliseconds: 1000),
tween: Tween(begin: 0.0, end: 1.0),
builder: (context, fraction, child) => SizedBox(
height: 20,
width: 20,
child: CustomPaint(
painter: DownloadPainter(
color: context.accentColor,
fraction: fraction,
progressColor: context.accentColor,
progress: task.progress / 100),
),
2020-03-31 18:36:20 +02:00
),
),
2020-02-09 13:29:09 +01:00
),
),
2020-03-31 18:36:20 +02:00
);
break;
case 6:
return Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
_resumeDownload(task.episode);
},
child: Container(
height: 50.0,
alignment: Alignment.center,
2020-08-28 22:52:35 +02:00
padding: EdgeInsets.symmetric(horizontal: 15),
child: TweenAnimationBuilder(
duration: Duration(milliseconds: 500),
tween: Tween(begin: 0.0, end: 1.0),
builder: (context, fraction, child) => SizedBox(
height: 20,
width: 20,
child: CustomPaint(
painter: DownloadPainter(
color: context.accentColor,
fraction: 1,
progressColor: context.accentColor,
progress: task.progress / 100,
pauseProgress: fraction),
),
2020-03-31 18:36:20 +02:00
),
),
2020-02-09 13:29:09 +01:00
),
),
2020-03-31 18:36:20 +02:00
);
break;
case 3:
Provider.of<AudioPlayerNotifier>(context, listen: false)
.updateMediaItem(task.episode);
return Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
_deleteDownload(task.episode);
},
child: Container(
height: 50.0,
alignment: Alignment.center,
2020-08-28 22:52:35 +02:00
padding: EdgeInsets.symmetric(horizontal: 15),
child: SizedBox(
height: 20,
width: 20,
child: CustomPaint(
painter: DownloadPainter(
color: context.accentColor,
fraction: 1,
progressColor: context.accentColor,
progress: 1,
),
),
),
),
),
);
2020-03-31 18:36:20 +02:00
break;
case 4:
return _buttonOnMenu(Icon(Icons.refresh, color: Colors.red),
() => _retryDownload(task.episode));
break;
default:
return Center();
2020-02-09 13:29:09 +01:00
}
}
}