From 6bb0a8520d42ce40c486a94d46181ee45cf9b924 Mon Sep 17 00:00:00 2001 From: Efrain Date: Sat, 10 Jul 2021 18:00:39 -0500 Subject: [PATCH] [Application] Allow clearing all the items in the inventory --- lib/application/inventory/inventory_bloc.dart | 98 +++++++++++-------- .../inventory/inventory_event.dart | 6 ++ lib/domain/services/data_service.dart | 2 + lib/domain/services/telemetry_service.dart | 2 + 4 files changed, 68 insertions(+), 40 deletions(-) diff --git a/lib/application/inventory/inventory_bloc.dart b/lib/application/inventory/inventory_bloc.dart index a6b4a0643..b02158a6c 100644 --- a/lib/application/inventory/inventory_bloc.dart +++ b/lib/application/inventory/inventory_bloc.dart @@ -21,14 +21,16 @@ class InventoryBloc extends Bloc { 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 mapEventToState(InventoryEvent event) async* { - final bool isLoading = state is _LoadingState; final s = await event.map( init: (_) async { final characters = _dataService.getAllCharactersInInventory(); @@ -42,61 +44,78 @@ class InventoryBloc extends Bloc { 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; @@ -104,10 +123,9 @@ class InventoryBloc extends Bloc { List 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, + ); } } diff --git a/lib/application/inventory/inventory_event.dart b/lib/application/inventory/inventory_event.dart index b73b93a1d..935449a91 100644 --- a/lib/application/inventory/inventory_event.dart +++ b/lib/application/inventory/inventory_event.dart @@ -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; } diff --git a/lib/domain/services/data_service.dart b/lib/domain/services/data_service.dart index 175cf69cc..fdd869d00 100644 --- a/lib/domain/services/data_service.dart +++ b/lib/domain/services/data_service.dart @@ -49,6 +49,8 @@ abstract class DataService { Future deleteItemFromInventory(String key, ItemType type); + Future deleteItemsFromInventory(ItemType type); + bool isItemInInventory(String key, ItemType type); /// This method redistributes all the materials in the inventory by calling [redistributeInventoryMaterial] diff --git a/lib/domain/services/telemetry_service.dart b/lib/domain/services/telemetry_service.dart index 5b777d063..4b0678614 100644 --- a/lib/domain/services/telemetry_service.dart +++ b/lib/domain/services/telemetry_service.dart @@ -54,6 +54,8 @@ abstract class TelemetryService { Future trackItemDeletedFromInventory(String key); + Future trackItemsDeletedFromInventory(ItemType type); + Future trackNotificationCreated(AppNotificationType type); Future trackNotificationUpdated(AppNotificationType type);