tsacdop-podcast-app-android/lib/util/episodegrid.dart

626 lines
28 KiB
Dart
Raw Normal View History

2020-02-09 13:29:09 +01:00
import 'dart:ui';
2020-07-26 12:20:42 +02:00
import 'package:auto_animated/auto_animated.dart';
2020-02-09 13:29:09 +01:00
import 'package:flutter/material.dart';
2020-07-26 12:20:42 +02:00
import 'package:fluttertoast/fluttertoast.dart';
2020-06-30 21:14:36 +02:00
import 'package:focused_menu/focused_menu.dart';
import 'package:focused_menu/modals.dart';
2020-02-09 13:29:09 +01:00
import 'package:google_fonts/google_fonts.dart';
2020-07-26 12:20:42 +02:00
import 'package:line_icons/line_icons.dart';
import 'package:provider/provider.dart';
import 'package:tuple/tuple.dart';
2020-03-22 18:03:53 +01:00
2020-07-26 12:20:42 +02:00
import '../episodes/episode_detail.dart';
2020-08-07 18:23:28 +02:00
import '../home/audioplayer.dart';
2020-07-26 12:20:42 +02:00
import '../local_storage/key_value_storage.dart';
import '../local_storage/sqflite_localpodcast.dart';
2020-07-07 17:29:21 +02:00
import '../state/audio_state.dart';
import '../state/download_state.dart';
2020-05-06 18:50:32 +02:00
import '../type/episodebrief.dart';
import '../type/play_histroy.dart';
2020-07-24 16:10:08 +02:00
import 'custom_widget.dart';
2020-07-26 12:20:42 +02:00
import 'extension_helper.dart';
import 'open_container.dart';
2020-04-11 19:23:12 +02:00
enum Layout { three, two, one }
2020-02-09 13:29:09 +01:00
2020-06-30 21:14:36 +02:00
// ignore: must_be_immutable
2020-02-09 13:29:09 +01:00
class EpisodeGrid extends StatelessWidget {
final List<EpisodeBrief> episodes;
2020-02-09 13:29:09 +01:00
final bool showFavorite;
final bool showDownload;
final bool showNumber;
final int episodeCount;
2020-04-11 19:23:12 +02:00
final Layout layout;
final bool reverse;
2020-07-23 20:42:25 +02:00
2020-07-24 16:10:08 +02:00
/// Count of animation items.
final int initNum;
2020-07-23 20:42:25 +02:00
2020-07-24 16:10:08 +02:00
EpisodeGrid({
Key key,
@required this.episodes,
this.initNum = 12,
this.showDownload = false,
this.showFavorite = false,
this.showNumber = false,
this.episodeCount = 0,
this.layout = Layout.three,
this.reverse,
}) : super(key: key);
2020-06-05 20:33:47 +02:00
2020-04-11 19:23:12 +02:00
Future<int> _isListened(EpisodeBrief episode) async {
2020-07-26 12:20:42 +02:00
var dbHelper = DBHelper();
2020-04-11 19:23:12 +02:00
return await dbHelper.isListened(episode.enclosureUrl);
}
Future<Tuple5<int, bool, bool, bool, List<int>>> _initData(
2020-07-04 16:42:56 +02:00
EpisodeBrief episode) async {
2020-07-26 12:20:42 +02:00
var menuList = await _getEpisodeMenu();
var tapToOpen = await _getTapToOpenPopupMenu();
var listened = await _isListened(episode);
var liked = await _isLiked(episode);
var downloaded = await _isDownloaded(episode);
return Tuple5(listened, liked, downloaded, tapToOpen, menuList);
2020-06-30 21:14:36 +02:00
}
2020-04-11 19:23:12 +02:00
Future<bool> _isLiked(EpisodeBrief episode) async {
2020-07-26 12:20:42 +02:00
var dbHelper = DBHelper();
2020-04-11 19:23:12 +02:00
return await dbHelper.isLiked(episode.enclosureUrl);
}
Future<List<int>> _getEpisodeMenu() async {
2020-07-26 12:20:42 +02:00
var popupMenuStorage = KeyValueStorage(episodePopupMenuKey);
var list = await popupMenuStorage.getMenu();
return list;
}
Future<bool> _isDownloaded(EpisodeBrief episode) async {
2020-07-26 12:20:42 +02:00
var dbHelper = DBHelper();
return await dbHelper.isDownloaded(episode.enclosureUrl);
}
Future<bool> _getTapToOpenPopupMenu() async {
2020-07-26 12:20:42 +02:00
var tapToOpenPopupMenuStorage = KeyValueStorage(tapToOpenPopupMenuKey);
var boo = await tapToOpenPopupMenuStorage.getBool(defaultValue: false);
return boo;
}
2020-08-30 10:37:32 +02:00
Future<void> _markListened(EpisodeBrief episode) async {
2020-07-26 12:20:42 +02:00
var dbHelper = DBHelper();
2020-08-10 15:10:12 +02:00
final history = PlayHistory(episode.title, episode.enclosureUrl, 0, 1);
await dbHelper.saveHistory(history);
}
2020-08-30 10:37:32 +02:00
Future<void> _markNotListened(String url) async {
2020-08-10 15:10:12 +02:00
var dbHelper = DBHelper();
await dbHelper.markNotListened(url);
}
2020-08-30 10:37:32 +02:00
Future<void> _saveLiked(String url) async {
var dbHelper = DBHelper();
2020-06-11 17:13:10 +02:00
await dbHelper.setLiked(url);
}
2020-08-30 10:37:32 +02:00
Future<void> _setUnliked(String url) async {
var dbHelper = DBHelper();
2020-06-11 17:13:10 +02:00
await dbHelper.setUniked(url);
}
2020-07-23 20:42:25 +02:00
/// Episode title widget.
Widget _title(EpisodeBrief episode) => Container(
alignment:
layout == Layout.one ? Alignment.centerLeft : Alignment.topLeft,
padding: EdgeInsets.only(top: 2.0),
child: Text(
episode.title,
maxLines: layout == Layout.one ? 1 : 4,
overflow:
layout == Layout.one ? TextOverflow.ellipsis : TextOverflow.fade,
),
);
2020-06-05 20:33:47 +02:00
2020-07-23 20:42:25 +02:00
/// Circel avatar widget.
Widget _circleImage(BuildContext context,
{EpisodeBrief episode, Color color, bool boo}) =>
Container(
height: context.width / 16,
width: context.width / 16,
child: boo
? Center()
: CircleAvatar(
backgroundColor: color.withOpacity(0.5),
2020-08-15 19:43:45 +02:00
backgroundImage: episode.avatarImage),
);
2020-06-05 20:33:47 +02:00
2020-06-30 21:14:36 +02:00
Widget _downloadIndicater(BuildContext context,
{EpisodeBrief episode, bool isDownloaded}) =>
showDownload || layout != Layout.three
2020-06-30 21:14:36 +02:00
? isDownloaded
? Container(
height: 20,
width: 20,
2020-08-28 22:53:17 +02:00
alignment: Alignment.center,
2020-06-30 21:14:36 +02:00
margin: EdgeInsets.symmetric(horizontal: 5),
2020-08-30 10:37:32 +02:00
padding: EdgeInsets.fromLTRB(2, 2, 2, 3),
2020-06-30 21:14:36 +02:00
decoration: BoxDecoration(
color: context.accentColor,
shape: BoxShape.circle,
),
2020-08-28 22:53:17 +02:00
child: CustomPaint(
size: Size(12, 12),
painter: DownloadPainter(
stroke: 1.0,
color: context.accentColor,
fraction: 1,
progressColor: Colors.white,
progress: 1,
),
2020-06-30 21:14:36 +02:00
),
)
: Center()
: Center();
2020-06-05 20:33:47 +02:00
2020-07-23 20:42:25 +02:00
/// New indicator widget.
Widget _isNewIndicator(EpisodeBrief episode) => episode.isNew == 1
? Container(
padding: EdgeInsets.symmetric(horizontal: 2),
child: Text('New',
style: TextStyle(color: Colors.red, fontStyle: FontStyle.italic)),
)
: Center();
2020-07-23 20:42:25 +02:00
/// Count indicator widget.
Widget _numberIndicater(BuildContext context, {int index, Color color}) =>
showNumber
? Container(
alignment: Alignment.topRight,
child: Text(
reverse
? (index + 1).toString()
: (episodeCount - index).toString(),
style: GoogleFonts.teko(
textStyle: TextStyle(
fontSize: context.width / 24,
color: color,
),
),
),
)
: Center();
2020-06-05 20:33:47 +02:00
2020-07-23 20:42:25 +02:00
/// Pubdate widget
Widget _pubDate(BuildContext context, {EpisodeBrief episode, Color color}) =>
Text(
2020-07-23 20:42:25 +02:00
episode.pubDate.toDate(context),
2020-08-01 09:30:24 +02:00
overflow: TextOverflow.visible,
2020-09-14 17:18:13 +02:00
textAlign: TextAlign.center,
style: TextStyle(
2020-09-14 17:18:13 +02:00
height: 1,
fontSize: context.width / 35,
color: color,
fontStyle: FontStyle.italic),
);
2020-02-09 13:29:09 +01:00
@override
Widget build(BuildContext context) {
2020-07-26 12:20:42 +02:00
var _width = context.width;
2020-06-30 21:14:36 +02:00
var audio = Provider.of<AudioPlayerNotifier>(context, listen: false);
var downloader = Provider.of<DownloadState>(context, listen: false);
final options = LiveOptions(
delay: Duration.zero,
showItemInterval: Duration(milliseconds: 50),
showItemDuration: Duration(milliseconds: 50),
);
final scrollController = ScrollController();
2020-07-02 14:58:55 +02:00
final s = context.s;
return SliverPadding(
2020-04-11 19:23:12 +02:00
padding: const EdgeInsets.only(
top: 10.0, bottom: 5.0, left: 15.0, right: 15.0),
sliver: LiveSliverGrid.options(
controller: scrollController,
options: options,
itemCount: episodes.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
childAspectRatio:
layout == Layout.three ? 1 : layout == Layout.two ? 1.5 : 4,
crossAxisCount:
layout == Layout.three ? 3 : layout == Layout.two ? 2 : 1,
mainAxisSpacing: 6.0,
crossAxisSpacing: 6.0,
),
itemBuilder: (context, index, animation) {
2020-08-15 19:43:45 +02:00
final c = episodes[index].backgroudColor(context);
2020-06-30 21:14:36 +02:00
scrollController.addListener(() {});
return FadeTransition(
2020-06-05 20:33:47 +02:00
opacity: Tween<double>(begin: index < initNum ? 0 : 1, end: 1)
.animate(animation),
child: Selector<AudioPlayerNotifier,
Tuple3<EpisodeBrief, List<String>, bool>>(
selector: (_, audio) => Tuple3(
audio?.episode,
audio.queue.playlist.map((e) => e.enclosureUrl).toList(),
audio.episodeState),
2020-03-22 18:03:53 +01:00
builder: (_, data, __) => OpenContainerWrapper(
episode: episodes[index],
2020-06-30 21:14:36 +02:00
closedBuilder: (context, action, boo) => FutureBuilder<
Tuple5<int, bool, bool, bool, List<int>>>(
2020-06-30 21:14:36 +02:00
future: _initData(episodes[index]),
initialData: Tuple5(0, false, false, false, []),
2020-07-26 12:20:42 +02:00
builder: (context, snapshot) {
var isListened = snapshot.data.item1;
var isLiked = snapshot.data.item2;
var isDownloaded = snapshot.data.item3;
var tapToOpen = snapshot.data.item4;
var menuList = snapshot.data.item5;
2020-07-24 16:10:08 +02:00
return Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(5.0)),
color: isListened > 0
? context.brightness == Brightness.light
? Colors.grey[200]
: Color.fromRGBO(40, 40, 40, 1)
: context.scaffoldBackgroundColor,
boxShadow: [
BoxShadow(
color: context.brightness == Brightness.light
? context.primaryColor
: Color.fromRGBO(40, 40, 40, 1),
blurRadius: 0.5,
spreadRadius: 0.5,
),
]),
alignment: Alignment.center,
child: Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(5.0)),
border: Border.all(
color: context.brightness == Brightness.light
? context.primaryColor
: context.scaffoldBackgroundColor,
width: 1.0,
),
),
child: FocusedMenuHolder(
blurSize: 0.0,
menuItemExtent: 45,
menuBoxDecoration: BoxDecoration(
color: Colors.transparent,
borderRadius:
BorderRadius.all(Radius.circular(15.0))),
duration: Duration(milliseconds: 100),
tapMode:
tapToOpen ? TapMode.onTap : TapMode.onLongPress,
animateMenuItems: false,
blurBackgroundColor:
context.brightness == Brightness.light
? Colors.white38
: Colors.black38,
bottomOffsetHeight: 10,
menuOffset: 6,
menuItems: <FocusedMenuItem>[
FocusedMenuItem(
backgroundColor:
context.brightness == Brightness.light
? context.primaryColor
2020-07-25 12:32:05 +02:00
: context.dialogBackgroundColor,
2020-07-24 16:10:08 +02:00
title: Text(data.item1 != episodes[index]
? s.play
: s.playing),
trailingIcon: Icon(
LineIcons.play_circle_solid,
color: Theme.of(context).accentColor,
),
onPressed: () {
2020-07-26 12:20:42 +02:00
if (data.item1 != episodes[index]) {
2020-07-24 16:10:08 +02:00
audio.episodeLoad(episodes[index]);
2020-07-26 12:20:42 +02:00
}
2020-07-24 16:10:08 +02:00
}),
menuList.contains(1)
? FocusedMenuItem(
backgroundColor:
2020-06-30 21:14:36 +02:00
context.brightness == Brightness.light
? context.primaryColor
2020-07-25 12:32:05 +02:00
: context.dialogBackgroundColor,
2020-07-24 16:10:08 +02:00
title: data.item2.contains(
episodes[index].enclosureUrl)
? Text(s.remove)
: Text(s.later),
trailingIcon: Icon(
LineIcons.clock_solid,
color: Colors.cyan,
),
onPressed: () {
if (!data.item2.contains(
episodes[index].enclosureUrl)) {
audio.addToPlaylist(episodes[index]);
Fluttertoast.showToast(
msg: s.toastAddPlaylist,
gravity: ToastGravity.BOTTOM,
);
} else {
audio
.delFromPlaylist(episodes[index]);
Fluttertoast.showToast(
msg: s.toastRemovePlaylist,
gravity: ToastGravity.BOTTOM,
);
}
})
: null,
menuList.contains(2)
? FocusedMenuItem(
backgroundColor:
context.brightness == Brightness.light
? context.primaryColor
2020-07-25 12:32:05 +02:00
: context.dialogBackgroundColor,
2020-07-24 16:10:08 +02:00
title: isLiked
? Text(s.unlike)
: Text(s.like),
trailingIcon: Icon(LineIcons.heart,
color: Colors.red, size: 21),
onPressed: () async {
if (isLiked) {
await _setUnliked(
episodes[index].enclosureUrl);
audio.setEpisodeState = true;
Fluttertoast.showToast(
msg: s.unliked,
gravity: ToastGravity.BOTTOM,
);
} else {
await _saveLiked(
episodes[index].enclosureUrl);
audio.setEpisodeState = true;
Fluttertoast.showToast(
msg: s.liked,
gravity: ToastGravity.BOTTOM,
);
}
})
: null,
menuList.contains(3)
? FocusedMenuItem(
backgroundColor:
context.brightness == Brightness.light
? context.primaryColor
2020-07-25 12:32:05 +02:00
: context.dialogBackgroundColor,
2020-07-24 16:10:08 +02:00
title: isListened > 0
2020-08-10 15:10:12 +02:00
? Text(s.markNotListened,
2020-07-24 16:10:08 +02:00
style: TextStyle(
color: context.textColor
.withOpacity(0.5)))
: Text(
s.markListened,
maxLines: 1,
overflow: TextOverflow.ellipsis,
2020-07-23 20:42:25 +02:00
),
2020-07-24 16:10:08 +02:00
trailingIcon: SizedBox(
width: 23,
height: 23,
child: CustomPaint(
painter: ListenedAllPainter(
Colors.blue,
stroke: 1.5)),
),
onPressed: () async {
if (isListened < 1) {
await _markListened(episodes[index]);
audio.setEpisodeState = true;
Fluttertoast.showToast(
msg: s.markListened,
gravity: ToastGravity.BOTTOM,
);
2020-08-10 15:10:12 +02:00
} else {
await _markNotListened(
episodes[index].enclosureUrl);
audio.setEpisodeState = true;
Fluttertoast.showToast(
msg: s.markNotListened,
gravity: ToastGravity.BOTTOM,
);
2020-07-24 16:10:08 +02:00
}
})
: null,
menuList.contains(4)
? FocusedMenuItem(
backgroundColor:
context.brightness == Brightness.light
? context.primaryColor
2020-07-25 12:32:05 +02:00
: context.dialogBackgroundColor,
2020-07-24 16:10:08 +02:00
title: isDownloaded
? Text(s.downloaded,
style: TextStyle(
color: context.textColor
.withOpacity(0.5)))
: Text(s.download),
trailingIcon: Icon(
LineIcons.download_solid,
color: Colors.green),
onPressed: () {
2020-07-26 12:20:42 +02:00
if (!isDownloaded) {
2020-07-24 16:10:08 +02:00
downloader.startTask(episodes[index]);
2020-07-26 12:20:42 +02:00
}
2020-07-24 16:10:08 +02:00
})
: null
],
action: action,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
flex: layout == Layout.one ? 1 : 2,
child: Row(
2020-04-11 19:23:12 +02:00
mainAxisAlignment:
MainAxisAlignment.start,
2020-08-01 09:30:24 +02:00
crossAxisAlignment:
CrossAxisAlignment.center,
2020-04-11 19:23:12 +02:00
children: <Widget>[
2020-07-24 16:10:08 +02:00
layout != Layout.one
? _circleImage(context,
episode: episodes[index],
2020-08-15 19:43:45 +02:00
color: c,
2020-07-24 16:10:08 +02:00
boo: boo)
: _pubDate(context,
episode: episodes[index],
2020-08-15 19:43:45 +02:00
color: c),
2020-07-24 16:10:08 +02:00
Spacer(),
_isNewIndicator(episodes[index]),
_downloadIndicater(context,
episode: episodes[index],
isDownloaded: isDownloaded),
_numberIndicater(context,
2020-08-15 19:43:45 +02:00
index: index, color: c)
2020-07-24 16:10:08 +02:00
],
),
),
Expanded(
flex: layout == Layout.one ? 3 : 5,
child: layout != Layout.one
? _title(episodes[index])
: Row(
crossAxisAlignment:
CrossAxisAlignment.center,
children: [
_circleImage(context,
episode: episodes[index],
2020-08-15 19:43:45 +02:00
color: c,
2020-07-24 16:10:08 +02:00
boo: boo),
SizedBox(
width: 5,
),
Expanded(
child:
_title(episodes[index]))
],
),
2020-07-24 16:10:08 +02:00
),
Expanded(
flex: 1,
child: Row(
crossAxisAlignment:
2020-08-01 09:30:24 +02:00
CrossAxisAlignment.start,
mainAxisAlignment:
MainAxisAlignment.start,
2020-07-24 16:10:08 +02:00
children: <Widget>[
if (layout != Layout.one)
2020-08-01 09:30:24 +02:00
_pubDate(context,
episode: episodes[index],
2020-08-15 19:43:45 +02:00
color: c),
2020-07-24 16:10:08 +02:00
Spacer(),
2020-08-01 09:30:24 +02:00
if (layout != Layout.three &&
episodes[index].duration != 0)
2020-09-14 17:18:13 +02:00
Align(
2020-08-01 09:30:24 +02:00
alignment: Alignment.center,
child: Text(
episodes[index].duration.toTime,
style: TextStyle(
fontSize: _width / 35),
),
),
if (episodes[index].duration != 0 &&
episodes[index].enclosureLength !=
null &&
episodes[index].enclosureLength !=
0 &&
layout != Layout.three)
Text(
'|',
style: TextStyle(
fontSize: _width / 35,
),
),
if (layout != Layout.three &&
episodes[index].enclosureLength !=
null &&
episodes[index].enclosureLength !=
0)
2020-09-14 17:18:13 +02:00
Align(
2020-08-01 09:30:24 +02:00
alignment: Alignment.center,
child: Text(
'${(episodes[index].enclosureLength) ~/ 1000000}MB',
style: TextStyle(
fontSize: _width / 35),
),
),
2020-07-24 16:10:08 +02:00
Padding(
padding: EdgeInsets.all(1),
2020-04-11 19:23:12 +02:00
),
2020-08-01 09:30:24 +02:00
if ((showFavorite ||
layout != Layout.three) &&
isLiked)
Icon(
Icons.favorite,
color: Colors.red,
size: _width / 35,
)
2020-04-11 19:23:12 +02:00
],
),
),
2020-07-24 16:10:08 +02:00
],
2020-03-22 18:03:53 +01:00
),
2020-07-24 16:10:08 +02:00
),
),
),
);
2020-04-11 19:23:12 +02:00
}),
),
),
);
},
),
);
2020-02-09 13:29:09 +01:00
}
}
2020-03-22 18:03:53 +01:00
class OpenContainerWrapper extends StatelessWidget {
const OpenContainerWrapper({
this.closedBuilder,
this.episode,
this.playerRunning,
});
final OpenContainerBuilder closedBuilder;
final EpisodeBrief episode;
final bool playerRunning;
@override
Widget build(BuildContext context) {
2020-07-30 19:18:56 +02:00
return Selector<AudioPlayerNotifier, Tuple2<bool, PlayerHeight>>(
selector: (_, audio) => Tuple2(audio.playerRunning, audio.playerHeight),
2020-03-22 18:03:53 +01:00
builder: (_, data, __) => OpenContainer(
2020-07-30 19:18:56 +02:00
playerRunning: data.item1,
playerHeight: kMinPlayerHeight[data.item2.index],
2020-08-15 19:43:45 +02:00
flightWidget: CircleAvatar(backgroundImage: episode.avatarImage),
2020-03-22 18:03:53 +01:00
transitionDuration: Duration(milliseconds: 400),
beginColor: Theme.of(context).primaryColor,
endColor: Theme.of(context).primaryColor,
closedColor: Theme.of(context).brightness == Brightness.light
? Theme.of(context).primaryColor
: Theme.of(context).scaffoldBackgroundColor,
openColor: Theme.of(context).scaffoldBackgroundColor,
openElevation: 0,
closedElevation: 0,
openShape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0))),
closedShape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(5.0))),
transitionType: ContainerTransitionType.fadeThrough,
2020-07-26 12:20:42 +02:00
openBuilder: (context, _, boo) {
2020-03-22 18:03:53 +01:00
return EpisodeDetail(
episodeItem: episode,
hide: boo,
);
},
tappable: true,
closedBuilder: closedBuilder,
),
);
}
}