forked from immich-app/immich
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(mobile): Use ImmichThumbnail and local thumbnail image provi…
…der (immich-app#7279) * Refactor to use ImmichThumbnail and local thumbnail image provider format * dart format linter errors linter * Adds blurhash format * Fixes image blur * uses hook instead of stateful widget to be more consistent * Uses blurhash hook state * Uses blurhash ref instead of state * Fixes fade in duration for fade in placeholder * Fixes an issue where thumbnails fail to load if too many thumbnail requests are made simultaenously --------- Co-authored-by: Alex Tran <[email protected]>
- Loading branch information
1 parent
5e485e3
commit d76baee
Showing
20 changed files
with
588 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
mobile/lib/modules/asset_viewer/image_providers/immich_local_thumbnail_provider.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import 'dart:async'; | ||
import 'dart:ui' as ui; | ||
|
||
import 'package:cached_network_image/cached_network_image.dart'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/painting.dart'; | ||
import 'package:immich_mobile/shared/models/asset.dart'; | ||
import 'package:photo_manager/photo_manager.dart'; | ||
|
||
/// The local image provider for an asset | ||
/// Only viable | ||
class ImmichLocalThumbnailProvider extends ImageProvider<Asset> { | ||
final Asset asset; | ||
final int height; | ||
final int width; | ||
|
||
ImmichLocalThumbnailProvider({ | ||
required this.asset, | ||
this.height = 256, | ||
this.width = 256, | ||
}) : assert(asset.local != null, 'Only usable when asset.local is set'); | ||
|
||
/// Converts an [ImageProvider]'s settings plus an [ImageConfiguration] to a key | ||
/// that describes the precise image to load. | ||
@override | ||
Future<Asset> obtainKey(ImageConfiguration configuration) { | ||
return SynchronousFuture(asset); | ||
} | ||
|
||
@override | ||
ImageStreamCompleter loadImage(Asset key, ImageDecoderCallback decode) { | ||
final chunkEvents = StreamController<ImageChunkEvent>(); | ||
return MultiImageStreamCompleter( | ||
codec: _codec(key, decode, chunkEvents), | ||
scale: 1.0, | ||
chunkEvents: chunkEvents.stream, | ||
informationCollector: () sync* { | ||
yield ErrorDescription(asset.fileName); | ||
}, | ||
); | ||
} | ||
|
||
// Streams in each stage of the image as we ask for it | ||
Stream<ui.Codec> _codec( | ||
Asset key, | ||
ImageDecoderCallback decode, | ||
StreamController<ImageChunkEvent> chunkEvents, | ||
) async* { | ||
// Load a small thumbnail | ||
final thumbBytes = await asset.local?.thumbnailDataWithSize( | ||
const ThumbnailSize.square(32), | ||
quality: 75, | ||
); | ||
if (thumbBytes != null) { | ||
final buffer = await ui.ImmutableBuffer.fromUint8List(thumbBytes); | ||
final codec = await decode(buffer); | ||
yield codec; | ||
} else { | ||
debugPrint("Loading thumb for ${asset.fileName} failed"); | ||
} | ||
|
||
final normalThumbBytes = | ||
await asset.local?.thumbnailDataWithSize(ThumbnailSize(width, height)); | ||
if (normalThumbBytes == null) { | ||
throw StateError( | ||
"Loading thumb for local photo ${asset.fileName} failed", | ||
); | ||
} | ||
final buffer = await ui.ImmutableBuffer.fromUint8List(normalThumbBytes); | ||
final codec = await decode(buffer); | ||
yield codec; | ||
|
||
chunkEvents.close(); | ||
} | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
if (other is! ImmichLocalThumbnailProvider) return false; | ||
if (identical(this, other)) return true; | ||
return asset == other.asset; | ||
} | ||
|
||
@override | ||
int get hashCode => asset.hashCode; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.