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

284 lines
8.6 KiB
Dart
Raw Normal View History

import 'dart:async';
2020-07-26 12:20:42 +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';
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> {
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,
);
}
_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
}
_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
}
_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;
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,
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(
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
),
() => _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: 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,
padding: EdgeInsets.symmetric(horizontal: 18),
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,
padding: EdgeInsets.symmetric(horizontal: 18),
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
}
}
}