Skip to content

Commit

Permalink
[Application] Allow clearing all the items in the inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jul 10, 2021
1 parent c38a809 commit 6bb0a85
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 40 deletions.
98 changes: 58 additions & 40 deletions lib/application/inventory/inventory_bloc.dart
Expand Up @@ -21,14 +21,16 @@ class InventoryBloc extends Bloc<InventoryEvent, InventoryState> {
final CharacterBloc _characterBloc;
final WeaponBloc _weaponBloc;

_LoadedState get currentState => state as _LoadedState;

InventoryBloc(this._genshinService, this._dataService, this._telemetryService, this._characterBloc, this._weaponBloc)
: super(const InventoryState.loading());
InventoryBloc(
this._genshinService,
this._dataService,
this._telemetryService,
this._characterBloc,
this._weaponBloc,
) : super(const InventoryState.loading());

@override
Stream<InventoryState> mapEventToState(InventoryEvent event) async* {
final bool isLoading = state is _LoadingState;
final s = await event.map(
init: (_) async {
final characters = _dataService.getAllCharactersInInventory();
Expand All @@ -42,72 +44,88 @@ class InventoryBloc extends Bloc<InventoryEvent, InventoryState> {
await _dataService.addItemToInventory(e.key, ItemType.character, 1);
_characterBloc.add(CharacterEvent.addedToInventory(key: e.key, wasAdded: true));

if (isLoading) {
return state;
}

final characters = _dataService.getAllCharactersInInventory();
return currentState.copyWith.call(characters: characters);
return state.map(
loading: (state) => state,
loaded: (state) => state.copyWith.call(characters: _dataService.getAllCharactersInInventory()),
);
},
addWeapon: (e) async {
await _telemetryService.trackItemAddedToInventory(e.key, 1);
await _dataService.addItemToInventory(e.key, ItemType.weapon, 1);
_weaponBloc.add(WeaponEvent.addedToInventory(key: e.key, wasAdded: true));

if (isLoading) {
return state;
}

final weapons = _dataService.getAllWeaponsInInventory();
return currentState.copyWith.call(weapons: weapons);
return state.map(
loading: (state) => state,
loaded: (state) => state.copyWith.call(weapons: _dataService.getAllWeaponsInInventory()),
);
},
deleteCharacter: (e) async {
await _telemetryService.trackItemDeletedFromInventory(e.key);
await _dataService.deleteItemFromInventory(e.key, ItemType.character);
_characterBloc.add(CharacterEvent.addedToInventory(key: e.key, wasAdded: false));

if (isLoading) {
return state;
}

final characters = _dataService.getAllCharactersInInventory();
return currentState.copyWith.call(characters: characters);
return state.map(
loading: (state) => state,
loaded: (state) => state.copyWith.call(characters: _dataService.getAllCharactersInInventory()),
);
},
deleteWeapon: (e) async {
await _telemetryService.trackItemDeletedFromInventory(e.key);
await _dataService.deleteItemFromInventory(e.key, ItemType.weapon);
_weaponBloc.add(WeaponEvent.addedToInventory(key: e.key, wasAdded: false));

if (isLoading) {
return state;
}

final weapons = _dataService.getAllWeaponsInInventory();
return currentState.copyWith.call(weapons: weapons);
return state.map(
loading: (state) => state,
loaded: (state) => state.copyWith.call(weapons: _dataService.getAllWeaponsInInventory()),
);
},
updateMaterial: (e) async {
await _telemetryService.trackItemUpdatedInInventory(e.key, e.quantity);
await _dataService.updateItemInInventory(e.key, ItemType.material, e.quantity);

if (isLoading) {
return state;
}

final materials = _dataService.getAllMaterialsInInventory();
return currentState.copyWith.call(materials: materials);
return state.map(
loading: (state) => state,
loaded: (state) => state.copyWith.call(materials: _dataService.getAllMaterialsInInventory()),
);
},
close: (_) async => const InventoryState.loaded(characters: [], weapons: [], materials: []),
clearAllCharacters: (_) async {
await _telemetryService.trackItemsDeletedFromInventory(ItemType.character);
await _dataService.deleteItemsFromInventory(ItemType.character);

return state.map(
loading: (state) => state,
loaded: (state) => state.copyWith.call(characters: []),
);
},
clearAllWeapons: (_) async {
await _telemetryService.trackItemsDeletedFromInventory(ItemType.weapon);
await _dataService.deleteItemsFromInventory(ItemType.weapon);

return state.map(
loading: (state) => state,
loaded: (state) => state.copyWith.call(weapons: []),
);
},
clearAllMaterials: (_) async {
await _telemetryService.trackItemsDeletedFromInventory(ItemType.material);
await _dataService.deleteItemsFromInventory(ItemType.material);

return state.map(
loading: (state) => state,
loaded: (state) => state.copyWith.call(materials: _dataService.getAllMaterialsInInventory()),
);
},
);

yield s;
}

List<String> getItemsKeysToExclude() {
final upcoming = _genshinService.getUpcomingKeys();
if (state is _LoadedState) {
return currentState.characters.map((e) => e.key).toList() + currentState.weapons.map((e) => e.key).toList() + upcoming;
}

return upcoming;
return state.maybeMap(
loaded: (state) => state.characters.map((e) => e.key).toList() + state.weapons.map((e) => e.key).toList() + upcoming,
orElse: () => upcoming,
);
}
}
6 changes: 6 additions & 0 deletions lib/application/inventory/inventory_event.dart
Expand Up @@ -26,4 +26,10 @@ class InventoryEvent with _$InventoryEvent {
}) = _AddMaterial;

const factory InventoryEvent.close() = _Close;

const factory InventoryEvent.clearAllCharacters() = _ClearAllCharacters;

const factory InventoryEvent.clearAllWeapons() = _ClearAllWeapons;

const factory InventoryEvent.clearAllMaterials() = _ClearAllMaterials;
}
2 changes: 2 additions & 0 deletions lib/domain/services/data_service.dart
Expand Up @@ -49,6 +49,8 @@ abstract class DataService {

Future<void> deleteItemFromInventory(String key, ItemType type);

Future<void> deleteItemsFromInventory(ItemType type);

bool isItemInInventory(String key, ItemType type);

/// This method redistributes all the materials in the inventory by calling [redistributeInventoryMaterial]
Expand Down
2 changes: 2 additions & 0 deletions lib/domain/services/telemetry_service.dart
Expand Up @@ -54,6 +54,8 @@ abstract class TelemetryService {

Future<void> trackItemDeletedFromInventory(String key);

Future<void> trackItemsDeletedFromInventory(ItemType type);

Future<void> trackNotificationCreated(AppNotificationType type);

Future<void> trackNotificationUpdated(AppNotificationType type);
Expand Down

0 comments on commit 6bb0a85

Please sign in to comment.