- Introduced MatchRepository and MatchResultsViewModel to handle match data. - Replaced task-related code with match-related functionality in the app. - Added match results view and updated navigation to reflect changes. - Removed task-related models, services, and view models. - Added JSON data for match results and integrated it into the app. - Updated tests to validate match results loading and UI behavior.
129 lines
3.3 KiB
Dart
129 lines
3.3 KiB
Dart
enum CardType { red, yellow, green }
|
|
|
|
enum Team { home, away }
|
|
|
|
enum GoalType { fieldGoal, shortCorner, penaltyStroke }
|
|
|
|
class Card {
|
|
final String datetime;
|
|
final int player;
|
|
final CardType cardType;
|
|
final int seconds;
|
|
final Team team;
|
|
final int duration;
|
|
|
|
Card({
|
|
required this.datetime,
|
|
required this.player,
|
|
required this.cardType,
|
|
required this.seconds,
|
|
required this.team,
|
|
required this.duration,
|
|
});
|
|
}
|
|
|
|
class Goal {
|
|
final String datetime;
|
|
final int player;
|
|
final GoalType goalType;
|
|
final int seconds;
|
|
final Team team;
|
|
|
|
Goal({
|
|
required this.datetime,
|
|
required this.player,
|
|
required this.goalType,
|
|
required this.seconds,
|
|
required this.team,
|
|
});
|
|
|
|
}
|
|
|
|
class Match {
|
|
Match({
|
|
required this.datetime,
|
|
required this.homeScore,
|
|
required this.awayScore,
|
|
required this.goals,
|
|
required this.cards,
|
|
});
|
|
|
|
final String datetime;
|
|
final int homeScore;
|
|
final int awayScore;
|
|
final List<Goal> goals;
|
|
final List<Card> cards;
|
|
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
'datetime': datetime,
|
|
'homeScore': homeScore,
|
|
'awayScore': awayScore,
|
|
'goals': goals.map((goal) => {
|
|
'datetime': goal.datetime,
|
|
'player': goal.player,
|
|
'goal_type': goal.goalType.index,
|
|
'elapsed': goal.seconds,
|
|
'team': goal.team.index,
|
|
}).toList(),
|
|
'cards': cards.map((card) => {
|
|
'datetime': card.datetime,
|
|
'player': card.player,
|
|
'card_type': card.cardType.index,
|
|
'elapsed': card.seconds,
|
|
'team': card.team.index,
|
|
'duration': card.duration,
|
|
}).toList(),
|
|
};
|
|
}
|
|
|
|
factory Match.fromJson(Map<String, dynamic> json) {
|
|
return Match(
|
|
datetime: json['datetime'] as String,
|
|
homeScore: json['homeScore'] as int,
|
|
awayScore: json['awayScore'] as int,
|
|
goals: ((json['goals'] as List<dynamic>?) ?? []).map((goalJson) {
|
|
final goal = goalJson as Map<String, dynamic>;
|
|
return Goal(
|
|
datetime: goal['datetime'] as String,
|
|
player: goal['player'] as int,
|
|
goalType: _enumValue(
|
|
GoalType.values,
|
|
goal['goal_type'] ?? goal['goalType'],
|
|
GoalType.fieldGoal,
|
|
),
|
|
seconds: (goal['elapsed'] ?? goal['seconds'] ?? 0) as int,
|
|
team: _enumValue(Team.values, goal['team'], Team.home),
|
|
);
|
|
}).toList(),
|
|
cards: ((json['cards'] as List<dynamic>?) ?? []).map((cardJson) {
|
|
final card = cardJson as Map<String, dynamic>;
|
|
return Card(
|
|
datetime: card['datetime'] as String,
|
|
player: card['player'] as int,
|
|
cardType: _enumValue(
|
|
CardType.values,
|
|
card['card_type'] ?? card['cardType'],
|
|
CardType.red,
|
|
),
|
|
seconds: (card['elapsed'] ?? card['seconds'] ?? 0) as int,
|
|
team: _enumValue(Team.values, card['team'], Team.home),
|
|
duration: card['duration'] as int? ?? 0,
|
|
);
|
|
}).toList(),
|
|
);
|
|
}
|
|
|
|
static T _enumValue<T extends Enum>(List<T> values, dynamic value, T fallback) {
|
|
if (value is int && value >= 0 && value < values.length) {
|
|
return values[value];
|
|
}
|
|
if (value is String) {
|
|
for (final item in values) {
|
|
if (item.name == value.split('.').last) return item;
|
|
}
|
|
}
|
|
return fallback;
|
|
}
|
|
}
|