[Radarr] Automatic Search Functionality (#63)

Closes #57 
Closes #28
This commit is contained in:
Jagandeep Brar
2020-01-23 13:31:39 -06:00
committed by GitHub
parent 40341bd446
commit f12414d252
4 changed files with 68 additions and 24 deletions

View File

@@ -808,6 +808,39 @@ class RadarrAPI {
return false;
}
static Future<bool> automaticSearchMovie(int id) async {
List<dynamic> values = Values.radarrValues;
if(values[0] == false) {
return false;
}
try {
String uri = '${values[1]}/api/command?apikey=${values[2]}';
http.Response response = await http.post(
Uri.encodeFull(uri),
headers: {
'Content-Type': 'application/json',
},
body: json.encode({
'name': 'MoviesSearch',
'movieIds': [id],
}),
);
if(response.statusCode == 201) {
Map body = json.decode(response.body);
if(body.containsKey('status')) {
return true;
}
} else {
logError('automaticSearchMovie', '<POST> HTTP Status Code (${response.statusCode})', null);
}
} catch (e) {
logError('automaticSearchMovie', 'Failed to automatically search for movie releases ($id)', e);
return null;
}
logWarning('automaticSearchMovie', 'Failed to automatically search for movie releases ($id)');
return null;
}
static Future<List<RadarrSearchEntry>> searchMovies(String search) async {
List<dynamic> values = Values.radarrValues;
if(values[0] == false) {

View File

@@ -182,6 +182,7 @@ class _State extends State<RadarrMovieDetails> {
buildOverview(entry, context),
RadarrReleases(
entry: entry,
scaffoldKey: _scaffoldKey,
),
buildFiles(entry, _scaffoldKey, _refreshData, context),
],

View File

@@ -6,11 +6,13 @@ import 'package:lunasea/system/functions.dart';
import 'package:lunasea/system/ui.dart';
class RadarrReleases extends StatefulWidget {
final GlobalKey<ScaffoldState> scaffoldKey;
final RadarrCatalogueEntry entry;
RadarrReleases({
Key key,
@required this.entry,
@required this.scaffoldKey,
}): super(key: key);
State<RadarrReleases> createState() {
@@ -20,7 +22,6 @@ class RadarrReleases extends StatefulWidget {
class _State extends State<RadarrReleases> {
final _searchController = TextEditingController();
final _scaffoldKey = GlobalKey<ScaffoldState>();
bool _searched = false;
List<RadarrReleaseEntry> _entries;
String _message = 'Please Search for Releases';
@@ -34,14 +35,10 @@ class _State extends State<RadarrReleases> {
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
body: _buildReleases(),
floatingActionButton: _buildFloatingActionButton(),
);
return _buildReleases();
}
Future<void> _startSearch() async {
Future<void> _startManualSearch() async {
if(mounted) {
setState(() {
_message = 'Searching...';
@@ -57,27 +54,40 @@ class _State extends State<RadarrReleases> {
}
}
Widget _buildFloatingActionButton() {
return FloatingActionButton(
heroTag: null,
tooltip: 'Search',
child: Elements.getIcon(Icons.search),
onPressed: _startSearch,
);
Future<void> _startAutomaticSearch() async {
if(await RadarrAPI.automaticSearchMovie(widget.entry.movieID)) {
Notifications.showSnackBar(widget.scaffoldKey, 'Searching for ${widget.entry.title}...');
} else {
Notifications.showSnackBar(widget.scaffoldKey, 'Failed to search for ${widget.entry.title}...');
}
}
Widget _buildAutomaticSearchButton() {
return Elements.getButton('Automatic Search', () async {
_startAutomaticSearch();
});
}
Widget _buildManualSearchButton() {
return Elements.getButton('Manual Search', () async {
_startManualSearch();
}, backgroundColor: Colors.orange);
}
Widget _buildReleases() {
return Scrollbar(
child: ListView.builder(
itemCount: _entries == null || _entries.length == 0 ? _searched ? 3 : 1 : _entries.length+2,
itemCount: _entries == null || _entries.length == 0 ? _searched ? 5 : 3 : _entries.length+4,
itemBuilder: (context, index) {
switch(index) {
case 0: return _buildSearchBar();
case 1: return Elements.getDivider();
case 1: return _buildAutomaticSearchButton();
case 2: return _buildManualSearchButton();
case 3: return Elements.getDivider();
}
return _entries == null || _entries.length == 0 ?
Notifications.centeredMessage(_message) :
_buildEntry(_entries[index-2]);
_buildEntry(_entries[index-4]);
},
padding: Elements.getListViewPadding(extraBottom: true),
),
@@ -108,9 +118,9 @@ class _State extends State<RadarrReleases> {
onPressed: () async {
if(release.approved) {
if(await _startDownload(release.guid, release.indexerId)) {
Notifications.showSnackBar(_scaffoldKey, 'Download starting...');
Notifications.showSnackBar(widget.scaffoldKey, 'Download starting...');
} else {
Notifications.showSnackBar(_scaffoldKey, 'Failed to start download');
Notifications.showSnackBar(widget.scaffoldKey, 'Failed to start download');
}
} else {
await _showWarnings(release);
@@ -122,9 +132,9 @@ class _State extends State<RadarrReleases> {
List<dynamic> values = await RadarrDialogs.showDownloadWarningPrompt(context);
if(values[0]) {
if(await _startDownload(release.guid, release.indexerId)) {
Notifications.showSnackBar(_scaffoldKey, 'Download starting...');
Notifications.showSnackBar(widget.scaffoldKey, 'Download starting...');
} else {
Notifications.showSnackBar(_scaffoldKey, 'Failed to start download');
Notifications.showSnackBar(widget.scaffoldKey, 'Failed to start download');
}
}
}

View File

@@ -86,7 +86,7 @@ class Elements {
);
}
static Row getButton(String text, Function onTapHandler) {
static Row getButton(String text, Function onTapHandler, { Color backgroundColor, Color textColor = Colors.white} ) {
return Row(
children: <Widget>[
Expanded(
@@ -95,14 +95,14 @@ class Elements {
title: Text(
text,
style: TextStyle(
color: Colors.white,
color: textColor,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
onTap: onTapHandler,
),
color: Color(Constants.ACCENT_COLOR),
color: backgroundColor == null ? Color(Constants.ACCENT_COLOR) : backgroundColor,
margin: getCardMargin(),
elevation: 4.0,
),