-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
character_bloc.dart
126 lines (117 loc) · 4.9 KB
/
character_bloc.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:genshindb/domain/assets.dart';
import 'package:genshindb/domain/enums/enums.dart';
import 'package:genshindb/domain/models/models.dart';
import 'package:genshindb/domain/services/genshin_service.dart';
import 'package:genshindb/domain/services/locale_service.dart';
import 'package:genshindb/domain/services/telemetry_service.dart';
part 'character_bloc.freezed.dart';
part 'character_event.dart';
part 'character_state.dart';
class CharacterBloc extends Bloc<CharacterEvent, CharacterState> {
final GenshinService _genshinService;
final TelemetryService _telemetryService;
final LocaleService _localeService;
CharacterBloc(this._genshinService, this._telemetryService, this._localeService) : super(const CharacterState.loading());
@override
Stream<CharacterState> mapEventToState(
CharacterEvent event,
) async* {
yield const CharacterState.loading();
final s = await event.when(
loadFromName: (name) async {
await _telemetryService.trackCharacterLoaded(name);
final char = _genshinService.getCharacter(name);
final translation = _genshinService.getCharacterTranslation(name);
return _buildInitialState(char, translation);
},
loadFromImg: (img) async {
await _telemetryService.trackCharacterLoaded(img, loadedFromName: false);
final char = _genshinService.getCharacterByImg(img);
final translation = _genshinService.getCharacterTranslation(char.key);
return _buildInitialState(char, translation);
},
);
yield s;
}
CharacterState _buildInitialState(CharacterFileModel char, TranslationCharacterFile translation) {
return CharacterState.loaded(
name: translation.name,
region: char.region,
role: char.role,
isFemale: char.isFemale,
fullImage: Assets.getCharacterFullPath(char.fullImage),
secondFullImage: char.secondFullImage != null ? Assets.getCharacterFullPath(char.secondFullImage) : null,
description: translation.description,
rarity: char.rarity,
birthday: _localeService.formatCharBirthDate(char.birthday),
elementType: char.elementType,
weaponType: char.weaponType,
ascensionMaterials: char.ascensionMaterials,
talentAscensionsMaterials: char.talentAscensionMaterials,
skills: translation.skills.map((e) {
final skill = char.skills.firstWhere((s) => s.key == e.key);
final abilities = e.abilities.map((a) {
final type =
skill.abilities != null && skill.abilities.any((x) => x.key == a.key) ? skill.abilities.firstWhere((x) => x.key == a.key).type : null;
return CharacterSkillAbilityModel(
type: type,
name: a.name,
description: a.description,
descriptions: a.descriptions,
secondDescription: a.secondDescription,
);
}).toList();
return CharacterSkillCardModel(
image: skill.fullImagePath,
title: e.title,
type: skill.type,
abilities: abilities,
description: e.description,
);
}).toList(),
passives: translation.passives.map((e) {
final passive = char.passives.firstWhere((p) => p.key == e.key);
return CharacterPassiveTalentModel(
unlockedAt: passive.unlockedAt,
image: passive.fullImagePath,
title: e.title,
description: e.description,
descriptions: e.descriptions,
);
}).toList(),
constellations: translation.constellations.map((e) {
final constellation = char.constellations.firstWhere((c) => c.key == e.key);
return CharacterConstellationModel(
number: constellation.number,
image: constellation.fullImagePath,
title: e.title,
description: e.description,
secondDescription: e.secondDescription,
descriptions: e.descriptions,
);
}).toList(),
multiTalentAscensionMaterials: char.multiTalentAscensionMaterials,
builds: char.builds.map((build) {
return CharacterBuildCardModel(
isForSupport: build.isSupport,
weapons: build.weaponImages.map((e) => _genshinService.getWeaponForCardByImg(e)).toList(),
artifacts: build.artifacts.map(
(e) {
final one = e.one != null ? _genshinService.getArtifactForCardByImg(e.one) : null;
final multiples = e.multiples
.map((m) => CharacterBuildMultipleArtifactModel(
quantity: m.quantity,
artifact: _genshinService.getArtifactForCardByImg(m.image),
))
.toList();
return CharacterBuildArtifactModel(one: one, multiples: multiples, stats: e.stats);
},
).toList(),
);
}).toList(),
);
}
}