Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Add Manual Import for Sonarr queue item with warnings #769

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/api/sonarr/controllers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import 'package:lunasea/api/sonarr/models.dart';
import 'package:lunasea/api/sonarr/types.dart';
import 'package:dio/dio.dart';
import 'package:intl/intl.dart';
import 'package:lunasea/core.dart';

import 'models/manual_import/manual_import.dart';

// Calendar
part 'controllers/calendar.dart';
Expand All @@ -22,6 +25,7 @@ part 'controllers/command/rescan_series.dart';
part 'controllers/command/rss_sync.dart';
part 'controllers/command/season_search.dart';
part 'controllers/command/series_search.dart';
part 'controllers/command/manual_import.dart';

// Episode File
part 'controllers/episode_file.dart';
Expand Down Expand Up @@ -94,3 +98,6 @@ part 'controllers/tag/update_tag.dart';
// Wanted/Missing
part 'controllers/wanted.dart';
part 'controllers/wanted/get_missing.dart';

// ManualImport
part 'controllers/manual_import.dart';
13 changes: 13 additions & 0 deletions lib/api/sonarr/controllers/command/manual_import.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
part of sonarr_commands;

Future<SonarrCommand> _commandManualImport(
Dio client,
List<SonarrManualImport> manualImports
) async {
Response response = await client.post('command', data: {
'name': 'ManualImport',
'files': manualImports,
'importMode': 'auto'
});
return SonarrCommand.fromJson(response.data);
}
22 changes: 22 additions & 0 deletions lib/api/sonarr/controllers/manual_import.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
part of sonarr_commands;

/// Sends a request to import a queued item within Sonarr.
///
/// [SonarrControllerManualImport] internally handles routing the HTTP client to the API calls.
class SonarrControllerManualImport {
final Dio _client;

/// Create a series command handler using an initialized [Dio] client.
SonarrControllerManualImport(this._client);

/// Handler for [manualimport]
///
/// Sends a request to manually import a queued item with warnings.
///
/// Required Parameters:
/// - `records`: The records to import
Future<void> import({
required List<SonarrManualImport> manualImports
}) async =>
_commandManualImport(_client, manualImports);
}
4 changes: 4 additions & 0 deletions lib/api/sonarr/models/command/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class SonarrCommand {
@JsonKey(name: 'updateScheduledTask')
bool? updateScheduledTask;

@JsonKey(name: 'importMode')
String? importMode;

/// Identifier of command instance
@JsonKey(name: 'id')
int? id;
Expand All @@ -90,6 +93,7 @@ class SonarrCommand {
this.startedOn,
this.sendUpdatesToClient,
this.updateScheduledTask,
this.importMode,
this.id,
});

Expand Down
55 changes: 55 additions & 0 deletions lib/api/sonarr/models/manual_import/manual_import.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import 'dart:convert';
import 'package:json_annotation/json_annotation.dart';
import 'package:lunasea/modules/sonarr.dart';

part 'manual_import.g.dart';

@JsonSerializable(explicitToJson: true, includeIfNull: false)
class SonarrManualImport {

@JsonKey(name: 'path')
String? path;

@JsonKey(name: 'seriesId')
int? seriesId;

@JsonKey(name: 'episodeIds')
List<int>? episodeIds;

@JsonKey(name: 'releaseGroup')
String? releaseGroup;

@JsonKey(name: 'quality')
SonarrEpisodeFileQuality? quality;

@JsonKey(name: 'language')
SonarrEpisodeFileLanguage? language;

@JsonKey(name: 'downloadId')
String? downloadId;

@JsonKey(name: 'id')
int? id;

SonarrManualImport({
this.path,
this.seriesId,
this.episodeIds,
this.releaseGroup,
this.quality,
this.language,
this.downloadId,
this.id,
});

/// Returns a JSON-encoded string version of this object.
@override
String toString() => json.encode(this.toJson());

/// Deserialize a JSON map to a [SonarrManualImport] object.
factory SonarrManualImport.fromJson(Map<String, dynamic> json) =>
_$SonarrManualImportFromJson(json);

/// Serialize a [SonarrManualImport] object to a JSON map.
Map<String, dynamic> toJson() => _$SonarrManualImportToJson(this);
}
4 changes: 4 additions & 0 deletions lib/api/sonarr/sonarr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class SonarrAPI {
required this.system,
required this.tag,
required this.wanted,
required this.manualImport
});

factory SonarrAPI({
Expand Down Expand Up @@ -60,6 +61,7 @@ class SonarrAPI {
system: SonarrControllerSystem(_dio),
tag: SonarrControllerTag(_dio),
wanted: SonarrControllerWanted(_dio),
manualImport: SonarrControllerManualImport(_dio)
);
}

Expand All @@ -83,6 +85,7 @@ class SonarrAPI {
system: SonarrControllerSystem(client),
tag: SonarrControllerTag(client),
wanted: SonarrControllerWanted(client),
manualImport: SonarrControllerManualImport(client)
);
}

Expand All @@ -103,4 +106,5 @@ class SonarrAPI {
final SonarrControllerSystem system;
final SonarrControllerTag tag;
final SonarrControllerWanted wanted;
final SonarrControllerManualImport manualImport;
}
47 changes: 46 additions & 1 deletion lib/modules/sonarr/core/api_controller.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:lunasea/api/sonarr/models/manual_import/manual_import.dart';
import 'package:lunasea/core.dart';
import 'package:lunasea/extensions/string/string.dart';
import 'package:lunasea/modules/sonarr.dart';
Expand Down Expand Up @@ -797,4 +798,48 @@ class SonarrAPIController {
}
return false;
}
}

Future<bool> manualImportFromQueue({
required BuildContext context,
required SonarrQueueRecord queueRecord,
bool showSnackbar = true
}) async {
if (context.read<SonarrState>().enabled) {
return context
.read<SonarrState>()
.api!
.manualImport
.import(manualImports: [SonarrManualImport(
path: queueRecord.outputPath,
seriesId: queueRecord.seriesId,
episodeIds: [queueRecord.episodeId!],
quality: queueRecord.quality,
language: queueRecord.language,
downloadId: queueRecord.downloadId,
id: queueRecord.id,
)])
.then((_) {
if (showSnackbar)
showLunaSuccessSnackBar(
title: 'sonarr.ManuallyImportedFromQueue'.tr(),
message: queueRecord.title,
);
return true;
}).catchError((error, stack) {
LunaLogger().error(
'Failed to import queue record: ${queueRecord.id}',
error,
stack,
);
if (showSnackbar)
showLunaErrorSnackBar(
title: 'sonarr.FailedToManuallyImportFromQueue'.tr(),
error: error,
);
return false;
});
}

return false;
}
}
28 changes: 24 additions & 4 deletions lib/modules/sonarr/core/dialogs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -746,21 +746,39 @@ class SonarrDialogs {
return _flag;
}

Future<void> showQueueStatusMessages(
Future<bool> showQueueStatusMessages(
BuildContext context,
List<SonarrQueueStatusMessage> messages,
SonarrQueueRecord record,
) async {
bool _flag = false;

void _setValues(bool flag) {
_flag = flag;
Navigator.of(context, rootNavigator: true).pop();
}

var messages = record.statusMessages!;
if (messages.isEmpty) {
return LunaDialogs().textPreview(
await LunaDialogs().textPreview(
context,
'sonarr.Messages'.tr(),
'sonarr.NoMessagesFound'.tr(),
);

return false;
}
await LunaDialog.dialog(
context: context,
title: 'sonarr.Messages'.tr(),
cancelButtonText: 'lunasea.Close'.tr(),
buttons: record.errorMessage != null ? [] : [
LunaDialog.button(
text: 'Import',
onPressed: () => {
_setValues(true)
},
),
],
contentPadding: LunaDialog.listDialogContentPadding(),
content: List.generate(
messages.length,
Expand Down Expand Up @@ -828,8 +846,10 @@ class SonarrDialogs {
),
),
);
}

return _flag;
}

Future<Tuple2<bool, int>> setQueuePageSize(BuildContext context) async {
bool _flag = false;
GlobalKey<FormState> _formKey = GlobalKey<FormState>();
Expand Down
26 changes: 24 additions & 2 deletions lib/modules/sonarr/routes/queue/widgets/queue_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,32 @@ class _State extends State<SonarrQueueTile> {
color: LunaColours.orange,
text: 'sonarr.Messages'.tr(),
onTap: () async {
SonarrDialogs().showQueueStatusMessages(
bool result = await SonarrDialogs().showQueueStatusMessages(
context,
widget.queueRecord.statusMessages!,
widget.queueRecord,
);

if (result) {
SonarrAPIController()
.manualImportFromQueue(context: context, queueRecord: widget.queueRecord)
.then((_) {
switch (widget.type) {
case SonarrQueueTileType.ALL:
context.read<SonarrQueueState>().fetchQueue(
context,
hardCheck: true,
);
break;
case SonarrQueueTileType.EPISODE:
context.read<SonarrSeasonDetailsState>().fetchState(
context,
shouldFetchEpisodes: false,
shouldFetchFiles: false,
);
break;
}
});
}
},
),
// if (widget.queueRecord.status == SonarrQueueStatus.COMPLETED &&
Expand Down