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';
|
2020-03-01 13:17:06 +01:00
|
|
|
import 'package:tsacdop/class/fireside_data.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-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-20 16:44:42 +01:00
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
2020-03-14 04:14:24 +01:00
|
|
|
import 'package:line_icons/line_icons.dart';
|
2020-02-11 14:48:11 +01:00
|
|
|
|
2020-03-01 13:17:06 +01:00
|
|
|
import 'package:tsacdop/class/podcast_group.dart';
|
|
|
|
import 'package:tsacdop/settings/settting.dart';
|
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';
|
2020-02-20 10:09:21 +01:00
|
|
|
import 'package:tsacdop/local_storage/sqflite_localpodcast.dart';
|
2020-02-11 14:48:11 +01:00
|
|
|
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) {
|
2020-02-20 16:44:42 +01:00
|
|
|
ImportOmpl importOmpl = Provider.of<ImportOmpl>(context, listen: false);
|
|
|
|
GroupList groupList = Provider.of<GroupList>(context, listen: false);
|
2020-03-03 17:04:23 +01:00
|
|
|
|
2020-02-21 16:04:02 +01:00
|
|
|
_refreshAll() async {
|
2020-02-12 04:08:28 +01:00
|
|
|
var dbHelper = DBHelper();
|
2020-02-21 16:04:02 +01:00
|
|
|
List<PodcastLocal> podcastList = await dbHelper.getPodcastLocalAll();
|
2020-02-12 04:08:28 +01:00
|
|
|
await Future.forEach(podcastList, (podcastLocal) async {
|
|
|
|
importOmpl.rssTitle = podcastLocal.title;
|
|
|
|
importOmpl.importState = ImportState.parse;
|
2020-02-20 16:44:42 +01:00
|
|
|
await dbHelper.updatePodcastRss(podcastLocal);
|
2020-02-12 04:08:28 +01:00
|
|
|
print('Refresh ' + podcastLocal.title);
|
|
|
|
});
|
|
|
|
importOmpl.importState = ImportState.complete;
|
|
|
|
}
|
|
|
|
|
2020-02-11 14:01:57 +01:00
|
|
|
saveOmpl(String rss) async {
|
|
|
|
var dbHelper = DBHelper();
|
2020-02-21 16:04:02 +01:00
|
|
|
importOmpl.importState = ImportState.import;
|
2020-02-11 14:01:57 +01:00
|
|
|
|
2020-02-21 16:04:02 +01:00
|
|
|
Response response = await Dio().get(rss);
|
|
|
|
if (response.statusCode == 200) {
|
2020-02-11 14:01:57 +01:00
|
|
|
var _p = RssFeed.parse(response.data);
|
2020-03-01 13:17:06 +01:00
|
|
|
|
2020-02-11 14:01:57 +01:00
|
|
|
var dir = await getApplicationDocumentsDirectory();
|
2020-03-01 13:17:06 +01:00
|
|
|
|
|
|
|
String _realUrl = response.redirects.isEmpty ? rss : response.realUri.toString();
|
|
|
|
|
2020-02-21 16:04:02 +01:00
|
|
|
print(_realUrl);
|
|
|
|
bool _checkUrl = await dbHelper.checkPodcast(_realUrl);
|
2020-03-01 13:17:06 +01:00
|
|
|
|
2020-02-21 16:04:02 +01:00
|
|
|
if (_checkUrl) {
|
2020-03-01 13:17:06 +01:00
|
|
|
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);
|
|
|
|
String _uuid = Uuid().v4();
|
2020-02-21 16:04:02 +01:00
|
|
|
File("${dir.path}/$_uuid.png")
|
|
|
|
..writeAsBytesSync(img.encodePng(thumbnail));
|
2020-03-01 13:17:06 +01:00
|
|
|
|
2020-02-21 16:04:02 +01:00
|
|
|
String _imagePath = "${dir.path}/$_uuid.png";
|
|
|
|
String _primaryColor = await getColor(File("${dir.path}/$_uuid.png"));
|
2020-03-01 13:17:06 +01:00
|
|
|
String _author = _p.itunes.author ?? _p.author ?? '';
|
|
|
|
String _provider = _p.generator ?? '';
|
|
|
|
String _link = _p.link ?? '';
|
2020-02-21 16:04:02 +01:00
|
|
|
PodcastLocal podcastLocal = PodcastLocal(
|
|
|
|
_p.title,
|
|
|
|
_p.itunes.image.href,
|
|
|
|
_realUrl,
|
|
|
|
_primaryColor,
|
2020-02-23 14:20:07 +01:00
|
|
|
_author,
|
2020-02-21 16:04:02 +01:00
|
|
|
_uuid,
|
2020-02-23 14:20:07 +01:00
|
|
|
_imagePath,
|
|
|
|
_provider,
|
|
|
|
_link);
|
2020-02-21 16:04:02 +01:00
|
|
|
|
|
|
|
podcastLocal.description = _p.description;
|
|
|
|
|
2020-03-01 13:17:06 +01:00
|
|
|
await groupList.subscribe(podcastLocal);
|
|
|
|
|
|
|
|
if (_provider.contains('fireside'))
|
|
|
|
{
|
|
|
|
FiresideData data = FiresideData(_uuid, _link);
|
|
|
|
await data.fatchData();
|
|
|
|
}
|
2020-02-21 16:04:02 +01:00
|
|
|
|
|
|
|
importOmpl.importState = ImportState.parse;
|
|
|
|
|
|
|
|
await dbHelper.savePodcastRss(_p, _uuid);
|
|
|
|
|
|
|
|
importOmpl.importState = ImportState.complete;
|
|
|
|
} else {
|
|
|
|
importOmpl.importState = ImportState.error;
|
|
|
|
|
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: 'Podcast Subscribed Already',
|
|
|
|
gravity: ToastGravity.TOP,
|
|
|
|
);
|
|
|
|
await Future.delayed(Duration(seconds: 5));
|
|
|
|
importOmpl.importState = ImportState.stop;
|
|
|
|
}
|
2020-02-22 13:25:06 +01:00
|
|
|
} else {
|
2020-02-16 09:25:53 +01:00
|
|
|
importOmpl.importState = ImportState.error;
|
2020-02-21 16:04:02 +01:00
|
|
|
|
2020-02-22 13:25:06 +01:00
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: 'Network error, Subscribe failed',
|
|
|
|
gravity: ToastGravity.TOP,
|
|
|
|
);
|
|
|
|
await Future.delayed(Duration(seconds: 5));
|
|
|
|
importOmpl.importState = ImportState.stop;
|
2020-02-11 14:01:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 13:29:09 +01:00
|
|
|
void _saveOmpl(String path) async {
|
|
|
|
File file = File(path);
|
2020-03-14 04:14:24 +01:00
|
|
|
try{String opml = file.readAsStringSync();
|
2020-02-21 16:04:02 +01:00
|
|
|
|
|
|
|
var content = xml.parse(opml);
|
|
|
|
var total = content
|
|
|
|
.findAllElements('outline')
|
|
|
|
.map((ele) => OmplOutline.parse(ele))
|
|
|
|
.toList();
|
|
|
|
if (total.length == 0) {
|
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: 'File Not Valid',
|
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
);
|
|
|
|
} else {
|
2020-02-09 13:29:09 +01:00
|
|
|
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;
|
2020-02-22 13:25:06 +01:00
|
|
|
try {
|
|
|
|
await saveOmpl(total[i].xmlUrl);
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
2020-02-11 14:01:57 +01:00
|
|
|
print(total[i].text);
|
|
|
|
}
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
print('Import fisnished');
|
2020-03-14 04:14:24 +01:00
|
|
|
}}
|
|
|
|
catch(e){
|
|
|
|
print(e);
|
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: 'File error, Subscribe failed',
|
|
|
|
gravity: ToastGravity.TOP,
|
|
|
|
);
|
|
|
|
await Future.delayed(Duration(seconds: 5));
|
|
|
|
importOmpl.importState = ImportState.stop;
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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>(
|
2020-03-01 13:17:06 +01:00
|
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(10))),
|
|
|
|
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(
|
|
|
|
children: <Widget>[
|
2020-03-14 04:14:24 +01:00
|
|
|
Icon(LineIcons.cloud_download_alt_solid),
|
2020-03-01 13:17:06 +01:00
|
|
|
Padding(padding: EdgeInsets.symmetric(horizontal: 5.0),),
|
|
|
|
Text('Refresh All'),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2020-02-12 04:08:28 +01:00
|
|
|
),
|
2020-03-01 13:17:06 +01:00
|
|
|
PopupMenuItem(
|
|
|
|
value: 2,
|
|
|
|
child: Container(
|
|
|
|
padding: EdgeInsets.only(left: 10),
|
|
|
|
child: Row(
|
|
|
|
children: <Widget>[
|
2020-03-14 04:14:24 +01:00
|
|
|
Icon(LineIcons.paperclip_solid),
|
2020-03-01 13:17:06 +01:00
|
|
|
Padding(padding: EdgeInsets.symmetric(horizontal: 5.0),),
|
|
|
|
Text('Import OMPL'),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
// PopupMenuItem(
|
|
|
|
// value: 3,
|
|
|
|
// child: setting.theme != 2 ? Text('Night Mode') : Text('Light Mode'),
|
|
|
|
// ),
|
|
|
|
PopupMenuItem(
|
|
|
|
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-01 13:17:06 +01:00
|
|
|
Padding(padding: EdgeInsets.symmetric(horizontal: 5.0),),
|
|
|
|
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-01 13:17:06 +01:00
|
|
|
Padding(padding: EdgeInsets.symmetric(horizontal: 5.0),),
|
|
|
|
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) {
|
|
|
|
_refreshAll();
|
2020-02-22 13:25:06 +01:00
|
|
|
} else if (value == 3) {
|
2020-03-01 13:17:06 +01:00
|
|
|
// setting.theme != 2 ? setting.setTheme(2) : setting.setTheme(1);
|
|
|
|
} else if (value == 4) {
|
|
|
|
Navigator.push(
|
|
|
|
context, MaterialPageRoute(builder: (context) => Settings()));
|
|
|
|
}
|
2020-02-09 13:29:09 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|