nc-photos/app/lib/mobile/android/content_uri_image_provider.dart

66 lines
1.7 KiB
Dart
Raw Normal View History

2022-11-13 10:49:36 +01:00
import 'dart:ui' as ui show Codec, ImmutableBuffer;
import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:nc_photos_plugin/nc_photos_plugin.dart';
2022-12-08 16:39:13 +01:00
import 'package:to_string/to_string.dart';
2022-12-08 16:39:13 +01:00
part 'content_uri_image_provider.g.dart';
@toString
class ContentUriImage extends ImageProvider<ContentUriImage>
with EquatableMixin {
/// Creates an object that decodes a content Uri as an image.
const ContentUriImage(
this.uri, {
this.scale = 1.0,
});
@override
obtainKey(ImageConfiguration configuration) {
return SynchronousFuture<ContentUriImage>(this);
}
@override
2022-11-13 10:49:36 +01:00
ImageStreamCompleter loadBuffer(
ContentUriImage key, DecoderBufferCallback decode) {
return MultiFrameImageStreamCompleter(
codec: _loadAsync(key, decode),
scale: key.scale,
debugLabel: key.uri,
informationCollector: () => <DiagnosticsNode>[
ErrorDescription("Content uri: $uri"),
],
);
}
Future<ui.Codec> _loadAsync(
2022-11-13 10:49:36 +01:00
ContentUriImage key, DecoderBufferCallback decode) async {
assert(key == this);
final bytes = await ContentUri.readUri(uri);
if (bytes.lengthInBytes == 0) {
// The file may become available later.
2022-06-20 13:49:58 +02:00
PaintingBinding.instance.imageCache.evict(key);
throw StateError("$uri is empty and cannot be loaded as an image.");
}
2022-11-13 10:49:36 +01:00
final ui.ImmutableBuffer buffer =
await ui.ImmutableBuffer.fromUint8List(bytes);
return decode(buffer);
}
@override
get props => [
uri,
scale,
];
@override
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
final String uri;
/// The scale to place in the [ImageInfo] object of the image.
final double scale;
}