import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:get_it/get_it.dart'; import 'package:logging/logging.dart'; import '../../services/finamp_settings_helper.dart'; import '../../services/downloads_helper.dart'; import '../../models/finamp_models.dart'; import '../../models/jellyfin_models.dart'; import '../error_snackbar.dart'; class DownloadDialog extends StatefulWidget { const DownloadDialog({ Key? key, required this.parents, required this.items, required this.viewId, }) : assert(parents.length == items.length), super(key: key); final List parents; final List> items; final String viewId; @override State createState() => _DownloadDialogState(); } class _DownloadDialogState extends State { DownloadsHelper downloadsHelper = GetIt.instance(); DownloadLocation? selectedDownloadLocation; @override Widget build(BuildContext context) { return AlertDialog( title: Text(AppLocalizations.of(context)!.addDownloads), content: DropdownButton( hint: Text(AppLocalizations.of(context)!.location), isExpanded: true, onChanged: (value) => setState(() { selectedDownloadLocation = value; }), value: selectedDownloadLocation, items: FinampSettingsHelper.finampSettings.downloadLocationsMap.values .map((e) => DropdownMenuItem( value: e, child: Text(e.name), )) .toList()), actions: [ TextButton( child: Text(MaterialLocalizations.of(context).cancelButtonLabel), onPressed: () => Navigator.of(context).pop(), ), TextButton( onPressed: selectedDownloadLocation == null ? null : () async { Navigator.of(context).pop(selectedDownloadLocation); }, child: Text(AppLocalizations.of(context)!.addButtonLabel), ) ], ); } } /// This function is used by DownloadDialog to check/add downloads. Future checkedAddDownloads( BuildContext context, { required DownloadLocation downloadLocation, required List parents, required List> items, required String viewId, }) async { final downloadsHelper = GetIt.instance(); final checkedAddDownloadsLogger = Logger("CheckedAddDownloads"); // If the default "internal storage" path is set and doesn't // exist, it may have been moved by an iOS update. if (downloadLocation.useHumanReadableNames == false && !await Directory(downloadLocation.path).exists()) { checkedAddDownloadsLogger .warning("Internal storage path doesn't exist! Resetting."); await FinampSettingsHelper.resetDefaultDownloadLocation(); } for (int i = 0; i < parents.length; i++) { downloadsHelper .addDownloads( parent: parents[i], items: items[i], useHumanReadableNames: downloadLocation.useHumanReadableNames, viewId: viewId, downloadLocation: downloadLocation, ) .onError((error, stackTrace) => errorSnackbar(error, context)); } }