Skip to content

Commit

Permalink
Add confirmation dialog before deleting downloaded album
Browse files Browse the repository at this point in the history
  • Loading branch information
rom4nik committed May 18, 2022
1 parent 5db4d65 commit 1a660da
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
23 changes: 9 additions & 14 deletions lib/components/AlbumScreen/DownloadButton.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -53,23 +53,18 @@ class _DownloadButtonState extends State<DownloadButton> {
// 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 ==
Expand Down
43 changes: 43 additions & 0 deletions lib/components/AlbumScreen/DownloadedAlbumDeleteDialog.dart
Original file line number Diff line number Diff line change
@@ -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<BaseItemDto> items;
final BaseItemDto parent;

@override
Widget build(BuildContext context) {
final _downloadsHelper = GetIt.instance<DownloadsHelper>();
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();
},
),
],
);
}
}

0 comments on commit 1a660da

Please sign in to comment.