simplified code across the app and consolidated duplicated helpers
This commit is contained in:
@@ -3,8 +3,10 @@ import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../routing/app_routes.dart';
|
||||
import '../../utils/downloads/download_job.dart';
|
||||
import '../../utils/downloads/download_manager.dart';
|
||||
import '../../utils/haptics.dart';
|
||||
import '../confirm_dialog.dart';
|
||||
import '../info_dialog.dart';
|
||||
|
||||
@@ -21,20 +23,45 @@ bool ensureDownloadStillExists(BuildContext context, DownloadJob job) {
|
||||
if (path == null || File(path).existsSync()) return true;
|
||||
|
||||
DownloadManager.instance.markOpened(job);
|
||||
if (job.remotePath.isNotEmpty) {
|
||||
ConfirmDialog(
|
||||
title: 'Datei nicht mehr verfügbar',
|
||||
content:
|
||||
'Die heruntergeladene Datei wurde vom System aus dem Zwischenspeicher entfernt.\nErneut herunterladen?',
|
||||
confirmButton: 'Herunterladen',
|
||||
onConfirm: () => unawaited(DownloadManager.instance.retry(job)),
|
||||
).asDialog(context);
|
||||
} else {
|
||||
InfoDialog.show(
|
||||
context,
|
||||
'Die heruntergeladene Datei wurde vom System aus dem Zwischenspeicher entfernt. Bitte lade sie erneut herunter.',
|
||||
title: 'Datei nicht mehr verfügbar',
|
||||
);
|
||||
}
|
||||
showFileGoneDialog(
|
||||
context,
|
||||
subject: 'Die heruntergeladene Datei',
|
||||
onRedownload: job.remotePath.isEmpty
|
||||
? null
|
||||
: () => unawaited(DownloadManager.instance.retry(job)),
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Opens a finished download in the viewer, unless its file vanished.
|
||||
void openFinishedDownload(BuildContext context, DownloadJob job, String path) {
|
||||
if (!ensureDownloadStillExists(context, job)) return;
|
||||
DownloadManager.instance.markOpened(job);
|
||||
Haptics.success();
|
||||
AppRoutes.openFileViewer(context, path, remoteFile: job.remoteFile);
|
||||
}
|
||||
|
||||
/// Tells the user that the system cleared a cached file ([subject], e.g.
|
||||
/// "Die Datei") and offers a re-download when [onRedownload] is given.
|
||||
void showFileGoneDialog(
|
||||
BuildContext context, {
|
||||
required String subject,
|
||||
VoidCallback? onRedownload,
|
||||
}) {
|
||||
const title = 'Datei nicht mehr verfügbar';
|
||||
final gone = '$subject wurde vom System aus dem Zwischenspeicher entfernt.';
|
||||
if (onRedownload == null) {
|
||||
InfoDialog.show(
|
||||
context,
|
||||
'$gone Bitte lade sie erneut herunter.',
|
||||
title: title,
|
||||
);
|
||||
return;
|
||||
}
|
||||
ConfirmDialog(
|
||||
title: title,
|
||||
content: '$gone\nErneut herunterladen?',
|
||||
confirmButton: 'Herunterladen',
|
||||
onConfirm: onRedownload,
|
||||
).asDialog(context);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user