306 lines
11 KiB
Dart
306 lines
11 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:intl/intl.dart';
|
||
import 'package:url_launcher/url_launcher.dart';
|
||
import '../services/app_settings.dart';
|
||
import '../services/league_service.dart';
|
||
import '../theme/app_theme.dart';
|
||
import '../widgets/ui.dart';
|
||
import 'session_screen.dart' show targetCount, shotCount;
|
||
|
||
/// Koledar → Liga: the club league from liga.bl3kunja.top – which tournament
|
||
/// is open, the plan of all of them, and the live standings. Read when the
|
||
/// tab is opened (and on pull to refresh), never in the background.
|
||
class LeagueView extends StatefulWidget {
|
||
final LeagueService? service; // tests pass one with a fake client
|
||
const LeagueView({super.key, this.service});
|
||
|
||
@override
|
||
State<LeagueView> createState() => _LeagueViewState();
|
||
}
|
||
|
||
class _LeagueViewState extends State<LeagueView> {
|
||
static const String _ourClub = 'Kovinar'; // our shooters are highlighted
|
||
|
||
late final LeagueService _service = widget.service ?? LeagueService();
|
||
LeagueStandings? _data;
|
||
Object? _error;
|
||
bool _loading = true;
|
||
|
||
String _t(String key) => AppSettings.instance.t(key);
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
_load();
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
_service.dispose();
|
||
super.dispose();
|
||
}
|
||
|
||
Future<void> _load() async {
|
||
setState(() => _loading = true);
|
||
try {
|
||
final data = await _service.standings();
|
||
if (!mounted) return;
|
||
setState(() {
|
||
_data = data;
|
||
_error = null;
|
||
_loading = false;
|
||
});
|
||
} catch (e) {
|
||
if (!mounted) return;
|
||
setState(() {
|
||
_error = e;
|
||
_loading = false;
|
||
});
|
||
}
|
||
}
|
||
|
||
Future<void> _openSite() =>
|
||
launchUrl(Uri.parse('${LeagueService.site}/lestvica'), mode: LaunchMode.externalApplication);
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final data = _data;
|
||
if (data == null) {
|
||
if (_loading) return const Center(child: CircularProgressIndicator());
|
||
return Center(
|
||
child: Padding(
|
||
padding: const EdgeInsets.all(32),
|
||
child: EmptyState(
|
||
icon: Icons.cloud_off_rounded,
|
||
title: _t('Could not load the league'),
|
||
hint: '$_error',
|
||
action: SizedBox(
|
||
width: 180,
|
||
child: PrimaryButton(label: _t('Retry'), icon: Icons.refresh_rounded, height: 46, onTap: _load),
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
return RefreshIndicator(
|
||
onRefresh: _load,
|
||
child: ListView(
|
||
padding: const EdgeInsets.fromLTRB(AppTheme.edge, 0, AppTheme.edge, 24),
|
||
children: [
|
||
_statusCard(data),
|
||
const SizedBox(height: 12),
|
||
_standingsCard(data),
|
||
const SizedBox(height: 12),
|
||
SoftButton(label: _t('Open the league website'), icon: Icons.open_in_new_rounded, onTap: _openSite),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
// Which tournament is open (or next), the plan of all of them, the rules
|
||
Widget _statusCard(LeagueStandings data) {
|
||
final c = Palette.current;
|
||
final open = data.openRound;
|
||
final next = data.nextRound;
|
||
final day = DateFormat('d. M.');
|
||
final (String title, String? sub, Color dot) = open != null
|
||
? (
|
||
_t('{n}. tournament is open').replaceAll('{n}', '${open.number}'),
|
||
[
|
||
if (open.endsAt != null) _t('until {d}').replaceAll('{d}', day.format(open.endsAt!)),
|
||
_t('Entries: {n}').replaceAll('{n}', '${open.submissions}'),
|
||
].join(' · '),
|
||
c.pos,
|
||
)
|
||
: next != null
|
||
? (
|
||
_t('{n}. tournament').replaceAll('{n}', '${next.number}'),
|
||
next.startsAt == null ? null : _t('from {d}').replaceAll('{d}', day.format(next.startsAt!)),
|
||
c.muted,
|
||
)
|
||
: (_t('The league has finished'), null, c.faint);
|
||
return AppCard(
|
||
padding: const EdgeInsets.fromLTRB(16, 14, 16, 14),
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
Kicker(data.league.name),
|
||
const SizedBox(height: 8),
|
||
Row(
|
||
children: [
|
||
Container(width: 9, height: 9, decoration: BoxDecoration(color: dot, shape: BoxShape.circle)),
|
||
const SizedBox(width: 8),
|
||
Expanded(child: Text(title, style: TextStyle(fontSize: 18, fontWeight: FontWeight.w700, color: c.ink))),
|
||
],
|
||
),
|
||
if (sub != null) ...[
|
||
const SizedBox(height: 2),
|
||
Padding(
|
||
padding: const EdgeInsets.only(left: 17),
|
||
child: Text(sub, style: TextStyle(fontSize: 13, color: c.muted)),
|
||
),
|
||
],
|
||
const SizedBox(height: 14),
|
||
Row(
|
||
children: [
|
||
for (int i = 0; i < data.rounds.length; i++) ...[
|
||
if (i > 0) const SizedBox(width: 6),
|
||
Expanded(child: _roundChip(data.rounds[i])),
|
||
],
|
||
],
|
||
),
|
||
const SizedBox(height: 12),
|
||
Text(
|
||
[
|
||
_t('Best {n} count').replaceAll('{n}', '${data.league.counted}'),
|
||
targetCount(data.league.targets),
|
||
shotCount(data.league.shots),
|
||
].join(' · '),
|
||
style: TextStyle(fontSize: 12, color: c.muted),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
// One tournament of the plan: open in the accent, done ticked, to come faint
|
||
Widget _roundChip(LeagueRound r) {
|
||
final c = Palette.current;
|
||
final month = r.startsAt == null
|
||
? ''
|
||
: DateFormat('LLL', AppSettings.instance.dateLocale).format(r.startsAt!).replaceAll('.', '');
|
||
final fg = r.isOpen ? c.onAccent : (r.isDone ? c.ink2 : c.faint);
|
||
return Container(
|
||
height: 50,
|
||
decoration: BoxDecoration(
|
||
color: r.isOpen ? c.accent : (r.isDone ? c.tile : Colors.transparent),
|
||
borderRadius: BorderRadius.circular(12),
|
||
border: r.isUpcoming ? Border.all(color: c.line) : null,
|
||
),
|
||
child: Column(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
Row(
|
||
mainAxisSize: MainAxisSize.min,
|
||
children: [
|
||
if (r.isDone) ...[Icon(Icons.check_rounded, size: 13, color: fg), const SizedBox(width: 2)],
|
||
Text('${r.number}.', style: TextStyle(fontSize: 15, fontWeight: FontWeight.w800, color: fg)),
|
||
],
|
||
),
|
||
Text(month, style: TextStyle(fontSize: 10.5, fontWeight: FontWeight.w600, color: fg.withValues(alpha: 0.8))),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
Widget _standingsCard(LeagueStandings data) {
|
||
final c = Palette.current;
|
||
// Only tournaments that have started get a column, so early in the season
|
||
// names have room; the plan of all of them is in the status card
|
||
final rounds = data.rounds.where((r) => !r.isUpcoming).length.clamp(1, data.rounds.length);
|
||
return AppCard(
|
||
padding: const EdgeInsets.fromLTRB(12, 14, 12, 6),
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||
children: [
|
||
Padding(
|
||
padding: const EdgeInsets.only(left: 4, bottom: 6),
|
||
child: Row(
|
||
children: [
|
||
Expanded(child: Kicker(_t('Standings'))),
|
||
for (int i = 1; i <= rounds; i++)
|
||
SizedBox(width: 30, child: Text('$i.', textAlign: TextAlign.center, style: AppTheme.kicker(c))),
|
||
SizedBox(width: 48, child: Text('Σ', textAlign: TextAlign.right, style: AppTheme.kicker(c))),
|
||
],
|
||
),
|
||
),
|
||
if (data.players.isEmpty)
|
||
Padding(
|
||
padding: const EdgeInsets.symmetric(vertical: 20),
|
||
child: Text(_t('No results yet'), textAlign: TextAlign.center, style: TextStyle(color: c.muted)),
|
||
),
|
||
for (final p in data.players) _playerRow(p, rounds),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
Widget _playerRow(LeaguePlayer p, int rounds) {
|
||
final c = Palette.current;
|
||
final ours = p.club.contains(_ourClub);
|
||
// 1–3 in gold, silver and bronze
|
||
final medal = switch (p.rank) {
|
||
1 => c.gold,
|
||
2 => const Color(0xFFB8BEC6),
|
||
3 => const Color(0xFFCD8B4E),
|
||
_ => null,
|
||
};
|
||
return Container(
|
||
margin: const EdgeInsets.only(bottom: 4),
|
||
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 7),
|
||
decoration: BoxDecoration(
|
||
color: ours ? c.accentSoft : Colors.transparent,
|
||
borderRadius: BorderRadius.circular(10),
|
||
),
|
||
child: Row(
|
||
children: [
|
||
Container(
|
||
width: 26,
|
||
height: 26,
|
||
alignment: Alignment.center,
|
||
decoration: BoxDecoration(
|
||
color: medal?.withValues(alpha: 0.22) ?? c.tile,
|
||
shape: BoxShape.circle,
|
||
),
|
||
child: Text('${p.rank}',
|
||
style: TextStyle(fontSize: 12, fontWeight: FontWeight.w800, color: medal ?? c.ink2, fontFeatures: AppTheme.tnum)),
|
||
),
|
||
const SizedBox(width: 10),
|
||
Expanded(
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
Text(p.name,
|
||
maxLines: 2,
|
||
overflow: TextOverflow.ellipsis,
|
||
style: TextStyle(fontSize: 14, fontWeight: FontWeight.w700, color: c.ink)),
|
||
Text(p.club,
|
||
maxLines: 1,
|
||
overflow: TextOverflow.ellipsis,
|
||
style: TextStyle(fontSize: 11.5, color: ours ? c.accent : c.muted, fontWeight: ours ? FontWeight.w600 : null)),
|
||
],
|
||
),
|
||
),
|
||
// Each tournament: counted ones plain, dropped ones struck through
|
||
for (int i = 0; i < rounds; i++)
|
||
SizedBox(
|
||
width: 30,
|
||
child: Text(
|
||
i < p.cells.length && p.cells[i] != null ? '${p.cells[i]!.score}' : '–',
|
||
textAlign: TextAlign.center,
|
||
style: TextStyle(
|
||
fontSize: 11.5,
|
||
fontWeight: FontWeight.w600,
|
||
fontFeatures: AppTheme.tnum,
|
||
color: i < p.cells.length && p.cells[i] != null ? (p.cells[i]!.counted ? c.ink2 : c.faint) : c.faint,
|
||
decoration: i < p.cells.length && p.cells[i] != null && !p.cells[i]!.counted ? TextDecoration.lineThrough : null,
|
||
),
|
||
),
|
||
),
|
||
SizedBox(
|
||
width: 48,
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.end,
|
||
children: [
|
||
Text('${p.finalScore}',
|
||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w800, color: c.ink, fontFeatures: AppTheme.tnum)),
|
||
Text('${p.tens}×10', style: TextStyle(fontSize: 10.5, color: c.muted, fontFeatures: AppTheme.tnum)),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
}
|