claude refactor

This commit is contained in:
2026-05-04 13:54:39 +02:00
parent 9973f12733
commit 551c1bf1fa
125 changed files with 4484 additions and 2544 deletions
+157 -159
View File
@@ -1,32 +1,22 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:loader_overlay/loader_overlay.dart';
import 'package:nextcloud/nextcloud.dart';
import 'package:persistent_bottom_nav_bar_v2/persistent_bottom_nav_bar_v2.dart';
import 'package:provider/provider.dart';
import '../../../api/marianumcloud/webdav/queries/listFiles/cacheableFile.dart';
import '../../../api/marianumcloud/webdav/queries/listFiles/listFilesCache.dart';
import '../../../api/marianumcloud/webdav/queries/listFiles/listFilesResponse.dart';
import '../../../api/marianumcloud/webdav/webdavApi.dart';
import '../../../model/files/filesProps.dart';
import '../../../storage/base/settingsProvider.dart';
import '../../../widget/loadingSpinner.dart';
import '../../../widget/placeholderView.dart';
import '../../../state/app/infrastructure/loadableState/loadable_state.dart';
import '../../../state/app/infrastructure/loadableState/view/loadable_state_consumer.dart';
import '../../../state/app/infrastructure/utilityWidgets/bloc_module.dart';
import '../../../state/app/modules/files/bloc/files_bloc.dart';
import '../../../state/app/modules/files/bloc/files_state.dart';
import '../../../state/app/modules/settings/bloc/settings_cubit.dart';
import '../../../widget/filePick.dart';
import '../../../widget/placeholderView.dart';
import 'fileElement.dart';
import 'filesUploadDialog.dart';
class Files extends StatefulWidget {
final List<String> path;
Files({List<String>? path, super.key}) : path = path ?? [];
@override
State<Files> createState() => _FilesState();
}
class BetterSortOption {
String displayName;
int Function(CacheableFile, CacheableFile) compare;
@@ -35,111 +25,107 @@ class BetterSortOption {
BetterSortOption({required this.displayName, required this.icon, required this.compare});
}
enum SortOption {
name,
date,
size
}
enum SortOption { name, date, size }
class SortOptions {
static Map<SortOption, BetterSortOption> options = {
SortOption.name: BetterSortOption(
displayName: 'Name',
icon: Icons.sort_by_alpha_outlined,
compare: (CacheableFile a, CacheableFile b) => a.name.compareTo(b.name)
compare: (a, b) => a.name.compareTo(b.name),
),
SortOption.date: BetterSortOption(
displayName: 'Datum',
icon: Icons.history_outlined,
compare: (CacheableFile a, CacheableFile b) => a.modifiedAt!.compareTo(b.modifiedAt!)
compare: (a, b) => a.modifiedAt!.compareTo(b.modifiedAt!),
),
SortOption.size: BetterSortOption(
displayName: 'Größe',
icon: Icons.sd_card_outlined,
compare: (CacheableFile a, CacheableFile b) {
if(a.isDirectory || b.isDirectory) return a.isDirectory ? 1 : 0;
if(a.size == null) return 0;
if(b.size == null) return 1;
compare: (a, b) {
if (a.isDirectory || b.isDirectory) return a.isDirectory ? 1 : 0;
if (a.size == null) return 0;
if (b.size == null) return 1;
return a.size!.compareTo(b.size!);
}
)
},
),
};
static BetterSortOption getOption(SortOption option) => options[option]!;
}
class _FilesState extends State<Files> {
FilesProps props = FilesProps();
ListFilesResponse? data;
class Files extends StatelessWidget {
final List<String> path;
late SettingsProvider settings = Provider.of<SettingsProvider>(context, listen: false);
Files({List<String>? path, super.key}) : path = path ?? [];
SortOption currentSort = SortOption.name;
bool currentSortDirection = true;
@override
Widget build(BuildContext context) => BlocModule<FilesBloc, LoadableState<FilesState>>(
create: (_) => FilesBloc(initialPath: path),
child: (context, _, _) => _FilesView(path: path),
);
}
class _FilesView extends StatefulWidget {
final List<String> path;
const _FilesView({required this.path});
@override
State<_FilesView> createState() => _FilesViewState();
}
class _FilesViewState extends State<_FilesView> {
late final SettingsCubit settings;
late SortOption currentSort;
late bool currentSortDirection;
@override
void initState() {
super.initState();
settings = context.read<SettingsCubit>();
currentSort = settings.val().fileSettings.sortBy;
currentSortDirection = settings.val().fileSettings.ascending;
_query();
}
void _query() {
ListFilesCache(
path: widget.path.isEmpty ? '/' : widget.path.join('/'),
onUpdate: (ListFilesResponse d) {
d.files.removeWhere((element) => element.name.isEmpty || element.name == widget.path.lastOrNull());
setState(() {
data = d;
});
}
);
}
Future<void> mediaUpload(List<String>? paths) async {
if(paths == null) return;
if (paths == null) return;
final bloc = context.read<FilesBloc>();
pushScreen(
context,
withNavBar: false,
screen: FilesUploadDialog(filePaths: paths, remotePath: widget.path.join('/'), onUploadFinished: (uploadedFilePaths) => _query()),
screen: FilesUploadDialog(
filePaths: paths,
remotePath: widget.path.join('/'),
onUploadFinished: (_) => bloc.refresh(),
),
);
return;
}
@override
Widget build(BuildContext context) {
var files = data?.sortBy(
sortOption: currentSort,
foldersToTop: Provider.of<SettingsProvider>(context).val().fileSettings.sortFoldersToTop,
reversed: currentSortDirection
) ?? List<CacheableFile>.empty();
final bloc = context.read<FilesBloc>();
return Scaffold(
appBar: AppBar(
title: Text(widget.path.isNotEmpty ? widget.path.last : 'Dateien'),
actions: [
// IconButton(
// icon: const Icon(Icons.search),
// onPressed: () => {
// // TODO implement search
// },
// ),
PopupMenuButton<bool>(
icon: Icon(currentSortDirection ? Icons.text_rotate_up : Icons.text_rotation_down),
itemBuilder: (context) => [true, false].map((e) => PopupMenuItem<bool>(
value: e,
enabled: e != currentSortDirection,
child: Row(
children: [
Icon(e ? Icons.text_rotate_up : Icons.text_rotation_down, color: Theme.of(context).colorScheme.onSurface),
const SizedBox(width: 15),
Text(e ? 'Aufsteigend' : 'Absteigend')
],
)
)).toList(),
itemBuilder: (context) => [true, false]
.map((e) => PopupMenuItem<bool>(
value: e,
enabled: e != currentSortDirection,
child: Row(
children: [
Icon(
e ? Icons.text_rotate_up : Icons.text_rotation_down,
color: Theme.of(context).colorScheme.onSurface,
),
const SizedBox(width: 15),
Text(e ? 'Aufsteigend' : 'Absteigend'),
],
),
))
.toList(),
onSelected: (e) {
setState(() {
currentSortDirection = e;
@@ -149,17 +135,19 @@ class _FilesState extends State<Files> {
),
PopupMenuButton<SortOption>(
icon: const Icon(Icons.sort),
itemBuilder: (context) => SortOptions.options.keys.map((key) => PopupMenuItem<SortOption>(
value: key,
enabled: key != currentSort,
child: Row(
children: [
Icon(SortOptions.getOption(key).icon, color: Theme.of(context).colorScheme.onSurface),
const SizedBox(width: 15),
Text(SortOptions.getOption(key).displayName),
],
)
)).toList(),
itemBuilder: (context) => SortOptions.options.keys
.map((key) => PopupMenuItem<SortOption>(
value: key,
enabled: key != currentSort,
child: Row(
children: [
Icon(SortOptions.getOption(key).icon, color: Theme.of(context).colorScheme.onSurface),
const SizedBox(width: 15),
Text(SortOptions.getOption(key).displayName),
],
),
))
.toList(),
onSelected: (e) {
setState(() {
currentSort = e;
@@ -172,81 +160,91 @@ class _FilesState extends State<Files> {
floatingActionButton: FloatingActionButton(
heroTag: 'uploadFile',
backgroundColor: Theme.of(context).primaryColor,
onPressed: () {
showDialog(context: context, builder: (context) => SimpleDialog(
children: [
ListTile(
leading: const Icon(Icons.create_new_folder_outlined),
title: const Text('Ordner erstellen'),
onTap: () {
Navigator.of(context).pop();
showDialog(context: context, builder: (context) {
var inputController = TextEditingController();
return AlertDialog(
title: const Text('Neuer Ordner'),
content: TextField(
controller: inputController,
decoration: const InputDecoration(
labelText: 'Name',
),
),
actions: [
TextButton(onPressed: () {
Navigator.of(context).pop();
}, child: const Text('Abbrechen')),
TextButton(onPressed: () {
WebdavApi.webdav.then((webdav) {
webdav.mkcol(PathUri.parse("${widget.path.join("/")}/${inputController.text}")).then((value) => _query());
});
Navigator.of(context).pop();
}, child: const Text('Ordner erstellen')),
],
);
});
},
),
ListTile(
leading: const Icon(Icons.upload_file),
title: const Text('Aus Dateien hochladen'),
onTap: () {
FilePick.documentPick().then(mediaUpload);
Navigator.of(context).pop();
},
),
Visibility(
visible: !Platform.isIOS,
child: ListTile(
leading: const Icon(Icons.add_a_photo_outlined),
title: const Text('Aus Gallerie hochladen'),
onTap: () {
FilePick.multipleGalleryPick().then((value) {
if(value != null) mediaUpload(value.map((e) => e.path).toList());
});
Navigator.of(context).pop();
},
),
),
],
));
},
onPressed: () => _showAddDialog(context, bloc),
child: const Icon(Icons.add),
),
body: data == null ? const LoadingSpinner() : data!.files.isEmpty ? const PlaceholderView(icon: Icons.folder_off_rounded, text: 'Der Ordner ist leer') : LoaderOverlay(
child: RefreshIndicator(
onRefresh: () {
_query();
return Future.delayed(const Duration(seconds: 3));
body: LoadableStateConsumer<FilesBloc, FilesState>(
child: (state, _) {
final listing = state.listing;
if (listing == null) return const SizedBox.shrink();
if (listing.files.isEmpty) {
return const PlaceholderView(icon: Icons.folder_off_rounded, text: 'Der Ordner ist leer');
}
final files = listing.sortBy(
sortOption: currentSort,
foldersToTop: context.watch<SettingsCubit>().val().fileSettings.sortFoldersToTop,
reversed: currentSortDirection,
);
return LoaderOverlay(
child: ListView.builder(
padding: EdgeInsets.zero,
itemCount: files.length,
itemBuilder: (context, index) => FileElement(files[index], widget.path, bloc.refresh),
),
);
},
),
);
}
void _showAddDialog(BuildContext context, FilesBloc bloc) {
showDialog(
context: context,
builder: (dialogCtx) => SimpleDialog(children: [
ListTile(
leading: const Icon(Icons.create_new_folder_outlined),
title: const Text('Ordner erstellen'),
onTap: () {
Navigator.of(dialogCtx).pop();
_showCreateFolderDialog(context, bloc);
},
child: ListView.builder(
padding: EdgeInsets.zero,
itemCount: files.length,
itemBuilder: (context, index) {
var file = files.toList()[index];
return FileElement(file, widget.path, _query);
),
ListTile(
leading: const Icon(Icons.upload_file),
title: const Text('Aus Dateien hochladen'),
onTap: () {
FilePick.documentPick().then(mediaUpload);
Navigator.of(dialogCtx).pop();
},
),
Visibility(
visible: !Platform.isIOS,
child: ListTile(
leading: const Icon(Icons.add_a_photo_outlined),
title: const Text('Aus Gallerie hochladen'),
onTap: () {
FilePick.multipleGalleryPick().then((value) {
if (value != null) mediaUpload(value.map((e) => e.path).toList());
});
Navigator.of(dialogCtx).pop();
},
),
)
)
),
]),
);
}
void _showCreateFolderDialog(BuildContext context, FilesBloc bloc) {
final inputController = TextEditingController();
showDialog(
context: context,
builder: (dialogCtx) => AlertDialog(
title: const Text('Neuer Ordner'),
content: TextField(
controller: inputController,
decoration: const InputDecoration(labelText: 'Name'),
),
actions: [
TextButton(onPressed: () => Navigator.of(dialogCtx).pop(), child: const Text('Abbrechen')),
TextButton(
onPressed: () {
bloc.createFolder(inputController.text);
Navigator.of(dialogCtx).pop();
},
child: const Text('Ordner erstellen'),
),
],
),
);
}
}
+52 -13
View File
@@ -65,6 +65,28 @@ class _FilesUploadDialogState extends State<FilesUploadDialog> {
);
}
void _showUploadError(String message) {
setState(() {
_isUploading = false;
_overallProgressValue = 0.0;
_infoText = '';
});
showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: const Text('Upload fehlgeschlagen'),
contentPadding: const EdgeInsets.all(10),
content: Text(message),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Schließen', textAlign: TextAlign.center),
),
],
),
);
}
Future<void> uploadFiles({bool override = false}) async {
setState(() {
_isUploading = true;
@@ -74,16 +96,24 @@ class _FilesUploadDialogState extends State<FilesUploadDialog> {
}
});
var webdavClient = await WebdavApi.webdav;
final webdavClient = await WebdavApi.webdav;
if (!override) {
var result = (await webdavClient.propfind(PathUri.parse(widget.remotePath))).responses;
List<dynamic> result;
try {
result = (await webdavClient.propfind(PathUri.parse(widget.remotePath))).responses;
} catch (e) {
if (!mounted) return;
_showUploadError('Verbindung fehlgeschlagen: $e');
return;
}
var conflictingFiles = _uploadableFiles.where((file) {
var fileName = file.fileName;
return result.any((element) => Uri.decodeComponent(element.href!).endsWith('/$fileName'));
}).toList();
if(conflictingFiles.isNotEmpty) {
if (!mounted) return;
bool replaceFiles = await showDialog(
context: context,
barrierDismissible: false,
@@ -157,17 +187,24 @@ class _FilesUploadDialogState extends State<FilesUploadDialog> {
_infoText = '${_uploadableFiles.indexOf(file) + 1}/${_uploadableFiles.length}';
});
var uploadTask = await webdavClient.putFile(
File(filePath),
FileStat.statSync(filePath),
PathUri.parse(fullRemotePath),
onProgress: (progress) {
setState(() {
file._uploadProgress = progress;
_overallProgressValue = ((progress + _uploadableFiles.indexOf(file)) / _uploadableFiles.length).toDouble();
});
},
);
final dynamic uploadTask;
try {
uploadTask = await webdavClient.putFile(
File(filePath),
FileStat.statSync(filePath),
PathUri.parse(fullRemotePath),
onProgress: (progress) {
setState(() {
file._uploadProgress = progress;
_overallProgressValue = ((progress + _uploadableFiles.indexOf(file)) / _uploadableFiles.length).toDouble();
});
},
);
} catch (e) {
if (!mounted) return;
_showUploadError('Upload fehlgeschlagen für "$fileName": $e');
return;
}
if(uploadTask.statusCode < 200 || uploadTask.statusCode > 299) {
setState(() {
@@ -175,6 +212,7 @@ class _FilesUploadDialogState extends State<FilesUploadDialog> {
_overallProgressValue = 0.0;
_infoText = '';
});
if (!mounted) return;
Navigator.of(context).pop();
showHttpErrorCode(uploadTask.statusCode);
} else {
@@ -187,6 +225,7 @@ class _FilesUploadDialogState extends State<FilesUploadDialog> {
_overallProgressValue = 0.0;
_infoText = '';
});
if (!mounted) return;
Navigator.of(context).pop();
widget.onUploadFinished(uploadetFilePaths);
}