From 17c83be5561fba4b1b20199b0a8113b0f5f90817 Mon Sep 17 00:00:00 2001 From: Marty Fuhry Date: Sun, 12 Feb 2023 16:00:36 -0500 Subject: [PATCH] invalidates cachednetworkimage when new profile photo is uploaded --- .../upload_profile_image.provider.dart | 14 ++++++++---- .../modules/home/ui/home_page_app_bar.dart | 6 ++--- .../profile_drawer/profile_drawer_header.dart | 22 +++++++------------ 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/mobile/lib/modules/home/providers/upload_profile_image.provider.dart b/mobile/lib/modules/home/providers/upload_profile_image.provider.dart index 66060da383..7c6be8e559 100644 --- a/mobile/lib/modules/home/providers/upload_profile_image.provider.dart +++ b/mobile/lib/modules/home/providers/upload_profile_image.provider.dart @@ -1,5 +1,6 @@ import 'dart:convert'; +import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:image_picker/image_picker.dart'; @@ -72,7 +73,7 @@ class UploadProfileImageState { class UploadProfileImageNotifier extends StateNotifier { - UploadProfileImageNotifier(this._userSErvice) + UploadProfileImageNotifier(this._userService) : super( UploadProfileImageState( profileImagePath: '', @@ -80,12 +81,17 @@ class UploadProfileImageNotifier ), ); - final UserService _userSErvice; + final UserService _userService; - Future upload(XFile file) async { + Future upload(XFile file, { + String? invalidateUrl, + }) async { state = state.copyWith(status: UploadProfileStatus.loading); - var res = await _userSErvice.uploadProfileImage(file); + var res = await _userService.uploadProfileImage(file); + if (invalidateUrl != null) { + await CachedNetworkImage.evictFromCache(invalidateUrl); + } if (res != null) { debugPrint("Succesfully upload profile image"); diff --git a/mobile/lib/modules/home/ui/home_page_app_bar.dart b/mobile/lib/modules/home/ui/home_page_app_bar.dart index 71fa50e665..22a8c8798e 100644 --- a/mobile/lib/modules/home/ui/home_page_app_bar.dart +++ b/mobile/lib/modules/home/ui/home_page_app_bar.dart @@ -1,6 +1,7 @@ import 'dart:math'; import 'package:auto_route/auto_route.dart'; +import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; import 'package:hive/hive.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; @@ -47,7 +48,6 @@ class HomePageAppBar extends ConsumerWidget with PreferredSizeWidget { ); } else { String endpoint = Hive.box(userInfoBox).get(serverEndpointKey); - var dummy = Random().nextInt(1024); return InkWell( onTap: () { Scaffold.of(context).openDrawer(); @@ -57,8 +57,8 @@ class HomePageAppBar extends ConsumerWidget with PreferredSizeWidget { radius: 18, child: CircleAvatar( backgroundColor: Theme.of(context).primaryColor.withOpacity(0.1), - backgroundImage: NetworkImage( - '$endpoint/user/profile-image/${authState.userId}?d=${dummy++}', + backgroundImage: CachedNetworkImageProvider( + '$endpoint/user/profile-image/${authState.userId}', ), radius: 17, ), diff --git a/mobile/lib/modules/home/ui/profile_drawer/profile_drawer_header.dart b/mobile/lib/modules/home/ui/profile_drawer/profile_drawer_header.dart index 1a0a830602..df90ad58ce 100644 --- a/mobile/lib/modules/home/ui/profile_drawer/profile_drawer_header.dart +++ b/mobile/lib/modules/home/ui/profile_drawer/profile_drawer_header.dart @@ -1,5 +1,6 @@ import 'dart:math'; +import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:hive_flutter/hive_flutter.dart'; @@ -22,7 +23,6 @@ class ProfileDrawerHeader extends HookConsumerWidget { AuthenticationState authState = ref.watch(authenticationProvider); final uploadProfileImageStatus = ref.watch(uploadProfileImageProvider).status; - var dummy = Random().nextInt(1024); final isDarkMode = Theme.of(context).brightness == Brightness.dark; buildUserProfileImage() { @@ -34,15 +34,16 @@ class ProfileDrawerHeader extends HookConsumerWidget { ); } - if (uploadProfileImageStatus == UploadProfileStatus.idle) { + if (uploadProfileImageStatus == UploadProfileStatus.idle || + uploadProfileImageStatus == UploadProfileStatus.success) { if (authState.profileImagePath.isNotEmpty) { return CircleAvatar( backgroundColor: Theme.of(context).primaryColor, radius: 35, child: CircleAvatar( radius: 34, - backgroundImage: NetworkImage( - '$endpoint/user/profile-image/${authState.userId}?d=${dummy++}', + backgroundImage: CachedNetworkImageProvider( + '$endpoint/user/profile-image/${authState.userId}', ), backgroundColor: Colors.transparent, ), @@ -56,16 +57,6 @@ class ProfileDrawerHeader extends HookConsumerWidget { } } - if (uploadProfileImageStatus == UploadProfileStatus.success) { - return CircleAvatar( - radius: 35, - backgroundImage: NetworkImage( - '$endpoint/user/profile-image/${authState.userId}?d=${dummy++}', - ), - backgroundColor: Colors.transparent, - ); - } - if (uploadProfileImageStatus == UploadProfileStatus.failure) { return const CircleAvatar( radius: 35, @@ -89,6 +80,9 @@ class ProfileDrawerHeader extends HookConsumerWidget { ); if (image != null) { + final url = '$endpoint/user/profile-image/${authState.userId}'; + await CachedNetworkImage.evictFromCache(url); + print('done evicting image'); var success = await ref.watch(uploadProfileImageProvider.notifier).upload(image);