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

269 lines
8.5 KiB
Dart
Raw Normal View History

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-05-06 18:50:32 +02:00
import 'package:tsacdop/local_storage/key_value_storage.dart';
2020-07-13 09:41:59 +02:00
import 'package:tsacdop/service/ompl_build.dart';
2020-07-13 14:04:44 +02:00
import 'package:tsacdop/webfeed/webfeed.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';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:line_icons/line_icons.dart';
import 'package:intl/intl.dart';
2020-02-11 14:48:11 +01:00
2020-05-06 18:50:32 +02:00
import '../settings/settting.dart';
import '../state/refresh_podcast.dart';
import '../state/subscribe_podcast.dart';
2020-07-02 14:58:55 +02:00
import '../util/context_extension.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(),
);
}
}
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 {
int refreshDate;
2020-07-06 11:50:20 +02:00
final s = context.s;
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);
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);
} else {
2020-04-01 11:36:45 +02:00
return DateFormat.yMMMd()
.format(DateTime.fromMillisecondsSinceEpoch(refreshDate));
}
}
2020-07-13 09:41:59 +02:00
void _saveOmpl(String path) async {
var subscribeWorker = Provider.of<SubscribeWorker>(context, listen: false);
2020-07-02 14:58:55 +02:00
final s = context.s;
2020-07-13 09:41:59 +02:00
File file = File(path);
try {
Map data = PodcastsBackup.parseOMPL(file);
2020-07-13 14:04:44 +02:00
data.forEach((title, list) async {
for (var rss in list) {
if (rss.xmlUrl != null) {
SubscribeItem item = SubscribeItem(rss.xmlUrl, rss.text);
2020-07-13 09:41:59 +02:00
await subscribeWorker.setSubscribeItem(item);
await Future.delayed(Duration(milliseconds: 500));
2020-07-13 14:04:44 +02:00
print(rss.text);
2020-02-11 14:01:57 +01:00
}
2020-02-09 13:29:09 +01:00
}
2020-07-13 09:41:59 +02:00
});
} catch (e) {
print(e);
Fluttertoast.showToast(
msg: s.toastFileError,
gravity: ToastGravity.TOP,
);
// try {
// String opml = file.readAsStringSync();
// var content = xml.XmlDocument.parse(opml);
// String title = content
// .findAllElements('head')
// .first
// .findElements('title')
// .first
// .text;
// print(title);
// if (title != 'Tsacdop Subscriptions') {
// var total = content
// .findAllElements('outline')
// .map((ele) => OmplOutline.parse(ele))
// .toList();
// if (total.length == 0) {
// Fluttertoast.showToast(
// msg: s.toastFileNotValid,
// gravity: ToastGravity.BOTTOM,
// );
// } else {
// for (int i = 0; i < total.length; i++) {
// if (total[i].xmlUrl != null) {
// // 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));
// print(total[i].text);
// }
// }
// }
// print('Import fisnished');
// }
// } catch (e) {
// print(e);
// Fluttertoast.showToast(
// msg: s.toastFileError,
// gravity: ToastGravity.TOP,
// );
//await Future.delayed(Duration(seconds: 5));
// importOmpl.importState = ImportState.stop;
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 {
String filePath = await FilePicker.getFilePath(type: FileType.any);
if (filePath == '') {
return;
2020-02-09 13:29:09 +01:00
}
2020-07-13 09:41:59 +02:00
print('File Path' + filePath);
//importOmpl.importState = ImportState.start;
Fluttertoast.showToast(
msg: s.toastReadFile,
gravity: ToastGravity.TOP,
);
_saveOmpl(filePath);
} on PlatformException catch (e) {
print(e.toString());
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>(
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(
crossAxisAlignment: CrossAxisAlignment.start,
2020-03-01 13:17:06 +01:00
children: <Widget>[
Icon(LineIcons.cloud_download_alt_solid),
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-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) {
if (snapshot.hasData)
return Text(
snapshot.data,
style: TextStyle(color: Colors.red, fontSize: 12),
);
else
return Center();
})
],
),
2020-03-01 13:17:06 +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-01 13:17:06 +01:00
),
),
),
PopupMenuItem(
2020-03-01 13:17:06 +01:00
value: 4,
child: Container(
padding: EdgeInsets.only(left: 10),
child: Row(
children: <Widget>[
Icon(LineIcons.cog_solid),
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0),
),
2020-07-02 14:58:55 +02:00
Text(s.homeToprightMenuSettings),
2020-03-01 13:17: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>[
Icon(LineIcons.info_circle_solid),
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()));
} else if (value == 2) {
2020-02-09 13:29:09 +01:00
_getFilePath();
} else if (value == 1) {
//_refreshAll();
refreshWorker.start();
} else if (value == 3) {
// 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-02-09 13:29:09 +01:00
},
);
}
}