import 'package:flutter/material.dart'; import '../models/camera_setup.dart'; import '../services/app_settings.dart'; import '../theme/app_theme.dart'; import '../widgets/section.dart'; import '../widgets/text_prompt.dart'; import '../widgets/ui.dart'; /// Locations (strelišča), number of positions, and the camera IP of every /// position at every location. class CameraSettingsScreen extends StatelessWidget { const CameraSettingsScreen({super.key}); @override Widget build(BuildContext context) { return ListenableBuilder( listenable: AppSettings.instance, builder: (context, _) { final s = AppSettings.instance; final t = s.t; final setup = s.cameras; final c = Palette.current; const firstPort = CameraSetup.firstPort; return Scaffold( body: Column( children: [ BackHeader(title: t('Cameras'), subtitle: t('Locations, positions and camera addresses')), Expanded( child: ListView( // Keep the last row clear of the system gesture bar (edge-to-edge) padding: EdgeInsets.fromLTRB(AppTheme.edge, 0, AppTheme.edge, 24 + MediaQuery.paddingOf(context).bottom), children: [ SectionHeader(t('Locations')), GroupCard(children: [ for (final location in setup.locations) SettingsTile( icon: location == setup.clubLocation ? Icons.verified_user_outlined : Icons.place_outlined, iconColor: location == setup.clubLocation ? c.accent : c.muted, title: location, subtitle: location == setup.clubLocation ? t('Added with the club code') : null, value: _cameraCount(s, setup.camerasIn(location)), onTap: () => _editLocation(context, location), ), SettingsTile( icon: Icons.add_rounded, title: t('Add location'), titleColor: c.muted, showChevron: false, onTap: () => _addLocation(context), ), ]), SectionHeader(t('Positions')), GroupCard(children: [ SettingsTile( title: t('Number of positions'), trailing: Stepper3( value: '${setup.positions}', valueWidth: 44, onMinus: setup.positions > 1 ? () => s.setPositionCount(setup.positions - 1) : null, onPlus: setup.positions < CameraSetup.maxPositions ? () => s.setPositionCount(setup.positions + 1) : null, ), ), ]), Padding( padding: const EdgeInsets.fromLTRB(4, 10, 4, 0), child: Text(t('One tile on the Shooting screen for each position.'), style: TextStyle(fontSize: 12, color: c.muted)), ), for (final location in setup.locations) ...[ SectionHeader(location), GroupCard(children: [ SettingsTile( icon: Icons.bolt_rounded, iconColor: c.accent, title: t('Set all cameras from one IP'), subtitle: t('Ports {from}–{to}') .replaceAll('{from}', '$firstPort') .replaceAll('{to}', '${firstPort + setup.positions - 1}'), onTap: () => _editHost(context, location), ), for (int p = 1; p <= setup.positions; p++) SettingsTile( title: '${t('Camera')} $p', value: setup.ipFor(p, location).isEmpty ? t('Not set up') : setup.ipFor(p, location), mono: setup.ipFor(p, location).isNotEmpty, valueColor: setup.ipFor(p, location).isEmpty ? c.faint : c.ink2, onTap: () => _editCamera(context, p, location), ), ]), ], ], ), ), ], ), ); }, ); } static String _cameraCount(AppSettings s, int n) { if (s.language == 'Slovenian') { final mod = n % 100; return '$n ${mod == 1 ? 'kamera' : mod == 2 ? 'kameri' : (mod == 3 || mod == 4) ? 'kamere' : 'kamer'}'; } return s.t('{n} cameras').replaceAll('{n}', '$n'); } Future _addLocation(BuildContext context) async { final s = AppSettings.instance; final name = await showTextPrompt(context, title: s.t('Add location'), hint: s.t('e.g. Home')); if (name == null || name.isEmpty) return; if (!await s.addLocation(name) && context.mounted) { ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(s.t('This location already exists')))); } } Future _editLocation(BuildContext context, String location) async { final s = AppSettings.instance; final name = await showTextPrompt( context, title: s.t('Location'), initial: location, resetLabel: s.t('Delete'), onReset: () => s.removeLocation(location), ); if (name != null && name.isNotEmpty && name != location) await s.renameLocation(location, name); } Future _editHost(BuildContext context, String location) async { final s = AppSettings.instance; final first = s.cameras.ipFor(1, location); final host = await showTextPrompt( context, title: '${s.t('Set all cameras from one IP')} · $location', initial: first.split(':').first, mono: true, hint: '192.168.0.10', keyboardType: TextInputType.url, ); if (host != null && host.isNotEmpty) s.setLocationHost(location, host); } Future _editCamera(BuildContext context, int position, String location) async { final s = AppSettings.instance; final ip = await showTextPrompt( context, title: '${s.t('Camera')} $position · $location', initial: s.cameras.ipFor(position, location), mono: true, hint: '192.168.0.10:${CameraSetup.firstPort + position - 1}', keyboardType: TextInputType.url, resetLabel: s.t('Remove'), onReset: () => s.setCameraIp(position, location, ''), ); if (ip != null && ip.isNotEmpty) s.setCameraIp(position, location, ip); } }