update
This commit is contained in:
1 parent
97c10c8d12
commit
212410f716
10 files changed
+1061
-32
No files matched your search
@@ -1,11 +1,20 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'dart:async';
|
||||
|
||||
import '../../../../data/repositories/match_repository.dart';
|
||||
import '../../../../data/services/match_data_service.dart';
|
||||
import '../../../../domain/models/hockey_game.dart';
|
||||
|
||||
class MatchResultsViewModel extends ChangeNotifier {
|
||||
MatchResultsViewModel({required this.repository});
|
||||
MatchResultsViewModel({
|
||||
required MatchRepository repository,
|
||||
Duration updateInterval = const Duration(seconds: 1),
|
||||
}) : _dataService = MatchDataService(
|
||||
repository: repository,
|
||||
updateInterval: updateInterval,
|
||||
);
|
||||
|
||||
final MatchRepository repository;
|
||||
final MatchDataService _dataService;
|
||||
List<Match> _matches = [];
|
||||
bool _isLoading = false;
|
||||
String? _errorMessage;
|
||||
@@ -14,13 +23,31 @@ class MatchResultsViewModel extends ChangeNotifier {
|
||||
bool get isLoading => _isLoading;
|
||||
String? get errorMessage => _errorMessage;
|
||||
|
||||
Future<void> removeMatch(Match match) async {
|
||||
try {
|
||||
await _dataService.removeMatch(match);
|
||||
} catch (_) {
|
||||
_errorMessage = 'Unable to save match results.';
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loadMatches() async {
|
||||
_isLoading = true;
|
||||
_errorMessage = null;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
_matches = await repository.loadMatches();
|
||||
await _dataService.start(
|
||||
onMatchesChanged: (matches) {
|
||||
_matches = matches;
|
||||
notifyListeners();
|
||||
},
|
||||
onError: (_) {
|
||||
_errorMessage = 'Unable to save match results.';
|
||||
notifyListeners();
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
_errorMessage = 'Unable to load match results.';
|
||||
} finally {
|
||||
@@ -28,4 +55,10 @@ class MatchResultsViewModel extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_dataService.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
@@ -16,21 +16,47 @@ class MatchResultsView extends StatelessWidget {
|
||||
return ListenableBuilder(
|
||||
listenable: viewModel,
|
||||
builder: (context, _) {
|
||||
final matches = viewModel.matches;
|
||||
|
||||
if (viewModel.isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (viewModel.errorMessage != null) {
|
||||
return Center(child: Text(viewModel.errorMessage!));
|
||||
}
|
||||
if (viewModel.matches.isEmpty) {
|
||||
if (matches.isEmpty) {
|
||||
return const Center(child: Text('No match results available.'));
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: viewModel.matches.length,
|
||||
itemBuilder: (context, index) =>
|
||||
_MatchCard(match: viewModel.matches[index]),
|
||||
itemCount: matches.length,
|
||||
findChildIndexCallback: (key) {
|
||||
if (key is! ValueKey<Match>) return null;
|
||||
final index = matches.indexWhere(
|
||||
(match) => identical(match, key.value),
|
||||
);
|
||||
return index == -1 ? null : index;
|
||||
},
|
||||
itemBuilder: (context, index) {
|
||||
final match = matches[index];
|
||||
|
||||
return Dismissible(
|
||||
key: ValueKey(match),
|
||||
direction: DismissDirection.endToStart,
|
||||
background: Container(
|
||||
alignment: Alignment.centerRight,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: const Icon(Icons.delete_outline, color: Colors.white),
|
||||
),
|
||||
onDismissed: (_) => viewModel.removeMatch(match),
|
||||
child: _MatchCard(match: match),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -54,10 +80,7 @@ class _MatchCardState extends State<_MatchCard> {
|
||||
final match = widget.match;
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
final textTheme = Theme.of(context).textTheme;
|
||||
final date = DateTime.tryParse(match.datetime)?.toLocal();
|
||||
final dateLabel = date == null
|
||||
? match.datetime
|
||||
: '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')} ${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}';
|
||||
final dateLabel = _exportDate(match.datetime);
|
||||
|
||||
return Card(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
@@ -245,7 +268,7 @@ String _teamName(Team team) => team == Team.home ? 'Home' : 'Away';
|
||||
String _matchShareText(Match match) {
|
||||
final buffer = StringBuffer()
|
||||
..writeln('Match result')
|
||||
..writeln(match.datetime)
|
||||
..writeln(_exportDate(match.datetime))
|
||||
..writeln('Home ${match.homeScore} - ${match.awayScore} Away');
|
||||
|
||||
if (match.goals.isNotEmpty) {
|
||||
@@ -277,6 +300,33 @@ String _formatTime(int seconds) {
|
||||
return '$minute:$second';
|
||||
}
|
||||
|
||||
String _exportDate(String rawDateTime) {
|
||||
final parsed = DateTime.tryParse(rawDateTime)?.toLocal();
|
||||
if (parsed == null) {
|
||||
return rawDateTime;
|
||||
}
|
||||
|
||||
final weekday = _weekdayName(parsed.weekday);
|
||||
final day = parsed.day.toString().padLeft(2, '0');
|
||||
final month = parsed.month.toString().padLeft(2, '0');
|
||||
final year = parsed.year.toString().substring(2);
|
||||
final hour = parsed.hour.toString().padLeft(2, '0');
|
||||
final minute = parsed.minute.toString().padLeft(2, '0');
|
||||
|
||||
return '$weekday $day-$month-$year $hour:$minute';
|
||||
}
|
||||
|
||||
String _weekdayName(int weekday) => switch (weekday) {
|
||||
DateTime.monday => 'Mon',
|
||||
DateTime.tuesday => 'Tue',
|
||||
DateTime.wednesday => 'Wed',
|
||||
DateTime.thursday => 'Thu',
|
||||
DateTime.friday => 'Fri',
|
||||
DateTime.saturday => 'Sat',
|
||||
DateTime.sunday => 'Sun',
|
||||
_ => 'Unknown',
|
||||
};
|
||||
|
||||
String _goalTypeName(GoalType type) => switch (type) {
|
||||
GoalType.fieldGoal => 'Field goal',
|
||||
GoalType.shortCorner => 'Short corner',
|
||||
|
||||
Reference in new issue
Block a user