Add history list .

This commit is contained in:
stonegate 2020-08-10 03:07:03 +08:00
parent ff719ab108
commit ac58b4cb18
2 changed files with 393 additions and 192 deletions

View File

@ -8,8 +8,10 @@ import 'package:line_icons/line_icons.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:tuple/tuple.dart'; import 'package:tuple/tuple.dart';
import '../local_storage/sqflite_localpodcast.dart';
import '../state/audio_state.dart'; import '../state/audio_state.dart';
import '../type/episodebrief.dart'; import '../type/episodebrief.dart';
import '../type/play_histroy.dart';
import '../type/playlist.dart'; import '../type/playlist.dart';
import '../util/custom_widget.dart'; import '../util/custom_widget.dart';
import '../util/extension_helper.dart'; import '../util/extension_helper.dart';
@ -34,29 +36,16 @@ class _PlaylistPageState extends State<PlaylistPage> {
} }
} }
ScrollController _controller; Future<double> _getListenTime() async {
_scrollListener() { var dbHelper = DBHelper();
var value = _controller.offset; var listenTime = await dbHelper.listenMins(0);
setState(() => _topHeight = (100 - value) > 60 ? 100 - value : 60); return listenTime;
} }
double _topHeight; bool _loadHistory = false;
List<EpisodeBrief> episodes = []; 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final s = context.s; final s = context.s;
@ -71,7 +60,6 @@ class _PlaylistPageState extends State<PlaylistPage> {
child: Scaffold( child: Scaffold(
backgroundColor: Theme.of(context).primaryColor, backgroundColor: Theme.of(context).primaryColor,
appBar: AppBar( appBar: AppBar(
title: _topHeight == 60 ? Text(s.homeMenuPlaylist) : Center(),
elevation: 0, elevation: 0,
backgroundColor: context.primaryColor, backgroundColor: context.primaryColor,
), ),
@ -86,192 +74,241 @@ class _PlaylistPageState extends State<PlaylistPage> {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[ children: <Widget>[
Container( Row(
height: _topHeight, children: <Widget>[
child: Row( Expanded(
children: <Widget>[ flex: 2,
Expanded( child: Container(
flex: 2, height: 100,
child: Container( padding: EdgeInsets.only(
height: _topHeight, left: 60,
padding: EdgeInsets.only( ),
left: 60, alignment: Alignment.centerLeft,
), child: Column(
alignment: Alignment.centerLeft, crossAxisAlignment: CrossAxisAlignment.start,
child: RichText( children: [
text: TextSpan( Row(
text: _topHeight > 90 children: [
? '${s.homeMenuPlaylist}\n' Text(
: '', _loadHistory
style: TextStyle( ? s.settingsHistory
color: context.textColor, : s.homeMenuPlaylist,
fontSize: 30, style: TextStyle(
), color: context.textColor,
children: <TextSpan>[ fontSize: 30,
TextSpan(
text: episodes.length.toString(),
style: GoogleFonts.cairo(
textStyle: TextStyle(
color: Theme.of(context).accentColor,
fontSize: 25,
),
), ),
), ),
TextSpan( SizedBox(width: 5),
text: episodes.length < 2 IconButton(
? 'episode' icon: _loadHistory
: 'episodes', ? Icon(Icons.playlist_play)
style: TextStyle( : Icon(Icons.history),
color: context.accentColor, onPressed: () => setState(() {
fontSize: 15, _loadHistory = !_loadHistory;
)), }))
TextSpan(
text:
_sumPlaylistLength(episodes).toString(),
style: GoogleFonts.cairo(
textStyle: TextStyle(
color: context.accentColor,
fontSize: 25,
)),
),
TextSpan(
text: 'mins',
style: TextStyle(
color: context.accentColor,
fontSize: 15,
)),
], ],
), ),
), _loadHistory
), ? FutureBuilder<double>(
), future: _getListenTime(),
Expanded( initialData: 0.0,
flex: 1, builder: (context, snapshot) => RichText(
child: Container( text: TextSpan(
padding: EdgeInsets.all(5.0), text: 'Today ',
margin: EdgeInsets.only(right: 20.0, bottom: 5.0), style: GoogleFonts.cairo(
decoration: data.item2 textStyle: TextStyle(
? BoxDecoration( color:
color: context.brightness == Brightness.dark Theme.of(context).accentColor,
? Colors.grey[800] fontSize: 20,
: 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}")),
),
Padding(
padding: EdgeInsets.symmetric(
horizontal: 15),
child: SizedBox(
width: 20,
height: 15,
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( children: <TextSpan>[
padding: EdgeInsets.symmetric( TextSpan(
horizontal: 15), text:
child: SizedBox( '${snapshot.data.toStringAsFixed(0)} ',
width: 20, style: GoogleFonts.cairo(
height: 15, textStyle: TextStyle(
child: WaveLoader( color: context.accentColor,
fontSize: 25,
)),
),
TextSpan(
text: 'mins',
style: TextStyle(
color: context.accentColor, color: context.accentColor,
fontSize: 15,
)), )),
],
),
),
)
: RichText(
text: TextSpan(
text: episodes.length.toString(),
style: GoogleFonts.cairo(
textStyle: TextStyle(
color:
Theme.of(context).accentColor,
fontSize: 25,
), ),
),
children: <TextSpan>[
TextSpan(
text: episodes.length < 2
? 'episode'
: 'episodes',
style: TextStyle(
color: context.accentColor,
fontSize: 15,
)),
TextSpan(
text: _sumPlaylistLength(episodes)
.toString(),
style: GoogleFonts.cairo(
textStyle: TextStyle(
color: context.accentColor,
fontSize: 25,
)),
),
TextSpan(
text: 'mins',
style: TextStyle(
color: context.accentColor,
fontSize: 15,
)),
], ],
) ),
: IconButton( ),
padding: EdgeInsets.all(0), ],
alignment: Alignment.center,
icon: Icon(Icons.play_circle_filled,
size: 40, color: (context).accentColor),
onPressed: () {
audio.playlistLoad();
// setState(() {});
}),
), ),
), ),
], ),
), Expanded(
flex: 1,
child: Container(
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(
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}")),
// ),
// Padding(
// padding: EdgeInsets.symmetric(
// horizontal: 15),
// child: SizedBox(
// width: 20,
// height: 15,
// 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,
child: WaveLoader(
color: context.accentColor,
)),
),
],
)
: IconButton(
alignment: Alignment.center,
icon: Icon(Icons.play_circle_filled,
size: 40, color: (context).accentColor),
onPressed: () {
audio.playlistLoad();
// setState(() {});
}),
),
),
],
), ),
Divider( Divider(
height: 3, height: 3,
), ),
Expanded( Expanded(
child: ReorderableListView( child: _loadHistory
scrollController: _controller, ? HistoryList()
onReorder: (oldIndex, newIndex) { : ReorderableListView(
if (newIndex > oldIndex) { // scrollController: _playlistController,
newIndex -= 1; onReorder: (oldIndex, newIndex) {
} if (newIndex > oldIndex) {
audio.reorderPlaylist(oldIndex, newIndex); newIndex -= 1;
// final episodeRemove = episodes.removeAt(oldIndex); }
// print(episodeRemove.title); audio.reorderPlaylist(oldIndex, newIndex);
setState(() { // final episodeRemove = episodes.removeAt(oldIndex);
// episodes.insert(newIndex, episodeRemove); // print(episodeRemove.title);
}); setState(() {
}, // episodes.insert(newIndex, episodeRemove);
scrollDirection: Axis.vertical, });
children: data.item2 },
? episodes.map<Widget>((episode) { scrollDirection: Axis.vertical,
if (episode.enclosureUrl != children: data.item2
episodes.first.enclosureUrl) { ? episodes.map<Widget>((episode) {
return DismissibleContainer( if (episode.enclosureUrl !=
episode: episode, episodes.first.enclosureUrl) {
key: ValueKey(episode.enclosureUrl), return DismissibleContainer(
); episode: episode,
} else { key: ValueKey(episode.enclosureUrl),
return Container( );
key: ValueKey('sd'), } else {
); return Container(
} key: ValueKey('sd'),
}).toList() );
: episodes }
.map<Widget>((episode) => DismissibleContainer( }).toList()
episode: episode, : episodes
key: ValueKey(episode.enclosureUrl), .map<Widget>((episode) =>
)) DismissibleContainer(
.toList()), episode: episode,
key: ValueKey(episode.enclosureUrl),
))
.toList()),
), ),
], ],
); );
@ -315,7 +352,7 @@ class _DismissibleContainerState extends State<DismissibleContainer> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
var audio = Provider.of<AudioPlayerNotifier>(context, listen: false); var audio = Provider.of<AudioPlayerNotifier>(context, listen: false);
final s = context.s; final s = context.s;
var _c = (Theme.of(context).brightness == Brightness.light) var c = (Theme.of(context).brightness == Brightness.light)
? widget.episode.primaryColor.colorizedark() ? widget.episode.primaryColor.colorizedark()
: widget.episode.primaryColor.colorizeLight(); : widget.episode.primaryColor.colorizeLight();
return AnimatedContainer( return AnimatedContainer(
@ -399,11 +436,11 @@ class _DismissibleContainerState extends State<DismissibleContainer> {
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
Icon(Icons.unfold_more, color: _c), Icon(Icons.unfold_more, color: c),
CircleAvatar( CircleAvatar(
//backgroundColor: _c.withOpacity(0.5), //backgroundColor: _c.withOpacity(0.5),
backgroundImage: backgroundImage:
FileImage(File("${widget.episode.imagePath}")), FileImage(File(widget.episode.imagePath)),
), ),
], ],
), ),
@ -447,3 +484,151 @@ class _DismissibleContainerState extends State<DismissibleContainer> {
); );
} }
} }
class HistoryList extends StatefulWidget {
HistoryList({Key key}) : super(key: key);
@override
_HistoryListState createState() => _HistoryListState();
}
class _HistoryListState extends State<HistoryList> {
var dbHelper = DBHelper();
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(() {
_top = _top + 100;
});
}
}
int _top = 100;
@override
Widget build(BuildContext context) {
final s = context.s;
final audio = context.watch<AudioPlayerNotifier>();
return FutureBuilder<List<PlayHistory>>(
future: getPlayRecords(_top),
builder: (context, snapshot) {
return snapshot.hasData
? NotificationListener<ScrollNotification>(
onNotification: (scrollInfo) {
if (scrollInfo.metrics.pixels ==
scrollInfo.metrics.maxScrollExtent &&
snapshot.data.length == _top) _loadMoreData();
return true;
},
child: ListView.builder(
scrollDirection: Axis.vertical,
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
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;
var c =
(Theme.of(context).brightness == Brightness.light)
? episode.primaryColor.colorizedark()
: episode.primaryColor.colorizeLight();
return ListTile(
contentPadding:
EdgeInsets.only(left: 40, right: 20, top: 10),
onTap: () => audio.episodeLoad(episode),
leading: CircleAvatar(
backgroundColor: c.withOpacity(0.5),
backgroundImage: FileImage(File(episode.imagePath)),
),
title: Text(
snapshot.data[index].title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
subtitle: Row(
children: <Widget>[
Text(
date.toDate(context),
style: TextStyle(
fontSize: 15,
),
),
SizedBox(width: 5),
if (seekValue < 0.9)
Material(
color: Colors.transparent,
child: InkWell(
onTap: () async {
audio.episodeLoad(episode,
startPosition:
(seconds * 1000).toInt());
},
child: Container(
height: 20,
alignment: Alignment.center,
padding:
EdgeInsets.symmetric(horizontal: 5),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(10.0)),
color: context.accentColor,
),
child: Text(
seconds.toTime,
style: TextStyle(color: Colors.white),
),
),
),
),
SizedBox(width: 5),
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,
);
});
},
),
],
),
);
}),
)
: Center(
child: SizedBox(
height: 25,
width: 25,
child: CircularProgressIndicator()),
);
});
}
}

View File

@ -274,6 +274,22 @@ class DBHelper {
return playHistory; return playHistory;
} }
/// History list in playlist page, not include marked episdoes.
Future<List<PlayHistory>> getPlayRecords(int top) async {
var dbClient = await database;
List<Map> list = await dbClient.rawQuery(
"""SELECT title, enclosure_url, seconds, seek_value, add_date FROM PlayHistory
WHERE seconds != 0 ORDER BY add_date DESC LIMIT ?
""", [top]);
var playHistory = <PlayHistory>[];
for (var record in list) {
playHistory.add(PlayHistory(record['title'], record['enclosure_url'],
(record['seconds']).toInt(), record['seek_value'],
playdate: DateTime.fromMillisecondsSinceEpoch(record['add_date'])));
}
return playHistory;
}
Future<int> isListened(String url) async { Future<int> isListened(String url) async {
var dbClient = await database; var dbClient = await database;
var i = 0; var i = 0;