You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pinball/lib/leaderboard/bloc/leaderboard_bloc.dart

65 lines
1.8 KiB

feat: leaderboard bloc (#57) * feat: added bloc for request ranking * feat: added empty repository * test: tests for bloc * refactor: adapt bloc events and models to what leaderboard repository has * feat: extension to convert between CharacterTheme and CharacterType * doc: documented leaderboard bloc * refactor: merge with leaderboard_repository * doc: completed doc * chore: unused import, trailing comma * chore: removed ios files * Update lib/leaderboard/bloc/leaderboard_bloc.dart Co-authored-by: Erick <erickzanardoo@gmail.com> * Update lib/leaderboard/bloc/leaderboard_bloc.dart Co-authored-by: Erick <erickzanardoo@gmail.com> * refactor: remove props from abstract event class to force childs to implement it * Update lib/leaderboard/bloc/leaderboard_bloc.dart Co-authored-by: Alejandro Santiago <dev@alestiago.com> * Update lib/leaderboard/bloc/leaderboard_event.dart Co-authored-by: Alejandro Santiago <dev@alestiago.com> * Update lib/leaderboard/bloc/leaderboard_event.dart Co-authored-by: Alejandro Santiago <dev@alestiago.com> * Update lib/leaderboard/bloc/leaderboard_state.dart Co-authored-by: Alejandro Santiago <dev@alestiago.com> * Update lib/leaderboard/bloc/leaderboard_state.dart Co-authored-by: Alejandro Santiago <dev@alestiago.com> * Update lib/leaderboard/bloc/leaderboard_state.dart Co-authored-by: Alejandro Santiago <dev@alestiago.com> * Update lib/leaderboard/bloc/leaderboard_state.dart Co-authored-by: Alejandro Santiago <dev@alestiago.com> * Update lib/leaderboard/bloc/leaderboard_event.dart Co-authored-by: Alejandro Santiago <dev@alestiago.com> * chore: ignore doc for file * chore: wrong reference at doc * Update lib/leaderboard/bloc/leaderboard_state.dart Co-authored-by: Alejandro Santiago <dev@alestiago.com> * Update lib/leaderboard/bloc/leaderboard_state.dart Co-authored-by: Alejandro Santiago <dev@alestiago.com> * Update lib/leaderboard/bloc/leaderboard_state.dart Co-authored-by: Alejandro Santiago <dev@alestiago.com> * refactor: leaderboard state initial method Co-authored-by: Erick <erickzanardoo@gmail.com> Co-authored-by: Alejandro Santiago <dev@alestiago.com>
3 years ago
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:leaderboard_repository/leaderboard_repository.dart';
part 'leaderboard_event.dart';
part 'leaderboard_state.dart';
/// {@template leaderboard_bloc}
/// Manages leaderboard events.
///
/// Uses a [LeaderboardRepository] to request and update players participations.
/// {@endtemplate}
class LeaderboardBloc extends Bloc<LeaderboardEvent, LeaderboardState> {
/// {@macro leaderboard_bloc}
LeaderboardBloc(this._leaderboardRepository)
: super(const LeaderboardState.initial()) {
on<Top10Fetched>(_onTop10Fetched);
on<LeaderboardEntryAdded>(_onLeaderboardEntryAdded);
}
final LeaderboardRepository _leaderboardRepository;
Future<void> _onTop10Fetched(
Top10Fetched event,
Emitter<LeaderboardState> emit,
) async {
emit(state.copyWith(status: LeaderboardStatus.loading));
try {
final top10Leaderboard =
await _leaderboardRepository.fetchTop10Leaderboard();
emit(
state.copyWith(
status: LeaderboardStatus.success,
leaderboard: top10Leaderboard,
),
);
} catch (error) {
emit(state.copyWith(status: LeaderboardStatus.error));
addError(error);
}
}
Future<void> _onLeaderboardEntryAdded(
LeaderboardEntryAdded event,
Emitter<LeaderboardState> emit,
) async {
emit(state.copyWith(status: LeaderboardStatus.loading));
try {
final ranking =
await _leaderboardRepository.addLeaderboardEntry(event.entry);
emit(
state.copyWith(
status: LeaderboardStatus.success,
ranking: ranking,
),
);
} catch (error) {
emit(state.copyWith(status: LeaderboardStatus.error));
addError(error);
}
}
}