-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
calculator_asc_materials_item_bloc.dart
132 lines (107 loc) · 3.9 KB
/
calculator_asc_materials_item_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
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import '../../common/assets.dart';
import '../../models/models.dart';
import '../../services/genshing_service.dart';
part 'calculator_asc_materials_item_bloc.freezed.dart';
part 'calculator_asc_materials_item_event.dart';
part 'calculator_asc_materials_item_state.dart';
class CalculatorAscMaterialsItemBloc extends Bloc<CalculatorAscMaterialsItemEvent, CalculatorAscMaterialsItemState> {
final GenshinService _genshinService;
static int minSkillLevel = 1;
static int maxSkillLevel = 10;
static int minAscensionLevel = 1;
static int maxAscensionLevel = 6;
_LoadedState get currentState => state as _LoadedState;
CalculatorAscMaterialsItemBloc(this._genshinService) : super(const CalculatorAscMaterialsItemState.loading());
@override
Stream<CalculatorAscMaterialsItemState> mapEventToState(
CalculatorAscMaterialsItemEvent event,
) async* {
if (event is _Init) {
yield const CalculatorAscMaterialsItemState.loading();
}
final s = event.map(
load: (e) {
if (e.isCharacter) {
final char = _genshinService.getCharacter(e.key);
final translation = _genshinService.getCharacterTranslation(e.key);
return CalculatorAscMaterialsItemState.loaded(
name: translation.name,
imageFullPath: Assets.getCharacterPath(char.image),
currentLevel: minAscensionLevel,
desiredLevel: maxAscensionLevel,
skills: translation.skills
.map(
(e) => CharacterSkill.skill(name: e.title, currentLevel: minSkillLevel, desiredLevel: maxSkillLevel),
)
.toList(),
);
}
final weapon = _genshinService.getWeapon(e.key);
final translation = _genshinService.getWeaponTranslation(e.key);
return CalculatorAscMaterialsItemState.loaded(
name: translation.name,
imageFullPath: weapon.fullImagePath,
currentLevel: minAscensionLevel,
desiredLevel: maxAscensionLevel,
);
},
currentLevelChanged: (e) {
return _ascensionChanged(e.newValue, currentState.desiredLevel);
},
desiredLevelChanged: (e) {
return _ascensionChanged(currentState.currentLevel, e.newValue);
},
skillCurrentLevelChanged: (e) {
return _skillChanged(e.index, e.newValue, true);
},
skillDesiredLevelChanged: (e) {
return _skillChanged(e.index, e.newValue, false);
},
);
yield s;
}
CalculatorAscMaterialsItemState _ascensionChanged(int currentLevel, int desiredLevel) {
var cl = currentLevel;
var dl = desiredLevel;
if (cl > dl) {
dl = cl;
} else if (dl < cl) {
cl = dl;
}
if (cl > maxAscensionLevel || cl < minAscensionLevel) {
return currentState;
}
if (dl > maxAscensionLevel || dl < minAscensionLevel) {
return currentState;
}
return currentState.copyWith.call(currentLevel: cl, desiredLevel: dl);
}
CalculatorAscMaterialsItemState _skillChanged(int skillIndex, int newValue, bool currentChanged) {
final skills = <CharacterSkill>[];
for (var i = 0; i < currentState.skills.length; i++) {
final item = currentState.skills[i];
if (i != skillIndex) {
skills.add(item);
continue;
}
var cl = currentChanged ? newValue : item.currentLevel;
var dl = currentChanged ? item.desiredLevel : newValue;
if (cl > dl) {
dl = cl;
} else if (dl < cl) {
cl = dl;
}
if (cl > maxSkillLevel || cl < minSkillLevel) {
return currentState;
}
if (dl > maxSkillLevel || dl < minSkillLevel) {
return currentState;
}
skills.add(item.copyWith.call(currentLevel: cl, desiredLevel: dl));
}
return currentState.copyWith.call(skills: skills);
}
}