Skip to content

Commit

Permalink
[Application] Added a few missing validations
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jan 23, 2022
1 parent 5386a3f commit fce56ec
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 52 deletions.
103 changes: 61 additions & 42 deletions lib/application/custom_build/custom_build_bloc.dart
Expand Up @@ -29,11 +29,10 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
static int maxNumberOfWeapons = 10;
static int maxNumberOfTeamCharacters = 10;

CustomBuildBloc(this._genshinService, this._dataService, this._customBuildsBloc) : super(const CustomBuildState.loading()) {
on<CustomBuildEvent>(_handleEvent);
}
CustomBuildBloc(this._genshinService, this._dataService, this._customBuildsBloc) : super(const CustomBuildState.loading());

Future<void> _handleEvent(CustomBuildEvent event, Emitter<CustomBuildState> emit) async {
@override
Stream<CustomBuildState> mapEventToState(CustomBuildEvent event) async* {
//TODO: SHOULD I TRHOW ON INVALID REQUEST ?
//IN MOST CASES THERE ARE SOME VALIDATIONS FOR THINGS LIKE
// if (!state.weapons.any((el) => el.key == e.key)) {
Expand Down Expand Up @@ -62,52 +61,48 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
loaded: (state) => state.copyWith.call(showOnCharacterDetail: e.newValue),
orElse: () => state,
),
addWeapon: (e) async => state.maybeMap(
loaded: (state) => _addWeapon(e, state),
orElse: () => state,
),
weaponRefinementChanged: (e) async => state.maybeMap(
loaded: (state) => _weaponRefinementChanged(e, state),
isRecommendedChanged: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(isRecommended: e.newValue),
orElse: () => state,
),
weaponsOrderChanged: (e) async => state.maybeMap(
loaded: (state) => _weaponsOrderChanged(e, state),
addNote: (e) async => state.maybeMap(
loaded: (state) => _addNote(e, state),
orElse: () => state,
),
deleteWeapon: (e) async => state.maybeMap(
loaded: (state) => _deleteWeapon(e, state),
deleteNote: (e) async => state.maybeMap(
loaded: (state) => _deleteNote(e, state),
orElse: () => state,
),
addArtifact: (e) async => state.maybeMap(
loaded: (state) => _addArtifact(e, state),
addSkillPriority: (e) async => state.maybeMap(
loaded: (state) => _addSkillPriority(e, state),
orElse: () => state,
),
addNote: (e) async => state.maybeMap(
loaded: (state) => _addNote(e, state),
deleteSkillPriority: (e) async => state.maybeMap(
loaded: (state) => _deleteSkillPriority(e, state),
orElse: () => state,
),
deleteNote: (e) async => state.maybeMap(
loaded: (state) => _deleteNote(e, state),
addWeapon: (e) async => state.maybeMap(
loaded: (state) => _addWeapon(e, state),
orElse: () => state,
),
deleteWeapons: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(weapons: []),
weaponRefinementChanged: (e) async => state.maybeMap(
loaded: (state) => _weaponRefinementChanged(e, state),
orElse: () => state,
),
deleteArtifacts: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(artifacts: [], subStatsSummary: []),
weaponsOrderChanged: (e) async => state.maybeMap(
loaded: (state) => _weaponsOrderChanged(e, state),
orElse: () => state,
),
deleteSkillPriority: (e) async => state.maybeMap(
loaded: (state) => _deleteSkillPriority(e, state),
deleteWeapon: (e) async => state.maybeMap(
loaded: (state) => _deleteWeapon(e, state),
orElse: () => state,
),
addSkillPriority: (e) async => state.maybeMap(
loaded: (state) => _addSkillPriority(e, state),
deleteWeapons: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(weapons: []),
orElse: () => state,
),
isRecommendedChanged: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(isRecommended: e.newValue),
addArtifact: (e) async => state.maybeMap(
loaded: (state) => _addArtifact(e, state),
orElse: () => state,
),
addArtifactSubStats: (e) async => state.maybeMap(
Expand All @@ -118,6 +113,10 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
loaded: (state) => _deleteArtifact(e, state),
orElse: () => state,
),
deleteArtifacts: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(artifacts: [], subStatsSummary: []),
orElse: () => state,
),
addTeamCharacter: (e) async => state.maybeMap(
loaded: (state) => _addTeamCharacter(e, state),
orElse: () => state,
Expand All @@ -140,7 +139,7 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
),
);

emit(s);
yield s;
}

CustomBuildState _init(int? key, String initialTitle) {
Expand Down Expand Up @@ -182,15 +181,15 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {

CustomBuildState _addNote(_AddNote e, _LoadedState state) {
if (e.note.isNullEmptyOrWhitespace || state.notes.length >= maxNumberOfNotes) {
return state;
throw Exception('Note is not valid');
}
final newNote = CustomBuildNoteModel(index: state.notes.length, note: e.note);
return state.copyWith.call(notes: [...state.notes, newNote]);
}

CustomBuildState _deleteNote(_DeleteNote e, _LoadedState state) {
if (e.index < 0 || e.index >= state.notes.length) {
return state;
throw Exception('The provided note index = ${e.index} is not valid');
}

final notes = [...state.notes];
Expand All @@ -199,15 +198,18 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
}

CustomBuildState _addSkillPriority(_AddSkillPriority e, _LoadedState state) {
if (state.skillPriorities.contains(e.type) || !validSkillTypes.contains(e.type)) {
if (state.skillPriorities.contains(e.type)) {
return state;
}
if (!validSkillTypes.contains(e.type)) {
throw Exception('Skill type = ${e.type} is not valid');
}
return state.copyWith.call(skillPriorities: [...state.skillPriorities, e.type]);
}

CustomBuildState _deleteSkillPriority(_DeleteSkillPriority e, _LoadedState state) {
if (e.index < 0 || e.index >= state.skillPriorities.length) {
return state;
throw Exception('The provided skill index = ${e.index} is not valid');
}

final skillPriorities = [...state.skillPriorities];
Expand All @@ -234,9 +236,18 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {

CustomBuildState _addWeapon(_AddWeapon e, _LoadedState state) {
if (state.weapons.any((el) => el.key == e.key)) {
throw Exception('Weapons cannot be repeated');
throw Exception('Weapons cannot be repeated in the state');
}

if (state.weapons.length + 1 > maxNumberOfWeapons) {
throw Exception('Cannot add more than = $maxNumberOfWeapons weapons to the state');
}

final weapon = _genshinService.getWeaponForCard(e.key);
if (state.character.weaponType != weapon.type) {
throw Exception('Type = ${weapon.type} is not valid for character = ${state.character.key}');
}

final newOne = CustomBuildWeaponModel(
key: e.key,
index: state.weapons.length,
Expand Down Expand Up @@ -269,7 +280,7 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
CustomBuildState _weaponRefinementChanged(_WeaponRefinementChanged e, _LoadedState state) {
final current = state.weapons.firstWhereOrNull((el) => el.key == e.key);
if (current == null) {
return state;
throw Exception('Weapon = ${e.key} does not exist in the state');
}

if (current.refinement == e.newValue) {
Expand All @@ -292,7 +303,7 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {

CustomBuildState _deleteWeapon(_DeleteWeapon e, _LoadedState state) {
if (!state.weapons.any((el) => el.key == e.key)) {
return state;
throw Exception('Weapon = ${e.key} does not exist');
}

final updated = [...state.weapons];
Expand All @@ -301,6 +312,10 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
}

CustomBuildState _addArtifact(_AddArtifact e, _LoadedState state) {
if (state.artifacts.length + 1 > ArtifactType.values.length) {
throw Exception('Cannot add more than = ${ArtifactType.values.length} artifacts to the state');
}

final fullArtifact = _genshinService.getArtifact(e.key);
final translation = _genshinService.getArtifactTranslation(e.key);
final img = _genshinService.getArtifactRelatedPart(fullArtifact.fullImagePath, fullArtifact.image, translation.bonus.length, e.type);
Expand Down Expand Up @@ -338,7 +353,7 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
CustomBuildState _addArtifactSubStats(_AddArtifactSubStats e, _LoadedState state) {
final artifact = state.artifacts.firstWhereOrNull((el) => el.type == e.type);
if (artifact == null) {
return state;
throw Exception('Artifact type = ${e.type} is not in the state');
}

final possibleSubStats = getArtifactPossibleSubStats(artifact.statType);
Expand All @@ -356,7 +371,7 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {

CustomBuildState _deleteArtifact(_DeleteArtifact e, _LoadedState state) {
if (!state.artifacts.any((el) => el.type == e.type)) {
return state;
throw Exception('Artifact type = ${e.type} is not in the state');
}

final updated = [...state.artifacts];
Expand All @@ -366,7 +381,11 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {

CustomBuildState _addTeamCharacter(_AddTeamCharacter e, _LoadedState state) {
if (state.teamCharacters.length + 1 == maxNumberOfTeamCharacters) {
return state;
throw Exception('Cannot add more than = $maxNumberOfTeamCharacters team characters to the state');
}

if (e.key == state.character.key) {
throw Exception('The selected character cannot be in the team characters');
}

final char = _genshinService.getCharacterForCard(e.key);
Expand Down Expand Up @@ -413,7 +432,7 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {

CustomBuildState _deleteTeamCharacter(_DeleteTeamCharacter e, _LoadedState state) {
if (!state.teamCharacters.any((el) => el.key == e.key)) {
return state;
throw Exception('Team character = ${e.key} is not in the state');
}

final updated = [...state.teamCharacters];
Expand Down
13 changes: 5 additions & 8 deletions lib/application/custom_builds/custom_builds_bloc.dart
Expand Up @@ -2,22 +2,19 @@ import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:shiori/domain/models/models.dart';
import 'package:shiori/domain/services/data_service.dart';
import 'package:shiori/domain/services/genshin_service.dart';

part 'custom_builds_bloc.freezed.dart';
part 'custom_builds_event.dart';
part 'custom_builds_state.dart';

class CustomBuildsBloc extends Bloc<CustomBuildsEvent, CustomBuildsState> {
final GenshinService _genshinService;
final DataService _dataService;

CustomBuildsBloc(this._genshinService, this._dataService) : super(const CustomBuildsState.loaded()) {
on<CustomBuildsEvent>((event, emit) => _handleEvent(event, emit));
}
CustomBuildsBloc(this._dataService) : super(const CustomBuildsState.loaded());

Future<void> _handleEvent(CustomBuildsEvent event, Emitter<CustomBuildsState> emit) async {
final newState = await event.map(
@override
Stream<CustomBuildsState> mapEventToState(CustomBuildsEvent event) async* {
final s = await event.map(
load: (_) async {
final builds = _dataService.customBuilds.getAllCustomBuilds();
return state.copyWith.call(builds: builds);
Expand All @@ -30,6 +27,6 @@ class CustomBuildsBloc extends Bloc<CustomBuildsEvent, CustomBuildsState> {
},
);

emit(newState);
yield s;
}
}
3 changes: 1 addition & 2 deletions lib/injection.dart
Expand Up @@ -132,9 +132,8 @@ class Injection {
}

static CustomBuildsBloc get customBuildsBloc {
final genshinService = getIt<GenshinService>();
final dataService = getIt<DataService>();
return CustomBuildsBloc(genshinService, dataService);
return CustomBuildsBloc(dataService);
}

//TODO: USE THIS PROP
Expand Down

0 comments on commit fce56ec

Please sign in to comment.