Files
SDKmuta/lib/screens/settings_screen.dart
T
2026-10-26 20:48:16 +01:00

443 lines
15 KiB
Dart

import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../services/app_settings.dart';
import '../services/notification_service.dart';
import '../theme/app_theme.dart';
import '../utils/target_colors.dart';
import '../widgets/section.dart';
import '../widgets/text_prompt.dart';
import 'camera_settings_screen.dart';
class SettingsScreen extends StatelessWidget {
const SettingsScreen({super.key});
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 language = AppSettings.languages.firstWhere(
(l) => l['name'] == s.language,
orElse: () => AppSettings.languages.first,
);
return ListView(
padding: const EdgeInsets.fromLTRB(16, 4, 16, 24),
children: [
SectionHeader(t('Language')),
GroupCard(children: [
SettingsTile(
leading: Text(language['flag']!, style: const TextStyle(fontSize: 22)),
title: t(language['name']!),
subtitle: s.followSystemLanguage ? t('Automatic (phone language)') : null,
onTap: () => _pickLanguage(context),
),
]),
SectionHeader(t('Appearance')),
GroupCard(children: [
SettingsTile(
title: t('Bullet color'),
valueLeading: _ColorDot(s.bulletColor, size: 18),
value: t(s.bulletColorName),
onTap: () => _pickBulletColor(context),
),
SettingsTile(
title: t('Target'),
valueLeading: _TargetThumb(s.selectedTarget, size: 30),
value: s.targetName(s.selectedTarget),
onTap: () => _pickTarget(context),
),
]),
SectionHeader(t('Club')),
GroupCard(children: [
if (s.clubUnlocked)
SettingsTile(
leading: const Icon(Icons.verified_rounded, color: Colors.green),
title: t('Member access'),
subtitle: t('Calendar and contacts unlocked'),
value: t('Remove'),
onTap: () => _confirmLock(context),
)
else
SettingsTile(
leading: Icon(Icons.lock_outline_rounded, color: Surfaces.current.textSecondary),
title: t('Club code'),
subtitle: t('Unlocks calendar and contacts'),
onTap: () => _enterClubCode(context),
),
]),
Padding(
padding: const EdgeInsets.fromLTRB(4, 8, 4, 0),
child: Text(
t('Member access is currently available only for SDK Kovinar Muta. Support for other clubs may be added in the future.'),
style: TextStyle(fontSize: 12, color: Surfaces.current.textSecondary),
),
),
if (s.clubUnlocked) ...[
SectionHeader(t('Notifications')),
GroupCard(children: [
SettingsSwitchTile(
title: t('Dežuranje'),
value: s.notifyDezuranje,
activeColor: s.bulletColor,
onChanged: (v) => s.setNotifyDezuranje(v).then((_) => _reschedule()),
),
SettingsSwitchTile(
title: t('Sestanek'),
value: s.notifySestanek,
activeColor: s.bulletColor,
onChanged: (v) => s.setNotifySestanek(v).then((_) => _reschedule()),
),
SettingsSwitchTile(
title: t('Tekma'),
value: s.notifyTekma,
activeColor: s.bulletColor,
onChanged: (v) => s.setNotifyTekma(v).then((_) => _reschedule()),
),
SettingsTile(
title: t('Remind me'),
value: _hoursBefore(s.reminderHours),
onTap: () => _pickReminder(context),
),
]),
],
SectionHeader(t('Cameras')),
GroupCard(children: [
SettingsTile(
title: t('Camera Settings'),
value: _camerasSummary(s),
onTap: () => Navigator.of(context).push(
MaterialPageRoute(builder: (_) => const CameraSettingsScreen()),
),
),
]),
const SizedBox(height: 28),
FutureBuilder<PackageInfo>(
future: _packageInfo,
builder: (context, snap) {
final info = snap.data;
if (info == null) return const SizedBox.shrink();
return Text(
'${info.appName} ${info.version}',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 12, color: Surfaces.current.textSecondary),
);
},
),
],
);
},
);
}
static void _reschedule() => NotificationService().rescheduleFromCalendar();
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'),
confirmLabel: s.t('Unlock'),
);
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')),
backgroundColor: ok ? Colors.green[700] : Colors.red[700],
));
}
Future<void> _confirmLock(BuildContext context) async {
final s = AppSettings.instance;
final confirmed = await showDialog<bool>(
context: context,
builder: (dialog) => AlertDialog(
title: Text(s.t('Member access')),
content: Text(s.t('Remove club access from this phone?')),
actions: [
TextButton(
onPressed: () => Navigator.pop(dialog, false),
child: Text(s.t('Cancel'), style: const TextStyle(color: Colors.white70)),
),
FilledButton(
style: FilledButton.styleFrom(backgroundColor: Colors.red[700]),
onPressed: () => Navigator.pop(dialog, true),
child: Text(s.t('Remove')),
),
],
),
);
if (confirmed == true) {
await s.lockClub();
_reschedule(); // cancels reminders
}
}
static String _hoursBefore(int hours) =>
AppSettings.instance.t('{n} h before').replaceAll('{n}', '$hours');
// Location names (e.g. "Kovinar · Doma"), or a hint when nothing is set up yet.
static String _camerasSummary(AppSettings s) {
final locations = s.cameras.locations;
return locations.isEmpty ? s.t('Not set up') : locations.join(' · ');
}
void _pickLanguage(BuildContext context) {
final s = AppSettings.instance;
showModalBottomSheet(
context: context,
builder: (sheet) => SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
_SheetTitle(s.t('Select Language')),
SheetOption(
leading: const Text('📱', style: TextStyle(fontSize: 22)),
label: '${s.t('Automatic (phone language)')} · ${s.t(AppSettings.systemLanguage)}',
selected: s.followSystemLanguage,
selectedColor: s.bulletColor,
onTap: () {
s.useSystemLanguage();
Navigator.pop(sheet);
},
),
for (final lang in AppSettings.languages)
SheetOption(
leading: Text(lang['flag']!, style: const TextStyle(fontSize: 22)),
label: s.t(lang['name']!),
selected: !s.followSystemLanguage && lang['name'] == s.language,
selectedColor: s.bulletColor,
onTap: () {
s.setLanguage(lang['name']!);
Navigator.pop(sheet);
},
),
const SizedBox(height: 8),
],
),
),
);
}
void _pickReminder(BuildContext context) {
final s = AppSettings.instance;
showModalBottomSheet(
context: context,
builder: (sheet) => SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
_SheetTitle(s.t('Remind me')),
for (final h in AppSettings.reminderHourOptions)
SheetOption(
label: _hoursBefore(h),
selected: h == s.reminderHours,
selectedColor: s.bulletColor,
onTap: () {
s.setReminderHours(h).then((_) => _reschedule());
Navigator.pop(sheet);
},
),
const SizedBox(height: 8),
],
),
),
);
}
void _pickBulletColor(BuildContext context) {
final s = AppSettings.instance;
showModalBottomSheet(
context: context,
builder: (sheet) => SafeArea(
child: Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 20),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
_SheetTitle(s.t('Bullet color')),
Wrap(
spacing: 16,
runSpacing: 16,
alignment: WrapAlignment.center,
children: [
for (final entry in AppSettings.bulletColors.entries)
GestureDetector(
onTap: () {
s.setBulletColor(entry.key);
Navigator.pop(sheet);
},
child: _ColorDot(
entry.value,
size: 48,
selected: entry.key == s.bulletColorName,
),
),
],
),
],
),
),
),
);
}
void _pickTarget(BuildContext context) {
final s = AppSettings.instance;
showModalBottomSheet(
context: context,
isScrollControlled: true,
builder: (sheet) => DraggableScrollableSheet(
expand: false,
initialChildSize: 0.75,
maxChildSize: 0.95,
builder: (context, scroll) => Column(
children: [
_SheetTitle(s.t('Target')),
Expanded(
child: GridView.builder(
controller: scroll,
padding: EdgeInsets.fromLTRB(16, 0, 16, 24 + MediaQuery.paddingOf(context).bottom),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 12,
crossAxisSpacing: 12,
childAspectRatio: 0.8,
),
itemCount: AppSettings.targets.length,
itemBuilder: (context, i) {
final path = AppSettings.targets[i];
final selected = path == s.selectedTarget;
return GestureDetector(
onTap: () {
s.setTarget(path);
Navigator.pop(sheet);
},
child: Column(
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: selected ? s.bulletColor : Colors.transparent,
width: 3,
),
),
child: _TargetThumb(path, radius: 9),
),
),
const SizedBox(height: 6),
Text(
s.targetName(path),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12,
color: selected ? Colors.white : Colors.white70,
fontWeight: selected ? FontWeight.bold : FontWeight.normal,
),
),
],
),
);
},
),
),
],
),
),
);
}
}
class _SheetTitle extends StatelessWidget {
final String text;
const _SheetTitle(this.text);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(bottom: 12),
child: Text(
text,
style: const TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold),
),
);
}
}
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) {
return Container(
width: size,
height: size,
decoration: BoxDecoration(
color: color,
shape: BoxShape.circle,
border: Border.all(
color: selected ? Colors.white : Colors.black26,
width: selected ? 3 : 1,
),
),
child: selected ? const Icon(Icons.check_rounded, color: Colors.white) : null,
);
}
}
class _TargetThumb extends StatelessWidget {
final String path;
final double? size;
final double radius;
const _TargetThumb(this.path, {this.size, this.radius = 6});
@override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.circular(radius),
child: Container(
width: size,
height: size,
color: TargetColors.getBackgroundColor(path),
padding: const EdgeInsets.all(2),
child: Image.asset(
path,
fit: BoxFit.contain,
cacheWidth: 300,
errorBuilder: (_, __, ___) => const Icon(Icons.image_not_supported, color: Colors.white54),
),
),
);
}
}