Skip to content

Commit

Permalink
fixing player avatars not saving (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkieres authored Nov 26, 2022
1 parent 98b79bf commit eb65303
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions board_games_companion/lib/services/player_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ class PlayerService extends BaseHiveService<Player, PlayerService> {
}

if (player.avatarFileToSave != null) {
final File? savedAvatarImage = await saveAvatar(player);
if (savedAvatarImage == null) {
return false;
}
player = await saveAvatar(player);
}

final existingPlayer = storageBox.get(player.id);
Expand Down Expand Up @@ -90,9 +87,9 @@ class PlayerService extends BaseHiveService<Player, PlayerService> {
return true;
}

Future<File?> saveAvatar(Player player) async {
Future<Player> saveAvatar(Player player) async {
if (player.avatarFileToSave == null) {
return null;
return player;
}

final avatarImageName = const Uuid().v4();
Expand All @@ -102,9 +99,17 @@ class PlayerService extends BaseHiveService<Player, PlayerService> {
_fileExtensionRegex.firstMatch(player.avatarFileToSave!.path)!.group(0);
}

player = player.copyWith(avatarFileName: '$avatarImageName$avatarImageNameFileExtension');
final avatarFileName = '$avatarImageName$avatarImageNameFileExtension';
final File? savedAvatarImage = await fileService.saveToDocumentsDirectory(
avatarFileName,
player.avatarFileToSave!,
);

if (savedAvatarImage == null) {
return player;
}

return fileService.saveToDocumentsDirectory(player.avatarFileName!, player.avatarFileToSave!);
return player.copyWith(avatarFileName: avatarFileName);
}

Future<bool> deleteAvatar(String avatarFileName) async {
Expand Down

0 comments on commit eb65303

Please sign in to comment.