From 1a660da273f16fc27c5efa1ac636df7412f72051 Mon Sep 17 00:00:00 2001 From: rom4nik <46846000+rom4nik@users.noreply.github.com> Date: Mon, 7 Mar 2022 22:34:35 +0100 Subject: [PATCH] Add confirmation dialog before deleting downloaded album --- .../AlbumScreen/DownloadButton.dart | 23 ++++------ .../DownloadedAlbumDeleteDialog.dart | 43 +++++++++++++++++++ 2 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 lib/components/AlbumScreen/DownloadedAlbumDeleteDialog.dart diff --git a/lib/components/AlbumScreen/DownloadButton.dart b/lib/components/AlbumScreen/DownloadButton.dart index 1cf76bbb1..fc8bc85a2 100644 --- a/lib/components/AlbumScreen/DownloadButton.dart +++ b/lib/components/AlbumScreen/DownloadButton.dart @@ -7,8 +7,8 @@ import '../../services/FinampSettingsHelper.dart'; import '../../services/JellyfinApiData.dart'; import '../../models/JellyfinModels.dart'; import '../../models/FinampModels.dart'; -import '../errorSnackbar.dart'; import 'DownloadDialog.dart'; +import 'DownloadedAlbumDeleteDialog.dart'; class DownloadButton extends StatefulWidget { const DownloadButton({ @@ -53,23 +53,18 @@ class _DownloadButtonState extends State { // If offline, we don't allow the user to delete items. // If we did, we'd have to implement listeners for MusicScreenTabView so that the user can't delete a parent, go back, and select the same parent. // If they did, AlbumScreen would show an error since the item no longer exists. - // Also, the user could delete the parent and immediately redownload it, which will either cause unwanted network usage or cause more errors becuase the user is offline. + // Also, the user could delete the parent and immediately redownload it, which will either cause unwanted network usage or cause more errors because the user is offline. onPressed: isOffline ?? false ? null : () { if (isDownloaded) { - _downloadsHelper - .deleteDownloads( - jellyfinItemIds: widget.items.map((e) => e.id).toList(), - deletedFor: widget.parent.id, - ) - .then((_) { - _checkIfDownloaded(); - ScaffoldMessenger.of(context).showSnackBar( - const SnackBar(content: Text("Downloads deleted."))); - }, - onError: (error, stackTrace) => - errorSnackbar(error, context)); + showDialog( + context: context, + builder: (context) => DownloadedAlbumDeleteDialog( + items: widget.items, + parent: widget.parent, + ), + ).whenComplete(() => _checkIfDownloaded()); } else { if (FinampSettingsHelper .finampSettings.downloadLocationsMap.length == diff --git a/lib/components/AlbumScreen/DownloadedAlbumDeleteDialog.dart b/lib/components/AlbumScreen/DownloadedAlbumDeleteDialog.dart new file mode 100644 index 000000000..9519b92cc --- /dev/null +++ b/lib/components/AlbumScreen/DownloadedAlbumDeleteDialog.dart @@ -0,0 +1,43 @@ +import 'package:flutter/material.dart'; +import 'package:get_it/get_it.dart'; + +import '../../models/JellyfinModels.dart'; +import '../../services/DownloadsHelper.dart'; +import '../errorSnackbar.dart'; + +class DownloadedAlbumDeleteDialog extends StatelessWidget { + const DownloadedAlbumDeleteDialog({ + Key? key, + required this.items, + required this.parent, + }) : super(key: key); + + final List items; + final BaseItemDto parent; + + @override + Widget build(BuildContext context) { + final _downloadsHelper = GetIt.instance(); + return AlertDialog( + title: const Text("Are you sure?"), + actions: [ + TextButton( + child: const Text("CANCEL"), + onPressed: () => Navigator.of(context).pop(), + ), + TextButton( + child: const Text("DELETE"), + onPressed: () { + _downloadsHelper.deleteDownloads( + jellyfinItemIds: items.map((e) => e.id).toList(), + deletedFor: parent.id + ).onError((error, stackTrace) => errorSnackbar(error, context)); + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text("Downloads deleted."))); + Navigator.of(context).pop(); + }, + ), + ], + ); + } +}