2020-03-14 04:14:24 +01:00
|
|
|
import 'dart:async';
|
2020-08-28 22:52:35 +02:00
|
|
|
import 'dart:math' as math;
|
2020-09-14 21:02:32 +02:00
|
|
|
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-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';
|
2020-07-07 17:29:21 +02:00
|
|
|
import '../state/setting_state.dart';
|
2020-08-23 11:41:43 +02:00
|
|
|
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';
|
2020-07-25 07:42:48 +02:00
|
|
|
import '../util/extension_helper.dart';
|
2020-06-10 09:42:40 +02:00
|
|
|
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> {
|
|
|
|
bool _permissionReady;
|
2020-03-31 18:36:20 +02:00
|
|
|
bool _usingData;
|
|
|
|
StreamSubscription _connectivity;
|
2020-02-09 13:29:09 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_permissionReady = false;
|
2020-03-31 18:36:20 +02:00
|
|
|
_connectivity = Connectivity().onConnectivityChanged.listen((result) {
|
|
|
|
_usingData = result == ConnectivityResult.mobile;
|
|
|
|
});
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2020-03-31 18:36:20 +02:00
|
|
|
_connectivity.cancel();
|
2020-02-09 13:29:09 +01:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2020-03-31 18:36:20 +02:00
|
|
|
void _requestDownload(EpisodeBrief episode, bool downloadUsingData) async {
|
2020-02-09 13:29:09 +01:00
|
|
|
_permissionReady = await _checkPermmison();
|
2020-07-26 12:20:42 +02:00
|
|
|
var _dataConfirm = true;
|
2020-03-31 18:36:20 +02:00
|
|
|
if (_permissionReady) {
|
|
|
|
if (downloadUsingData && _usingData) {
|
|
|
|
_dataConfirm = await _useDataConfirem();
|
|
|
|
}
|
|
|
|
if (_dataConfirm) {
|
|
|
|
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(
|
|
|
|
msg: 'Download removed',
|
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-25 07:42:48 +02:00
|
|
|
_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-07-25 07:42:48 +02:00
|
|
|
_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-07-25 07:42:48 +02:00
|
|
|
_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-03-31 18:36:20 +02:00
|
|
|
Future<bool> _useDataConfirem() async {
|
2020-07-26 12:20:42 +02:00
|
|
|
var ifUseData = false;
|
2020-07-25 07:42:48 +02:00
|
|
|
final s = context.s;
|
2020-06-10 09:42:40 +02:00
|
|
|
await generalDialog(
|
|
|
|
context,
|
2020-07-25 07:42:48 +02:00
|
|
|
title: Text(s.cellularConfirm),
|
|
|
|
content: Text(s.cellularConfirmDes),
|
2020-06-10 09:42:40 +02:00
|
|
|
actions: <Widget>[
|
|
|
|
FlatButton(
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
child: Text(
|
2020-07-25 07:42:48 +02:00
|
|
|
s.cancel,
|
2020-06-10 09:42:40 +02:00
|
|
|
style: TextStyle(color: Colors.grey[600]),
|
|
|
|
),
|
2020-03-31 18:36:20 +02:00
|
|
|
),
|
2020-06-10 09:42:40 +02:00
|
|
|
FlatButton(
|
|
|
|
onPressed: () {
|
|
|
|
ifUseData = true;
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
child: Text(
|
2020-07-25 07:42:48 +02:00
|
|
|
s.confirm,
|
2020-06-10 09:42:40 +02:00
|
|
|
style: TextStyle(color: Colors.red),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
2020-03-31 18:36:20 +02:00
|
|
|
);
|
|
|
|
return ifUseData;
|
|
|
|
}
|
|
|
|
|
2020-02-13 03:51:46 +01:00
|
|
|
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:
|
|
|
|
return Selector<SettingState, bool>(
|
|
|
|
selector: (_, settings) => settings.downloadUsingData,
|
|
|
|
builder: (_, data, __) => _buttonOnMenu(
|
2020-07-25 10:59:27 +02:00
|
|
|
Center(
|
|
|
|
child: SizedBox(
|
2020-08-23 11:41:43 +02:00
|
|
|
height: 20,
|
|
|
|
width: 20,
|
2020-07-25 10:59:27 +02:00
|
|
|
child: CustomPaint(
|
|
|
|
painter: DownloadPainter(
|
|
|
|
color: Colors.grey[700],
|
|
|
|
fraction: 0,
|
|
|
|
progressColor: context.accentColor,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2020-03-31 18:36:20 +02:00
|
|
|
),
|
|
|
|
() => _requestDownload(task.episode, data)),
|
|
|
|
);
|
|
|
|
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),
|
2020-07-25 10:59:27 +02:00
|
|
|
child: TweenAnimationBuilder(
|
|
|
|
duration: Duration(milliseconds: 1000),
|
|
|
|
tween: Tween(begin: 0.0, end: 1.0),
|
|
|
|
builder: (context, fraction, child) => SizedBox(
|
2020-08-23 11:41:43 +02:00
|
|
|
height: 20,
|
|
|
|
width: 20,
|
2020-07-25 10:59:27 +02:00
|
|
|
child: CustomPaint(
|
|
|
|
painter: DownloadPainter(
|
2020-08-23 11:41:43 +02:00
|
|
|
color: context.accentColor,
|
2020-07-25 10:59:27 +02:00
|
|
|
fraction: fraction,
|
|
|
|
progressColor: context.accentColor,
|
|
|
|
progress: task.progress / 100),
|
|
|
|
),
|
2020-03-31 18:36:20 +02:00
|
|
|
),
|
2020-02-13 03:51:46 +01: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),
|
2020-07-25 10:59:27 +02:00
|
|
|
child: TweenAnimationBuilder(
|
|
|
|
duration: Duration(milliseconds: 500),
|
|
|
|
tween: Tween(begin: 0.0, end: 1.0),
|
|
|
|
builder: (context, fraction, child) => SizedBox(
|
2020-08-23 11:41:43 +02:00
|
|
|
height: 20,
|
|
|
|
width: 20,
|
2020-07-25 10:59:27 +02:00
|
|
|
child: CustomPaint(
|
|
|
|
painter: DownloadPainter(
|
2020-08-23 11:41:43 +02:00
|
|
|
color: context.accentColor,
|
2020-07-25 10:59:27 +02:00
|
|
|
fraction: 1,
|
|
|
|
progressColor: context.accentColor,
|
|
|
|
progress: task.progress / 100,
|
|
|
|
pauseProgress: fraction),
|
|
|
|
),
|
2020-03-31 18:36:20 +02:00
|
|
|
),
|
2020-02-13 03:51:46 +01: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);
|
2020-08-23 11:41:43 +02:00
|
|
|
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),
|
2020-08-23 11:41:43 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|