From 30011f47613805975fcaef35caa613e3c1bbc116 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Mon, 21 Jun 2021 13:13:22 +0200 Subject: [PATCH] refactor: Use flutter cache manager for avatar push cashing --- lib/utils/background_push.dart | 43 ++++++++-------------------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/lib/utils/background_push.dart b/lib/utils/background_push.dart index d4970c6a..3f2c12c3 100644 --- a/lib/utils/background_push.dart +++ b/lib/utils/background_push.dart @@ -22,17 +22,16 @@ import 'dart:convert'; import 'dart:io'; import 'dart:ui'; +import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:matrix/matrix.dart'; import 'package:fcm_shared_isolate/fcm_shared_isolate.dart'; -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:unifiedpush/unifiedpush.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n_en.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; -import 'package:path_provider/path_provider.dart'; import 'package:vrouter/vrouter.dart'; import 'platform_infos.dart'; import '../config/app_config.dart'; @@ -616,15 +615,18 @@ class BackgroundPush { // The person object for the android message style notification final avatar = room.avatar == null ? null - : await downloadAndSaveAvatar( - room.avatar, - client, - width: 126, - height: 126, + : await DefaultCacheManager().getSingleFile( + event.room.avatar + .getThumbnail( + client, + width: 126, + height: 126, + ) + .toString(), ); final person = Person( name: room.getLocalizedDisplayname(MatrixLocals(l10n)), - icon: avatar == null ? null : BitmapFilePathAndroidIcon(avatar), + icon: avatar == null ? null : BitmapFilePathAndroidIcon(avatar.path), ); // Show notification @@ -693,31 +695,6 @@ class BackgroundPush { } } - Future downloadAndSaveAvatar(Uri content, Client client, - {int width, int height}) async { - final thumbnail = width == null && height == null ? false : true; - final tempDirectory = (await getTemporaryDirectory()).path; - final prefix = thumbnail ? 'thumbnail' : ''; - final file = - File('$tempDirectory/${prefix}_${content.toString().split("/").last}'); - - if (!file.existsSync()) { - final url = thumbnail - ? content.getThumbnail(client, width: width, height: height) - : content.getDownloadLink(client); - final request = await HttpClient().getUrl(url); - final response = await request.close(); - if (response.statusCode >= 300) { - // we are not in the 2xx range - return null; - } - final bytes = await consolidateHttpClientResponseBytes(response); - await file.writeAsBytes(bytes); - } - - return file.path; - } - AndroidNotificationDetails _getAndroidNotificationDetails( {MessagingStyleInformation styleInformation, String ticker}) { final color = (context != null ? Theme.of(context).primaryColor : null) ??