ente/lib/ui/tools/debug/path_storage_viewer.dart

102 lines
2.7 KiB
Dart
Raw Normal View History

2022-12-22 04:48:06 +00:00
import 'package:flutter/foundation.dart';
2022-12-13 09:34:14 +00:00
import 'package:flutter/material.dart';
2022-12-22 04:48:06 +00:00
import 'package:flutter/services.dart';
2022-12-13 09:34:14 +00:00
import 'package:photos/theme/ente_theme.dart';
import 'package:photos/ui/common/loading_widget.dart';
import 'package:photos/ui/components/captioned_text_widget.dart';
import 'package:photos/ui/components/menu_item_widget.dart';
import 'package:photos/utils/data_util.dart';
2022-12-22 08:15:52 +00:00
import 'package:photos/utils/directory_content.dart';
2022-12-13 09:34:14 +00:00
2022-12-22 04:48:06 +00:00
class PathStorageItem {
2022-12-13 09:34:14 +00:00
final String path;
2022-12-22 04:48:06 +00:00
final String title;
final bool allowCacheClear;
PathStorageItem.name(
this.path,
this.title, {
this.allowCacheClear = false,
});
}
class PathStorageViewer extends StatefulWidget {
2022-12-22 08:15:52 +00:00
final PathStorageItem item;
final bool removeTopRadius;
final bool removeBottomRadius;
final bool enableDoubleTapClear;
2022-12-13 09:34:14 +00:00
const PathStorageViewer(
2022-12-22 08:15:52 +00:00
this.item, {
this.removeTopRadius = false,
this.removeBottomRadius = false,
this.enableDoubleTapClear = false,
Key? key,
2022-12-13 09:34:14 +00:00
}) : super(key: key);
@override
State<PathStorageViewer> createState() => _PathStorageViewerState();
}
class _PathStorageViewerState extends State<PathStorageViewer> {
2022-12-22 08:27:12 +00:00
Map<String, int>? _sizeAndFileCountInfo;
2022-12-13 09:34:14 +00:00
@override
void initState() {
2022-12-22 08:15:52 +00:00
_initLogs();
super.initState();
}
void _initLogs() {
directoryStat(widget.item.path).then((logs) {
2022-12-13 09:34:14 +00:00
setState(() {
2022-12-22 08:27:12 +00:00
_sizeAndFileCountInfo = logs;
2022-12-13 09:34:14 +00:00
});
});
}
@override
Widget build(BuildContext context) {
return _getBody();
}
Widget _getBody() {
2022-12-22 08:27:12 +00:00
if (_sizeAndFileCountInfo == null) {
2022-12-13 09:34:14 +00:00
return const EnteLoadingWidget();
}
2022-12-22 08:27:12 +00:00
final int fileCount = _sizeAndFileCountInfo!["fileCount"] ?? -1;
final int size = _sizeAndFileCountInfo!['size'] ?? 0;
2022-12-13 09:34:14 +00:00
2022-12-22 04:48:06 +00:00
return MenuItemWidget(
alignCaptionedTextToLeft: true,
captionedTextWidget: CaptionedTextWidget(
2022-12-22 08:15:52 +00:00
title: widget.item.title,
2022-12-22 04:48:06 +00:00
subTitle: '$fileCount',
subTitleColor: getEnteColorScheme(context).textFaint,
),
trailingWidget: Text(
formatBytes(size),
style: getEnteTextTheme(context)
.small
.copyWith(color: getEnteColorScheme(context).textFaint),
2022-12-13 09:34:14 +00:00
),
2022-12-22 08:15:52 +00:00
borderRadius: 8,
2022-12-22 04:48:06 +00:00
menuItemColor: getEnteColorScheme(context).fillFaint,
2022-12-22 08:15:52 +00:00
isBottomBorderRadiusRemoved: widget.removeBottomRadius,
isTopBorderRadiusRemoved: widget.removeTopRadius,
2022-12-22 04:48:06 +00:00
onTap: () async {
if (kDebugMode) {
2022-12-22 08:15:52 +00:00
await Clipboard.setData(ClipboardData(text: widget.item.path));
debugPrint(widget.item.path);
}
},
onDoubleTap: () async {
if (widget.item.allowCacheClear && widget.enableDoubleTapClear) {
await deleteDirectoryContents(widget.item.path);
_initLogs();
2022-12-22 04:48:06 +00:00
}
},
2022-12-13 09:34:14 +00:00
);
}
}