Skip to content

Commit

Permalink
[Application] Added initial custom build blocs
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jan 1, 2022
1 parent 46f57de commit db06e79
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 6 deletions.
21 changes: 16 additions & 5 deletions lib/application/artifacts/artifacts_bloc.dart
Expand Up @@ -20,7 +20,7 @@ class ArtifactsBloc extends Bloc<ArtifactsEvent, ArtifactsState> {
@override
Stream<ArtifactsState> mapEventToState(ArtifactsEvent event) async* {
final s = event.map(
init: (e) => _buildInitialState(excludeKeys: e.excludeKeys),
init: (e) => _buildInitialState(excludeKeys: e.excludeKeys, type: e.type),
artifactFilterTypeChanged: (e) => currentState.copyWith.call(tempArtifactFilterType: e.artifactFilterType),
rarityChanged: (e) => currentState.copyWith.call(tempRarity: e.rarity),
sortDirectionTypeChanged: (e) => currentState.copyWith.call(tempSortDirectionType: e.sortDirectionType),
Expand All @@ -29,20 +29,29 @@ class ArtifactsBloc extends Bloc<ArtifactsEvent, ArtifactsState> {
artifactFilterType: currentState.artifactFilterType,
rarity: currentState.rarity,
sortDirectionType: currentState.sortDirectionType,
excludeKeys: currentState.excludeKeys,
type: currentState.type,
),
applyFilterChanges: (_) => _buildInitialState(
search: currentState.search,
artifactFilterType: currentState.tempArtifactFilterType,
rarity: currentState.tempRarity,
sortDirectionType: currentState.tempSortDirectionType,
excludeKeys: currentState.excludeKeys,
type: currentState.type,
),
cancelChanges: (_) => currentState.copyWith.call(
tempArtifactFilterType: currentState.artifactFilterType,
tempRarity: currentState.rarity,
tempSortDirectionType: currentState.sortDirectionType,
excludeKeys: currentState.excludeKeys,
type: currentState.type,
),
collapseNotes: (e) => currentState.copyWith.call(collapseNotes: e.collapse),
resetFilters: (_) => _buildInitialState(excludeKeys: state.maybeMap(loaded: (state) => state.excludeKeys, orElse: () => [])),
resetFilters: (_) => _buildInitialState(
excludeKeys: currentState.excludeKeys,
type: currentState.type,
),
);

yield s;
Expand All @@ -54,9 +63,10 @@ class ArtifactsBloc extends Bloc<ArtifactsEvent, ArtifactsState> {
int rarity = 0,
ArtifactFilterType artifactFilterType = ArtifactFilterType.name,
SortDirectionType sortDirectionType = SortDirectionType.asc,
ArtifactType? type,
}) {
final isLoaded = state is _LoadedState;
var data = _genshinService.getArtifactsForCard();
var data = _genshinService.getArtifactsForCard(type: type);
if (excludeKeys.isNotEmpty) {
data = data.where((el) => !excludeKeys.contains(el.key)).toList();
}
Expand All @@ -74,6 +84,7 @@ class ArtifactsBloc extends Bloc<ArtifactsEvent, ArtifactsState> {
sortDirectionType: sortDirectionType,
tempSortDirectionType: sortDirectionType,
excludeKeys: excludeKeys,
type: type,
);
}

Expand All @@ -87,7 +98,7 @@ class ArtifactsBloc extends Bloc<ArtifactsEvent, ArtifactsState> {

_sortData(data, artifactFilterType, sortDirectionType);

final s = currentState.copyWith.call(
return currentState.copyWith.call(
artifacts: data,
search: search,
rarity: rarity,
Expand All @@ -97,8 +108,8 @@ class ArtifactsBloc extends Bloc<ArtifactsEvent, ArtifactsState> {
sortDirectionType: sortDirectionType,
tempSortDirectionType: sortDirectionType,
excludeKeys: excludeKeys,
type: type,
);
return s;
}

void _sortData(List<ArtifactCardModel> data, ArtifactFilterType artifactFilterType, SortDirectionType sortDirectionType) {
Expand Down
5 changes: 4 additions & 1 deletion lib/application/artifacts/artifacts_event.dart
Expand Up @@ -2,7 +2,10 @@ part of 'artifacts_bloc.dart';

@freezed
class ArtifactsEvent with _$ArtifactsEvent {
const factory ArtifactsEvent.init({@Default(<String>[]) List<String> excludeKeys}) = _Init;
const factory ArtifactsEvent.init({
@Default(<String>[]) List<String> excludeKeys,
ArtifactType? type,
}) = _Init;

const factory ArtifactsEvent.collapseNotes({required bool collapse}) = _CollapseNotesChanged;

Expand Down
1 change: 1 addition & 0 deletions lib/application/artifacts/artifacts_state.dart
Expand Up @@ -14,5 +14,6 @@ class ArtifactsState with _$ArtifactsState {
required SortDirectionType sortDirectionType,
required SortDirectionType tempSortDirectionType,
@Default(<String>[]) List<String> excludeKeys,
ArtifactType? type,
}) = _LoadedState;
}
2 changes: 2 additions & 0 deletions lib/application/bloc.dart
Expand Up @@ -10,6 +10,8 @@ export 'calculator_asc_materials/sessions_order/calculator_asc_materials_session
export 'changelog/changelog_bloc.dart';
export 'character/character_bloc.dart';
export 'characters/characters_bloc.dart';
export 'custom_build/custom_build_bloc.dart';
export 'custom_builds/custom_builds_bloc.dart';
export 'elements/elements_bloc.dart';
export 'game_codes/game_codes_bloc.dart';
export 'home/home_bloc.dart';
Expand Down
134 changes: 134 additions & 0 deletions lib/application/custom_build/custom_build_bloc.dart
@@ -0,0 +1,134 @@
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:shiori/domain/enums/enums.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_build_bloc.freezed.dart';
part 'custom_build_event.dart';
part 'custom_build_state.dart';

class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
final GenshinService _genshinService;
final DataService _dataService;

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

Future<void> _handleEvent(CustomBuildEvent event, Emitter<CustomBuildState> emit) async {
final s = await event.map(
load: (e) async => _init(e.key),
characterChanged: (e) async => state.maybeMap(
loaded: (state) {
final newCharacter = _genshinService.getCharacterForCard(e.newKey);
return state.copyWith.call(character: newCharacter);
},
orElse: () => state,
),
titleChanged: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(title: e.newValue),
orElse: () => state,
),
roleChanged: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(type: e.newValue),
orElse: () => state,
),
subRoleChanged: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(subType: e.newValue),
orElse: () => state,
),
showOnCharacterDetailChanged: (e) async => state.maybeMap(
loaded: (state) => state.copyWith.call(showOnCharacterDetail: e.newValue),
orElse: () => state,
),
addWeapon: (e) async => state.maybeMap(
loaded: (state) {
//TODO: CHECK FOR REPEATED
final weapon = _genshinService.getWeaponForCard(e.key);
final weapons = [...state.weapons, weapon];
return state.copyWith.call(weapons: weapons);
},
orElse: () => state,
),
deleteWeapon: (e) async => state,
weaponOrderChanged: (e) async => state,
addArtifact: (e) async => state.maybeMap(
loaded: (state) {
//TODO: CHECK FOR REPEATED
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);
final artifact = _genshinService.getArtifactForCard(e.key).copyWith.call(image: img);
final artifacts = [...state.artifacts, artifact];
return state.copyWith.call(artifacts: artifacts);
},
orElse: () => state,
),
saveChanges: (e) async => state.maybeMap(
loaded: (state) => _saveChanges(state),
orElse: () async => state,
),
reset: (e) async => state,
);

emit(s);
}

CustomBuildState _init(int? key) {
if (key != null) {
final build = _dataService.getCustomBuild(key);
return CustomBuildState.loaded(
key: key,
title: build.title,
type: build.type,
subType: build.subType,
showOnCharacterDetail: build.showOnCharacterDetail,
character: build.character,
weapons: build.weapons,
artifacts: build.artifacts,
);
}

final character = _genshinService.getCharactersForCard().first;
return CustomBuildState.loaded(
title: '',
type: CharacterRoleType.dps,
subType: CharacterRoleSubType.none,
showOnCharacterDetail: true,
character: character,
weapons: [],
artifacts: [],
);
}

Future<CustomBuildState> _saveChanges(_LoadedState state) async {
if (state.key != null) {
await _dataService.updateCustomBuild(
state.key!,
state.title,
state.type,
state.subType,
state.showOnCharacterDetail,
state.weapons.map((e) => e.key).toList(),
{},
[],
);

return _init(state.key);
}
final build = await _dataService.saveCustomBuild(
state.character.key,
state.title,
state.type,
state.subType,
state.showOnCharacterDetail,
state.weapons.map((e) => e.key).toList(),
{},
[],
);

return _init(build.key);
}
}
30 changes: 30 additions & 0 deletions lib/application/custom_build/custom_build_event.dart
@@ -0,0 +1,30 @@
part of 'custom_build_bloc.dart';

@freezed
class CustomBuildEvent with _$CustomBuildEvent {
const factory CustomBuildEvent.load({int? key}) = _Init;

const factory CustomBuildEvent.characterChanged({required String newKey}) = _CharacterChanged;

const factory CustomBuildEvent.titleChanged({required String newValue}) = _TitleChanged;

const factory CustomBuildEvent.roleChanged({required CharacterRoleType newValue}) = _RoleChanged;

const factory CustomBuildEvent.subRoleChanged({required CharacterRoleSubType newValue}) = _SubRoleChanged;

const factory CustomBuildEvent.showOnCharacterDetailChanged({required bool newValue}) = _ShowOnCharacterDetailChanged;

const factory CustomBuildEvent.addWeapon({required String key}) = _AddWeapon;

const factory CustomBuildEvent.deleteWeapon({required String key}) = _DeleteWeapon;

const factory CustomBuildEvent.weaponOrderChanged({required String key, required int newIndex}) = _WeaponOrderChanged;

const factory CustomBuildEvent.addArtifact({required String key, required ArtifactType type}) = _AddArtifact;

const factory CustomBuildEvent.saveChanges() = _SaveChanges;

const factory CustomBuildEvent.reset() = _Reset;

//TODO: SHARE, SBUSTATS, TALENETS, ARTIFACT'S PIECE BONUS
}
17 changes: 17 additions & 0 deletions lib/application/custom_build/custom_build_state.dart
@@ -0,0 +1,17 @@
part of 'custom_build_bloc.dart';

@freezed
class CustomBuildState with _$CustomBuildState {
const factory CustomBuildState.loading() = _LoadingState;

const factory CustomBuildState.loaded({
int? key,
required String title,
required CharacterRoleType type,
required CharacterRoleSubType subType,
required bool showOnCharacterDetail,
required CharacterCardModel character,
required List<WeaponCardModel> weapons,
required List<ArtifactCardModel> artifacts,
}) = _LoadedState;
}
86 changes: 86 additions & 0 deletions lib/application/custom_builds/custom_builds_bloc.dart
@@ -0,0 +1,86 @@
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));
}

Future<void> _handleEvent(CustomBuildsEvent event, Emitter<CustomBuildsState> emit) async {
final newState = event.map(
load: (_) {
// final dummyA = CustomBuildModel(
// key: 1,
// position: 1,
// title: 'Physical Dps',
// type: CharacterRoleType.dps,
// subType: CharacterRoleSubType.electro,
// showOnCharacterDetail: true,
// character: _genshinService.getCharacterForCard('keqing'),
// weapons: [
// _genshinService.getWeaponForCard('blackcliff-longsword'),
// _genshinService.getWeaponForCard('sword-of-descension'),
// _genshinService.getWeaponForCard('iron-sting'),
// _genshinService.getWeaponForCard('mistsplitter-reforged'),
// _genshinService.getWeaponForCard('prototype-rancour'),
// _genshinService.getWeaponForCard('sword-of-descension'),
// _genshinService.getWeaponForCard('blackcliff-longsword'),
// _genshinService.getWeaponForCard('sword-of-descension'),
// ],
// artifacts: [
// _genshinService.getArtifactForCard('shimenawas-reminiscence'),
// _genshinService.getArtifactForCard('thundersoother'),
// ],
// );
// final dummyB = CustomBuildModel(
// key: 1,
// position: 2,
// title: 'Physical Dps',
// type: CharacterRoleType.dps,
// subType: CharacterRoleSubType.electro,
// showOnCharacterDetail: true,
// character: _genshinService.getCharacterForCard('ganyu'),
// weapons: [
// _genshinService.getWeaponForCard('blackcliff-longsword'),
// _genshinService.getWeaponForCard('sword-of-descension'),
// _genshinService.getWeaponForCard('iron-sting'),
// _genshinService.getWeaponForCard('mistsplitter-reforged'),
// _genshinService.getWeaponForCard('prototype-rancour'),
// _genshinService.getWeaponForCard('sword-of-descension'),
// _genshinService.getWeaponForCard('blackcliff-longsword'),
// _genshinService.getWeaponForCard('sword-of-descension'),
// ],
// artifacts: [
// _genshinService.getArtifactForCard('shimenawas-reminiscence'),
// _genshinService.getArtifactForCard('thundersoother'),
// ],
// );
// final builds = _dataService.getAllCustomBuilds()
// ..add(dummyA)
// ..add(dummyB)
// ..add(dummyA)
// ..add(dummyB);
// return state.copyWith.call(builds: builds);

final builds = _dataService.getAllCustomBuilds();

return state.copyWith.call(builds: builds);
},
delete: (e) {
return state;
},
);

emit(newState);
}
}
8 changes: 8 additions & 0 deletions lib/application/custom_builds/custom_builds_event.dart
@@ -0,0 +1,8 @@
part of 'custom_builds_bloc.dart';

@freezed
class CustomBuildsEvent with _$CustomBuildsEvent {
const factory CustomBuildsEvent.load() = _Load;

const factory CustomBuildsEvent.delete({required int key}) = _Delete;
}
8 changes: 8 additions & 0 deletions lib/application/custom_builds/custom_builds_state.dart
@@ -0,0 +1,8 @@
part of 'custom_builds_bloc.dart';

@freezed
class CustomBuildsState with _$CustomBuildsState {
const factory CustomBuildsState.loaded({
@Default(<CustomBuildModel>[]) List<CustomBuildModel> builds,
}) = _LoadedState;
}

0 comments on commit db06e79

Please sign in to comment.