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

208 lines
7.0 KiB
Dart
Raw Normal View History

2020-10-28 13:10:43 +01:00
import 'dart:async';
import 'dart:developer' as developer;
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:line_icons/line_icons.dart';
import 'package:provider/provider.dart';
import '../local_storage/key_value_storage.dart';
import '../service/opml_build.dart';
import '../settings/settting.dart';
import '../state/podcast_group.dart';
import '../state/refresh_podcast.dart';
import '../util/extension_helper.dart';
import 'about.dart';
class PopupMenu extends StatefulWidget {
@override
_PopupMenuState createState() => _PopupMenuState();
}
class _PopupMenuState extends State<PopupMenu> {
Future<String> _getRefreshDate(BuildContext context) async {
2022-04-30 17:16:19 +02:00
int? refreshDate;
2020-10-28 13:10:43 +01:00
var refreshstorage = KeyValueStorage('refreshdate');
var i = await refreshstorage.getInt();
if (i == 0) {
var refreshstorage = KeyValueStorage('refreshdate');
await refreshstorage.saveInt(DateTime.now().millisecondsSinceEpoch);
refreshDate = DateTime.now().millisecondsSinceEpoch;
} else {
refreshDate = i;
}
2022-05-15 18:18:19 +02:00
return refreshDate.toDate(context);
2020-10-28 13:10:43 +01:00
}
void _saveOmpl(String path) async {
var subscribeWorker = Provider.of<GroupList>(context, listen: false);
var rssExp = RegExp(r'^(https?):\/\/(.*)');
final s = context.s;
var file = File(path);
try {
final opml = file.readAsStringSync();
Map<String, List<OmplOutline>> data = PodcastsBackup.parseOPML(opml);
for (var entry in data.entries) {
var title = entry.key;
var list = entry.value.reversed;
for (var rss in list) {
2022-04-30 17:16:19 +02:00
var rssLink = rssExp.stringMatch(rss.xmlUrl!);
2020-10-28 13:10:43 +01:00
if (rssLink != null) {
var item = SubscribeItem(rssLink, rss.text, group: title);
await subscribeWorker.setSubscribeItem(item);
await Future.delayed(Duration(milliseconds: 200));
}
}
}
} catch (e) {
2022-04-30 17:16:19 +02:00
developer.log(e.toString(), name: 'OMPL parse error');
2020-10-28 13:10:43 +01:00
Fluttertoast.showToast(
2022-04-30 17:16:19 +02:00
msg: s!.toastFileError,
2020-10-28 13:10:43 +01:00
gravity: ToastGravity.TOP,
);
}
}
void _getFilePath() async {
final s = context.s;
try {
var filePickResult =
await FilePicker.platform.pickFiles(type: FileType.any);
if (filePickResult == null) {
return;
}
Fluttertoast.showToast(
2022-04-30 17:16:19 +02:00
msg: s!.toastReadFile,
2020-10-28 13:10:43 +01:00
gravity: ToastGravity.TOP,
);
2022-04-30 17:16:19 +02:00
final filePath = filePickResult.files.first.path!;
2020-10-28 13:10:43 +01:00
_saveOmpl(filePath);
} on PlatformException catch (e) {
developer.log(e.toString(), name: 'Get OMPL file');
}
}
@override
Widget build(BuildContext context) {
var refreshWorker = Provider.of<RefreshWorker>(context, listen: false);
2022-04-30 17:16:19 +02:00
final s = context.s!;
2020-10-28 13:10:43 +01:00
return Material(
color: Colors.transparent,
borderRadius: BorderRadius.circular(100),
clipBehavior: Clip.hardEdge,
2020-11-06 15:08:26 +01:00
child: SizedBox(
height: 40,
width: 40,
child: PopupMenuButton<int>(
icon: Icon(Icons.more_vert),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
elevation: 1,
tooltip: s.menu,
itemBuilder: (context) => [
PopupMenuItem(
value: 1,
child: Container(
padding: EdgeInsets.only(left: 10),
child: Row(
2020-12-28 14:39:05 +01:00
crossAxisAlignment: CrossAxisAlignment.center,
2020-11-06 15:08:26 +01:00
children: <Widget>[
2021-02-09 18:55:51 +01:00
Icon(LineIcons.alternateRedo, size: 20),
2020-11-06 15:08:26 +01:00
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
s.homeToprightMenuRefreshAll,
),
FutureBuilder<String>(
future: _getRefreshDate(context),
builder: (_, snapshot) {
if (snapshot.hasData) {
return Text(
2022-04-30 17:16:19 +02:00
snapshot.data!,
2020-11-06 15:08:26 +01:00
style:
TextStyle(color: Colors.red, fontSize: 12),
);
} else {
return Center();
}
})
],
),
],
),
2020-10-28 13:10:43 +01:00
),
),
2020-11-06 15:08:26 +01:00
PopupMenuItem(
value: 2,
child: Container(
padding: EdgeInsets.only(left: 10),
child: Row(
children: <Widget>[
2021-02-09 18:55:51 +01:00
Icon(LineIcons.paperclip),
2020-11-06 15:08:26 +01:00
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0),
),
Text(s.homeToprightMenuImportOMPL),
],
),
2020-10-28 13:10:43 +01:00
),
),
2020-11-06 15:08:26 +01:00
PopupMenuItem(
value: 4,
child: Container(
padding: EdgeInsets.only(left: 10),
child: Row(
children: <Widget>[
2021-02-09 18:55:51 +01:00
Icon(LineIcons.cog),
2020-11-06 15:08:26 +01:00
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0),
),
Text(s.settings),
],
),
2020-10-28 13:10:43 +01:00
),
),
2020-11-06 15:08:26 +01:00
PopupMenuItem(
value: 5,
child: Container(
padding: EdgeInsets.only(left: 10),
child: Row(
children: <Widget>[
2021-02-09 18:55:51 +01:00
Icon(LineIcons.infoCircle),
2020-11-06 15:08:26 +01:00
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0),
),
Text(s.homeToprightMenuAbout),
],
),
2020-10-28 13:10:43 +01:00
),
),
2020-11-06 15:08:26 +01:00
],
onSelected: (value) {
if (value == 5) {
Navigator.push(
context, MaterialPageRoute(builder: (context) => AboutApp()));
} else if (value == 2) {
_getFilePath();
} else if (value == 1) {
refreshWorker.start([]);
} else if (value == 3) {
// setting.theme != 2 ? setting.setTheme(2) : setting.setTheme(1);
} else if (value == 4) {
Navigator.push(
context, MaterialPageRoute(builder: (context) => Settings()));
}
},
),
2020-10-28 13:10:43 +01:00
),
);
}
}