-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
characters_bloc.dart
202 lines (185 loc) · 7.42 KB
/
characters_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.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/settings_service.dart';
part 'characters_bloc.freezed.dart';
part 'characters_event.dart';
part 'characters_state.dart';
class CharactersBloc extends Bloc<CharactersEvent, CharactersState> {
final GenshinService _genshinService;
final SettingsService _settingsService;
CharactersBloc(this._genshinService, this._settingsService) : super(const CharactersState.loading());
_LoadedState get currentState => state as _LoadedState;
@override
Stream<CharactersState> mapEventToState(
CharactersEvent event,
) async* {
final s = event.map(
init: (_) => _buildInitialState(elementTypes: ElementType.values, weaponTypes: WeaponType.values),
characterFilterTypeChanged: (e) => currentState.copyWith.call(tempCharacterFilterType: e.characterFilterType),
elementTypeChanged: (e) {
var types = <ElementType>[];
if (currentState.tempElementTypes.contains(e.elementType)) {
types = currentState.tempElementTypes.where((t) => t != e.elementType).toList();
} else {
types = currentState.tempElementTypes + [e.elementType];
}
return currentState.copyWith.call(tempElementTypes: types);
},
rarityChanged: (e) => currentState.copyWith.call(tempRarity: e.rarity),
itemStatusChanged: (e) => currentState.copyWith.call(tempStatusType: e.statusType),
sortDirectionTypeChanged: (e) => currentState.copyWith.call(tempSortDirectionType: e.sortDirectionType),
weaponTypeChanged: (e) {
var types = <WeaponType>[];
if (currentState.tempWeaponTypes.contains(e.weaponType)) {
types = currentState.tempWeaponTypes.where((t) => t != e.weaponType).toList();
} else {
types = currentState.tempWeaponTypes + [e.weaponType];
}
return currentState.copyWith.call(tempWeaponTypes: types);
},
roleTypeChanged: (e) => currentState.copyWith.call(tempRoleType: e.roleType),
searchChanged: (e) => _buildInitialState(
search: e.search,
characterFilterType: currentState.characterFilterType,
elementTypes: currentState.elementTypes,
rarity: currentState.rarity,
statusType: currentState.statusType,
sortDirectionType: currentState.sortDirectionType,
weaponTypes: currentState.weaponTypes,
roleType: currentState.tempRoleType,
),
applyFilterChanges: (_) => _buildInitialState(
search: currentState.search,
characterFilterType: currentState.tempCharacterFilterType,
elementTypes: currentState.tempElementTypes,
rarity: currentState.tempRarity,
statusType: currentState.tempStatusType,
sortDirectionType: currentState.tempSortDirectionType,
weaponTypes: currentState.tempWeaponTypes,
roleType: currentState.tempRoleType,
),
cancelChanges: (_) => currentState.copyWith.call(
tempCharacterFilterType: currentState.characterFilterType,
tempElementTypes: currentState.elementTypes,
tempRarity: currentState.rarity,
tempStatusType: currentState.statusType,
tempSortDirectionType: currentState.sortDirectionType,
tempWeaponTypes: currentState.weaponTypes,
tempRoleType: currentState.roleType,
),
);
yield s;
}
//TODO: FALTA UN DELAY EN EL SEARCH
CharactersState _buildInitialState({
String search,
List<WeaponType> weaponTypes = const [],
List<ElementType> elementTypes = const [],
int rarity = 0,
ItemStatusType statusType = ItemStatusType.all,
CharacterFilterType characterFilterType = CharacterFilterType.name,
SortDirectionType sortDirectionType = SortDirectionType.asc,
CharacterRoleType roleType = CharacterRoleType.all,
}) {
final isLoaded = state is _LoadedState;
var characters = _genshinService.getCharactersForCard();
if (!isLoaded) {
final selectedWeaponTypes = WeaponType.values.toList();
final selectedElementTypes = ElementType.values.toList();
_sortData(characters, characterFilterType, sortDirectionType);
return CharactersState.loaded(
characters: characters,
search: search,
weaponTypes: selectedWeaponTypes,
tempWeaponTypes: selectedWeaponTypes,
elementTypes: selectedElementTypes,
tempElementTypes: selectedElementTypes,
rarity: rarity,
tempRarity: rarity,
statusType: statusType,
tempStatusType: statusType,
characterFilterType: characterFilterType,
tempCharacterFilterType: characterFilterType,
sortDirectionType: sortDirectionType,
tempSortDirectionType: sortDirectionType,
showCharacterDetails: _settingsService.showCharacterDetails,
roleType: roleType,
tempRoleType: roleType,
);
}
if (search != null && search.isNotEmpty) {
characters = characters.where((el) => el.name.toLowerCase().contains(search.toLowerCase())).toList();
}
if (rarity > 0) {
characters = characters.where((el) => el.stars == rarity).toList();
}
if (weaponTypes.isNotEmpty) {
characters = characters.where((el) => weaponTypes.contains(el.weaponType)).toList();
}
if (elementTypes.isNotEmpty) {
characters = characters.where((el) => elementTypes.contains(el.elementType)).toList();
}
if (roleType != CharacterRoleType.all) {
characters = characters.where((el) => el.roleType == roleType).toList();
}
switch (statusType) {
case ItemStatusType.released:
characters = characters.where((el) => !el.isComingSoon).toList();
break;
case ItemStatusType.comingSoon:
characters = characters.where((el) => el.isComingSoon).toList();
break;
case ItemStatusType.newItem:
characters = characters.where((el) => el.isNew).toList();
break;
default:
break;
}
_sortData(characters, characterFilterType, sortDirectionType);
final s = currentState.copyWith.call(
characters: characters,
search: search,
weaponTypes: weaponTypes,
tempWeaponTypes: weaponTypes,
elementTypes: elementTypes,
tempElementTypes: elementTypes,
rarity: rarity,
tempRarity: rarity,
statusType: statusType,
tempStatusType: statusType,
characterFilterType: characterFilterType,
tempCharacterFilterType: characterFilterType,
sortDirectionType: sortDirectionType,
tempSortDirectionType: sortDirectionType,
);
return s;
}
void _sortData(
List<CharacterCardModel> data,
CharacterFilterType characterFilterType,
SortDirectionType sortDirectionType,
) {
switch (characterFilterType) {
case CharacterFilterType.name:
if (sortDirectionType == SortDirectionType.asc) {
data.sort((x, y) => x.name.compareTo(y.name));
} else {
data.sort((x, y) => y.name.compareTo(x.name));
}
break;
case CharacterFilterType.rarity:
if (sortDirectionType == SortDirectionType.asc) {
data.sort((x, y) => x.stars.compareTo(y.stars));
} else {
data.sort((x, y) => y.stars.compareTo(x.stars));
}
break;
default:
break;
}
}
}