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

166 lines
5.0 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:dio/dio.dart';
import 'package:provider/provider.dart';
import 'package:xml/xml.dart' as xml;
import 'package:file_picker/file_picker.dart';
import 'package:flutter/services.dart';
2020-02-11 14:01:57 +01:00
import 'package:path_provider/path_provider.dart';
import 'package:color_thief_flutter/color_thief_flutter.dart';
import 'package:image/image.dart' as img;
2020-02-11 14:48:11 +01:00
2020-02-09 13:29:09 +01:00
import 'about.dart';
2020-02-11 14:48:11 +01:00
import 'package:tsacdop/class/podcastlocal.dart';
import 'package:tsacdop/class/sqflite_localpodcast.dart';
import 'package:tsacdop/class/importompl.dart';
import 'package:tsacdop/webfeed/webfeed.dart';
2020-02-09 13:29:09 +01:00
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 StatelessWidget {
2020-02-11 14:01:57 +01:00
Future<String> getColor(File file) async {
final imageProvider = FileImage(file);
var colorImage = await getImageFromProvider(imageProvider);
var color = await getColorFromImage(colorImage);
String primaryColor = color.toString();
return primaryColor;
2020-02-09 13:29:09 +01:00
}
@override
Widget build(BuildContext context) {
ImportOmpl importOmpl = Provider.of<ImportOmpl>(context);
2020-02-09 13:29:09 +01:00
_refreshAll() async {
var dbHelper = DBHelper();
List<PodcastLocal> podcastList = await dbHelper.getPodcastLocal();
await Future.forEach(podcastList, (podcastLocal) async {
importOmpl.rssTitle = podcastLocal.title;
importOmpl.importState = ImportState.parse;
Response response = await Dio().get(podcastLocal.rssUrl);
var _p = RssFeed.parse(response.data);
await dbHelper.savePodcastRss(_p);
print('Refresh ' + podcastLocal.title);
});
importOmpl.importState = ImportState.complete;
importOmpl.importState = ImportState.stop;
}
2020-02-11 14:01:57 +01:00
saveOmpl(String rss) async {
var dbHelper = DBHelper();
try {
importOmpl.importState = ImportState.import;
Response response = await Dio().get(rss);
var _p = RssFeed.parse(response.data);
var dir = await getApplicationDocumentsDirectory();
Response<List<int>> imageResponse = await Dio().get<List<int>>(
_p.itunes.image.href,
options: Options(responseType: ResponseType.bytes));
img.Image image = img.decodeImage(imageResponse.data);
img.Image thumbnail = img.copyResize(image, width: 300);
File("${dir.path}/${_p.title}.png")
..writeAsBytesSync(img.encodePng(thumbnail));
String _primaryColor =
await getColor(File("${dir.path}/${_p.title}.png"));
PodcastLocal podcastLocal = PodcastLocal(
_p.title, _p.itunes.image.href, rss, _primaryColor, _p.author);
podcastLocal.description = _p.description;
print('_p.description');
await dbHelper.savePodcastLocal(podcastLocal);
importOmpl.importState = ImportState.parse;
await dbHelper.savePodcastRss(_p);
2020-02-11 14:01:57 +01:00
} catch (e) {
print(e);
}
}
2020-02-09 13:29:09 +01:00
void _saveOmpl(String path) async {
File file = File(path);
String opml = file.readAsStringSync();
try {
var content = xml.parse(opml);
var total = content
.findAllElements('outline')
.map((ele) => OmplOutline.parse(ele))
.toList();
for (int i = 0; i < total.length; i++) {
2020-02-11 14:01:57 +01:00
if (total[i].xmlUrl != null) {
importOmpl.rssTitle = total[i].text;
await saveOmpl(total[i].xmlUrl);
print(total[i].text);
}
2020-02-09 13:29:09 +01:00
}
importOmpl.importState = ImportState.complete;
2020-02-11 14:01:57 +01:00
importOmpl.importState = ImportState.stop;
2020-02-09 13:29:09 +01:00
print('Import fisnished');
} catch (e) {
print(e);
importOmpl.importState = ImportState.error;
}
}
void _getFilePath() async {
try {
String filePath = await FilePicker.getFilePath(type: FileType.ANY);
if (filePath == '') {
return;
}
print('File Path' + filePath);
importOmpl.importState = ImportState.start;
_saveOmpl(filePath);
} on PlatformException catch (e) {
print(e.toString());
}
}
return PopupMenuButton<int>(
elevation: 3,
2020-02-09 13:29:09 +01:00
tooltip: 'Menu',
itemBuilder: (context) => [
PopupMenuItem(
value: 1,
child: Text('Refresh All'),
2020-02-09 13:29:09 +01:00
),
PopupMenuItem(
value: 2,
child: Text('Impoer OMPL'),
),
PopupMenuItem(
value: 3,
2020-02-09 13:29:09 +01:00
child: Text('About'),
),
],
onSelected: (value) {
if (value == 3) {
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();
2020-02-09 13:29:09 +01:00
}
},
);
}
}