-
-
Notifications
You must be signed in to change notification settings - Fork 47
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 charts bloc
- Loading branch information
Showing
5 changed files
with
90 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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/genshin_service.dart'; | ||
import 'package:shiori/domain/services/telemetry_service.dart'; | ||
|
||
part 'charts_bloc.freezed.dart'; | ||
part 'charts_event.dart'; | ||
part 'charts_state.dart'; | ||
|
||
class ChartsBloc extends Bloc<ChartsEvent, ChartsState> { | ||
final GenshinService _genshinService; | ||
final TelemetryService _telemetryService; | ||
|
||
ChartsBloc(this._genshinService, this._telemetryService) : super(const ChartsState.loading()); | ||
|
||
@override | ||
Stream<ChartsState> mapEventToState(ChartsEvent event) async* { | ||
final s = await event.map( | ||
init: (_) async => _init(), | ||
elementSelected: (e) async => _elementSelected(e.type), | ||
); | ||
|
||
yield s; | ||
} | ||
|
||
Future<ChartsState> _init() async { | ||
await _telemetryService.trackChartsOpened(); | ||
final tops = [ | ||
..._genshinService.getTopCharts(ChartType.topFiveStarCharacterMostReruns), | ||
..._genshinService.getTopCharts(ChartType.topFiveStarCharacterLeastReruns), | ||
..._genshinService.getTopCharts(ChartType.topFiveStarWeaponMostReruns), | ||
..._genshinService.getTopCharts(ChartType.topFiveStarWeaponLeastReruns), | ||
..._genshinService.getTopCharts(ChartType.topFourStarCharacterMostReruns), | ||
..._genshinService.getTopCharts(ChartType.topFourStarCharacterLeastReruns), | ||
..._genshinService.getTopCharts(ChartType.topFourStarWeaponMostReruns), | ||
..._genshinService.getTopCharts(ChartType.topFourStarWeaponLeastReruns), | ||
]; | ||
final birthdays = _genshinService.getCharacterBirthdaysForCharts(); | ||
final elements = _genshinService.getElementsForCharts(); | ||
return ChartsState.initial(tops: tops, birthdays: birthdays, elements: elements, filteredElements: elements); | ||
} | ||
|
||
ChartsState _elementSelected(ElementType type) => state.maybeMap( | ||
initial: (state) { | ||
final selectedTypes = [...state.selectedElementTypes]; | ||
if (selectedTypes.contains(type)) { | ||
selectedTypes.remove(type); | ||
} else { | ||
selectedTypes.add(type); | ||
} | ||
|
||
final filteredElements = selectedTypes.isEmpty ? state.elements : state.elements.where((el) => selectedTypes.contains(el.type)).toList(); | ||
return state.copyWith(selectedElementTypes: selectedTypes, filteredElements: filteredElements); | ||
}, | ||
orElse: () => state, | ||
); | ||
} |
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,10 @@ | ||
part of 'charts_bloc.dart'; | ||
|
||
@freezed | ||
class ChartsEvent with _$ChartsEvent { | ||
const factory ChartsEvent.init() = _Init; | ||
|
||
const factory ChartsEvent.elementSelected({ | ||
required ElementType type, | ||
}) = _ElementSelected; | ||
} |
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,14 @@ | ||
part of 'charts_bloc.dart'; | ||
|
||
@freezed | ||
class ChartsState with _$ChartsState { | ||
const factory ChartsState.loading() = _LoadingState; | ||
|
||
const factory ChartsState.initial({ | ||
required List<ChartTopItemModel> tops, | ||
required List<ChartBirthdayMonthModel> birthdays, | ||
required List<ChartElementItemModel> elements, | ||
required List<ChartElementItemModel> filteredElements, | ||
@Default(<ElementType>[]) List<ElementType> selectedElementTypes, | ||
}) = _InitialState; | ||
} |
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