Files
SDKmuta/lib/screens/spectator_screen.dart
T
2026-11-27 14:37:44 +01:00

235 lines
8.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_mjpeg/flutter_mjpeg.dart';
import '../services/app_settings.dart';
import '../services/wifi_binding.dart';
import '../theme/app_theme.dart';
import '../widgets/ui.dart';
import 'stream_screen.dart';
/// Opazuj: every chosen camera of the current location live, two per row.
class SpectatorScreen extends StatefulWidget {
final String selectedLanguage;
const SpectatorScreen({super.key, required this.selectedLanguage});
@override
State<SpectatorScreen> createState() => _SpectatorScreenState();
}
class _SpectatorScreenState extends State<SpectatorScreen> {
final Set<int> _hidden = {}; // positions switched off in "Izberi kamere"
late final Future<void> _wifiReady;
bool _ready = false;
@override
void initState() {
super.initState();
_wifiReady = WifiBinding.acquire();
// Streams must start after traffic is pinned to Wi-Fi
_wifiReady.then((_) {
if (mounted) setState(() => _ready = true);
});
}
@override
void dispose() {
WifiBinding.release();
super.dispose();
}
String _t(String key) => AppSettings.instance.t(key);
String? get _location {
final cams = AppSettings.instance.cameras;
if (cams.locations.isEmpty) return null;
return cams.locations.contains(cams.activeLocation) ? cams.activeLocation : cams.locations.first;
}
// Positions with a camera at the location, with their address
List<(int, String)> get _cameras {
final s = AppSettings.instance;
final location = _location;
if (location == null) return const [];
return [
for (int p = 1; p <= s.cameras.positions; p++)
if (s.cameras.ipFor(p, location).isNotEmpty) (p, s.cameras.ipFor(p, location)),
];
}
void _showCameraSelection() {
final c = Palette.current;
final s = AppSettings.instance;
showAppSheet(
context,
title: _t('Select Cameras'),
subtitle: _location,
builder: (sheet) => StatefulBuilder(
builder: (context, setSheet) => Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
for (int p = 1; p <= s.cameras.positions; p++)
Builder(builder: (_) {
final ip = _location == null ? '' : s.cameras.ipFor(p, _location!);
final enabled = ip.isNotEmpty;
final on = enabled && !_hidden.contains(p);
void toggle() {
if (!enabled) return;
setState(() => on ? _hidden.add(p) : _hidden.remove(p));
setSheet(() {});
}
return InkWell(
onTap: toggle,
child: Container(
height: 52,
decoration: BoxDecoration(border: p > 1 ? Border(top: BorderSide(color: c.line2)) : null),
child: Row(
children: [
SizedBox.square(
dimension: 24,
child: Checkbox(value: on, onChanged: enabled ? (_) => toggle() : null),
),
const SizedBox(width: 14),
Expanded(
child: Text('${_t('Camera')} $p',
style: TextStyle(fontSize: 15, fontWeight: FontWeight.w500, color: enabled ? c.ink : c.faint)),
),
Text(enabled ? ip.split(':').first : _t('Not set up'),
style: TextStyle(
fontSize: 12, fontFamily: enabled ? AppTheme.mono : null, color: enabled ? c.muted : c.faint)),
],
),
),
);
}),
const SizedBox(height: 16),
PrimaryButton(label: _t('Done'), onTap: () => Navigator.pop(sheet)),
],
),
),
);
}
String _cameraCount(int n) {
if (AppSettings.instance.language == 'Slovenian') {
final mod = n % 100;
return '$n ${mod == 1 ? 'kamera' : mod == 2 ? 'kameri' : (mod == 3 || mod == 4) ? 'kamere' : 'kamer'}';
}
return _t('{n} cameras').replaceAll('{n}', '$n');
}
@override
Widget build(BuildContext context) {
final c = Palette.current;
final visible = [for (final cam in _cameras) if (!_hidden.contains(cam.$1)) cam];
final location = _location;
return Scaffold(
body: Column(
children: [
BackHeader(
title: _t('Spectate'),
subtitle: [_cameraCount(visible.length), if (location != null) location].join(' · '),
actions: [
Material(
color: c.tile,
borderRadius: BorderRadius.circular(12),
clipBehavior: Clip.antiAlias,
child: InkWell(
onTap: _showCameraSelection,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
child: Row(
children: [
Icon(Icons.grid_view_rounded, size: 17, color: c.ink),
const SizedBox(width: 6),
Text(_t('Choose'), style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600, color: c.ink)),
],
),
),
),
),
],
),
Expanded(
child: !_ready
? const Center(child: CircularProgressIndicator())
: LayoutBuilder(builder: (context, box) {
final w = (box.maxWidth - 2 * AppTheme.edge - 12) / 2;
return SingleChildScrollView(
padding: EdgeInsets.fromLTRB(AppTheme.edge, 4, AppTheme.edge, 24 + MediaQuery.paddingOf(context).bottom),
child: Wrap(
spacing: 12,
runSpacing: 12,
children: [
for (final (p, ip) in visible) SizedBox(width: w, child: _cameraCard(c, p, ip, location!)),
],
),
);
}),
),
],
),
);
}
Widget _cameraCard(Palette c, int position, String ip, String location) {
final url = ip.startsWith(RegExp(r'https?://')) ? ip : 'http://$ip';
void open() => Navigator.push(
context,
MaterialPageRoute(builder: (_) => CameraStreamScreen(cameraIndex: position, ip: ip, location: location)),
);
return AppCard(
padding: EdgeInsets.zero,
onTap: open,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
AspectRatio(
aspectRatio: 0.78,
child: Stack(
fit: StackFit.expand,
children: [
ColoredBox(
color: Colors.black,
child: Mjpeg(
stream: url,
isLive: true,
fit: BoxFit.cover,
error: (context, error, stackTrace) => ColoredBox(
color: c.tile,
child: Icon(Icons.videocam_off_outlined, color: c.muted, size: 30),
),
),
),
Positioned(
top: 10,
left: 10,
child: StatusChip(label: _t('Live'), dot: true, color: c.red, background: Colors.black.withValues(alpha: 0.55)),
),
],
),
),
Padding(
padding: const EdgeInsets.fromLTRB(12, 10, 10, 12),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('${_t('Camera')} $position', style: TextStyle(fontSize: 14.5, fontWeight: FontWeight.w700, color: c.ink)),
Text(location, maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: 11.5, color: c.muted)),
],
),
),
Icon(Icons.open_in_new_rounded, size: 17, color: c.muted),
],
),
),
],
),
);
}
}