2020-02-09 13:29:09 +01:00
|
|
|
import 'dart:ui';
|
2020-03-14 04:14:24 +01:00
|
|
|
import 'dart:async';
|
2020-02-09 13:29:09 +01:00
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2020-03-14 04:14:24 +01:00
|
|
|
import 'package:provider/provider.dart';
|
2020-02-09 13:29:09 +01:00
|
|
|
import 'package:flutter_downloader/flutter_downloader.dart';
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
2020-03-31 18:36:20 +02:00
|
|
|
import 'package:connectivity/connectivity.dart';
|
2020-05-06 18:50:32 +02:00
|
|
|
|
|
|
|
import '../state/download_state.dart';
|
|
|
|
import '../state/audiostate.dart';
|
|
|
|
import '../state/settingstate.dart';
|
|
|
|
import '../type/episodebrief.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-03-31 18:36:20 +02:00
|
|
|
bool _dataConfirm = true;
|
|
|
|
if (_permissionReady) {
|
|
|
|
if (downloadUsingData && _usingData) {
|
|
|
|
_dataConfirm = await _useDataConfirem();
|
|
|
|
}
|
|
|
|
if (_dataConfirm) {
|
|
|
|
Provider.of<DownloadState>(context, listen: false).startTask(episode);
|
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: 'Downloading',
|
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
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-03-31 18:36:20 +02:00
|
|
|
void _pauseDownload(EpisodeBrief episode) async {
|
|
|
|
Provider.of<DownloadState>(context, listen: false).pauseTask(episode);
|
2020-02-09 13:29:09 +01:00
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: 'Download paused',
|
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-31 18:36:20 +02:00
|
|
|
void _resumeDownload(EpisodeBrief episode) async {
|
|
|
|
Provider.of<DownloadState>(context, listen: false).resumeTask(episode);
|
2020-02-09 13:29:09 +01:00
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: 'Download resumed',
|
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-31 18:36:20 +02:00
|
|
|
void _retryDownload(EpisodeBrief episode) async {
|
|
|
|
Provider.of<DownloadState>(context, listen: false).retryTask(episode);
|
2020-02-13 03:51:46 +01:00
|
|
|
Fluttertoast.showToast(
|
2020-02-09 13:29:09 +01:00
|
|
|
msg: 'Download again',
|
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> _checkPermmison() async {
|
2020-04-13 17:53:21 +02:00
|
|
|
PermissionStatus permission = await Permission.storage.status;
|
2020-02-09 13:29:09 +01:00
|
|
|
if (permission != PermissionStatus.granted) {
|
2020-04-13 17:53:21 +02:00
|
|
|
Map<Permission, PermissionStatus> permissions =
|
|
|
|
await [Permission.storage].request();
|
|
|
|
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 {
|
|
|
|
bool ifUseData = false;
|
2020-06-10 09:42:40 +02:00
|
|
|
await generalDialog(
|
|
|
|
context,
|
|
|
|
title: Text('Cellular data warn'),
|
|
|
|
content: Text('Are you sure you want to use cellular data to download?'),
|
|
|
|
actions: <Widget>[
|
|
|
|
FlatButton(
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
child: Text(
|
|
|
|
'CANCEL',
|
|
|
|
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(
|
|
|
|
'CONFIRM',
|
|
|
|
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, __) {
|
|
|
|
EpisodeTask _task = Provider.of<DownloadState>(context, listen: false)
|
|
|
|
.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,
|
|
|
|
child: Text('${_task.progress}%',
|
|
|
|
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(
|
|
|
|
Icon(
|
|
|
|
Icons.arrow_downward,
|
|
|
|
color: Colors.grey[700],
|
|
|
|
),
|
|
|
|
() => _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,
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 18.0),
|
|
|
|
child: SizedBox(
|
|
|
|
height: 18,
|
|
|
|
width: 18,
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
backgroundColor: Colors.grey[500],
|
|
|
|
strokeWidth: 2,
|
|
|
|
valueColor: AlwaysStoppedAnimation<Color>(
|
|
|
|
Theme.of(context).accentColor),
|
|
|
|
value: task.progress / 100,
|
|
|
|
),
|
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,
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 18),
|
|
|
|
child: SizedBox(
|
|
|
|
height: 18,
|
|
|
|
width: 18,
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
backgroundColor: Colors.grey[500],
|
|
|
|
strokeWidth: 2,
|
|
|
|
valueColor: AlwaysStoppedAnimation<Color>(Colors.red),
|
|
|
|
value: task.progress / 100,
|
|
|
|
),
|
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);
|
|
|
|
return _buttonOnMenu(
|
|
|
|
Icon(
|
|
|
|
Icons.done_all,
|
|
|
|
color: Theme.of(context).accentColor,
|
|
|
|
), () {
|
|
|
|
_deleteDownload(task.episode);
|
|
|
|
});
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|