diff --git a/lib/infrastructure/data_service.dart b/lib/infrastructure/data_service.dart index 74636a81e..7f7544655 100644 --- a/lib/infrastructure/data_service.dart +++ b/lib/infrastructure/data_service.dart @@ -29,6 +29,7 @@ class DataServiceImpl implements DataService { late Box _gameCodesBox; late Box _gameCodeRewardsBox; late Box _tierListBox; + late Box _customBuildsBox; late Box _notificationsCustomBox; late Box _notificationsExpeditionBox; @@ -67,6 +68,7 @@ class DataServiceImpl implements DataService { _gameCodesBox = await Hive.openBox('gameCodes'); _gameCodeRewardsBox = await Hive.openBox('gameCodeRewards'); _tierListBox = await Hive.openBox('tierList'); + _customBuildsBox = await Hive.openBox('customBuilds'); _notificationsCustomBox = await Hive.openBox('notificationsCustom'); _notificationsExpeditionBox = await Hive.openBox('notificationsExpedition'); @@ -1237,6 +1239,71 @@ class DataServiceImpl implements DataService { return _updateNotification(item, item.title, item.body, item.note, item.showNotification); } + @override + List getAllCustomBuilds() { + return _customBuildsBox.values.map(_mapToCustomBuildModel).toList()..sort((x, y) => x.character.name.compareTo(y.character.name)); + } + + @override + CustomBuildModel getCustomBuild(int key) { + final build = _customBuildsBox.values.firstWhere((e) => e.key == key); + return _mapToCustomBuildModel(build); + } + + @override + Future saveCustomBuild( + String charKey, + String title, + CharacterRoleType type, + CharacterRoleSubType subType, + bool showOnCharacterDetail, + List weaponKeys, + Map artifacts, + List talentPriority, + ) async { + final build = CustomBuild( + charKey, + showOnCharacterDetail, + title, + type.index, + subType.index, + weaponKeys, + artifacts, + talentPriority.map((e) => e.index).toList(), + ); + await _customBuildsBox.add(build); + return _mapToCustomBuildModel(build); + } + + @override + Future updateCustomBuild( + int key, + String title, + CharacterRoleType type, + CharacterRoleSubType subType, + bool showOnCharacterDetail, + List weaponKeys, + Map artifacts, + List talentPriority, + ) async { + final build = _customBuildsBox.get(key)!; + build.title = title; + build.roleType = type.index; + build.roleSubType = subType.index; + build.showOnCharacterDetail = showOnCharacterDetail; + build.weaponKeys = weaponKeys; + build.artifacts = artifacts; + build.talentPriority = talentPriority.map((e) => e.index).toList(); + + await build.save(); + return _mapToCustomBuildModel(build); + } + + @override + Future deleteCustomBuild(int key) async { + await _customBuildsBox.delete(key); + } + void _registerAdapters() { Hive.registerAdapter(CalculatorCharacterSkillAdapter()); Hive.registerAdapter(CalculatorItemAdapter()); @@ -1255,6 +1322,7 @@ class DataServiceImpl implements DataService { Hive.registerAdapter(NotificationRealmCurrencyAdapter()); Hive.registerAdapter(NotificationResinAdapter()); Hive.registerAdapter(NotificationWeeklyBossAdapter()); + Hive.registerAdapter(CustomBuildAdapter()); } ItemAscensionMaterials _buildForCharacter(CalculatorItem item, {int? calculatorItemKey, bool includeInventory = false}) { @@ -1566,4 +1634,20 @@ class DataServiceImpl implements DataService { await hiveObject.save(); return getNotification(hiveObject.key as int, AppNotificationType.values[notification.type]); } + + CustomBuildModel _mapToCustomBuildModel(CustomBuild build) { + final character = _genshinService.getCharacterForCard(build.characterKey); + final weapons = build.weaponKeys.map((e) => _genshinService.getWeaponForCard(e)).toList(); + // final artifacts = build.artifactKeys.map((e) => _genshinService.getArtifactForCard(e)).toList(); + return CustomBuildModel( + key: build.key as int, + title: build.title, + type: CharacterRoleType.values[build.roleType], + subType: CharacterRoleSubType.values[build.roleSubType], + showOnCharacterDetail: build.showOnCharacterDetail, + character: character, + weapons: weapons, + artifacts: [], + ); + } } diff --git a/lib/infrastructure/genshin_service.dart b/lib/infrastructure/genshin_service.dart index 4687feb2a..0839e4f88 100644 --- a/lib/infrastructure/genshin_service.dart +++ b/lib/infrastructure/genshin_service.dart @@ -204,8 +204,14 @@ class GenshinServiceImpl implements GenshinService { } @override - List getArtifactsForCard() { - return _artifactsFile.artifacts.map((e) => _toArtifactForCard(e)).toList(); + List getArtifactsForCard({ArtifactType? type}) { + return _artifactsFile.artifacts.map((e) => _toArtifactForCard(e, type: type)).where((e) { + //if a type was provided and it is different that crown, then return only the ones with more than 1 bonus + if (type != null && type != ArtifactType.crown) { + return e.bonus.length > 1; + } + return true; + }).toList(); } @override @@ -757,6 +763,21 @@ class GenshinServiceImpl implements GenshinService { return bonus == 1 ? [fullImagePath] : artifactOrder.map((e) => Assets.getArtifactPath('$imageWithoutExt$e.png')).toList(); } + @override + String getArtifactRelatedPart(String fullImagePath, String image, int bonus, ArtifactType type) { + if (bonus == 1 && type != ArtifactType.crown) { + throw Exception('Invalid artifact type'); + } + + if (bonus == 1) { + return fullImagePath; + } + + final imgs = getArtifactRelatedParts(fullImagePath, image, bonus); + final order = getArtifactOrder(type); + return imgs.firstWhere((el) => el.endsWith('$order.png')); + } + CharacterCardModel _toCharacterForCard(CharacterFileModel character) { final translation = getCharacterTranslation(character.key); @@ -809,16 +830,24 @@ class GenshinServiceImpl implements GenshinService { ); } - ArtifactCardModel _toArtifactForCard(ArtifactFileModel artifact) { + ArtifactCardModel _toArtifactForCard(ArtifactFileModel artifact, {ArtifactType? type}) { final translation = _translationFile.artifacts.firstWhere((t) => t.key == artifact.key); final bonus = getArtifactBonus(translation); - return ArtifactCardModel( + final mapped = ArtifactCardModel( key: artifact.key, name: translation.name, image: artifact.fullImagePath, rarity: artifact.maxRarity, bonus: bonus, ); + + //only search for other images if the artifact has more than 1 bonus effect + if (type != null && bonus.length > 1) { + final img = getArtifactRelatedPart(artifact.fullImagePath, artifact.image, bonus.length, type); + return mapped.copyWith.call(image: img); + } + + return mapped; } MaterialCardModel _toMaterialForCard(MaterialFileModel material) {