simplified code across the app and consolidated duplicated helpers
This commit is contained in:
+34
-41
@@ -20,7 +20,7 @@ import '../utils/screen_bound_image.dart';
|
||||
import 'app_progress_indicator.dart';
|
||||
import 'async_action_button.dart';
|
||||
import 'centered_leading.dart';
|
||||
import 'confirm_dialog.dart';
|
||||
import 'downloads/stale_download_guard.dart';
|
||||
import 'file_viewer/code_line.dart';
|
||||
import 'file_viewer/deferred_pdf_viewer.dart';
|
||||
import 'file_viewer/file_kind.dart';
|
||||
@@ -54,15 +54,20 @@ class _FileViewerState extends State<FileViewer> {
|
||||
Future<_TextPayload>? _textPayload;
|
||||
final PhotoViewController photoViewController = PhotoViewController();
|
||||
|
||||
late SettingsCubit settings = context.read<SettingsCubit>();
|
||||
late bool openExternal;
|
||||
Future<FileKind>? _fileKind;
|
||||
|
||||
String get _fileName => widget.path.split('/').last;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
openExternal =
|
||||
settings.val().fileViewSettings.alwaysOpenExternally ||
|
||||
context
|
||||
.read<SettingsCubit>()
|
||||
.val()
|
||||
.fileViewSettings
|
||||
.alwaysOpenExternally ||
|
||||
widget.openExternal;
|
||||
if (openExternal) {
|
||||
WidgetsBinding.instance.addPostFrameCallback(
|
||||
@@ -93,31 +98,23 @@ class _FileViewerState extends State<FileViewer> {
|
||||
bool _ensureLocalFile() {
|
||||
if (File(widget.path).existsSync()) return true;
|
||||
final remote = widget.remoteFile;
|
||||
if (remote == null) {
|
||||
InfoDialog.show(
|
||||
context,
|
||||
'Die Datei wurde vom System aus dem Zwischenspeicher entfernt. Bitte lade sie erneut herunter.',
|
||||
title: 'Datei nicht mehr verfügbar',
|
||||
);
|
||||
return false;
|
||||
}
|
||||
ConfirmDialog(
|
||||
title: 'Datei nicht mehr verfügbar',
|
||||
content:
|
||||
'Die Datei wurde vom System aus dem Zwischenspeicher entfernt.\nErneut herunterladen?',
|
||||
confirmButton: 'Herunterladen',
|
||||
onConfirm: () {
|
||||
// Pop the viewer before starting so the fresh download auto-opens.
|
||||
Navigator.of(context).pop();
|
||||
unawaited(
|
||||
DownloadManager.instance.start(
|
||||
remotePath: remote.path,
|
||||
name: remote.name,
|
||||
remoteFile: remote,
|
||||
),
|
||||
);
|
||||
},
|
||||
).asDialog(context);
|
||||
showFileGoneDialog(
|
||||
context,
|
||||
subject: 'Die Datei',
|
||||
onRedownload: remote == null
|
||||
? null
|
||||
: () {
|
||||
// Pop the viewer before starting so the fresh download auto-opens.
|
||||
Navigator.of(context).pop();
|
||||
unawaited(
|
||||
DownloadManager.instance.start(
|
||||
remotePath: remote.path,
|
||||
name: remote.name,
|
||||
remoteFile: remote,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -172,7 +169,7 @@ class _FileViewerState extends State<FileViewer> {
|
||||
}
|
||||
final bytes = await source.readAsBytes();
|
||||
final saved = await FilePicker.saveFile(
|
||||
fileName: widget.path.split('/').last,
|
||||
fileName: _fileName,
|
||||
bytes: bytes,
|
||||
);
|
||||
if (!mounted) return;
|
||||
@@ -226,7 +223,7 @@ class _FileViewerState extends State<FileViewer> {
|
||||
List<Widget> actions = const [],
|
||||
bool showActionsMenu = true,
|
||||
}) => AppBar(
|
||||
title: Text(widget.path.split('/').last),
|
||||
title: Text(_fileName),
|
||||
actions: [
|
||||
...actions,
|
||||
if (showActionsMenu)
|
||||
@@ -253,7 +250,7 @@ class _FileViewerState extends State<FileViewer> {
|
||||
Widget build(BuildContext context) {
|
||||
if (openExternal) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text(widget.path.split('/').last)),
|
||||
appBar: _appbar(showActionsMenu: false),
|
||||
body: const Center(child: AppProgressIndicator.large()),
|
||||
);
|
||||
}
|
||||
@@ -347,21 +344,17 @@ class _FileViewerState extends State<FileViewer> {
|
||||
|
||||
Widget _buildAudioView() => Scaffold(
|
||||
appBar: _appbar(),
|
||||
body: MediaPlayer(
|
||||
path: widget.path,
|
||||
isAudio: true,
|
||||
filename: widget.path.split('/').last,
|
||||
),
|
||||
body: MediaPlayer(path: widget.path, isAudio: true, filename: _fileName),
|
||||
);
|
||||
|
||||
Widget _buildTextView() => Scaffold(
|
||||
appBar: _appbar(),
|
||||
body: FutureBuilder<_TextPayload>(
|
||||
// Cached: a future created in build re-read the file on every rebuild.
|
||||
future: _textPayload ??= compute(
|
||||
_loadTextPayload,
|
||||
(widget.path, _textViewMaxBytes),
|
||||
),
|
||||
future: _textPayload ??= compute(_loadTextPayload, (
|
||||
widget.path,
|
||||
_textViewMaxBytes,
|
||||
)),
|
||||
builder: (context, snapshot) {
|
||||
if (!snapshot.hasData) {
|
||||
return const Center(child: AppProgressIndicator.large());
|
||||
@@ -428,7 +421,7 @@ class _FileViewerState extends State<FileViewer> {
|
||||
UnknownPreviewBlock(remoteFile: widget.remoteFile),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
widget.path.split('/').last,
|
||||
_fileName,
|
||||
style: theme.textTheme.titleSmall?.copyWith(
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user