-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
artifacts_bloc.dart
130 lines (116 loc) · 4.38 KB
/
artifacts_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
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';
part 'artifacts_bloc.freezed.dart';
part 'artifacts_event.dart';
part 'artifacts_state.dart';
class ArtifactsBloc extends Bloc<ArtifactsEvent, ArtifactsState> {
final GenshinService _genshinService;
ArtifactsBloc(this._genshinService) : super(const ArtifactsState.loading());
_LoadedState get currentState => state as _LoadedState;
@override
Stream<ArtifactsState> mapEventToState(
ArtifactsEvent event,
) async* {
final s = event.map(
init: (e) => _buildInitialState(excludeKeys: e.excludeKeys),
artifactFilterTypeChanged: (e) => currentState.copyWith.call(tempArtifactFilterType: e.artifactFilterType),
rarityChanged: (e) => currentState.copyWith.call(tempRarity: e.rarity),
sortDirectionTypeChanged: (e) => currentState.copyWith.call(tempSortDirectionType: e.sortDirectionType),
searchChanged: (e) => _buildInitialState(
search: e.search,
artifactFilterType: currentState.artifactFilterType,
rarity: currentState.rarity,
sortDirectionType: currentState.sortDirectionType,
),
applyFilterChanges: (_) => _buildInitialState(
search: currentState.search,
artifactFilterType: currentState.tempArtifactFilterType,
rarity: currentState.tempRarity,
sortDirectionType: currentState.tempSortDirectionType,
),
cancelChanges: (_) => currentState.copyWith.call(
tempArtifactFilterType: currentState.artifactFilterType,
tempRarity: currentState.rarity,
tempSortDirectionType: currentState.sortDirectionType,
),
collapseNotes: (e) => currentState.copyWith.call(collapseNotes: e.collapse),
resetFilters: (_) => _buildInitialState(excludeKeys: state.maybeMap(loaded: (state) => state.excludeKeys, orElse: () => [])),
);
yield s;
}
ArtifactsState _buildInitialState({
String? search,
List<String> excludeKeys = const [],
int rarity = 0,
ArtifactFilterType artifactFilterType = ArtifactFilterType.name,
SortDirectionType sortDirectionType = SortDirectionType.asc,
}) {
final isLoaded = state is _LoadedState;
var data = _genshinService.getArtifactsForCard();
if (excludeKeys.isNotEmpty) {
data = data.where((el) => !excludeKeys.contains(el.key)).toList();
}
if (!isLoaded) {
_sortData(data, artifactFilterType, sortDirectionType);
return ArtifactsState.loaded(
artifacts: data,
collapseNotes: false,
search: search,
rarity: rarity,
tempRarity: rarity,
artifactFilterType: artifactFilterType,
tempArtifactFilterType: artifactFilterType,
sortDirectionType: sortDirectionType,
tempSortDirectionType: sortDirectionType,
excludeKeys: excludeKeys,
);
}
if (search != null && search.isNotEmpty) {
data = data.where((el) => el.name.toLowerCase().contains(search.toLowerCase())).toList();
}
if (rarity > 0) {
data = data.where((el) => el.rarity == rarity).toList();
}
_sortData(data, artifactFilterType, sortDirectionType);
final s = currentState.copyWith.call(
artifacts: data,
search: search,
rarity: rarity,
tempRarity: rarity,
artifactFilterType: artifactFilterType,
tempArtifactFilterType: artifactFilterType,
sortDirectionType: sortDirectionType,
tempSortDirectionType: sortDirectionType,
excludeKeys: excludeKeys,
);
return s;
}
void _sortData(
List<ArtifactCardModel> data,
ArtifactFilterType artifactFilterType,
SortDirectionType sortDirectionType,
) {
switch (artifactFilterType) {
case ArtifactFilterType.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 ArtifactFilterType.rarity:
if (sortDirectionType == SortDirectionType.asc) {
data.sort((x, y) => x.rarity.compareTo(y.rarity));
} else {
data.sort((x, y) => y.rarity.compareTo(x.rarity));
}
break;
default:
break;
}
}
}