invalidates cachednetworkimage when new profile photo is uploaded

This commit is contained in:
Marty Fuhry 2023-02-12 16:00:36 -05:00
parent 432c2b9cda
commit 17c83be556
3 changed files with 21 additions and 21 deletions

View file

@ -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<UploadProfileImageState> {
UploadProfileImageNotifier(this._userSErvice)
UploadProfileImageNotifier(this._userService)
: super(
UploadProfileImageState(
profileImagePath: '',
@ -80,12 +81,17 @@ class UploadProfileImageNotifier
),
);
final UserService _userSErvice;
final UserService _userService;
Future<bool> upload(XFile file) async {
Future<bool> 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");

View file

@ -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,
),

View file

@ -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);