2020-02-09 13:29:09 +01:00
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:async';
|
2020-02-11 14:48:11 +01:00
|
|
|
|
2020-02-09 13:29:09 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
2020-04-18 06:48:02 +02:00
|
|
|
import 'package:tsacdop/class/refresh_podcast.dart';
|
|
|
|
import 'package:tsacdop/class/subscribe_podcast.dart';
|
2020-03-19 20:58:30 +01:00
|
|
|
import 'package:tsacdop/local_storage/key_value_storage.dart';
|
2020-02-09 13:29:09 +01:00
|
|
|
import 'package:xml/xml.dart' as xml;
|
|
|
|
import 'package:file_picker/file_picker.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
2020-02-20 16:44:42 +01:00
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
2020-03-14 04:14:24 +01:00
|
|
|
import 'package:line_icons/line_icons.dart';
|
2020-03-19 20:58:30 +01:00
|
|
|
import 'package:intl/intl.dart';
|
2020-02-11 14:48:11 +01:00
|
|
|
|
2020-03-01 13:17:06 +01:00
|
|
|
import 'package:tsacdop/settings/settting.dart';
|
2020-02-09 13:29:09 +01:00
|
|
|
import 'about.dart';
|
|
|
|
|
|
|
|
class OmplOutline {
|
|
|
|
final String text;
|
|
|
|
final String xmlUrl;
|
|
|
|
OmplOutline({this.text, this.xmlUrl});
|
|
|
|
|
|
|
|
factory OmplOutline.parse(xml.XmlElement element) {
|
|
|
|
if (element == null) return null;
|
|
|
|
return OmplOutline(
|
|
|
|
text: element.getAttribute("text")?.trim(),
|
|
|
|
xmlUrl: element.getAttribute("xmlUrl")?.trim(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 20:58:30 +01:00
|
|
|
class PopupMenu extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_PopupMenuState createState() => _PopupMenuState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _PopupMenuState extends State<PopupMenu> {
|
2020-04-01 11:36:45 +02:00
|
|
|
Future<String> _getRefreshDate() async {
|
2020-03-19 20:58:30 +01:00
|
|
|
int refreshDate;
|
|
|
|
KeyValueStorage refreshstorage = KeyValueStorage('refreshdate');
|
|
|
|
int i = await refreshstorage.getInt();
|
|
|
|
if (i == 0) {
|
|
|
|
KeyValueStorage refreshstorage = KeyValueStorage('refreshdate');
|
|
|
|
await refreshstorage.saveInt(DateTime.now().millisecondsSinceEpoch);
|
|
|
|
refreshDate = DateTime.now().millisecondsSinceEpoch;
|
|
|
|
} else {
|
|
|
|
refreshDate = i;
|
|
|
|
}
|
|
|
|
DateTime date = DateTime.fromMillisecondsSinceEpoch(refreshDate);
|
|
|
|
var diffrence = DateTime.now().difference(date);
|
2020-04-22 20:10:57 +02:00
|
|
|
if (diffrence.inSeconds < 60) {
|
|
|
|
return '${diffrence.inSeconds} seconds ago';
|
|
|
|
} else if (diffrence.inMinutes < 10) {
|
|
|
|
return '${diffrence.inMinutes} minutes ago';
|
2020-03-19 20:58:30 +01:00
|
|
|
} else if (diffrence.inHours < 1) {
|
2020-04-22 20:10:57 +02:00
|
|
|
return 'an hour ago';
|
|
|
|
} else if (diffrence.inHours <= 24) {
|
2020-04-01 11:36:45 +02:00
|
|
|
return '${diffrence.inHours} hours ago';
|
2020-03-19 20:58:30 +01:00
|
|
|
} else if (diffrence.inDays < 7) {
|
2020-04-22 20:10:57 +02:00
|
|
|
return '${diffrence.inDays} days ago';
|
2020-03-19 20:58:30 +01:00
|
|
|
} else {
|
2020-04-01 11:36:45 +02:00
|
|
|
return DateFormat.yMMMd()
|
2020-03-19 20:58:30 +01:00
|
|
|
.format(DateTime.fromMillisecondsSinceEpoch(refreshDate));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 13:29:09 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-04-18 06:48:02 +02:00
|
|
|
var refreshWorker = Provider.of<RefreshWorker>(context, listen: false);
|
|
|
|
var subscribeWorker = Provider.of<SubscribeWorker>(context, listen: false);
|
|
|
|
// _refreshAll() async {
|
|
|
|
// var dbHelper = DBHelper();
|
|
|
|
// List<PodcastLocal> podcastList = await dbHelper.getPodcastLocalAll();
|
|
|
|
// int i = 0;
|
|
|
|
// await Future.forEach(podcastList, (podcastLocal) async {
|
|
|
|
// importOmpl.rssTitle = podcastLocal.title;
|
|
|
|
// importOmpl.importState = ImportState.parse;
|
|
|
|
// i += await dbHelper.updatePodcastRss(podcastLocal);
|
|
|
|
// print('Refresh ' + podcastLocal.title);
|
|
|
|
// });
|
|
|
|
// KeyValueStorage refreshstorage = KeyValueStorage('refreshdate');
|
|
|
|
// await refreshstorage.saveInt(DateTime.now().millisecondsSinceEpoch);
|
|
|
|
// KeyValueStorage refreshcountstorage = KeyValueStorage('refreshcount');
|
|
|
|
// await refreshcountstorage.saveInt(i);
|
|
|
|
// importOmpl.importState = ImportState.complete;
|
|
|
|
// groupList.updateGroups();
|
|
|
|
// }
|
|
|
|
//
|
2020-02-09 13:29:09 +01:00
|
|
|
void _saveOmpl(String path) async {
|
|
|
|
File file = File(path);
|
2020-03-19 20:58:30 +01:00
|
|
|
try {
|
|
|
|
String opml = file.readAsStringSync();
|
2020-02-21 16:04:02 +01:00
|
|
|
|
2020-03-19 20:58:30 +01:00
|
|
|
var content = xml.parse(opml);
|
|
|
|
var total = content
|
|
|
|
.findAllElements('outline')
|
|
|
|
.map((ele) => OmplOutline.parse(ele))
|
|
|
|
.toList();
|
|
|
|
if (total.length == 0) {
|
|
|
|
Fluttertoast.showToast(
|
2020-04-18 06:48:02 +02:00
|
|
|
msg: 'File not valid',
|
2020-03-19 20:58:30 +01:00
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < total.length; i++) {
|
|
|
|
if (total[i].xmlUrl != null) {
|
2020-04-18 06:48:02 +02:00
|
|
|
// importOmpl.rssTitle = total[i].text;
|
|
|
|
//await saveOmpl(total[i].xmlUrl);
|
|
|
|
SubscribeItem item =
|
|
|
|
SubscribeItem(total[i].xmlUrl, total[i].text);
|
|
|
|
await subscribeWorker.setSubscribeItem(item);
|
|
|
|
await Future.delayed(Duration(milliseconds: 500));
|
2020-03-19 20:58:30 +01:00
|
|
|
print(total[i].text);
|
2020-02-22 13:25:06 +01:00
|
|
|
}
|
2020-02-11 14:01:57 +01:00
|
|
|
}
|
2020-03-19 20:58:30 +01:00
|
|
|
print('Import fisnished');
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
2020-03-19 20:58:30 +01:00
|
|
|
} catch (e) {
|
2020-03-14 04:14:24 +01:00
|
|
|
print(e);
|
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: 'File error, Subscribe failed',
|
|
|
|
gravity: ToastGravity.TOP,
|
|
|
|
);
|
2020-04-18 06:48:02 +02:00
|
|
|
//await Future.delayed(Duration(seconds: 5));
|
|
|
|
// importOmpl.importState = ImportState.stop;
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _getFilePath() async {
|
|
|
|
try {
|
2020-03-19 21:14:59 +01:00
|
|
|
String filePath = await FilePicker.getFilePath(type: FileType.any);
|
2020-02-09 13:29:09 +01:00
|
|
|
if (filePath == '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
print('File Path' + filePath);
|
2020-04-18 06:48:02 +02:00
|
|
|
//importOmpl.importState = ImportState.start;
|
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: 'Read file successfully',
|
|
|
|
gravity: ToastGravity.TOP,
|
|
|
|
);
|
2020-02-09 13:29:09 +01:00
|
|
|
_saveOmpl(filePath);
|
|
|
|
} on PlatformException catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PopupMenuButton<int>(
|
2020-03-19 20:58:30 +01:00
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.all(Radius.circular(10))),
|
2020-03-01 13:17:06 +01:00
|
|
|
elevation: 1,
|
2020-02-09 13:29:09 +01:00
|
|
|
tooltip: 'Menu',
|
|
|
|
itemBuilder: (context) => [
|
|
|
|
PopupMenuItem(
|
|
|
|
value: 1,
|
2020-03-01 13:17:06 +01:00
|
|
|
child: Container(
|
|
|
|
padding: EdgeInsets.only(left: 10),
|
|
|
|
child: Row(
|
2020-03-19 20:58:30 +01:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2020-03-01 13:17:06 +01:00
|
|
|
children: <Widget>[
|
2020-03-14 04:14:24 +01:00
|
|
|
Icon(LineIcons.cloud_download_alt_solid),
|
2020-03-19 20:58:30 +01:00
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 5.0),
|
|
|
|
),
|
|
|
|
Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: <Widget>[
|
|
|
|
Text(
|
|
|
|
'Refresh All',
|
|
|
|
),
|
2020-04-01 11:36:45 +02:00
|
|
|
FutureBuilder<String>(
|
|
|
|
future: _getRefreshDate(),
|
|
|
|
builder: (_, snapshot) {
|
|
|
|
if (snapshot.hasData)
|
|
|
|
return Text(
|
|
|
|
snapshot.data,
|
|
|
|
style: TextStyle(color: Colors.red, fontSize: 12),
|
|
|
|
);
|
|
|
|
else
|
|
|
|
return Center();
|
|
|
|
})
|
2020-03-19 20:58:30 +01:00
|
|
|
],
|
|
|
|
),
|
2020-03-01 13:17:06 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2020-02-12 04:08:28 +01:00
|
|
|
),
|
2020-03-19 20:58:30 +01:00
|
|
|
PopupMenuItem(
|
|
|
|
value: 2,
|
|
|
|
child: Container(
|
|
|
|
padding: EdgeInsets.only(left: 10),
|
|
|
|
child: Row(
|
|
|
|
children: <Widget>[
|
|
|
|
Icon(LineIcons.paperclip_solid),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 5.0),
|
|
|
|
),
|
|
|
|
Text('Import OMPL'),
|
|
|
|
],
|
2020-03-01 13:17:06 +01:00
|
|
|
),
|
|
|
|
),
|
2020-03-19 20:58:30 +01:00
|
|
|
),
|
|
|
|
|
|
|
|
// PopupMenuItem(
|
|
|
|
// value: 3,
|
|
|
|
// child: setting.theme != 2 ? Text('Night Mode') : Text('Light Mode'),
|
|
|
|
// ),
|
|
|
|
PopupMenuItem(
|
2020-03-01 13:17:06 +01:00
|
|
|
value: 4,
|
|
|
|
child: Container(
|
|
|
|
padding: EdgeInsets.only(left: 10),
|
|
|
|
child: Row(
|
|
|
|
children: <Widget>[
|
2020-03-14 04:14:24 +01:00
|
|
|
Icon(LineIcons.cog_solid),
|
2020-03-19 20:58:30 +01:00
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 5.0),
|
|
|
|
),
|
2020-03-01 13:17:06 +01:00
|
|
|
Text('Settings'),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2020-02-22 13:25:06 +01:00
|
|
|
),
|
|
|
|
PopupMenuItem(
|
2020-03-01 13:17:06 +01:00
|
|
|
value: 5,
|
|
|
|
child: Container(
|
|
|
|
padding: EdgeInsets.only(left: 10),
|
|
|
|
child: Row(
|
|
|
|
children: <Widget>[
|
2020-03-14 04:14:24 +01:00
|
|
|
Icon(LineIcons.info_circle_solid),
|
2020-03-19 20:58:30 +01:00
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 5.0),
|
|
|
|
),
|
2020-03-01 13:17:06 +01:00
|
|
|
Text('About'),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2020-02-09 13:29:09 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
onSelected: (value) {
|
2020-03-01 13:17:06 +01:00
|
|
|
if (value == 5) {
|
2020-02-09 13:29:09 +01:00
|
|
|
Navigator.push(
|
|
|
|
context, MaterialPageRoute(builder: (context) => AboutApp()));
|
2020-02-12 04:08:28 +01:00
|
|
|
} else if (value == 2) {
|
2020-02-09 13:29:09 +01:00
|
|
|
_getFilePath();
|
2020-02-12 04:08:28 +01:00
|
|
|
} else if (value == 1) {
|
2020-04-18 06:48:02 +02:00
|
|
|
//_refreshAll();
|
|
|
|
refreshWorker.start();
|
2020-02-22 13:25:06 +01:00
|
|
|
} else if (value == 3) {
|
2020-03-19 20:58:30 +01:00
|
|
|
// setting.theme != 2 ? setting.setTheme(2) : setting.setTheme(1);
|
|
|
|
} else if (value == 4) {
|
|
|
|
Navigator.push(
|
2020-03-01 13:17:06 +01:00
|
|
|
context, MaterialPageRoute(builder: (context) => Settings()));
|
2020-03-19 20:58:30 +01:00
|
|
|
}
|
2020-02-09 13:29:09 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|