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

555 lines
20 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/foundation.dart' show kDebugMode;
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import '../models/holiday_theme.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:url_launcher/url_launcher.dart';
import '../models/target_style.dart';
import '../services/app_settings.dart';
import '../utils/translations.dart';
import '../services/notification_service.dart';
import '../theme/app_theme.dart';
import '../widgets/section.dart';
import '../widgets/target_face.dart';
import '../widgets/text_prompt.dart';
import '../widgets/ui.dart';
import 'camera_settings_screen.dart';
class SettingsScreen extends StatelessWidget {
const SettingsScreen({super.key});
static const _privacyPolicyUrl = 'https://sdkovinarmuta.si/privacy-and-policy/';
static final Future<PackageInfo> _packageInfo = PackageInfo.fromPlatform();
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: AppSettings.instance,
builder: (context, _) {
final s = AppSettings.instance;
final t = s.t;
final c = Palette.current;
return ListView(
padding: EdgeInsets.zero,
children: [
PageHeader(title: t('Settings')),
Padding(
padding: const EdgeInsets.fromLTRB(AppTheme.edge, 0, AppTheme.edge, 28),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SectionHeader(t('Language')),
GroupCard(children: [
SettingsTile(
icon: Icons.language_rounded,
title: t('Language'),
value: t(s.language),
onTap: () => _pickLanguage(context),
),
]),
SectionHeader(t('Appearance')),
GroupCard(children: [
SettingsTile(
icon: Icons.dark_mode_outlined,
title: t('Theme'),
trailing: SizedBox(
width: 196,
child: SegmentedTabs(
height: 36,
accentSelected: true,
items: [SegmentItem(t('System')), SegmentItem(t('Light')), SegmentItem(t('Dark'))],
selected: AppSettings.themeModes.indexOf(s.themeMode),
onSelect: (i) => s.setThemeMode(AppSettings.themeModes[i]),
),
),
),
// Hit colour and target design are picked in one sheet
SettingsTile(
icon: Icons.track_changes_rounded,
title: t('Hits and target'),
valueLeading: Row(
mainAxisSize: MainAxisSize.min,
children: [
_ColorDot(s.bulletColor, size: 16),
const SizedBox(width: 8),
TargetThumb(s.target, size: 26),
],
),
value: s.target.name(s.language),
onTap: () => showAppearanceSheet(context),
),
]),
SectionHeader('${t('Club')} · SD Kovinar Muta'),
GroupCard(children: [
if (s.clubUnlocked)
SettingsTile(
icon: Icons.verified_user_outlined,
title: t('Member access'),
value: t('Unlocked'),
valueColor: c.pos,
showChevron: false,
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(t('Unlocked'), style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500, color: c.pos)),
const SizedBox(width: 8),
Container(
width: 22,
height: 22,
decoration: BoxDecoration(color: c.pos, shape: BoxShape.circle),
child: Icon(Icons.check_rounded, size: 15, color: Palette.onColor(c.pos)),
),
],
),
onTap: () => _confirmLock(context),
)
else
SettingsTile(
icon: Icons.lock_outline_rounded,
title: t('Club code'),
subtitle: t('Unlocks calendar and contacts'),
onTap: () => _enterClubCode(context),
),
]),
if (!s.clubUnlocked)
Padding(
padding: const EdgeInsets.fromLTRB(4, 10, 4, 0),
child: Text(
t('Member access is currently available only for SD Kovinar Muta. Support for other clubs may be added in the future.'),
style: TextStyle(fontSize: 12, height: 1.35, color: c.muted),
),
),
if (s.clubUnlocked) ...[
SectionHeader(t('Notifications')),
GroupCard(children: notificationTiles(context)),
],
SectionHeader(t('Cameras')),
GroupCard(children: [
SettingsTile(
icon: Icons.videocam_outlined,
title: t('Camera Settings'),
value: _camerasSummary(s),
onTap: () => Navigator.of(context).push(
MaterialPageRoute(builder: (_) => const CameraSettingsScreen()),
),
),
]),
SectionHeader(t('About')),
GroupCard(children: [
SettingsTile(
icon: Icons.shield_outlined,
title: t('Privacy policy'),
chevron: Icons.open_in_new_rounded,
onTap: () => launchUrl(Uri.parse(_privacyPolicyUrl), mode: LaunchMode.externalApplication),
),
FutureBuilder<PackageInfo>(
future: _packageInfo,
// Debug builds: long press previews a holiday
builder: (context, snap) => SettingsTile(
icon: Icons.info_outline_rounded,
title: t('Version'),
value: snap.data?.version ?? '',
),
),
]),
// Debug builds only (never in a release): check the holiday looks
if (kDebugMode) ...[
const SectionHeader('Debug'),
GroupCard(children: [
SettingsTile(
icon: Icons.celebration_outlined,
title: 'Holiday preview',
subtitle: 'Shows the app as on a day of the holiday',
value: s.debugToday == null ? 'Today' : (s.holiday?.name ?? 'Today'),
onTap: () => _previewHoliday(context),
),
]),
],
],
),
),
],
);
},
);
}
static void _reschedule() => NotificationService().rescheduleFromCalendar();
// Debug: see the app as on a day of each holiday (Nastavitve → Debug)
static void _previewHoliday(BuildContext context) {
final s = AppSettings.instance;
final year = DateTime.now().year;
showAppSheet(
context,
title: 'Holiday preview (debug)',
builder: (sheet) => Column(
children: [
SheetOption(
label: 'Today',
selected: s.debugToday == null,
showDivider: false,
onTap: () {
s.previewDay(null);
Navigator.pop(sheet);
},
),
for (final h in HolidayTheme.all)
Builder(builder: (_) {
// First day of the holiday this year
final day = h.easter != null
? HolidayTheme.easterDay(year, h.easter!.$1)
: DateTime(year, h.from!.$1, h.from!.$2);
final (start, end) = h.window(day);
return SheetOption(
label: h.name,
subtitle: '${DateFormat('d. M.').format(start)} – ${DateFormat('d. M.').format(end)}',
leading: TargetThumb(TargetStyle.byId(h.targetId), size: 32),
selected: s.holiday == h && s.debugToday != null,
onTap: () {
s.previewDay(day);
Navigator.pop(sheet);
},
);
}),
],
),
);
}
/// Dežuranje, Sestanki, Tekme and "Opomni me"; also shown from the calendar's bell.
static List<Widget> notificationTiles(BuildContext context) {
final s = AppSettings.instance;
final t = s.t;
return [
SettingsSwitchTile(
icon: Icons.shield_outlined,
title: t('Dežuranje'),
value: s.notifyDezuranje,
onChanged: (v) => s.setNotifyDezuranje(v).then((_) => _reschedule()),
),
SettingsSwitchTile(
icon: Icons.person_add_alt_outlined,
title: t('Sestanek'),
value: s.notifySestanek,
onChanged: (v) => s.setNotifySestanek(v).then((_) => _reschedule()),
),
SettingsSwitchTile(
icon: Icons.emoji_events_outlined,
title: t('Tekma'),
value: s.notifyTekma,
onChanged: (v) => s.setNotifyTekma(v).then((_) => _reschedule()),
),
SettingsTile(
icon: Icons.notifications_none_rounded,
title: t('Remind me'),
value: hoursBefore(s.reminderHours),
onTap: () => pickReminder(context),
),
];
}
Future<void> _enterClubCode(BuildContext context) async {
final s = AppSettings.instance;
final code = await showTextPrompt(
context,
title: s.t('Club code'),
message: s.t('Unlocks calendar and contacts.'),
titleIcon: Icons.lock_outline_rounded,
fieldIcon: Icons.key_rounded,
mono: true,
note: s.t('Member access is currently available only for SD Kovinar Muta.'),
confirmLabel: s.t('Unlock'),
confirmIcon: Icons.key_rounded,
);
if (code == null || code.isEmpty || !context.mounted) return;
// Unlocking takes about a second (slow key derivation on purpose)
showDialog(
context: context,
barrierDismissible: false,
builder: (_) => const Center(child: CircularProgressIndicator()),
);
final ok = await s.unlockClub(code);
if (!context.mounted) return;
Navigator.of(context).pop();
if (ok) _reschedule();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(s.t(ok ? 'Club access unlocked' : 'Wrong club code'))));
}
Future<void> _confirmLock(BuildContext context) async {
final s = AppSettings.instance;
final confirmed = await confirmDialog(
context,
title: s.t('Member access'),
message: s.t('Remove club access from this phone?'),
confirm: s.t('Remove'),
);
if (confirmed) {
await s.lockClub();
_reschedule(); // cancels reminders
}
}
static String hoursBefore(int hours) => AppSettings.instance.t('{n} h before').replaceAll('{n}', '$hours');
// "2 strelišči", or a hint when nothing is set up yet.
static String _camerasSummary(AppSettings s) {
final n = s.cameras.locations.length;
if (n == 0) return s.t('Not set up');
if (s.language == 'Slovenian') {
final mod = n % 100;
return '$n ${mod == 1 ? 'strelišče' : mod == 2 ? 'strelišči' : (mod == 3 || mod == 4) ? 'strelišča' : 'strelišč'}';
}
return s.cameras.locations.join(' · ');
}
void _pickLanguage(BuildContext context) {
final s = AppSettings.instance;
showAppSheet(
context,
title: s.t('Select Language'),
builder: (sheet) => Column(
children: [
// Each language in its own name, so it can be found from any language
for (final lang in AppSettings.languages)
SheetOption(
label: AppTranslations.translate(lang['name']!, lang['name']!),
leading: Text(lang['flag']!, style: const TextStyle(fontSize: 22)),
selected: lang['name'] == s.language,
onTap: () {
s.setLanguage(lang['name']!);
Navigator.pop(sheet);
},
),
],
),
);
}
static void pickReminder(BuildContext context) {
final s = AppSettings.instance;
showAppSheet(
context,
title: s.t('Remind me'),
builder: (sheet) => Column(
children: [
for (final h in AppSettings.reminderHourOptions)
SheetOption(
label: hoursBefore(h),
selected: h == s.reminderHours,
onTap: () {
s.setReminderHours(h).then((_) => _reschedule());
Navigator.pop(sheet);
},
),
],
),
);
}
}
/// Videz: hit colour (the accent of the whole app) and the target design.
void showAppearanceSheet(BuildContext context) {
final s = AppSettings.instance;
showAppSheet(
context,
builder: (sheet) => ListenableBuilder(
listenable: s,
builder: (context, _) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SheetTitle(s.t('Appearance'), trailing: TargetThumb(s.target, size: 52)),
Kicker(s.t('Bullet color'), padding: const EdgeInsets.only(bottom: 12)),
LayoutBuilder(builder: (context, box) {
const perRow = 8;
final size = ((box.maxWidth - (perRow - 1) * 10) / perRow).clamp(28.0, 40.0);
return Wrap(
spacing: 10,
runSpacing: 12,
children: [
// During a holiday its colour comes first (and is the one in use)
if (s.holiday != null)
GestureDetector(
onTap: s.useHolidayColor,
child: Tooltip(
message: s.holiday!.name,
child: _ColorDot(s.holiday!.color, size: size, selected: s.holidayColorActive),
),
),
for (final name in AppSettings.bulletColors.keys)
GestureDetector(
onTap: () => s.setBulletColor(name),
child: Tooltip(
message: s.t(name),
child: _ColorDot(s.colorFor(name), size: size,
selected: !s.holidayColorActive && name == s.bulletColorName),
),
),
],
);
}),
Kicker(s.t('Target style'), padding: const EdgeInsets.only(top: 22, bottom: 12)),
if (s.holiday != null) _HolidayCard(holiday: s.holiday!) else const _TargetGrid(styles: TargetStyle.all),
],
);
},
),
);
}
/// Three target designs per row; the chosen one has an accent border and a check.
class _TargetGrid extends StatelessWidget {
final List<TargetStyle> styles;
const _TargetGrid({required this.styles});
@override
Widget build(BuildContext context) {
final s = AppSettings.instance;
final c = Palette.current;
return LayoutBuilder(builder: (context, box) {
final w = (box.maxWidth - 2 * 10) / 3;
return Wrap(
spacing: 10,
runSpacing: 14,
children: [
for (final style in styles)
SizedBox(
width: w,
child: GestureDetector(
onTap: () => s.setTarget(style),
child: Column(
children: [
Stack(
children: [
Container(
height: w * 0.92,
width: w,
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: c.isDark ? c.card2 : c.tile,
borderRadius: BorderRadius.circular(14),
border: Border.all(color: style == s.target ? c.accent : Colors.transparent, width: 2),
),
child: Center(child: TargetThumb(style, size: w * 0.92 - 16)),
),
if (style == s.target)
Positioned(
top: 6,
right: 6,
child: Container(
width: 20,
height: 20,
decoration: BoxDecoration(color: c.accent, shape: BoxShape.circle),
child: Icon(Icons.check_rounded, size: 14, color: c.onAccent),
),
),
],
),
const SizedBox(height: 6),
Text(
style.name(s.language),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12,
fontWeight: style == s.target ? FontWeight.w700 : FontWeight.w500,
color: style == s.target ? c.ink : c.muted,
),
),
],
),
),
),
],
);
});
}
}
/// In the Videz sheet during a holiday: its target, locked until the holiday ends.
class _HolidayCard extends StatelessWidget {
final HolidayTheme holiday;
const _HolidayCard({required this.holiday});
@override
Widget build(BuildContext context) {
final s = AppSettings.instance;
final c = Palette.current;
final (_, end) = holiday.window(s.today);
return AppCard(
color: c.isDark ? c.card2 : c.card,
borderColor: holiday.color.withValues(alpha: 0.5),
padding: const EdgeInsets.all(14),
child: Row(
children: [
TargetThumb(TargetStyle.byId(holiday.targetId), size: 72),
const SizedBox(width: 14),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(Icons.celebration_outlined, size: 18, color: holiday.color),
const SizedBox(width: 6),
Flexible(
child: Text(holiday.name,
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w700, color: c.ink)),
),
],
),
const SizedBox(height: 4),
Text(
'${s.t('Holiday target until')} ${DateFormat('d. M.').format(end)}',
style: TextStyle(fontSize: 12.5, height: 1.35, fontWeight: FontWeight.w600, color: c.ink2),
),
Text(s.t('You can still change the hit colour.'),
style: TextStyle(fontSize: 12.5, height: 1.35, color: c.muted)),
],
),
),
const SizedBox(width: 6),
Icon(Icons.lock_outline_rounded, size: 18, color: c.faint),
],
),
);
}
}
class _ColorDot extends StatelessWidget {
final Color color;
final double size;
final bool selected;
const _ColorDot(this.color, {required this.size, this.selected = false});
@override
Widget build(BuildContext context) {
final c = Palette.current;
return Container(
width: size,
height: size,
padding: EdgeInsets.all(selected ? 3 : 0),
decoration: BoxDecoration(
shape: BoxShape.circle,
border: selected ? Border.all(color: c.ink, width: 2) : null,
),
child: Container(
decoration: BoxDecoration(color: color, shape: BoxShape.circle),
child: selected ? Icon(Icons.check_rounded, size: size * 0.5, color: Palette.onColor(color)) : null,
),
);
}
}