2020-02-09 13:29:09 +01:00
|
|
|
import 'dart:async';
|
2020-08-10 20:41:22 +02:00
|
|
|
import 'dart:developer' as developer;
|
2020-07-26 12:20:42 +02:00
|
|
|
import 'dart:io';
|
2020-02-11 14:48:11 +01:00
|
|
|
|
2020-02-09 13:29:09 +01:00
|
|
|
import 'package:file_picker/file_picker.dart';
|
2020-07-26 12:20:42 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2020-02-09 13:29:09 +01:00
|
|
|
import 'package:flutter/services.dart';
|
2020-02-20 16:44:42 +01:00
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
2020-03-19 20:58:30 +01:00
|
|
|
import 'package:intl/intl.dart';
|
2020-07-26 12:20:42 +02:00
|
|
|
import 'package:line_icons/line_icons.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
2020-02-11 14:48:11 +01:00
|
|
|
|
2020-07-26 12:20:42 +02:00
|
|
|
import '../local_storage/key_value_storage.dart';
|
|
|
|
import '../service/ompl_build.dart';
|
2020-05-06 18:50:32 +02:00
|
|
|
import '../settings/settting.dart';
|
2020-07-26 12:20:42 +02:00
|
|
|
import '../state/podcast_group.dart';
|
2020-05-06 18:50:32 +02:00
|
|
|
import '../state/refresh_podcast.dart';
|
2020-07-22 11:34:32 +02:00
|
|
|
import '../util/extension_helper.dart';
|
2020-02-09 13:29:09 +01:00
|
|
|
import 'about.dart';
|
|
|
|
|
2020-03-19 20:58:30 +01:00
|
|
|
class PopupMenu extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_PopupMenuState createState() => _PopupMenuState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _PopupMenuState extends State<PopupMenu> {
|
2020-07-06 11:50:20 +02:00
|
|
|
Future<String> _getRefreshDate(BuildContext context) async {
|
2020-03-19 20:58:30 +01:00
|
|
|
int refreshDate;
|
2020-07-06 11:50:20 +02:00
|
|
|
final s = context.s;
|
2020-07-26 12:20:42 +02:00
|
|
|
var refreshstorage = KeyValueStorage('refreshdate');
|
|
|
|
var i = await refreshstorage.getInt();
|
2020-03-19 20:58:30 +01:00
|
|
|
if (i == 0) {
|
2020-07-26 12:20:42 +02:00
|
|
|
var refreshstorage = KeyValueStorage('refreshdate');
|
2020-03-19 20:58:30 +01:00
|
|
|
await refreshstorage.saveInt(DateTime.now().millisecondsSinceEpoch);
|
|
|
|
refreshDate = DateTime.now().millisecondsSinceEpoch;
|
|
|
|
} else {
|
|
|
|
refreshDate = i;
|
|
|
|
}
|
2020-07-26 12:20:42 +02:00
|
|
|
var date = DateTime.fromMillisecondsSinceEpoch(refreshDate);
|
2020-07-06 11:50:20 +02:00
|
|
|
var difference = DateTime.now().difference(date);
|
|
|
|
if (difference.inSeconds < 60) {
|
|
|
|
return s.secondsAgo(difference.inSeconds);
|
|
|
|
} else if (difference.inMinutes < 60) {
|
|
|
|
return s.minsAgo(difference.inMinutes);
|
|
|
|
} else if (difference.inHours < 24) {
|
|
|
|
return s.hoursAgo(difference.inHours);
|
|
|
|
} else if (difference.inDays < 7) {
|
|
|
|
return s.daysAgo(difference.inDays);
|
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-07-13 09:41:59 +02:00
|
|
|
void _saveOmpl(String path) async {
|
2020-07-14 17:45:45 +02:00
|
|
|
var subscribeWorker = Provider.of<GroupList>(context, listen: false);
|
2020-07-26 12:20:42 +02:00
|
|
|
var rssExp = RegExp(r'^(https?):\/\/(.*)');
|
2020-07-02 14:58:55 +02:00
|
|
|
final s = context.s;
|
2020-07-26 12:20:42 +02:00
|
|
|
var file = File(path);
|
2020-07-13 09:41:59 +02:00
|
|
|
try {
|
2020-07-14 17:45:45 +02:00
|
|
|
Map<String, List<OmplOutline>> data = PodcastsBackup.parseOMPL(file);
|
|
|
|
for (var entry in data.entries) {
|
2020-07-26 12:20:42 +02:00
|
|
|
var title = entry.key;
|
2020-07-16 15:55:41 +02:00
|
|
|
var list = entry.value.reversed;
|
2020-07-13 14:04:44 +02:00
|
|
|
for (var rss in list) {
|
2020-07-26 12:20:42 +02:00
|
|
|
var rssLink = rssExp.stringMatch(rss.xmlUrl);
|
2020-07-18 10:00:52 +02:00
|
|
|
if (rssLink != null) {
|
2020-07-26 12:20:42 +02:00
|
|
|
var item = SubscribeItem(rssLink, rss.text, group: title);
|
2020-07-13 09:41:59 +02:00
|
|
|
await subscribeWorker.setSubscribeItem(item);
|
2020-07-16 15:55:41 +02:00
|
|
|
await Future.delayed(Duration(milliseconds: 200));
|
2020-02-11 14:01:57 +01:00
|
|
|
}
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
2020-07-14 17:45:45 +02:00
|
|
|
}
|
2020-07-13 09:41:59 +02:00
|
|
|
} catch (e) {
|
2020-08-10 20:41:22 +02:00
|
|
|
developer.log(e, name: 'OMPL parse error');
|
2020-07-13 09:41:59 +02:00
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: s.toastFileError,
|
|
|
|
gravity: ToastGravity.TOP,
|
|
|
|
);
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
2020-07-13 09:41:59 +02:00
|
|
|
}
|
2020-02-09 13:29:09 +01:00
|
|
|
|
2020-07-13 09:41:59 +02:00
|
|
|
void _getFilePath() async {
|
|
|
|
final s = context.s;
|
|
|
|
try {
|
2020-07-26 12:20:42 +02:00
|
|
|
var filePath = await FilePicker.getFilePath(type: FileType.any);
|
2020-07-13 09:41:59 +02:00
|
|
|
if (filePath == '') {
|
|
|
|
return;
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
2020-07-13 09:41:59 +02:00
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: s.toastReadFile,
|
|
|
|
gravity: ToastGravity.TOP,
|
|
|
|
);
|
|
|
|
_saveOmpl(filePath);
|
|
|
|
} on PlatformException catch (e) {
|
2020-08-10 20:41:22 +02:00
|
|
|
developer.log(e.toString(), name: 'Get OMPL file');
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
2020-07-13 09:41:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
var refreshWorker = Provider.of<RefreshWorker>(context, listen: false);
|
|
|
|
final s = context.s;
|
2020-02-09 13:29:09 +01:00
|
|
|
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-07-06 11:50:20 +02:00
|
|
|
tooltip: s.menu,
|
2020-02-09 13:29:09 +01:00
|
|
|
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(
|
2020-07-02 14:58:55 +02:00
|
|
|
s.homeToprightMenuRefreshAll,
|
2020-03-19 20:58:30 +01:00
|
|
|
),
|
2020-04-01 11:36:45 +02:00
|
|
|
FutureBuilder<String>(
|
2020-07-06 11:50:20 +02:00
|
|
|
future: _getRefreshDate(context),
|
2020-04-01 11:36:45 +02:00
|
|
|
builder: (_, snapshot) {
|
2020-07-26 12:20:42 +02:00
|
|
|
if (snapshot.hasData) {
|
2020-04-01 11:36:45 +02:00
|
|
|
return Text(
|
|
|
|
snapshot.data,
|
|
|
|
style: TextStyle(color: Colors.red, fontSize: 12),
|
|
|
|
);
|
2020-07-26 12:20:42 +02:00
|
|
|
} else {
|
2020-04-01 11:36:45 +02:00
|
|
|
return Center();
|
2020-07-26 12:20:42 +02:00
|
|
|
}
|
2020-04-01 11:36:45 +02:00
|
|
|
})
|
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),
|
|
|
|
),
|
2020-07-02 14:58:55 +02:00
|
|
|
Text(s.homeToprightMenuImportOMPL),
|
2020-03-19 20:58:30 +01:00
|
|
|
],
|
2020-03-01 13:17:06 +01:00
|
|
|
),
|
|
|
|
),
|
2020-03-19 20:58:30 +01:00
|
|
|
),
|
|
|
|
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-07-15 21:42:52 +02:00
|
|
|
Text(s.settings),
|
2020-03-01 13:17:06 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
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-07-02 14:58:55 +02:00
|
|
|
Text(s.homeToprightMenuAbout),
|
2020-03-01 13:17:06 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
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
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|