mirror of https://github.com/flutter/pinball.git
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>pull/71/head
parent
79bb95bef9
commit
e0e8fabce5
@ -0,0 +1,64 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
part of 'leaderboard_bloc.dart';
|
||||
|
||||
/// {@template leaderboard_event}
|
||||
/// Represents the events available for [LeaderboardBloc].
|
||||
/// {endtemplate}
|
||||
abstract class LeaderboardEvent extends Equatable {
|
||||
/// {@macro leaderboard_event}
|
||||
const LeaderboardEvent();
|
||||
}
|
||||
|
||||
/// {@template top_10_fetched}
|
||||
/// Request the top 10 [LeaderboardEntry]s.
|
||||
/// {endtemplate}
|
||||
class Top10Fetched extends LeaderboardEvent {
|
||||
/// {@macro top_10_fetched}
|
||||
const Top10Fetched();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
/// {@template leaderboard_entry_added}
|
||||
/// Writes a new [LeaderboardEntry].
|
||||
///
|
||||
/// Should be added when a player finishes a game.
|
||||
/// {endtemplate}
|
||||
class LeaderboardEntryAdded extends LeaderboardEvent {
|
||||
/// {@macro leaderboard_entry_added}
|
||||
const LeaderboardEntryAdded({required this.entry});
|
||||
|
||||
/// [LeaderboardEntry] to be written to the remote storage.
|
||||
final LeaderboardEntry entry;
|
||||
|
||||
@override
|
||||
List<Object?> get props => [entry];
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
// ignore_for_file: public_member_api_docs
|
||||
|
||||
part of 'leaderboard_bloc.dart';
|
||||
|
||||
/// Defines the request status.
|
||||
enum LeaderboardStatus {
|
||||
/// Request is being loaded.
|
||||
loading,
|
||||
|
||||
/// Request was processed successfully and received a valid response.
|
||||
success,
|
||||
|
||||
/// Request was processed unsuccessfully and received an error.
|
||||
error,
|
||||
}
|
||||
|
||||
/// {@template leaderboard_state}
|
||||
/// Represents the state of the leaderboard.
|
||||
/// {@endtemplate}
|
||||
class LeaderboardState extends Equatable {
|
||||
/// {@macro leaderboard_state}
|
||||
const LeaderboardState({
|
||||
required this.status,
|
||||
required this.ranking,
|
||||
required this.leaderboard,
|
||||
});
|
||||
|
||||
const LeaderboardState.initial()
|
||||
: status = LeaderboardStatus.loading,
|
||||
ranking = const LeaderboardRanking(
|
||||
ranking: 0,
|
||||
outOf: 0,
|
||||
),
|
||||
leaderboard = const [];
|
||||
|
||||
/// The current [LeaderboardStatus] of the state.
|
||||
final LeaderboardStatus status;
|
||||
|
||||
/// Rank of the current player.
|
||||
final LeaderboardRanking ranking;
|
||||
|
||||
/// List of top-ranked players.
|
||||
final List<LeaderboardEntry> leaderboard;
|
||||
|
||||
@override
|
||||
List<Object> get props => [status, ranking, leaderboard];
|
||||
|
||||
LeaderboardState copyWith({
|
||||
LeaderboardStatus? status,
|
||||
LeaderboardRanking? ranking,
|
||||
List<LeaderboardEntry>? leaderboard,
|
||||
}) {
|
||||
return LeaderboardState(
|
||||
status: status ?? this.status,
|
||||
ranking: ranking ?? this.ranking,
|
||||
leaderboard: leaderboard ?? this.leaderboard,
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
export 'bloc/leaderboard_bloc.dart';
|
@ -0,0 +1,166 @@
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:leaderboard_repository/leaderboard_repository.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball/leaderboard/leaderboard.dart';
|
||||
|
||||
class MockLeaderboardRepository extends Mock implements LeaderboardRepository {}
|
||||
|
||||
void main() {
|
||||
group('LeaderboardBloc', () {
|
||||
late LeaderboardRepository leaderboardRepository;
|
||||
|
||||
setUp(() {
|
||||
leaderboardRepository = MockLeaderboardRepository();
|
||||
});
|
||||
|
||||
test('initial state has state loading no ranking and empty leaderboard',
|
||||
() {
|
||||
final bloc = LeaderboardBloc(leaderboardRepository);
|
||||
expect(bloc.state.status, equals(LeaderboardStatus.loading));
|
||||
expect(bloc.state.ranking.ranking, equals(0));
|
||||
expect(bloc.state.ranking.outOf, equals(0));
|
||||
expect(bloc.state.leaderboard.isEmpty, isTrue);
|
||||
});
|
||||
|
||||
group('Top10Fetched', () {
|
||||
const top10Scores = [
|
||||
2500,
|
||||
2200,
|
||||
2200,
|
||||
2000,
|
||||
1800,
|
||||
1400,
|
||||
1300,
|
||||
1000,
|
||||
600,
|
||||
300,
|
||||
100,
|
||||
];
|
||||
|
||||
final top10Leaderboard = top10Scores
|
||||
.map(
|
||||
(score) => LeaderboardEntry(
|
||||
playerInitials: 'user$score',
|
||||
score: score,
|
||||
character: CharacterType.dash,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
|
||||
blocTest<LeaderboardBloc, LeaderboardState>(
|
||||
'emits [loading, success] statuses '
|
||||
'when fetchTop10Leaderboard succeeds',
|
||||
setUp: () {
|
||||
when(() => leaderboardRepository.fetchTop10Leaderboard()).thenAnswer(
|
||||
(_) async => top10Leaderboard,
|
||||
);
|
||||
},
|
||||
build: () => LeaderboardBloc(leaderboardRepository),
|
||||
act: (bloc) => bloc.add(Top10Fetched()),
|
||||
expect: () => [
|
||||
LeaderboardState.initial(),
|
||||
isA<LeaderboardState>()
|
||||
..having(
|
||||
(element) => element.status,
|
||||
'status',
|
||||
equals(LeaderboardStatus.success),
|
||||
)
|
||||
..having(
|
||||
(element) => element.leaderboard.length,
|
||||
'leaderboard',
|
||||
equals(top10Leaderboard.length),
|
||||
)
|
||||
],
|
||||
verify: (_) =>
|
||||
verify(() => leaderboardRepository.fetchTop10Leaderboard())
|
||||
.called(1),
|
||||
);
|
||||
|
||||
blocTest<LeaderboardBloc, LeaderboardState>(
|
||||
'emits [loading, error] statuses '
|
||||
'when fetchTop10Leaderboard fails',
|
||||
setUp: () {
|
||||
when(() => leaderboardRepository.fetchTop10Leaderboard()).thenThrow(
|
||||
Exception(),
|
||||
);
|
||||
},
|
||||
build: () => LeaderboardBloc(leaderboardRepository),
|
||||
act: (bloc) => bloc.add(Top10Fetched()),
|
||||
expect: () => <LeaderboardState>[
|
||||
LeaderboardState.initial(),
|
||||
LeaderboardState.initial().copyWith(status: LeaderboardStatus.error),
|
||||
],
|
||||
verify: (_) =>
|
||||
verify(() => leaderboardRepository.fetchTop10Leaderboard())
|
||||
.called(1),
|
||||
errors: () => [isA<Exception>()],
|
||||
);
|
||||
});
|
||||
|
||||
group('LeaderboardEntryAdded', () {
|
||||
final leaderboardEntry = LeaderboardEntry(
|
||||
playerInitials: 'ABC',
|
||||
score: 1500,
|
||||
character: CharacterType.dash,
|
||||
);
|
||||
|
||||
final ranking = LeaderboardRanking(ranking: 3, outOf: 4);
|
||||
|
||||
blocTest<LeaderboardBloc, LeaderboardState>(
|
||||
'emits [loading, success] statuses '
|
||||
'when addLeaderboardEntry succeeds',
|
||||
setUp: () {
|
||||
when(
|
||||
() => leaderboardRepository.addLeaderboardEntry(leaderboardEntry),
|
||||
).thenAnswer(
|
||||
(_) async => ranking,
|
||||
);
|
||||
},
|
||||
build: () => LeaderboardBloc(leaderboardRepository),
|
||||
act: (bloc) => bloc.add(LeaderboardEntryAdded(entry: leaderboardEntry)),
|
||||
expect: () => [
|
||||
LeaderboardState.initial(),
|
||||
isA<LeaderboardState>()
|
||||
..having(
|
||||
(element) => element.status,
|
||||
'status',
|
||||
equals(LeaderboardStatus.success),
|
||||
)
|
||||
..having(
|
||||
(element) => element.ranking,
|
||||
'ranking',
|
||||
equals(ranking),
|
||||
)
|
||||
],
|
||||
verify: (_) => verify(
|
||||
() => leaderboardRepository.addLeaderboardEntry(leaderboardEntry),
|
||||
).called(1),
|
||||
);
|
||||
|
||||
blocTest<LeaderboardBloc, LeaderboardState>(
|
||||
'emits [loading, error] statuses '
|
||||
'when addLeaderboardEntry fails',
|
||||
setUp: () {
|
||||
when(
|
||||
() => leaderboardRepository.addLeaderboardEntry(leaderboardEntry),
|
||||
).thenThrow(
|
||||
Exception(),
|
||||
);
|
||||
},
|
||||
build: () => LeaderboardBloc(leaderboardRepository),
|
||||
act: (bloc) => bloc.add(LeaderboardEntryAdded(entry: leaderboardEntry)),
|
||||
expect: () => <LeaderboardState>[
|
||||
LeaderboardState.initial(),
|
||||
LeaderboardState.initial().copyWith(status: LeaderboardStatus.error),
|
||||
],
|
||||
verify: (_) => verify(
|
||||
() => leaderboardRepository.addLeaderboardEntry(leaderboardEntry),
|
||||
).called(1),
|
||||
errors: () => [isA<Exception>()],
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:leaderboard_repository/leaderboard_repository.dart';
|
||||
import 'package:pinball/leaderboard/leaderboard.dart';
|
||||
|
||||
void main() {
|
||||
group('GameEvent', () {
|
||||
group('Top10Fetched', () {
|
||||
test('can be instantiated', () {
|
||||
expect(const Top10Fetched(), isNotNull);
|
||||
});
|
||||
|
||||
test('supports value equality', () {
|
||||
expect(
|
||||
Top10Fetched(),
|
||||
equals(const Top10Fetched()),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('LeaderboardEntryAdded', () {
|
||||
const leaderboardEntry = LeaderboardEntry(
|
||||
playerInitials: 'ABC',
|
||||
score: 1500,
|
||||
character: CharacterType.dash,
|
||||
);
|
||||
|
||||
test('can be instantiated', () {
|
||||
expect(const LeaderboardEntryAdded(entry: leaderboardEntry), isNotNull);
|
||||
});
|
||||
|
||||
test('supports value equality', () {
|
||||
expect(
|
||||
LeaderboardEntryAdded(entry: leaderboardEntry),
|
||||
equals(const LeaderboardEntryAdded(entry: leaderboardEntry)),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:leaderboard_repository/leaderboard_repository.dart';
|
||||
import 'package:pinball/leaderboard/leaderboard.dart';
|
||||
|
||||
void main() {
|
||||
group('LeaderboardState', () {
|
||||
test('supports value equality', () {
|
||||
expect(
|
||||
LeaderboardState.initial(),
|
||||
equals(
|
||||
LeaderboardState.initial(),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
group('constructor', () {
|
||||
test('can be instantiated', () {
|
||||
expect(
|
||||
LeaderboardState.initial(),
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('copyWith', () {
|
||||
const leaderboardEntry = LeaderboardEntry(
|
||||
playerInitials: 'ABC',
|
||||
score: 1500,
|
||||
character: CharacterType.dash,
|
||||
);
|
||||
|
||||
test(
|
||||
'copies correctly '
|
||||
'when no argument specified',
|
||||
() {
|
||||
const leaderboardState = LeaderboardState.initial();
|
||||
expect(
|
||||
leaderboardState.copyWith(),
|
||||
equals(leaderboardState),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'copies correctly '
|
||||
'when all arguments specified',
|
||||
() {
|
||||
const leaderboardState = LeaderboardState.initial();
|
||||
final otherLeaderboardState = LeaderboardState(
|
||||
status: LeaderboardStatus.success,
|
||||
ranking: LeaderboardRanking(ranking: 0, outOf: 0),
|
||||
leaderboard: const [leaderboardEntry],
|
||||
);
|
||||
expect(leaderboardState, isNot(equals(otherLeaderboardState)));
|
||||
|
||||
expect(
|
||||
leaderboardState.copyWith(
|
||||
status: otherLeaderboardState.status,
|
||||
ranking: otherLeaderboardState.ranking,
|
||||
leaderboard: otherLeaderboardState.leaderboard,
|
||||
),
|
||||
equals(otherLeaderboardState),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in new issue