♻️ Refactored the file detail
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import 'dart:io';
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:dismissible_page/dismissible_page.dart';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:file_saver/file_saver.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
@@ -227,18 +226,75 @@ class FileDetailScreen extends HookConsumerWidget {
|
||||
final uri = '$serverUrl/drive/files/${item.id}';
|
||||
|
||||
return switch (item.mimeType?.split('/').firstOrNull) {
|
||||
'image' => _buildImageContent(context, ref, uri),
|
||||
'video' => _buildVideoContent(context, ref, uri),
|
||||
'audio' => _buildAudioContent(context, ref, uri),
|
||||
'image' => _ImageContent(item: item, uri: uri),
|
||||
'video' => _VideoContent(item: item, uri: uri),
|
||||
'audio' => _AudioContent(item: item, uri: uri),
|
||||
_ when item.mimeType == 'application/pdf' => _PdfContent(uri: uri),
|
||||
_ when item.mimeType?.startsWith('text/') == true => _TextContent(
|
||||
uri: uri,
|
||||
),
|
||||
_ => _buildGenericContent(context, ref),
|
||||
_ => _GenericContent(item: item),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildImageContent(BuildContext context, WidgetRef ref, String uri) {
|
||||
class _PdfContent extends HookConsumerWidget {
|
||||
final String uri;
|
||||
|
||||
const _PdfContent({required this.uri});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final pdfViewer = useMemoized(() => SfPdfViewer.network(uri), [uri]);
|
||||
return pdfViewer;
|
||||
}
|
||||
}
|
||||
|
||||
class _TextContent extends HookConsumerWidget {
|
||||
final String uri;
|
||||
|
||||
const _TextContent({required this.uri});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final textFuture = useMemoized(
|
||||
() => ref
|
||||
.read(apiClientProvider)
|
||||
.get(uri)
|
||||
.then((response) => response.data as String),
|
||||
[uri],
|
||||
);
|
||||
|
||||
return FutureBuilder<String>(
|
||||
future: textFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
} else if (snapshot.hasError) {
|
||||
return Center(child: Text('Error loading text: ${snapshot.error}'));
|
||||
} else if (snapshot.hasData) {
|
||||
return SingleChildScrollView(
|
||||
padding: EdgeInsets.all(20),
|
||||
child: SelectableText(
|
||||
snapshot.data!,
|
||||
style: const TextStyle(fontFamily: 'monospace', fontSize: 14),
|
||||
),
|
||||
);
|
||||
}
|
||||
return const Center(child: Text('No content'));
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ImageContent extends HookConsumerWidget {
|
||||
final SnCloudFile item;
|
||||
final String uri;
|
||||
|
||||
const _ImageContent({required this.item, required this.uri});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final photoViewController = useMemoized(() => PhotoViewController(), []);
|
||||
final rotation = useState(0);
|
||||
final showOriginal = useState(false);
|
||||
@@ -247,14 +303,7 @@ class FileDetailScreen extends HookConsumerWidget {
|
||||
Shadow(color: Colors.black54, blurRadius: 5.0, offset: Offset(1.0, 1.0)),
|
||||
];
|
||||
|
||||
return DismissiblePage(
|
||||
isFullScreen: true,
|
||||
backgroundColor: Colors.transparent,
|
||||
direction: DismissiblePageDismissDirection.down,
|
||||
onDismissed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: Stack(
|
||||
return Stack(
|
||||
children: [
|
||||
Positioned.fill(
|
||||
child: PhotoView(
|
||||
@@ -280,11 +329,7 @@ class FileDetailScreen extends HookConsumerWidget {
|
||||
child: Row(
|
||||
children: [
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
Icons.remove,
|
||||
color: Colors.white,
|
||||
shadows: shadow,
|
||||
),
|
||||
icon: Icon(Icons.remove, color: Colors.white, shadows: shadow),
|
||||
onPressed: () {
|
||||
photoViewController.scale =
|
||||
(photoViewController.scale ?? 1) - 0.05;
|
||||
@@ -306,8 +351,7 @@ class FileDetailScreen extends HookConsumerWidget {
|
||||
),
|
||||
onPressed: () {
|
||||
rotation.value = (rotation.value - 1) % 4;
|
||||
photoViewController.rotation =
|
||||
rotation.value * -math.pi / 2;
|
||||
photoViewController.rotation = rotation.value * -math.pi / 2;
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
@@ -318,8 +362,7 @@ class FileDetailScreen extends HookConsumerWidget {
|
||||
),
|
||||
onPressed: () {
|
||||
rotation.value = (rotation.value + 1) % 4;
|
||||
photoViewController.rotation =
|
||||
rotation.value * -math.pi / 2;
|
||||
photoViewController.rotation = rotation.value * -math.pi / 2;
|
||||
},
|
||||
),
|
||||
const Spacer(),
|
||||
@@ -337,53 +380,59 @@ class FileDetailScreen extends HookConsumerWidget {
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildVideoContent(BuildContext context, WidgetRef ref, String uri) {
|
||||
class _VideoContent extends HookConsumerWidget {
|
||||
final SnCloudFile item;
|
||||
final String uri;
|
||||
|
||||
const _VideoContent({required this.item, required this.uri});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
var ratio =
|
||||
item.fileMeta?['ratio'] is num
|
||||
? item.fileMeta!['ratio'].toDouble()
|
||||
: 1.0;
|
||||
if (ratio == 0) ratio = 1.0;
|
||||
|
||||
return DismissiblePage(
|
||||
isFullScreen: true,
|
||||
backgroundColor: Colors.black,
|
||||
direction: DismissiblePageDismissDirection.down,
|
||||
onDismissed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: Center(
|
||||
return Center(
|
||||
child: AspectRatio(
|
||||
aspectRatio: ratio,
|
||||
child: UniversalVideo(uri: uri, autoplay: true),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildAudioContent(BuildContext context, WidgetRef ref, String uri) {
|
||||
return DismissiblePage(
|
||||
isFullScreen: true,
|
||||
backgroundColor: Theme.of(context).colorScheme.surface,
|
||||
direction: DismissiblePageDismissDirection.down,
|
||||
onDismissed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: Center(
|
||||
class _AudioContent extends HookConsumerWidget {
|
||||
final SnCloudFile item;
|
||||
final String uri;
|
||||
|
||||
const _AudioContent({required this.item, required this.uri});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
maxWidth: math.min(360, MediaQuery.of(context).size.width * 0.8),
|
||||
),
|
||||
child: UniversalAudio(uri: uri, filename: item.name),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildGenericContent(BuildContext context, WidgetRef ref) {
|
||||
class _GenericContent extends HookConsumerWidget {
|
||||
final SnCloudFile item;
|
||||
|
||||
const _GenericContent({required this.item});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
Future<void> downloadFile() async {
|
||||
try {
|
||||
showSnackBar('Downloading file...');
|
||||
@@ -412,14 +461,7 @@ class FileDetailScreen extends HookConsumerWidget {
|
||||
}
|
||||
}
|
||||
|
||||
return DismissiblePage(
|
||||
isFullScreen: true,
|
||||
backgroundColor: Theme.of(context).colorScheme.surface,
|
||||
direction: DismissiblePageDismissDirection.down,
|
||||
onDismissed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: Center(
|
||||
return Center(
|
||||
child: Container(
|
||||
margin: const EdgeInsets.all(32),
|
||||
padding: const EdgeInsets.all(32),
|
||||
@@ -483,56 +525,6 @@ class FileDetailScreen extends HookConsumerWidget {
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _PdfContent extends HookConsumerWidget {
|
||||
final String uri;
|
||||
|
||||
const _PdfContent({required this.uri});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final pdfViewer = useMemoized(() => SfPdfViewer.network(uri), [uri]);
|
||||
return pdfViewer;
|
||||
}
|
||||
}
|
||||
|
||||
class _TextContent extends HookConsumerWidget {
|
||||
final String uri;
|
||||
|
||||
const _TextContent({required this.uri});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final textFuture = useMemoized(
|
||||
() => ref
|
||||
.read(apiClientProvider)
|
||||
.get(uri)
|
||||
.then((response) => response.data as String),
|
||||
[uri],
|
||||
);
|
||||
|
||||
return FutureBuilder<String>(
|
||||
future: textFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
} else if (snapshot.hasError) {
|
||||
return Center(child: Text('Error loading text: ${snapshot.error}'));
|
||||
} else if (snapshot.hasData) {
|
||||
return SingleChildScrollView(
|
||||
padding: EdgeInsets.all(20),
|
||||
child: SelectableText(
|
||||
snapshot.data!,
|
||||
style: const TextStyle(fontFamily: 'monospace', fontSize: 14),
|
||||
),
|
||||
);
|
||||
}
|
||||
return const Center(child: Text('No content'));
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user