mirror of https://github.com/flutter/pinball.git
refactor: leaderboard model (#78)
* refactor: changed name of LeaderboardEntry at LeaderboardRepository to LeaderboardEntryData * chore: doc and analysis errors * refactor: removed findNested extensions (#77) * Update lib/leaderboard/bloc/leaderboard_bloc.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * Update lib/leaderboard/bloc/leaderboard_bloc.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * fix: leaderboard gen file * refactor: moved leaderboard models to separate path * test: fixed tests with leaderboard model * chore: doc Co-authored-by: Alejandro Santiago <dev@alestiago.com> Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com>pull/88/head
parent
92404fbdba
commit
573e16d0d5
@ -1 +1,2 @@
|
|||||||
export 'bloc/leaderboard_bloc.dart';
|
export 'bloc/leaderboard_bloc.dart';
|
||||||
|
export 'models/leader_board_entry.dart';
|
||||||
|
@ -0,0 +1,80 @@
|
|||||||
|
import 'package:leaderboard_repository/leaderboard_repository.dart';
|
||||||
|
import 'package:pinball_theme/pinball_theme.dart';
|
||||||
|
|
||||||
|
/// {@template leaderboard_entry}
|
||||||
|
/// A model representing a leaderboard entry containing the ranking position,
|
||||||
|
/// player's initials, score, and chosen character.
|
||||||
|
///
|
||||||
|
/// {@endtemplate}
|
||||||
|
class LeaderboardEntry {
|
||||||
|
/// {@macro leaderboard_entry}
|
||||||
|
LeaderboardEntry({
|
||||||
|
required this.rank,
|
||||||
|
required this.playerInitials,
|
||||||
|
required this.score,
|
||||||
|
required this.character,
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Ranking position for [LeaderboardEntry].
|
||||||
|
final String rank;
|
||||||
|
|
||||||
|
/// Player's chosen initials for [LeaderboardEntry].
|
||||||
|
final String playerInitials;
|
||||||
|
|
||||||
|
/// Score for [LeaderboardEntry].
|
||||||
|
final int score;
|
||||||
|
|
||||||
|
/// [CharacterTheme] for [LeaderboardEntry].
|
||||||
|
final AssetGenImage character;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Converts [LeaderboardEntryData] from repository to [LeaderboardEntry].
|
||||||
|
extension LeaderboardEntryDataX on LeaderboardEntryData {
|
||||||
|
/// Conversion method to [LeaderboardEntry]
|
||||||
|
LeaderboardEntry toEntry(int position) {
|
||||||
|
return LeaderboardEntry(
|
||||||
|
rank: position.toString(),
|
||||||
|
playerInitials: playerInitials,
|
||||||
|
score: score,
|
||||||
|
character: character.toTheme.characterAsset,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Converts [CharacterType] to [CharacterTheme] to show on UI character theme
|
||||||
|
/// from repository.
|
||||||
|
extension CharacterTypeX on CharacterType {
|
||||||
|
/// Conversion method to [CharacterTheme]
|
||||||
|
CharacterTheme get toTheme {
|
||||||
|
switch (this) {
|
||||||
|
case CharacterType.dash:
|
||||||
|
return const DashTheme();
|
||||||
|
case CharacterType.sparky:
|
||||||
|
return const SparkyTheme();
|
||||||
|
case CharacterType.android:
|
||||||
|
return const AndroidTheme();
|
||||||
|
case CharacterType.dino:
|
||||||
|
return const DinoTheme();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Converts [CharacterTheme] to [CharacterType] to persist at repository the
|
||||||
|
/// character theme from UI.
|
||||||
|
extension CharacterThemeX on CharacterTheme {
|
||||||
|
/// Conversion method to [CharacterType]
|
||||||
|
CharacterType get toType {
|
||||||
|
switch (runtimeType) {
|
||||||
|
case DashTheme:
|
||||||
|
return CharacterType.dash;
|
||||||
|
case SparkyTheme:
|
||||||
|
return CharacterType.sparky;
|
||||||
|
case AndroidTheme:
|
||||||
|
return CharacterType.android;
|
||||||
|
case DinoTheme:
|
||||||
|
return CharacterType.dino;
|
||||||
|
default:
|
||||||
|
return CharacterType.dash;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,19 +1,19 @@
|
|||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||||
|
|
||||||
part of 'leaderboard_entry.dart';
|
part of 'leaderboard_entry_data.dart';
|
||||||
|
|
||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
// JsonSerializableGenerator
|
// JsonSerializableGenerator
|
||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
|
|
||||||
LeaderboardEntry _$LeaderboardEntryFromJson(Map<String, dynamic> json) =>
|
LeaderboardEntryData _$LeaderboardEntryFromJson(Map<String, dynamic> json) =>
|
||||||
LeaderboardEntry(
|
LeaderboardEntryData(
|
||||||
playerInitials: json['playerInitials'] as String,
|
playerInitials: json['playerInitials'] as String,
|
||||||
score: json['score'] as int,
|
score: json['score'] as int,
|
||||||
character: $enumDecode(_$CharacterTypeEnumMap, json['character']),
|
character: $enumDecode(_$CharacterTypeEnumMap, json['character']),
|
||||||
);
|
);
|
||||||
|
|
||||||
Map<String, dynamic> _$LeaderboardEntryToJson(LeaderboardEntry instance) =>
|
Map<String, dynamic> _$LeaderboardEntryToJson(LeaderboardEntryData instance) =>
|
||||||
<String, dynamic>{
|
<String, dynamic>{
|
||||||
'playerInitials': instance.playerInitials,
|
'playerInitials': instance.playerInitials,
|
||||||
'score': instance.score,
|
'score': instance.score,
|
@ -1,2 +1,2 @@
|
|||||||
export 'leaderboard_entry.dart';
|
export 'leaderboard_entry_data.dart';
|
||||||
export 'leaderboard_ranking.dart';
|
export 'leaderboard_ranking.dart';
|
||||||
|
Loading…
Reference in new issue