Skip to content

Commit

Permalink
Merge pull request #1802 from ardriveapp/PE-6472-handle-error-at-imag…
Browse files Browse the repository at this point in the history
…e-preview-object-with-null-properties-null-error-null-check-operator-used-on-a-null-value

PE-6472: feat(image preview)
  • Loading branch information
thiagocarvalhodev authored Jul 23, 2024
2 parents 75777c2 + 5dceaae commit d371a60
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
19 changes: 15 additions & 4 deletions lib/blocs/fs_entry_preview/fs_entry_preview_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class FsEntryPreviewCubit extends Cubit<FsEntryPreviewState> {
final SecretKey? _fileKey;

StreamSubscription? _entrySubscription;
static final ValueNotifier<ImagePreviewNotification> imagePreviewNotifier =
ValueNotifier<ImagePreviewNotification>(ImagePreviewNotification());
static final ValueNotifier<ImagePreviewNotification?> imagePreviewNotifier =
ValueNotifier<ImagePreviewNotification?>(null);

final previewMaxFileSize = 1024 * 1024 * 100;
final allowedPreviewContentTypes = [];
Expand Down Expand Up @@ -180,10 +180,15 @@ class FsEntryPreviewCubit extends Cubit<FsEntryPreviewState> {
FileEntry file,
String dataUrl,
) async {
if (file.dataContentType == null) {
emit(FsEntryPreviewUnavailable());
return;
}

imagePreviewNotifier.value = ImagePreviewNotification(
isLoading: true,
filename: file.name,
contentType: file.dataContentType,
contentType: file.dataContentType!,
);

emit(FsEntryPreviewImage(previewUrl: dataUrl));
Expand Down Expand Up @@ -428,10 +433,16 @@ class FsEntryPreviewCubit extends Cubit<FsEntryPreviewState> {
if (isClosed) {
return;
}

if (file.dataContentType == null) {
emit(FsEntryPreviewUnavailable());
return;
}

imagePreviewNotifier.value = ImagePreviewNotification(
dataBytes: dataBytes,
filename: file.name,
contentType: file.dataContentType,
contentType: file.dataContentType!,
);
emit(FsEntryPreviewImage(previewUrl: dataUrl));
}
Expand Down
8 changes: 4 additions & 4 deletions lib/blocs/fs_entry_preview/image_preview_notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import 'dart:typed_data';
class ImagePreviewNotification {
bool isLoading;
Uint8List? dataBytes;
String? filename;
String? contentType;
String filename;
String contentType;

bool get isPreviewable => dataBytes != null;

ImagePreviewNotification({
this.dataBytes,
this.filename,
this.contentType,
required this.filename,
required this.contentType,
this.isLoading = false,
});
}
12 changes: 10 additions & 2 deletions lib/pages/drive_detail/components/fs_entry_preview_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,10 @@ class _ImagePreviewWidgetState extends State<ImagePreviewWidget> {
return ValueListenableBuilder(
valueListenable: FsEntryPreviewCubit.imagePreviewNotifier,
builder: (context, imagePreview, _) {
if (imagePreview == null) {
return const SizedBox.shrink();
}

final isLoading = imagePreview.isLoading;

if (!widget.isFullScreen) {
Expand Down Expand Up @@ -1579,8 +1583,12 @@ class _ImagePreviewWidgetState extends State<ImagePreviewWidget> {
child: ValueListenableBuilder(
valueListenable: FsEntryPreviewCubit.imagePreviewNotifier,
builder: (context, imagePreview, _) {
final filename = imagePreview.filename!;
final contentType = imagePreview.contentType!;
if (imagePreview == null) {
return const SizedBox.shrink();
}

final filename = imagePreview.filename;
final contentType = imagePreview.contentType;
final fileNameWithoutExtension =
getBasenameWithoutExtension(filePath: filename);
return Column(
Expand Down

0 comments on commit d371a60

Please sign in to comment.