Skip to content

Commit

Permalink
Crash fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jan 20, 2022
1 parent eb31aee commit 3109246
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 133 deletions.
34 changes: 18 additions & 16 deletions lib/application/custom_build/custom_build_bloc.dart
@@ -1,5 +1,6 @@
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:shiori/application/bloc.dart';
import 'package:shiori/domain/app_constants.dart';
import 'package:shiori/domain/enums/enums.dart';
import 'package:shiori/domain/extensions/string_extensions.dart';
Expand All @@ -14,6 +15,7 @@ part 'custom_build_state.dart';
class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
final GenshinService _genshinService;
final DataService _dataService;
final CustomBuildsBloc _customBuildsBloc;

static int maxTitleLength = 40;
static int maxNoteLength = 100;
Expand All @@ -27,7 +29,7 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
static int maxNumberOfWeapons = 10;
static int maxNumberOfTeamCharacters = 10;

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

Expand All @@ -40,7 +42,7 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
// }
// WHICH SHOULD NOT HAPPEN BUT MAYBE I SHOULD THROW AN EXCEPTION IN THERE
final s = await event.map(
load: (e) async => _init(e.key),
load: (e) async => _init(e.key, e.initialTitle),
characterChanged: (e) async => state.maybeMap(
loaded: (state) => _characterChanged(e, state),
orElse: () => state,
Expand Down Expand Up @@ -81,11 +83,6 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
loaded: (state) => _addArtifact(e, state),
orElse: () => state,
),
saveChanges: (e) async => state.maybeMap(
loaded: (state) => _saveChanges(state),
orElse: () async => state,
),
reset: (e) async => state,
addNote: (e) async => state.maybeMap(
loaded: (state) => _addNote(e, state),
orElse: () => state,
Expand Down Expand Up @@ -138,12 +135,16 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
loaded: (state) => state.copyWith.call(teamCharacters: []),
orElse: () => state,
),
saveChanges: (e) async => state.maybeMap(
loaded: (state) => _saveChanges(state),
orElse: () async => state,
),
);

emit(s);
}

CustomBuildState _init(int? key) {
CustomBuildState _init(int? key, String initialTitle) {
if (key != null) {
final build = _dataService.customBuilds.getCustomBuild(key);
return CustomBuildState.loaded(
Expand All @@ -165,7 +166,7 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {

final character = _genshinService.getCharactersForCard().first;
return CustomBuildState.loaded(
title: '',
title: initialTitle,
type: CharacterRoleType.dps,
subType: CharacterRoleSubType.none,
showOnCharacterDetail: true,
Expand Down Expand Up @@ -421,12 +422,13 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
state.isRecommended,
state.notes,
state.weapons,
[],
state.artifacts,
state.teamCharacters,
[],
state.skillPriorities,
);

return _init(state.key);
_customBuildsBloc.add(const CustomBuildsEvent.load());
return _init(state.key, state.title);
}
final build = await _dataService.customBuilds.saveCustomBuild(
state.character.key,
Expand All @@ -437,13 +439,13 @@ class CustomBuildBloc extends Bloc<CustomBuildEvent, CustomBuildState> {
state.isRecommended,
state.notes,
state.weapons,
//TODO: THIS
[],
state.artifacts,
state.teamCharacters,
[],
state.skillPriorities,
);

return _init(build.key);
_customBuildsBloc.add(const CustomBuildsEvent.load());
return _init(build.key, state.title);
}

List<StatType> _generateSubStatSummary(List<CustomBuildArtifactModel> artifacts) {
Expand Down
8 changes: 4 additions & 4 deletions lib/application/custom_build/custom_build_event.dart
Expand Up @@ -2,7 +2,10 @@ part of 'custom_build_bloc.dart';

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

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

Expand Down Expand Up @@ -66,8 +69,5 @@ class CustomBuildEvent with _$CustomBuildEvent {

const factory CustomBuildEvent.saveChanges() = _SaveChanges;

//TODO: DELETE THIS?
const factory CustomBuildEvent.reset() = _Reset;

//TODO: SHARE, SBUSTATS, TALENETS, ARTIFACT'S PIECE BONUS
}
65 changes: 7 additions & 58 deletions lib/application/custom_builds/custom_builds_bloc.dart
Expand Up @@ -17,67 +17,16 @@ class CustomBuildsBloc extends Bloc<CustomBuildsEvent, CustomBuildsState> {
}

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 newState = await event.map(
load: (_) async {
final builds = _dataService.customBuilds.getAllCustomBuilds();

return state.copyWith.call(builds: builds);
},
delete: (e) {
return state;
delete: (e) async {
await _dataService.customBuilds.deleteCustomBuild(e.key);
final builds = [...state.builds];
builds.removeWhere((el) => el.key == e.key);
return state.copyWith.call(builds: builds);
},
);

Expand Down
Expand Up @@ -12,13 +12,13 @@ class CustomBuildArtifact extends HiveObject {
String itemKey;

@HiveField(2)
ArtifactType type;
int type;

@HiveField(3)
StatType statType;
int statType;

@HiveField(4)
List<StatType> subStats;
List<int> subStats;

CustomBuildArtifact(this.buildItemKey, this.itemKey, this.type, this.statType, this.subStats);
}
Expand Up @@ -15,10 +15,10 @@ class CustomBuildTeamCharacter extends HiveObject {
String characterKey;

@HiveField(3)
CharacterRoleType roleType;
int roleType;

@HiveField(4)
CharacterRoleSubType subType;
int subType;

CustomBuildTeamCharacter(this.buildItemKey, this.index, this.characterKey, this.roleType, this.subType);
}
1 change: 0 additions & 1 deletion lib/infrastructure/data_service.dart
Expand Up @@ -1262,7 +1262,6 @@ class DataServiceImpl implements DataService {
Hive.registerAdapter(NotificationRealmCurrencyAdapter());
Hive.registerAdapter(NotificationResinAdapter());
Hive.registerAdapter(NotificationWeeklyBossAdapter());
Hive.registerAdapter(CustomBuildAdapter());
}

ItemAscensionMaterials _buildForCharacter(CalculatorItem item, {int? calculatorItemKey, bool includeInventory = false}) {
Expand Down
63 changes: 49 additions & 14 deletions lib/infrastructure/persistence/custom_builds_data_service.dart
Expand Up @@ -18,13 +18,22 @@ class CustomBuildsDataServiceImpl implements CustomBuildsDataService {

@override
Future<void> init() async {
_registerAdapters();
_buildsBox = await Hive.openBox<CustomBuild>('customBuilds');
_weaponsBox = await Hive.openBox<CustomBuildWeapon>('customBuildWeapons');
_artifactsBox = await Hive.openBox<CustomBuildArtifact>('customBuildArtifacts');
_notesBox = await Hive.openBox<CustomBuildNote>('customBuildNotes');
_teamCharactersBox = await Hive.openBox<CustomBuildTeamCharacter>('customBuildTeamCharacters');
}

void _registerAdapters() {
Hive.registerAdapter(CustomBuildAdapter());
Hive.registerAdapter(CustomBuildWeaponAdapter());
Hive.registerAdapter(CustomBuildArtifactAdapter());
Hive.registerAdapter(CustomBuildNoteAdapter());
Hive.registerAdapter(CustomBuildTeamCharacterAdapter());
}

@override
Future<void> deleteThemAll() {
return Future.wait([
Expand Down Expand Up @@ -108,12 +117,7 @@ class CustomBuildsDataServiceImpl implements CustomBuildsDataService {

await build.save();

await Future.wait([
_deleteWeapons(key),
_deleteArtifacts(key),
_deleteNotes(key),
_deleteTeamCharacters(key),
]);
await _deleteCustomBuildRelatedParts(key);

final buildNotes = await _saveNotes(key, notes);
final buildWeapons = await _saveWeapons(key, weapons);
Expand All @@ -126,8 +130,16 @@ class CustomBuildsDataServiceImpl implements CustomBuildsDataService {
Future<void> deleteCustomBuild(int key) {
return Future.wait([
_buildsBox.delete(key),
_deleteNotes(key),
_deleteCustomBuildRelatedParts(key),
]);
}

Future<void> _deleteCustomBuildRelatedParts(int key) {
return Future.wait([
_deleteWeapons(key),
_deleteArtifacts(key),
_deleteNotes(key),
_deleteTeamCharacters(key),
]);
}

Expand All @@ -144,13 +156,21 @@ class CustomBuildsDataServiceImpl implements CustomBuildsDataService {
}

Future<List<CustomBuildArtifact>> _saveArtifacts(int buildKey, List<CustomBuildArtifactModel> artifacts) async {
final buildArtifacts = artifacts.map((e) => CustomBuildArtifact(buildKey, e.key, e.type, e.statType, e.subStats)).toList();
final buildArtifacts = artifacts
.map(
(e) => CustomBuildArtifact(buildKey, e.key, e.type.index, e.statType.index, e.subStats.map((e) => e.index).toList()),
)
.toList();
await _artifactsBox.addAll(buildArtifacts);
return buildArtifacts;
}

Future<List<CustomBuildTeamCharacter>> _saveTeamCharacters(int buildKey, List<CustomBuildTeamCharacterModel> teamCharacters) async {
final buildTeamCharacters = teamCharacters.map((e) => CustomBuildTeamCharacter(buildKey, e.index, e.key, e.roleType, e.subType)).toList();
final buildTeamCharacters = teamCharacters
.map(
(e) => CustomBuildTeamCharacter(buildKey, e.index, e.key, e.roleType.index, e.subType.index),
)
.toList();
await _teamCharactersBox.addAll(buildTeamCharacters);
return buildTeamCharacters;
}
Expand Down Expand Up @@ -191,7 +211,6 @@ class CustomBuildsDataServiceImpl implements CustomBuildsDataService {
List<CustomBuildTeamCharacter> teamCharacters,
) {
final character = _genshinService.getCharacterForCard(build.characterKey);
// final artifacts = build.artifactKeys.map((e) => _genshinService.getArtifactForCard(e)).toList();
return CustomBuildModel(
key: build.key as int,
title: build.title,
Expand All @@ -214,8 +233,24 @@ class CustomBuildsDataServiceImpl implements CustomBuildsDataService {
subStatValue: weapon.subStatValue,
);
}).toList(),
//TODO: THIS
artifacts: [],
artifacts: artifacts.map((e) {
final fullArtifact = _genshinService.getArtifact(e.itemKey);
final translation = _genshinService.getArtifactTranslation(e.itemKey);
final image = _genshinService.getArtifactRelatedPart(
fullArtifact.fullImagePath,
fullArtifact.image,
translation.bonus.length,
ArtifactType.values[e.type],
);
return CustomBuildArtifactModel(
key: e.itemKey,
type: ArtifactType.values[e.type],
statType: StatType.values[e.statType],
image: image,
rarity: fullArtifact.maxRarity,
subStats: e.subStats.map((e) => StatType.values[e]).toList(),
);
}).toList(),
skillPriorities: build.skillPriorities.map((e) => CharacterSkillType.values[e]).toList(),
notes: notes.map((e) => CustomBuildNoteModel(index: e.index, note: e.note)).toList()..sort((x, y) => x.index.compareTo(y.index)),
teamCharacters: teamCharacters.map((e) {
Expand All @@ -225,8 +260,8 @@ class CustomBuildsDataServiceImpl implements CustomBuildsDataService {
index: e.index,
name: char.name,
image: char.image,
roleType: e.roleType,
subType: e.subType,
roleType: CharacterRoleType.values[e.roleType],
subType: CharacterRoleSubType.values[e.subType],
);
}).toList()
..sort((x, y) => x.index.compareTo(y.index)),
Expand Down
12 changes: 6 additions & 6 deletions lib/injection.dart
Expand Up @@ -137,12 +137,6 @@ class Injection {
return CustomBuildsBloc(genshinService, dataService);
}

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

//TODO: USE THIS PROP
// static CalculatorAscMaterialsItemBloc get calculatorAscMaterialsItemBloc {
// final genshinService = getIt<GenshinService>();
Expand Down Expand Up @@ -179,6 +173,12 @@ class Injection {
return CalculatorAscMaterialsSessionsOrderBloc(dataService, bloc);
}

static CustomBuildBloc getCustomBuildBloc(CustomBuildsBloc bloc) {
final genshinService = getIt<GenshinService>();
final dataService = getIt<DataService>();
return CustomBuildBloc(genshinService, dataService, bloc);
}

static Future<void> init() async {
final networkService = NetworkServiceImpl();
networkService.init();
Expand Down

0 comments on commit 3109246

Please sign in to comment.