Skip to content

Commit

Permalink
refactor: control play and pause of asset images (#1655)
Browse files Browse the repository at this point in the history
* refactor: control play and pause of asset images

* update
  • Loading branch information
YeungKC authored Sep 23, 2024
1 parent 8584125 commit 7c08b01
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
3 changes: 1 addition & 2 deletions lib/utils/app_lifecycle.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';

final _appObserver = _AppLifecycleObserver();
Expand All @@ -11,7 +10,7 @@ void initAppLifecycleObserver() {
/// Check if app is on foreground.
bool get isAppActive => _appObserver._isActive.value;

ValueListenable<bool> get appActiveListener => _appObserver._isActive;
ValueNotifier<bool> get appActiveListener => _appObserver._isActive;

class _AppLifecycleObserver extends WidgetsBindingObserver {
final _isActive = ValueNotifier<bool>(true);
Expand Down
50 changes: 50 additions & 0 deletions lib/widgets/cache_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ class MixinFileImage extends FileImage {
informationCollector: () => <DiagnosticsNode>[
ErrorDescription('Path: ${file.path}'),
],
controller: controller,
);

Future<ui.Codec> _loadAsync(
Expand Down Expand Up @@ -712,3 +713,52 @@ Future<Uint8List?> downloadImage(String url) async {

/// get md5 from key
String keyToMd5(String key) => md5.convert(utf8.encode(key)).toString();

class MixinAssetImage extends AssetImage {
const MixinAssetImage(
super.assetName, {
super.bundle,
super.package,
this.controller,
});

final ValueNotifier<bool>? controller;

@protected
Future<ui.Codec> _loadAsync(
AssetBundleImageKey key, {
required Future<ui.Codec> Function(ui.ImmutableBuffer buffer) decode,
}) async {
final ui.ImmutableBuffer buffer;
// Hot reload/restart could change whether an asset bundle or key in a
// bundle are available, or if it is a network backed bundle.
try {
buffer = await key.bundle.loadBuffer(key.name);
// ignore: avoid_catching_errors
} on FlutterError {
PaintingBinding.instance.imageCache.evict(key);
rethrow;
}
return decode(buffer);
}

@override
ImageStreamCompleter loadImage(
AssetBundleImageKey key, ImageDecoderCallback decode) {
InformationCollector? collector;
assert(() {
collector = () => <DiagnosticsNode>[
DiagnosticsProperty<ImageProvider>('Image provider', this),
DiagnosticsProperty<AssetBundleImageKey>('Image key', key),
];
return true;
}());
return _MultiFrameImageStreamCompleter(
codec: _loadAsync(key, decode: decode),
scale: key.scale,
debugLabel: key.name,
informationCollector: collector,
controller: controller,
);
}
}
17 changes: 11 additions & 6 deletions lib/widgets/conversation/badges_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'package:flutter_svg/svg.dart';
import 'package:mixin_bot_sdk_dart/mixin_bot_sdk_dart.dart';

import '../../../constants/resources.dart';
import '../../utils/app_lifecycle.dart';
import '../cache_image.dart';

class BadgesWidget extends StatelessWidget {
const BadgesWidget({
Expand All @@ -21,12 +23,15 @@ class BadgesWidget extends StatelessWidget {

switch ((verified, isBot, membership?.isValid, membership?.plan)) {
case (_, _, true, final plan?) when plan != Plan.none:
child = Image.asset(
{
Plan.basic: Resources.assetsImagesPlanBasicPng,
Plan.standard: Resources.assetsImagesPlanStandardPng,
Plan.premium: Resources.assetsImagesPlanPremiumGif,
}[plan]!,
child = Image(
image: MixinAssetImage(
{
Plan.basic: Resources.assetsImagesPlanBasicPng,
Plan.standard: Resources.assetsImagesPlanStandardPng,
Plan.premium: Resources.assetsImagesPlanPremiumGif,
}[plan]!,
controller: appActiveListener,
),
width: 14,
height: 14,
isAntiAlias: true,
Expand Down

0 comments on commit 7c08b01

Please sign in to comment.