Skip to content

Commit

Permalink
Merge pull request #773 from Baseflow/bugfix/use-loadbuffer
Browse files Browse the repository at this point in the history
use loadbuffer and deprecate load
  • Loading branch information
renefloor authored Aug 31, 2022
2 parents 607a2eb + f611c96 commit bdbfe33
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>9.0</string>
<string>11.0</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion cached_network_image/example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Uncomment this line to define a global platform for your project
platform :ios, '9.0'
platform :ios, '11.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down Expand Up @@ -417,7 +417,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -466,7 +466,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down
5 changes: 3 additions & 2 deletions cached_network_image/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: example
description: A new Flutter project.

environment:
sdk: ">=2.17.0 <3.0.0"
sdk: ">=2.18.0 <3.0.0"
flutter: ">=3.3.0"

dependencies:
flutter:
Expand All @@ -12,7 +13,7 @@ dependencies:
baseflow_plugin_template:
git:
url: https://github.com/Baseflow/baseflow_plugin_template.git
ref: v2.1.0
ref: 922aa52773308bec29f0cfa2661b43ecdedd50c5

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down
64 changes: 64 additions & 0 deletions cached_network_image/lib/src/image_provider/_image_loader.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'dart:ui';

import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart';
import 'package:flutter/material.dart';
Expand All @@ -14,6 +16,7 @@ import 'package:cached_network_image_platform_interface'

/// ImageLoader class to load images on IO platforms.
class ImageLoader implements platform.ImageLoader {
@Deprecated('use loadBufferAsync instead')
@override
Stream<ui.Codec> loadAsync(
String url,
Expand All @@ -27,6 +30,65 @@ class ImageLoader implements platform.ImageLoader {
Function()? errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb,
Function() evictImage,
) {
return _load(
url,
cacheKey,
chunkEvents,
decode,
cacheManager,
maxHeight,
maxWidth,
headers,
errorListener,
imageRenderMethodForWeb,
evictImage,
);
}

@override
Stream<ui.Codec> loadBufferAsync(
String url,
String? cacheKey,
StreamController<ImageChunkEvent> chunkEvents,
DecoderBufferCallback decode,
BaseCacheManager cacheManager,
int? maxHeight,
int? maxWidth,
Map<String, String>? headers,
Function()? errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb,
Function() evictImage) {
return _load(
url,
cacheKey,
chunkEvents,
(bytes) async {
final buffer = await ImmutableBuffer.fromUint8List(bytes);
return decode(buffer);
},
cacheManager,
maxHeight,
maxWidth,
headers,
errorListener,
imageRenderMethodForWeb,
evictImage,
);
}

Stream<ui.Codec> _load(
String url,
String? cacheKey,
StreamController<ImageChunkEvent> chunkEvents,
_FileDecoderCallback decode,
BaseCacheManager cacheManager,
int? maxHeight,
int? maxWidth,
Map<String, String>? headers,
Function()? errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb,
Function() evictImage,
) async* {
try {
assert(
Expand Down Expand Up @@ -75,3 +137,5 @@ class ImageLoader implements platform.ImageLoader {
}
}
}

typedef _FileDecoderCallback = Future<ui.Codec> Function(Uint8List);
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class CachedNetworkImageProvider
return SynchronousFuture<CachedNetworkImageProvider>(this);
}

@Deprecated(
'load is deprecated, use loadBuffer instead, see https://docs.flutter.dev/release/breaking-changes/image-provider-load-buffer')
@override
ImageStreamCompleter load(
image_provider.CachedNetworkImageProvider key, DecoderCallback decode) {
Expand All @@ -89,6 +91,8 @@ class CachedNetworkImageProvider
);
}

@Deprecated(
'_loadAsync is deprecated, use loadBuffer instead, see https://docs.flutter.dev/release/breaking-changes/image-provider-load-buffer')
Stream<ui.Codec> _loadAsync(
image_provider.CachedNetworkImageProvider key,
StreamController<ImageChunkEvent> chunkEvents,
Expand All @@ -110,6 +114,45 @@ class CachedNetworkImageProvider
);
}

@override
ImageStreamCompleter loadBuffer(image_provider.CachedNetworkImageProvider key,
DecoderBufferCallback decode) {
final chunkEvents = StreamController<ImageChunkEvent>();
return MultiImageStreamCompleter(
codec: _loadBufferAsync(key, chunkEvents, decode),
chunkEvents: chunkEvents.stream,
scale: key.scale,
informationCollector: () sync* {
yield DiagnosticsProperty<ImageProvider>(
'Image provider: $this \n Image key: $key',
this,
style: DiagnosticsTreeStyle.errorProperty,
);
},
);
}

Stream<ui.Codec> _loadBufferAsync(
image_provider.CachedNetworkImageProvider key,
StreamController<ImageChunkEvent> chunkEvents,
DecoderBufferCallback decode,
) {
assert(key == this);
return ImageLoader().loadBufferAsync(
url,
cacheKey,
chunkEvents,
decode,
cacheManager ?? DefaultCacheManager(),
maxHeight,
maxWidth,
headers,
errorListener,
imageRenderMethodForWeb,
() => PaintingBinding.instance.imageCache.evict(key),
);
}

@override
bool operator ==(dynamic other) {
if (other is CachedNetworkImageProvider) {
Expand All @@ -122,7 +165,7 @@ class CachedNetworkImageProvider
}

@override
int get hashCode => hashValues(cacheKey ?? url, scale, maxHeight, maxWidth);
int get hashCode => Object.hash(cacheKey ?? url, scale, maxHeight, maxWidth);

@override
String toString() => '$runtimeType("$url", scale: $scale)';
Expand Down
8 changes: 4 additions & 4 deletions cached_network_image/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ dependencies:
sdk: flutter
flutter_cache_manager: ^3.3.0
octo_image: ^1.0.0
cached_network_image_platform_interface: ^1.0.0
cached_network_image_web: ^1.0.0
cached_network_image_platform_interface: ^2.0.0
cached_network_image_web: ^1.0.2


dev_dependencies:
Expand All @@ -21,5 +21,5 @@ dev_dependencies:
file: ^6.1.0

environment:
flutter: ">=3.0.0"
sdk: ">=2.17.0 <3.0.0"
flutter: ">=3.3.0"
sdk: ">=2.18.0 <3.0.0"
3 changes: 3 additions & 0 deletions cached_network_image_platform_interface/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
## [2.0.0] - 2022-08-31
* Added loadBufferAsync for Flutter 3.3

## [1.0.0] - 2021-07-16
* Initial release
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum ImageRenderMethodForWeb {
class ImageLoader {
/// loads the images async and gives the resulted codecs on a Stream. The
/// Stream gives the option to show multiple images after each other.
@Deprecated('use loadBufferAsync instead')
Stream<ui.Codec> loadAsync(
String url,
String? cacheKey,
Expand All @@ -36,4 +37,22 @@ class ImageLoader {
) {
throw UnimplementedError();
}

/// loads the images async and gives the resulted codecs on a Stream. The
/// Stream gives the option to show multiple images after each other.
Stream<ui.Codec> loadBufferAsync(
String url,
String? cacheKey,
StreamController<ImageChunkEvent> chunkEvents,
DecoderBufferCallback decode,
BaseCacheManager cacheManager,
int? maxHeight,
int? maxWidth,
Map<String, String>? headers,
Function()? errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb,
Function() evictImage,
) {
throw UnimplementedError();
}
}
6 changes: 3 additions & 3 deletions cached_network_image_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: cached_network_image_platform_interface
description: Platform interface for CachedNetworkImage
version: 1.0.0
version: 2.0.0
homepage: https://github.com/Baseflow/flutter_cached_network_image

environment:
sdk: ">=2.12.0 <3.0.0"
flutter: ">=1.17.0"
sdk: ">=2.18.0 <3.0.0"
flutter: ">=3.3.0"

dependencies:
flutter:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;
Expand Down
3 changes: 3 additions & 0 deletions cached_network_image_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [1.0.2] - 2022-08-31
* Added loadBufferAsync and deprecated loadAsync

## [1.0.1] - 2021-08-02
* Bug: fixed CORS issues in HTML image version.

Expand Down
Loading

0 comments on commit bdbfe33

Please sign in to comment.