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

768 lines
35 KiB
Dart
Raw Normal View History

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-08-09 21:07:03 +02:00
import '../local_storage/sqflite_localpodcast.dart';
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';
2020-08-09 21:07:03 +02:00
import '../type/play_histroy.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';
2020-08-15 19:43:45 +02:00
enum InitPage { playlist, history }
class PlaylistPage extends StatefulWidget {
2020-08-15 19:43:45 +02:00
final InitPage initPage;
PlaylistPage({this.initPage, Key key}) : super(key: key);
@override
_PlaylistPageState createState() => _PlaylistPageState();
}
class _PlaylistPageState extends State<PlaylistPage> {
final textstyle = TextStyle(fontSize: 15.0, color: Colors.black);
2020-08-13 11:58:46 +02:00
var _loadList;
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;
}
}
2020-08-09 21:07:03 +02:00
Future<double> _getListenTime() async {
var dbHelper = DBHelper();
var listenTime = await dbHelper.listenMins(0);
return listenTime;
}
2020-08-09 21:07:03 +02:00
bool _loadHistory = false;
2020-08-13 11:58:46 +02:00
@override
void initState() {
super.initState();
2020-08-15 19:43:45 +02:00
if (widget.initPage == InitPage.playlist) {
_loadList = _ReorderablePlaylist();
} else {
_loadHistory = true;
_loadList = _HistoryList();
}
2020-08-13 11:58:46 +02:00
}
@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(
elevation: 0,
2020-08-11 19:06:56 +02:00
backgroundColor: context.accentColor.withAlpha(70),
),
body: SafeArea(
2020-08-13 11:58:46 +02:00
child: Selector<AudioPlayerNotifier,
Tuple4<Playlist, bool, bool, EpisodeBrief>>(
selector: (_, audio) => Tuple4(audio.queue, audio.playerRunning,
audio.queueUpdate, audio.episode),
builder: (_, data, __) {
2020-08-13 11:58:46 +02:00
var 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>[
2020-08-11 19:06:56 +02:00
Container(
color: context.accentColor.withAlpha(70),
child: Row(
children: <Widget>[
Expanded(
flex: 2,
child: Container(
height: 100,
padding: EdgeInsets.only(
left: 60,
),
alignment: Alignment.centerLeft,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
_loadHistory
? s.settingsHistory
: s.homeMenuPlaylist,
style: TextStyle(
color: context.textColor,
fontSize: 30,
),
),
2020-08-11 19:06:56 +02:00
SizedBox(width: 5),
IconButton(
icon: _loadHistory
? Icon(Icons.playlist_play)
: Icon(Icons.history),
onPressed: () => setState(() {
_loadHistory = !_loadHistory;
2020-08-13 11:58:46 +02:00
if (_loadHistory) {
_loadList = _HistoryList();
} else {
_loadList =
_ReorderablePlaylist();
}
2020-08-11 19:06:56 +02:00
}))
],
),
_loadHistory
? FutureBuilder<double>(
future: _getListenTime(),
initialData: 0.0,
builder: (context, snapshot) =>
RichText(
text: TextSpan(
text: 'Today ',
style: GoogleFonts.cairo(
textStyle: TextStyle(
color: Theme.of(context)
.accentColor,
fontSize: 20,
),
),
children: <TextSpan>[
TextSpan(
text:
'${snapshot.data.toStringAsFixed(0)} ',
style: GoogleFonts.cairo(
textStyle: TextStyle(
color: context.accentColor,
fontSize: 25,
)),
),
TextSpan(
text: 'mins',
style: TextStyle(
color: context.accentColor,
fontSize: 15,
)),
],
),
),
)
: RichText(
2020-08-09 21:07:03 +02:00
text: TextSpan(
2020-08-11 19:06:56 +02:00
text: episodes.length.toString(),
2020-08-09 21:07:03 +02:00
style: GoogleFonts.cairo(
textStyle: TextStyle(
color:
Theme.of(context).accentColor,
2020-08-11 19:06:56 +02:00
fontSize: 25,
),
),
2020-08-09 21:07:03 +02:00
children: <TextSpan>[
TextSpan(
2020-08-11 19:06:56 +02:00
text: episodes.length < 2
? 'episode'
: 'episodes',
style: TextStyle(
color: context.accentColor,
fontSize: 15,
)),
TextSpan(
text: _sumPlaylistLength(episodes)
.toString(),
2020-08-09 21:07:03 +02:00
style: GoogleFonts.cairo(
textStyle: TextStyle(
color: context.accentColor,
fontSize: 25,
)),
),
TextSpan(
text: 'mins',
style: TextStyle(
2020-06-27 20:27:39 +02:00
color: context.accentColor,
2020-08-09 21:07:03 +02:00
fontSize: 15,
2020-06-27 20:27:39 +02:00
)),
2020-08-09 21:07:03 +02:00
],
),
),
2020-08-11 19:06:56 +02:00
],
),
),
),
Expanded(
flex: 1,
child: Container(
alignment: Alignment.center,
padding: EdgeInsets.all(5.0),
margin: EdgeInsets.only(right: 20.0, bottom: 5.0),
decoration: data.item2
? BoxDecoration(
color: context.brightness == Brightness.dark
? Colors.grey[800]
: Colors.grey[200],
borderRadius:
BorderRadius.all(Radius.circular(10.0)),
)
: BoxDecoration(color: Colors.transparent),
child: data.item2
? Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment:
CrossAxisAlignment.center,
children: <Widget>[
CircleAvatar(
2020-08-15 19:43:45 +02:00
radius: 15,
backgroundImage:
data.item4.avatarImage),
2020-08-11 19:06:56 +02:00
Container(
width: 150,
alignment: Alignment.center,
child: Text(
2020-08-13 11:58:46 +02:00
data.item4.title,
2020-08-11 19:06:56 +02:00
maxLines: 1,
overflow: TextOverflow.fade,
textAlign: TextAlign.center,
2020-08-09 21:07:03 +02:00
),
2020-08-11 19:06:56 +02:00
),
Padding(
padding: EdgeInsets.symmetric(
horizontal: 15),
child: SizedBox(
width: 20,
height: 15,
child: WaveLoader(
2020-08-09 21:07:03 +02:00
color: context.accentColor,
)),
),
2020-08-11 19:06:56 +02:00
],
)
: IconButton(
icon: Icon(Icons.play_circle_filled,
2020-08-13 11:58:46 +02:00
size: 40, color: context.accentColor),
2020-08-11 19:06:56 +02:00
onPressed: () {
audio.playlistLoad();
// setState(() {});
}),
),
2020-04-01 11:36:45 +02:00
),
2020-08-11 19:06:56 +02:00
],
),
),
Expanded(
2020-08-13 11:58:46 +02:00
child: AnimatedSwitcher(
duration: Duration(milliseconds: 300),
child: _loadList)),
],
);
},
),
),
),
);
}
}
2020-03-31 18:36:20 +02:00
2020-08-13 11:58:46 +02:00
class _ReorderablePlaylist extends StatefulWidget {
_ReorderablePlaylist({Key key}) : super(key: key);
@override
__ReorderablePlaylistState createState() => __ReorderablePlaylistState();
}
class __ReorderablePlaylistState extends State<_ReorderablePlaylist> {
@override
Widget build(BuildContext context) {
var audio = Provider.of<AudioPlayerNotifier>(context, listen: false);
return Selector<AudioPlayerNotifier, Tuple2<Playlist, bool>>(
selector: (_, audio) => Tuple2(audio.queue, audio.playerRunning),
builder: (_, data, __) {
var episodes = data.item1.playlist;
return ReorderableListView(
onReorder: (oldIndex, newIndex) {
if (newIndex > oldIndex) {
newIndex -= 1;
}
audio.reorderPlaylist(oldIndex, newIndex);
setState(() {});
},
scrollDirection: Axis.vertical,
children: data.item2
? episodes.map<Widget>((episode) {
if (episode.enclosureUrl != episodes.first.enclosureUrl) {
return _DismissibleContainer(
episode: episode,
onRemove: (value) => setState(() {}),
key: ValueKey(episode.enclosureUrl),
);
} else {
return Container(
key: ValueKey('sd'),
);
}
}).toList()
: episodes
.map<Widget>((episode) => _DismissibleContainer(
episode: episode,
onRemove: (value) => setState(() {}),
key: ValueKey(episode.enclosureUrl),
))
.toList());
});
}
}
class _DismissibleContainer extends StatefulWidget {
2020-03-31 18:36:20 +02:00
final EpisodeBrief episode;
2020-08-13 11:58:46 +02:00
final ValueChanged<bool> onRemove;
_DismissibleContainer({this.episode, this.onRemove, Key key})
: super(key: key);
2020-03-31 18:36:20 +02:00
@override
2020-08-13 11:58:46 +02:00
__DismissibleContainerState createState() => __DismissibleContainerState();
2020-03-31 18:36:20 +02:00
}
2020-08-13 11:58:46 +02:00
class __DismissibleContainerState extends State<_DismissibleContainer> {
2020-03-31 18:36:20 +02:00
bool _delete;
Widget _episodeTag(String text, Color color) {
2020-08-13 11:58:46 +02:00
if (text == '') {
return Center();
}
2020-03-31 18:36:20 +02:00
return Container(
decoration: BoxDecoration(
2020-08-11 19:06:56 +02:00
color: color, borderRadius: BorderRadius.circular(15.0)),
height: 25.0,
2020-03-31 18:36:20 +02:00
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) {
2020-08-15 19:43:45 +02:00
final audio = Provider.of<AudioPlayerNotifier>(context, listen: false);
2020-07-06 11:50:20 +02:00
final s = context.s;
2020-08-15 19:43:45 +02:00
final c = widget.episode.backgroudColor(context);
2020-03-31 18:36:20 +02:00
return AnimatedContainer(
duration: Duration(milliseconds: 300),
2020-08-19 12:10:08 +02:00
curve: Curves.easeInSine,
alignment: Alignment.center,
2020-08-11 19:06:56 +02:00
height: _delete ? 0 : 90.0,
2020-03-31 18:36:20 +02:00
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,
),
),
],
),
2020-08-11 19:06:56 +02:00
height: 30,
color: context.accentColor,
2020-03-31 18:36:20 +02:00
),
onDismissed: (direction) async {
setState(() {
_delete = true;
});
2020-07-26 12:20:42 +02:00
var index = await audio.delFromPlaylist(widget.episode);
2020-08-13 11:58:46 +02:00
widget.onRemove(true);
2020-03-31 18:36:20 +02:00
final episodeRemove = widget.episode;
2020-08-19 12:10:08 +02:00
Scaffold.of(context).removeCurrentSnackBar();
2020-03-31 18:36:20 +02:00
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-08-13 11:58:46 +02:00
onPressed: () async {
await audio.addToPlaylistAt(episodeRemove, index);
widget.onRemove(false);
2020-03-31 18:36:20 +02:00
}),
));
},
2020-08-11 19:06:56 +02:00
child: SizedBox(
height: 90.0,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Expanded(
2020-08-13 11:58:46 +02:00
child: ListTile(
contentPadding: EdgeInsets.symmetric(vertical: 8),
onTap: () async {
await audio.episodeLoad(widget.episode);
widget.onRemove(true);
},
title: Container(
padding: EdgeInsets.fromLTRB(0, 5.0, 20.0, 5.0),
child: Text(
widget.episode.title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
2020-08-11 19:06:56 +02:00
),
2020-08-13 11:58:46 +02:00
),
leading: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.unfold_more, color: c),
CircleAvatar(
2020-08-15 19:43:45 +02:00
backgroundColor: c.withOpacity(0.5),
backgroundImage: widget.episode.avatarImage),
2020-08-13 11:58:46 +02:00
],
),
subtitle: Container(
padding: EdgeInsets.only(top: 5, bottom: 5),
height: 35,
child: Row(
children: <Widget>[
if (widget.episode.explicit == 1)
Container(
decoration: BoxDecoration(
color: Colors.red[800],
shape: BoxShape.circle),
height: 25.0,
width: 25.0,
margin: EdgeInsets.only(right: 10.0),
alignment: Alignment.center,
child: Text('E',
style: TextStyle(color: Colors.white))),
if (widget.episode.duration != 0)
_episodeTag(
widget.episode.duration == 0
? ''
: s.minsCount(
widget.episode.duration ~/ 60),
Colors.cyan[300]),
if (widget.episode.enclosureLength != null)
_episodeTag(
widget.episode.enclosureLength == 0
? ''
: '${(widget.episode.enclosureLength) ~/ 1000000}MB',
Colors.lightBlue[300]),
],
2020-08-11 19:06:56 +02:00
),
),
2020-08-13 11:58:46 +02:00
//trailing: Icon(Icons.menu),
2020-03-31 18:36:20 +02:00
),
),
2020-08-11 19:06:56 +02:00
Divider(
height: 2,
),
],
),
2020-03-31 18:36:20 +02:00
),
),
);
}
}
2020-08-09 21:07:03 +02:00
2020-08-13 11:58:46 +02:00
class _HistoryList extends StatefulWidget {
_HistoryList({Key key}) : super(key: key);
2020-08-09 21:07:03 +02:00
@override
2020-08-13 11:58:46 +02:00
__HistoryListState createState() => __HistoryListState();
2020-08-09 21:07:03 +02:00
}
2020-08-13 11:58:46 +02:00
class __HistoryListState extends State<_HistoryList> {
2020-08-09 21:07:03 +02:00
var dbHelper = DBHelper();
2020-08-16 10:05:04 +02:00
bool _loadMore = false;
Future _getData;
2020-08-09 21:07:03 +02:00
Future<List<PlayHistory>> getPlayRecords(int top) async {
List<PlayHistory> playHistory;
playHistory = await dbHelper.getPlayRecords(top);
for (var record in playHistory) {
await record.getEpisode();
}
return playHistory;
}
_loadMoreData() async {
if (mounted) {
setState(() {
2020-08-16 10:05:04 +02:00
_loadMore = true;
});
}
await Future.delayed(Duration(milliseconds: 500));
_top = _top + 20;
if (mounted) {
setState(() {
_getData = getPlayRecords(_top);
_loadMore = false;
2020-08-09 21:07:03 +02:00
});
}
}
2020-08-16 10:05:04 +02:00
int _top;
@override
void initState() {
super.initState();
_top = 20;
_getData = getPlayRecords(_top);
}
2020-08-09 21:07:03 +02:00
@override
Widget build(BuildContext context) {
final s = context.s;
final audio = context.watch<AudioPlayerNotifier>();
return FutureBuilder<List<PlayHistory>>(
2020-08-16 10:05:04 +02:00
future: _getData,
2020-08-09 21:07:03 +02:00
builder: (context, snapshot) {
return snapshot.hasData
? NotificationListener<ScrollNotification>(
onNotification: (scrollInfo) {
if (scrollInfo.metrics.pixels ==
scrollInfo.metrics.maxScrollExtent &&
2020-08-16 10:05:04 +02:00
snapshot.data.length == _top) {
if (!_loadMore) {
_loadMoreData();
}
}
2020-08-09 21:07:03 +02:00
return true;
},
child: ListView.builder(
scrollDirection: Axis.vertical,
2020-08-16 10:05:04 +02:00
itemCount: snapshot.data.length + 1,
2020-08-09 21:07:03 +02:00
itemBuilder: (context, index) {
2020-08-16 10:05:04 +02:00
if (index == snapshot.data.length) {
return SizedBox(
height: 2,
child: _loadMore
? LinearProgressIndicator()
: Center());
} else {
final seekValue = snapshot.data[index].seekValue;
final seconds = snapshot.data[index].seconds;
final date = snapshot
.data[index].playdate.millisecondsSinceEpoch;
final episode = snapshot.data[index].episode;
final c = episode.backgroudColor(context);
return SizedBox(
height: 90.0,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
child: Center(
child: ListTile(
contentPadding:
EdgeInsets.fromLTRB(24, 8, 20, 8),
onTap: () => audio.episodeLoad(episode),
leading: CircleAvatar(
backgroundColor: c.withOpacity(0.5),
backgroundImage: episode.avatarImage),
title: Padding(
padding:
EdgeInsets.symmetric(vertical: 5.0),
child: Text(
snapshot.data[index].title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
2020-08-13 11:58:46 +02:00
),
2020-08-16 10:05:04 +02:00
subtitle: Container(
height: 35,
child: Row(
mainAxisAlignment:
MainAxisAlignment.start,
crossAxisAlignment:
CrossAxisAlignment.center,
children: <Widget>[
if (seekValue < 0.9)
Padding(
padding:
const EdgeInsets.symmetric(
vertical: 5.0),
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: () async {
audio.episodeLoad(episode,
startPosition:
(seconds * 1000)
.toInt());
},
child: Stack(children: [
ShaderMask(
shaderCallback:
(bounds) {
return LinearGradient(
begin: Alignment
.centerLeft,
colors: <Color>[
Colors.cyan[600]
.withOpacity(
0.8),
Colors.white70
],
stops: [
seekValue,
seekValue
],
tileMode:
TileMode.mirror,
).createShader(
bounds);
},
child: Container(
height: 25,
alignment:
Alignment.center,
padding: EdgeInsets
.symmetric(
horizontal:
20),
decoration:
BoxDecoration(
borderRadius:
BorderRadius.all(
Radius.circular(
20.0)),
color: context
.accentColor,
),
child: Text(
seconds.toTime,
style: TextStyle(
color: Colors
.white),
),
2020-08-13 11:58:46 +02:00
),
),
2020-08-16 10:05:04 +02:00
]),
),
2020-08-11 19:06:56 +02:00
),
2020-08-13 11:58:46 +02:00
),
2020-08-16 10:05:04 +02:00
SizedBox(
child: Selector<
AudioPlayerNotifier,
Tuple2<List<EpisodeBrief>,
bool>>(
selector: (_, audio) => Tuple2(
audio.queue.playlist,
audio.queueUpdate),
builder: (_, data, __) {
return data.item1
.contains(episode)
? IconButton(
icon: Icon(
Icons
.playlist_add_check,
color: context
.accentColor),
onPressed: () async {
audio
.delFromPlaylist(
episode);
Fluttertoast
.showToast(
msg: s
.toastRemovePlaylist,
gravity:
ToastGravity
.BOTTOM,
);
})
: IconButton(
icon: Icon(
Icons
.playlist_add,
color: Colors
.grey[700]),
onPressed: () async {
audio.addToPlaylist(
episode);
Fluttertoast
.showToast(
msg: s
.toastAddPlaylist,
gravity:
ToastGravity
.BOTTOM,
);
});
},
),
2020-08-13 11:58:46 +02:00
),
2020-08-16 10:05:04 +02:00
Spacer(),
Text(
date.toDate(context),
style: TextStyle(
fontSize: 15,
),
2020-08-13 11:58:46 +02:00
),
2020-08-16 10:05:04 +02:00
],
),
2020-08-13 11:58:46 +02:00
),
2020-08-09 21:07:03 +02:00
),
),
),
2020-08-16 10:05:04 +02:00
Divider(height: 1)
],
),
);
}
2020-08-09 21:07:03 +02:00
}),
)
: Center(
child: SizedBox(
height: 25,
width: 25,
2020-08-16 10:05:04 +02:00
child: CircularProgressIndicator(
strokeWidth: 2,
)),
2020-08-09 21:07:03 +02:00
);
});
}
}