Skip to content

Commit

Permalink
fix: replace download multiple pops and add translations
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Jun 8, 2023
1 parent 6752adc commit 4a21249
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 25 deletions.
34 changes: 20 additions & 14 deletions lib/components/shared/dialogs/replace_downloaded_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'package:spotify/spotify.dart';
import 'package:spotube/extensions/context.dart';

final replaceDownloadedFileState = StateProvider<bool?>((ref) => null);

Expand All @@ -14,13 +15,14 @@ class ReplaceDownloadedDialog extends ConsumerWidget {
Widget build(BuildContext context, ref) {
final groupValue = ref.watch(replaceDownloadedFileState);
final theme = Theme.of(context);
final replaceAll = ref.watch(replaceDownloadedFileState);

return AlertDialog(
title: Text("Track ${track.name} Already Exists"),
title: Text(context.l10n.track_exists(track.name ?? "")),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text("Do you want to replace the already downloaded track?"),
Text(context.l10n.do_you_want_to_replace),
RadioListTile<bool>(
dense: true,
contentPadding: EdgeInsets.zero,
Expand All @@ -29,10 +31,10 @@ class ReplaceDownloadedDialog extends ConsumerWidget {
groupValue: groupValue,
onChanged: (value) {
if (value != null) {
ref.read(replaceDownloadedFileState.notifier).state = value;
ref.read(replaceDownloadedFileState.notifier).state = true;
}
},
title: const Text("Replace all downloaded tracks"),
title: Text(context.l10n.replace_downloaded_tracks),
),
RadioListTile<bool>(
dense: true,
Expand All @@ -42,25 +44,29 @@ class ReplaceDownloadedDialog extends ConsumerWidget {
groupValue: groupValue,
onChanged: (value) {
if (value != null) {
ref.read(replaceDownloadedFileState.notifier).state = value;
ref.read(replaceDownloadedFileState.notifier).state = false;
}
},
title: const Text("Skip downloading all downloaded tracks"),
title: Text(context.l10n.skip_download_tracks),
),
],
),
actions: [
OutlinedButton(
child: const Text("No"),
onPressed: () {
Navigator.pop(context, false);
},
onPressed: replaceAll == true
? null
: () {
Navigator.pop(context, false);
},
child: Text(context.l10n.skip),
),
FilledButton(
child: const Text("Yes"),
onPressed: () {
Navigator.pop(context, true);
},
onPressed: replaceAll == false
? null
: () {
Navigator.pop(context, true);
},
child: Text(context.l10n.replace),
),
],
);
Expand Down
8 changes: 7 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,11 @@
"something_went_wrong": "Something went wrong",
"piped_instance": "Piped Server Instance",
"piped_description": "The Piped server instance to use for track matching\nSome of them might not work well. So use at your own risk",
"generate_playlist": "Generate Playlist"
"generate_playlist": "Generate Playlist",
"track_exists": "Track {track} already exists",
"replace_downloaded_tracks": "Replace all downloaded tracks",
"skip_download_tracks": "Skip downloading all downloaded tracks",
"do_you_want_to_replace": "Do you want to replace the existing track??",
"replace": "Replace",
"skip": "Skip"
}
40 changes: 30 additions & 10 deletions lib/pages/root/root_app.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
Expand All @@ -9,7 +11,6 @@ import 'package:spotube/components/root/sidebar.dart';
import 'package:spotube/components/root/spotube_navigation_bar.dart';
import 'package:spotube/hooks/use_update_checker.dart';
import 'package:spotube/provider/download_manager_provider.dart';
import 'package:spotube/provider/authentication_provider.dart';

const rootPaths = {
0: "/",
Expand All @@ -29,19 +30,38 @@ class RootApp extends HookConsumerWidget {
Widget build(BuildContext context, ref) {
final index = useState(0);
final isMounted = useIsMounted();
final auth = ref.watch(AuthenticationNotifier.provider);

final showingDialogCompleter = useRef(Completer()..complete());
final downloader = ref.watch(downloadManagerProvider.notifier);

useEffect(() {
downloader.onFileExists = (track) async {
if (!isMounted()) return false;
return await showDialog<bool>(
context: context,
builder: (context) => ReplaceDownloadedDialog(
track: track,
),
) ??
false;

if (!showingDialogCompleter.value.isCompleted) {
await showingDialogCompleter.value.future;
}

final replaceAll = ref.read(replaceDownloadedFileState);

if (replaceAll != null) return replaceAll;

showingDialogCompleter.value = Completer();

if (context.mounted) {
final result = await showDialog<bool>(
context: context,
builder: (context) => ReplaceDownloadedDialog(
track: track,
),
) ??
false;

showingDialogCompleter.value.complete();
return result;
}

// it'll never reach here as root_app is always mounted
return false;
};
return null;
}, [downloader]);
Expand Down
14 changes: 14 additions & 0 deletions lib/provider/download_manager_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class DownloadManagerProvider extends StateNotifier<List<SpotubeTrack>> {
if (update.status == TaskStatus.complete) {
final track =
state.firstWhere((element) => element.id == update.task.taskId);

// resetting the replace downloaded file state on queue completion
if (state.last == track) {
ref.read(replaceDownloadedFileState.notifier).state = null;
}

state = state
.where((element) => element.id != update.task.taskId)
.toList();
Expand Down Expand Up @@ -141,6 +147,9 @@ class DownloadManagerProvider extends StateNotifier<List<SpotubeTrack>> {
final file = File(_getPathForTrack(track));
if (file.existsSync() &&
(replaceFileGlobal ?? await onFileExists?.call(track)) != true) {
if (state.isEmpty) {
ref.read(replaceDownloadedFileState.notifier).state = null;
}
return null;
}

Expand All @@ -159,6 +168,11 @@ class DownloadManagerProvider extends StateNotifier<List<SpotubeTrack>> {
}
return enqueue(e);
}));

if (tasks.isEmpty) {
ref.read(replaceDownloadedFileState.notifier).state = null;
}

return tasks.whereType<Task>().toList();
}

Expand Down

0 comments on commit 4a21249

Please sign in to comment.