2020-02-11 14:01:57 +01:00
|
|
|
import 'dart:io';
|
2020-02-11 14:48:11 +01:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:async';
|
2020-04-18 06:48:02 +02:00
|
|
|
import 'dart:math' as math;
|
2020-02-11 14:01:57 +01:00
|
|
|
|
2020-02-09 13:29:09 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:color_thief_flutter/color_thief_flutter.dart';
|
|
|
|
import 'package:dio/dio.dart';
|
2020-02-21 16:04:02 +01:00
|
|
|
import 'package:flutter/services.dart';
|
2020-02-09 13:29:09 +01:00
|
|
|
import 'package:provider/provider.dart';
|
2020-02-12 17:10:03 +01:00
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
2020-04-22 20:10:57 +02:00
|
|
|
|
2020-05-06 18:50:32 +02:00
|
|
|
import '../type/searchpodcast.dart';
|
|
|
|
import '../state/subscribe_podcast.dart';
|
|
|
|
import '../util/context_extension.dart';
|
|
|
|
import '../webfeed/webfeed.dart';
|
|
|
|
import '../.env.dart';
|
2020-02-09 13:29:09 +01:00
|
|
|
|
2020-04-29 19:32:45 +02:00
|
|
|
class MyHomePageDelegate extends SearchDelegate<int> {
|
|
|
|
final String searchFieldLabel;
|
|
|
|
MyHomePageDelegate({this.searchFieldLabel})
|
|
|
|
: super(searchFieldLabel: searchFieldLabel);
|
2020-02-09 13:29:09 +01:00
|
|
|
static Future<List> getList(String searchText) async {
|
2020-03-19 20:58:30 +01:00
|
|
|
String apiKey = environment['apiKey'];
|
2020-02-09 13:29:09 +01:00
|
|
|
String url =
|
2020-04-18 21:46:10 +02:00
|
|
|
"https://listennotes.p.rapidapi.com/api/v1/search?only_in=title%2Cdescription&q=" +
|
2020-02-09 13:29:09 +01:00
|
|
|
searchText +
|
|
|
|
"&sort_by_date=0&type=podcast";
|
|
|
|
Response response = await Dio().get(url,
|
|
|
|
options: Options(headers: {
|
2020-03-19 20:58:30 +01:00
|
|
|
'X-Mashape-Key': "$apiKey",
|
2020-02-09 13:29:09 +01:00
|
|
|
'Accept': "application/json"
|
|
|
|
}));
|
|
|
|
Map searchResultMap = jsonDecode(response.toString());
|
|
|
|
var searchResult = SearchPodcast.fromJson(searchResultMap);
|
|
|
|
return searchResult.results;
|
|
|
|
}
|
2020-02-23 14:20:07 +01:00
|
|
|
|
2020-04-18 06:48:02 +02:00
|
|
|
static Future getRss(String url) async {
|
|
|
|
try {
|
|
|
|
BaseOptions options = new BaseOptions(
|
|
|
|
connectTimeout: 10000,
|
|
|
|
receiveTimeout: 10000,
|
|
|
|
);
|
|
|
|
Response response = await Dio(options).get(url);
|
|
|
|
var p = RssFeed.parse(response.data);
|
|
|
|
return OnlinePodcast(
|
|
|
|
rss: url,
|
|
|
|
title: p.title,
|
|
|
|
publisher: p.author,
|
|
|
|
description: p.description,
|
|
|
|
image: p.itunes.image.href);
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RegExp rssExp = RegExp(r'^(https?):\/\/(.*)');
|
|
|
|
Widget invalidRss() => Container(
|
|
|
|
height: 50,
|
|
|
|
alignment: Alignment.center,
|
|
|
|
child: Text('Invalid rss link'),
|
|
|
|
);
|
|
|
|
|
2020-02-22 13:25:06 +01:00
|
|
|
@override
|
2020-02-23 14:20:07 +01:00
|
|
|
ThemeData appBarTheme(BuildContext context) => Theme.of(context);
|
2020-02-09 13:29:09 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget buildLeading(BuildContext context) {
|
|
|
|
return IconButton(
|
|
|
|
tooltip: 'Back',
|
|
|
|
icon: AnimatedIcon(
|
|
|
|
icon: AnimatedIcons.menu_arrow,
|
|
|
|
progress: transitionAnimation,
|
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
close(context, 1);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget buildSuggestions(BuildContext context) {
|
|
|
|
if (query.isEmpty)
|
|
|
|
return Center(
|
|
|
|
child: Container(
|
|
|
|
padding: EdgeInsets.only(top: 400),
|
|
|
|
child: Image(
|
2020-03-03 17:04:23 +01:00
|
|
|
image: Theme.of(context).brightness == Brightness.light
|
2020-03-19 20:58:30 +01:00
|
|
|
? AssetImage('assets/listennotes.png')
|
|
|
|
: AssetImage('assets/listennotes_light.png'),
|
2020-02-12 14:54:46 +01:00
|
|
|
height: 20,
|
2020-02-09 13:29:09 +01:00
|
|
|
),
|
|
|
|
));
|
2020-04-18 06:48:02 +02:00
|
|
|
else if (rssExp.stringMatch(query) != null)
|
|
|
|
return FutureBuilder(
|
|
|
|
future: getRss(rssExp.stringMatch(query)),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasError)
|
|
|
|
return invalidRss();
|
|
|
|
else if (snapshot.hasData)
|
2020-02-09 13:29:09 +01:00
|
|
|
return SearchResult(
|
2020-04-18 06:48:02 +02:00
|
|
|
onlinePodcast: snapshot.data,
|
2020-02-09 13:29:09 +01:00
|
|
|
);
|
2020-04-18 06:48:02 +02:00
|
|
|
else
|
|
|
|
return Container(
|
|
|
|
padding: EdgeInsets.only(top: 200),
|
|
|
|
alignment: Alignment.topCenter,
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
else
|
|
|
|
return FutureBuilder(
|
|
|
|
future: getList(query),
|
|
|
|
builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
|
|
|
|
if (!snapshot.hasData && query != null)
|
|
|
|
return Container(
|
|
|
|
padding: EdgeInsets.only(top: 200),
|
|
|
|
alignment: Alignment.topCenter,
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
);
|
|
|
|
List content = snapshot.data;
|
|
|
|
return ListView.builder(
|
|
|
|
scrollDirection: Axis.vertical,
|
|
|
|
itemCount: content.length,
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
return SearchResult(
|
|
|
|
onlinePodcast: content[index],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
List<Widget> buildActions(BuildContext context) {
|
|
|
|
return <Widget>[
|
|
|
|
if (query.isEmpty)
|
2020-02-11 14:01:57 +01:00
|
|
|
Center()
|
2020-02-09 13:29:09 +01:00
|
|
|
else
|
|
|
|
IconButton(
|
|
|
|
tooltip: 'Clear',
|
|
|
|
icon: const Icon(Icons.clear),
|
|
|
|
onPressed: () {
|
|
|
|
query = '';
|
|
|
|
},
|
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget buildResults(BuildContext context) {
|
|
|
|
if (query.isEmpty)
|
|
|
|
return Container(
|
|
|
|
height: 10,
|
|
|
|
width: 10,
|
|
|
|
margin: EdgeInsets.only(top: 400),
|
2020-02-11 14:01:57 +01:00
|
|
|
child: SizedBox(
|
|
|
|
height: 10,
|
2020-02-11 14:48:11 +01:00
|
|
|
child: Image.asset(
|
2020-02-11 14:01:57 +01:00
|
|
|
'assets/listennote.png',
|
|
|
|
fit: BoxFit.fill,
|
|
|
|
),
|
2020-02-09 13:29:09 +01:00
|
|
|
),
|
|
|
|
);
|
2020-04-18 06:48:02 +02:00
|
|
|
else if (rssExp.stringMatch(query) != null)
|
|
|
|
return FutureBuilder(
|
|
|
|
future: getRss(rssExp.stringMatch(query)),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasError)
|
|
|
|
return invalidRss();
|
|
|
|
else if (snapshot.hasData)
|
2020-02-09 13:29:09 +01:00
|
|
|
return SearchResult(
|
2020-04-18 06:48:02 +02:00
|
|
|
onlinePodcast: snapshot.data,
|
2020-02-09 13:29:09 +01:00
|
|
|
);
|
2020-04-18 06:48:02 +02:00
|
|
|
else
|
|
|
|
return Container(
|
|
|
|
padding: EdgeInsets.only(top: 200),
|
|
|
|
alignment: Alignment.topCenter,
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
else
|
|
|
|
return FutureBuilder(
|
|
|
|
future: getList(query),
|
|
|
|
builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
|
|
|
|
if (!snapshot.hasData && query != null)
|
|
|
|
return Container(
|
|
|
|
padding: EdgeInsets.only(top: 200),
|
|
|
|
alignment: Alignment.topCenter,
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
);
|
|
|
|
List content = snapshot.data;
|
|
|
|
return ListView.builder(
|
|
|
|
scrollDirection: Axis.vertical,
|
|
|
|
itemCount: content.length,
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
return SearchResult(
|
|
|
|
onlinePodcast: content[index],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SearchResult extends StatefulWidget {
|
|
|
|
final OnlinePodcast onlinePodcast;
|
|
|
|
SearchResult({this.onlinePodcast, Key key}) : super(key: key);
|
|
|
|
@override
|
|
|
|
_SearchResultState createState() => _SearchResultState();
|
|
|
|
}
|
|
|
|
|
2020-04-18 06:48:02 +02:00
|
|
|
class _SearchResultState extends State<SearchResult>
|
|
|
|
with SingleTickerProviderStateMixin {
|
2020-02-09 13:29:09 +01:00
|
|
|
bool _issubscribe;
|
2020-02-20 16:44:42 +01:00
|
|
|
bool _showDes;
|
2020-04-18 06:48:02 +02:00
|
|
|
AnimationController _controller;
|
|
|
|
Animation _animation;
|
|
|
|
double _value;
|
2020-02-09 13:29:09 +01:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_issubscribe = false;
|
2020-02-20 16:44:42 +01:00
|
|
|
_showDes = false;
|
2020-04-18 06:48:02 +02:00
|
|
|
_value = 0;
|
|
|
|
_controller =
|
|
|
|
AnimationController(vsync: this, duration: Duration(milliseconds: 300));
|
|
|
|
_animation = Tween<double>(begin: 0.0, end: 1.0).animate(_controller)
|
|
|
|
..addListener(() {
|
|
|
|
setState(() {
|
|
|
|
_value = _animation.value;
|
|
|
|
});
|
|
|
|
});
|
2020-02-09 13:29:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2020-04-18 06:48:02 +02:00
|
|
|
_controller.dispose();
|
2020-02-09 13:29:09 +01:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
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-04-18 06:48:02 +02:00
|
|
|
// var importOmpl = Provider.of<ImportOmpl>(context, listen: false);
|
|
|
|
// var groupList = Provider.of<GroupList>(context, listen: false);
|
|
|
|
var subscribeWorker = Provider.of<SubscribeWorker>(context, listen: false);
|
|
|
|
|
|
|
|
savePodcast(OnlinePodcast podcast) async {
|
2020-05-06 14:08:41 +02:00
|
|
|
SubscribeItem item = SubscribeItem(podcast.rss, podcast.title, imgUrl: podcast.image);
|
2020-04-18 06:48:02 +02:00
|
|
|
subscribeWorker.setSubscribeItem(item);
|
2020-02-11 14:48:11 +01:00
|
|
|
}
|
2020-02-11 14:01:57 +01:00
|
|
|
|
2020-02-09 13:29:09 +01:00
|
|
|
return Container(
|
2020-02-25 10:57:12 +01:00
|
|
|
decoration: BoxDecoration(
|
|
|
|
border: Border(
|
|
|
|
bottom: Divider.createBorderSide(context),
|
|
|
|
),
|
|
|
|
),
|
2020-02-20 16:44:42 +01:00
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
2020-02-21 16:04:02 +01:00
|
|
|
children: <Widget>[
|
2020-02-20 16:44:42 +01:00
|
|
|
ListTile(
|
2020-02-21 16:04:02 +01:00
|
|
|
contentPadding: EdgeInsets.symmetric(horizontal: 20.0),
|
|
|
|
onTap: () {
|
|
|
|
setState(() {
|
|
|
|
_showDes = !_showDes;
|
2020-04-18 06:48:02 +02:00
|
|
|
if (_value == 0)
|
|
|
|
_controller.forward();
|
|
|
|
else
|
|
|
|
_controller.reverse();
|
2020-02-21 16:04:02 +01:00
|
|
|
});
|
|
|
|
},
|
2020-02-20 16:44:42 +01:00
|
|
|
leading: ClipRRect(
|
|
|
|
borderRadius: BorderRadius.all(Radius.circular(20.0)),
|
|
|
|
child: Image.network(
|
|
|
|
widget.onlinePodcast.image,
|
|
|
|
height: 40.0,
|
|
|
|
width: 40.0,
|
|
|
|
fit: BoxFit.fitWidth,
|
|
|
|
alignment: Alignment.center,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
title: Text(widget.onlinePodcast.title),
|
2020-04-18 06:48:02 +02:00
|
|
|
subtitle: Text(widget.onlinePodcast.publisher ?? ''),
|
2020-02-21 16:04:02 +01:00
|
|
|
trailing: Row(
|
2020-02-20 16:44:42 +01:00
|
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
2020-04-18 06:48:02 +02:00
|
|
|
Transform.rotate(
|
|
|
|
angle: math.pi * _value,
|
|
|
|
child: Icon(Icons.keyboard_arrow_down),
|
|
|
|
),
|
2020-02-21 16:04:02 +01:00
|
|
|
Padding(padding: EdgeInsets.only(right: 10.0)),
|
2020-03-19 20:58:30 +01:00
|
|
|
Container(
|
|
|
|
width: 100,
|
|
|
|
height: 35,
|
|
|
|
child: !_issubscribe
|
2020-04-18 06:48:02 +02:00
|
|
|
? OutlineButton(
|
|
|
|
highlightedBorderColor: Theme.of(context).accentColor,
|
|
|
|
splashColor:
|
|
|
|
Theme.of(context).accentColor.withOpacity(0.8),
|
|
|
|
child: Text('Subscribe',
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).accentColor)),
|
|
|
|
onPressed: () {
|
|
|
|
savePodcast(widget.onlinePodcast);
|
|
|
|
setState(() => _issubscribe = true);
|
2020-04-22 20:10:57 +02:00
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: 'Podcast subscribed',
|
2020-04-25 15:50:27 +02:00
|
|
|
gravity: ToastGravity.BOTTOM,
|
2020-04-22 20:10:57 +02:00
|
|
|
);
|
2020-04-18 06:48:02 +02:00
|
|
|
})
|
2020-03-19 20:58:30 +01:00
|
|
|
: OutlineButton(
|
2020-04-18 06:48:02 +02:00
|
|
|
color: context.accentColor.withOpacity(0.8),
|
2020-03-19 20:58:30 +01:00
|
|
|
highlightedBorderColor: Colors.grey[500],
|
|
|
|
disabledTextColor: Colors.grey[500],
|
|
|
|
child: Text('Subscribe'),
|
|
|
|
disabledBorderColor: Colors.grey[500],
|
|
|
|
onPressed: () {}),
|
|
|
|
),
|
2020-02-20 16:44:42 +01:00
|
|
|
],
|
|
|
|
),
|
2020-02-09 13:29:09 +01:00
|
|
|
),
|
2020-02-21 16:04:02 +01:00
|
|
|
_showDes
|
|
|
|
? Container(
|
|
|
|
alignment: Alignment.centerLeft,
|
|
|
|
decoration: BoxDecoration(
|
2020-02-25 10:57:12 +01:00
|
|
|
color: Theme.of(context).accentColor,
|
2020-02-21 16:04:02 +01:00
|
|
|
borderRadius: BorderRadius.only(
|
|
|
|
topRight: Radius.circular(15.0),
|
|
|
|
bottomLeft: Radius.circular(15.0),
|
|
|
|
bottomRight: Radius.circular(15.0),
|
|
|
|
)),
|
2020-02-25 10:57:12 +01:00
|
|
|
margin: EdgeInsets.only(left: 70, right: 50, bottom: 10.0),
|
|
|
|
padding: EdgeInsets.all(15.0),
|
2020-02-21 16:04:02 +01:00
|
|
|
child: Text(
|
|
|
|
widget.onlinePodcast.description.trim(),
|
|
|
|
maxLines: 3,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
2020-03-01 13:17:06 +01:00
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.bodyText1
|
|
|
|
.copyWith(color: Colors.white),
|
2020-02-21 16:04:02 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
: Center(),
|
2020-02-20 16:44:42 +01:00
|
|
|
],
|
2020-02-09 13:29:09 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|