Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Application] Added initial custom build blocs
- Loading branch information
Showing
10 changed files
with
306 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |