tsacdop-podcast-app-android/lib/home/playlist.dart

450 lines
19 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:line_icons/line_icons.dart';
2020-07-26 12:20:42 +02:00
import 'package:provider/provider.dart';
import 'package:tuple/tuple.dart';
2020-04-01 11:36:45 +02:00
2020-07-07 17:29:21 +02:00
import '../state/audio_state.dart';
2020-05-06 18:50:32 +02:00
import '../type/episodebrief.dart';
import '../type/playlist.dart';
2020-07-24 16:10:08 +02:00
import '../util/custom_widget.dart';
2020-07-26 12:20:42 +02:00
import '../util/extension_helper.dart';
class PlaylistPage extends StatefulWidget {
@override
_PlaylistPageState createState() => _PlaylistPageState();
}
class _PlaylistPageState extends State<PlaylistPage> {
final textstyle = TextStyle(fontSize: 15.0, color: Colors.black);
int _sumPlaylistLength(List<EpisodeBrief> episodes) {
2020-07-26 12:20:42 +02:00
var sum = 0;
if (episodes.length == 0) {
return sum;
} else {
2020-07-13 09:41:59 +02:00
for (var episode in episodes) {
2020-04-11 19:23:12 +02:00
sum += episode.duration ~/ 60;
2020-07-13 09:41:59 +02:00
}
return sum;
}
}
ScrollController _controller;
_scrollListener() {
2020-07-26 12:20:42 +02:00
var value = _controller.offset;
setState(() => _topHeight = (100 - value) > 60 ? 100 - value : 60);
}
double _topHeight;
2020-08-08 19:25:52 +02:00
List<EpisodeBrief> episodes = [];
@override
void initState() {
super.initState();
_topHeight = 100;
_controller = ScrollController()..addListener(_scrollListener);
}
@override
void dispose() {
_controller.removeListener(_scrollListener);
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
2020-07-06 11:50:20 +02:00
final s = context.s;
var audio = Provider.of<AudioPlayerNotifier>(context, listen: false);
return AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
systemNavigationBarIconBrightness:
Theme.of(context).accentColorBrightness,
statusBarIconBrightness: Theme.of(context).accentColorBrightness,
systemNavigationBarColor: Theme.of(context).primaryColor,
),
child: Scaffold(
backgroundColor: Theme.of(context).primaryColor,
appBar: AppBar(
2020-07-06 11:50:20 +02:00
title: _topHeight == 60 ? Text(s.homeMenuPlaylist) : Center(),
elevation: 0,
2020-08-08 19:25:52 +02:00
backgroundColor: context.primaryColor,
),
body: SafeArea(
2020-04-01 11:36:45 +02:00
child: Selector<AudioPlayerNotifier, Tuple3<Playlist, bool, bool>>(
selector: (_, audio) =>
2020-03-31 18:36:20 +02:00
Tuple3(audio.queue, audio.playerRunning, audio.queueUpdate),
builder: (_, data, __) {
2020-08-08 19:25:52 +02:00
episodes = data.item1.playlist;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
2020-03-31 18:36:20 +02:00
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
height: _topHeight,
child: Row(
children: <Widget>[
Expanded(
flex: 2,
child: Container(
height: _topHeight,
padding: EdgeInsets.only(
left: 60,
),
alignment: Alignment.centerLeft,
child: RichText(
text: TextSpan(
2020-07-06 11:50:20 +02:00
text: _topHeight > 90
2020-07-26 12:20:42 +02:00
? '${s.homeMenuPlaylist}\n'
2020-07-06 11:50:20 +02:00
: '',
style: TextStyle(
2020-08-08 19:25:52 +02:00
color: context.textColor,
fontSize: 30,
),
children: <TextSpan>[
TextSpan(
text: episodes.length.toString(),
style: GoogleFonts.cairo(
textStyle: TextStyle(
color: Theme.of(context).accentColor,
fontSize: 25,
),
),
),
TextSpan(
text: episodes.length < 2
? 'episode'
: 'episodes',
style: TextStyle(
2020-08-08 19:25:52 +02:00
color: context.accentColor,
fontSize: 15,
)),
TextSpan(
text:
_sumPlaylistLength(episodes).toString(),
style: GoogleFonts.cairo(
textStyle: TextStyle(
2020-08-08 19:25:52 +02:00
color: context.accentColor,
fontSize: 25,
)),
),
TextSpan(
text: 'mins',
style: TextStyle(
2020-08-08 19:25:52 +02:00
color: context.accentColor,
fontSize: 15,
)),
],
),
),
),
),
Expanded(
flex: 1,
child: Container(
padding: EdgeInsets.all(5.0),
margin: EdgeInsets.only(right: 20.0, bottom: 5.0),
2020-04-11 19:23:12 +02:00
decoration: data.item2
? BoxDecoration(
color: context.brightness == Brightness.dark
? Colors.grey[800]
: Colors.grey[200],
borderRadius:
BorderRadius.all(Radius.circular(10.0)),
)
: BoxDecoration(
shape: BoxShape.circle,
color: Colors.transparent),
child: data.item2
? _topHeight < 90
? Row(
mainAxisAlignment:
MainAxisAlignment.center,
crossAxisAlignment:
CrossAxisAlignment.center,
children: <Widget>[
CircleAvatar(
radius: 12,
backgroundImage: FileImage(File(
"${episodes.first.imagePath}")),
2020-04-01 11:36:45 +02:00
),
Padding(
padding: EdgeInsets.symmetric(
horizontal: 15),
child: SizedBox(
width: 20,
height: 15,
2020-06-27 20:27:39 +02:00
child: WaveLoader(
color: context.accentColor,
)),
),
],
)
: Column(
mainAxisAlignment:
MainAxisAlignment.center,
crossAxisAlignment:
CrossAxisAlignment.center,
children: <Widget>[
CircleAvatar(
radius: 15,
//backgroundColor: _c.withOpacity(0.5),
backgroundImage: FileImage(File(
"${episodes.first.imagePath}")),
),
Container(
width: 150,
alignment: Alignment.center,
child: Text(
episodes.first.title,
maxLines: 1,
overflow: TextOverflow.fade,
textAlign: TextAlign.center,
),
),
Padding(
padding: EdgeInsets.symmetric(
horizontal: 15),
child: SizedBox(
width: 20,
height: 15,
2020-06-27 20:27:39 +02:00
child: WaveLoader(
color: context.accentColor,
)),
),
],
)
: IconButton(
2020-04-11 19:23:12 +02:00
padding: EdgeInsets.all(0),
alignment: Alignment.center,
icon: Icon(Icons.play_circle_filled,
2020-08-08 19:25:52 +02:00
size: 40, color: (context).accentColor),
onPressed: () {
audio.playlistLoad();
// setState(() {});
}),
),
2020-04-01 11:36:45 +02:00
),
],
),
),
Divider(
height: 3,
),
Expanded(
2020-03-31 18:36:20 +02:00
child: ReorderableListView(
2020-04-01 11:36:45 +02:00
scrollController: _controller,
2020-07-26 12:20:42 +02:00
onReorder: (oldIndex, newIndex) {
2020-03-31 18:36:20 +02:00
if (newIndex > oldIndex) {
newIndex -= 1;
}
2020-08-09 17:12:33 +02:00
audio.reorderPlaylist(oldIndex, newIndex);
// final episodeRemove = episodes.removeAt(oldIndex);
// print(episodeRemove.title);
2020-08-08 19:25:52 +02:00
setState(() {
2020-08-09 17:12:33 +02:00
// episodes.insert(newIndex, episodeRemove);
2020-08-08 19:25:52 +02:00
});
2020-03-31 18:36:20 +02:00
},
scrollDirection: Axis.vertical,
2020-04-01 11:36:45 +02:00
children: data.item2
? episodes.map<Widget>((episode) {
if (episode.enclosureUrl !=
2020-07-26 12:20:42 +02:00
episodes.first.enclosureUrl) {
2020-04-01 11:36:45 +02:00
return DismissibleContainer(
episode: episode,
key: ValueKey(episode.enclosureUrl),
);
2020-07-26 12:20:42 +02:00
} else {
2020-04-01 11:36:45 +02:00
return Container(
key: ValueKey('sd'),
);
2020-07-26 12:20:42 +02:00
}
2020-04-01 11:36:45 +02:00
}).toList()
: episodes
.map<Widget>((episode) => DismissibleContainer(
episode: episode,
key: ValueKey(episode.enclosureUrl),
))
.toList()),
),
],
);
},
),
),
),
);
}
}
2020-03-31 18:36:20 +02:00
class DismissibleContainer extends StatefulWidget {
final EpisodeBrief episode;
DismissibleContainer({this.episode, Key key}) : super(key: key);
@override
_DismissibleContainerState createState() => _DismissibleContainerState();
}
class _DismissibleContainerState extends State<DismissibleContainer> {
bool _delete;
Widget _episodeTag(String text, Color color) {
return Container(
decoration: BoxDecoration(
color: color, borderRadius: BorderRadius.all(Radius.circular(15.0))),
height: 23.0,
margin: EdgeInsets.only(right: 10.0),
padding: EdgeInsets.symmetric(horizontal: 8.0),
alignment: Alignment.center,
child: Text(text, style: TextStyle(fontSize: 14.0, color: Colors.black)),
);
}
@override
void initState() {
_delete = false;
super.initState();
}
@override
Widget build(BuildContext context) {
var audio = Provider.of<AudioPlayerNotifier>(context, listen: false);
2020-07-06 11:50:20 +02:00
final s = context.s;
2020-07-26 12:20:42 +02:00
var _c = (Theme.of(context).brightness == Brightness.light)
? widget.episode.primaryColor.colorizedark()
: widget.episode.primaryColor.colorizeLight();
2020-03-31 18:36:20 +02:00
return AnimatedContainer(
duration: Duration(milliseconds: 300),
alignment: Alignment.center,
2020-03-31 18:36:20 +02:00
height: _delete ? 0 : 95.0,
child: _delete
? Container(
2020-04-06 14:18:08 +02:00
color: Colors.transparent,
2020-03-31 18:36:20 +02:00
)
: Dismissible(
2020-07-26 12:20:42 +02:00
key: ValueKey('${widget.episode.enclosureUrl}t'),
2020-03-31 18:36:20 +02:00
background: Container(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
decoration: BoxDecoration(
shape: BoxShape.circle, color: Colors.red),
padding: EdgeInsets.all(5),
alignment: Alignment.center,
child: Icon(
LineIcons.trash_alt_solid,
color: Colors.white,
size: 15,
),
),
Container(
decoration: BoxDecoration(
shape: BoxShape.circle, color: Colors.red),
padding: EdgeInsets.all(5),
alignment: Alignment.center,
child: Icon(
LineIcons.trash_alt_solid,
color: Colors.white,
size: 15,
),
),
],
),
height: 50,
color: Theme.of(context).accentColor,
),
onDismissed: (direction) async {
setState(() {
_delete = true;
});
2020-07-26 12:20:42 +02:00
var index = await audio.delFromPlaylist(widget.episode);
2020-03-31 18:36:20 +02:00
final episodeRemove = widget.episode;
Fluttertoast.showToast(
2020-07-06 11:50:20 +02:00
msg: s.toastRemovePlaylist,
2020-03-31 18:36:20 +02:00
gravity: ToastGravity.BOTTOM,
);
Scaffold.of(context).showSnackBar(SnackBar(
2020-06-27 20:27:39 +02:00
behavior: SnackBarBehavior.floating,
2020-04-06 14:18:08 +02:00
backgroundColor: Colors.grey[800],
2020-07-06 11:50:20 +02:00
content: Text(s.toastRemovePlaylist,
2020-04-11 19:23:12 +02:00
style: TextStyle(color: Colors.white)),
2020-03-31 18:36:20 +02:00
action: SnackBarAction(
2020-04-06 14:18:08 +02:00
textColor: context.accentColor,
2020-07-06 11:50:20 +02:00
label: s.undo,
2020-03-31 18:36:20 +02:00
onPressed: () {
audio.addToPlaylistAt(episodeRemove, index);
}),
));
},
child: Column(
children: <Widget>[
ListTile(
title: Container(
padding: EdgeInsets.only(top: 10.0, bottom: 5.0),
child: Text(
widget.episode.title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
leading: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.unfold_more, color: _c),
CircleAvatar(
//backgroundColor: _c.withOpacity(0.5),
backgroundImage:
FileImage(File("${widget.episode.imagePath}")),
),
],
2020-03-31 18:36:20 +02:00
),
subtitle: Container(
padding: EdgeInsets.only(top: 5, bottom: 10),
child: Row(
children: <Widget>[
(widget.episode.explicit == 1)
? Container(
decoration: BoxDecoration(
color: Colors.red[800],
shape: BoxShape.circle),
height: 20.0,
width: 20.0,
margin: EdgeInsets.only(right: 10.0),
alignment: Alignment.center,
child: Text('E',
style: TextStyle(color: Colors.white)))
: Center(),
widget.episode.duration != 0
? _episodeTag(
2020-07-06 11:50:20 +02:00
s.minsCount(widget.episode.duration ~/ 60),
2020-03-31 18:36:20 +02:00
Colors.cyan[300])
: Center(),
widget.episode.enclosureLength != null
? _episodeTag(
2020-07-26 12:20:42 +02:00
'${(widget.episode.enclosureLength) ~/ 1000000}MB',
2020-03-31 18:36:20 +02:00
Colors.lightBlue[300])
: Center(),
],
),
),
//trailing: Icon(Icons.menu),
2020-03-31 18:36:20 +02:00
),
// Divider(
// height: 2,
// ),
],
),
),
);
}
}