From 06bed86a496c867a6b85ef6b8332f2521e850a1f Mon Sep 17 00:00:00 2001 From: Pete Gregory Date: Tue, 6 Jan 2026 00:21:19 +0000 Subject: [PATCH] Add accent colour picker, fix empty user card when accepting friend request, add button to transfer allocations --- lib/app.dart | 8 + lib/components/pages/profile.dart | 5 + lib/components/pages/settings.dart | 139 +++++++++++++++++- .../pages/traction/traction_page.dart | 68 ++++++++- lib/components/traction/traction_card.dart | 85 +++++++---- lib/services/accent_color_service.dart | 51 +++++++ .../data_service_friendships.dart | 8 + .../data_service/data_service_traction.dart | 15 ++ lib/services/theme_mode_service.dart | 47 ++++++ lib/ui/app_shell.dart | 44 ++++-- pubspec.yaml | 2 +- 11 files changed, 427 insertions(+), 45 deletions(-) create mode 100644 lib/services/accent_color_service.dart create mode 100644 lib/services/theme_mode_service.dart diff --git a/lib/app.dart b/lib/app.dart index 79f78ea..c614d44 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -2,8 +2,10 @@ import 'package:flutter/material.dart'; import 'package:mileograph_flutter/services/api_service.dart'; import 'package:mileograph_flutter/services/authservice.dart'; import 'package:mileograph_flutter/services/data_service.dart'; +import 'package:mileograph_flutter/services/accent_color_service.dart'; import 'package:mileograph_flutter/services/distance_unit_service.dart'; import 'package:mileograph_flutter/services/endpoint_service.dart'; +import 'package:mileograph_flutter/services/theme_mode_service.dart'; import 'package:mileograph_flutter/ui/app_shell.dart'; import 'package:provider/provider.dart'; @@ -17,9 +19,15 @@ class App extends StatelessWidget { ChangeNotifierProvider( create: (_) => EndpointService(), ), + ChangeNotifierProvider( + create: (_) => AccentColorService(), + ), ChangeNotifierProvider( create: (_) => DistanceUnitService(), ), + ChangeNotifierProvider( + create: (_) => ThemeModeService(), + ), ProxyProvider( update: (_, endpoint, api) { final service = api ?? ApiService(baseUrl: endpoint.baseUrl); diff --git a/lib/components/pages/profile.dart b/lib/components/pages/profile.dart index 6d4b028..482fa48 100644 --- a/lib/components/pages/profile.dart +++ b/lib/components/pages/profile.dart @@ -127,6 +127,9 @@ class _ProfilePageState extends State { ); if (!mounted) return; setState(() => _status = status); + final data = context.read(); + await data.fetchFriendships(); + await data.fetchPendingFriendships(); _showSnack('Friend request sent'); } catch (e) { _showSnack('Failed to send request: $e'); @@ -159,6 +162,7 @@ class _ProfilePageState extends State { final updated = await context.read().acceptFriendship(id); if (!mounted) return; setState(() => _status = updated); + await context.read().fetchFriendships(); _showSnack('Friend request accepted'); } catch (e) { _showSnack('Failed to accept: $e'); @@ -175,6 +179,7 @@ class _ProfilePageState extends State { final updated = await context.read().rejectFriendship(id); if (!mounted) return; setState(() => _status = updated); + await context.read().fetchPendingFriendships(); _showSnack('Friend request rejected'); } catch (e) { _showSnack('Failed to reject: $e'); diff --git a/lib/components/pages/settings.dart b/lib/components/pages/settings.dart index 559b919..f4078c6 100644 --- a/lib/components/pages/settings.dart +++ b/lib/components/pages/settings.dart @@ -3,8 +3,10 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:http/http.dart' as http; +import 'package:mileograph_flutter/services/accent_color_service.dart'; import 'package:mileograph_flutter/services/distance_unit_service.dart'; import 'package:mileograph_flutter/services/endpoint_service.dart'; +import 'package:mileograph_flutter/services/theme_mode_service.dart'; import 'package:mileograph_flutter/services/data_service.dart'; import 'package:provider/provider.dart'; @@ -18,6 +20,18 @@ class SettingsPage extends StatefulWidget { class _SettingsPageState extends State { late final TextEditingController _endpointController; bool _saving = false; + static const List _accentPalette = [ + Colors.red, + Colors.pink, + Colors.orange, + Colors.amber, + Colors.green, + Colors.teal, + Colors.blue, + Colors.indigo, + Colors.purple, + Colors.cyan, + ]; @override void initState() { @@ -130,7 +144,12 @@ class _SettingsPageState extends State { Widget build(BuildContext context) { final endpointService = context.watch(); final distanceUnitService = context.watch(); - if (!endpointService.isLoaded || !distanceUnitService.isLoaded) { + final accentService = context.watch(); + final themeModeService = context.watch(); + if (!endpointService.isLoaded || + !distanceUnitService.isLoaded || + !accentService.isLoaded || + !themeModeService.isLoaded) { return const Scaffold( body: Center(child: CircularProgressIndicator()), ); @@ -184,6 +203,73 @@ class _SettingsPageState extends State { }, ), const SizedBox(height: 24), + Text( + 'Accent colour', + style: Theme.of(context).textTheme.titleMedium?.copyWith( + fontWeight: FontWeight.w700, + ), + ), + const SizedBox(height: 8), + Text( + 'Choose your preferred accent colour or use system colours.', + style: Theme.of(context).textTheme.bodySmall, + ), + const SizedBox(height: 12), + Wrap( + spacing: 12, + runSpacing: 8, + crossAxisAlignment: WrapCrossAlignment.center, + children: [ + OutlinedButton.icon( + onPressed: + accentService.useSystem ? null : () => accentService.setUseSystem(true), + icon: const Icon(Icons.phone_android), + label: const Text('Use system colours'), + ), + ..._accentPalette.map( + (color) => _AccentSwatchButton( + color: color, + selected: + !accentService.useSystem && + accentService.seedColor.toARGB32() == color.toARGB32(), + onTap: () => accentService.setSeedColor(color), + ), + ), + ], + ), + const SizedBox(height: 24), + Text( + 'Theme mode', + style: Theme.of(context).textTheme.titleMedium?.copyWith( + fontWeight: FontWeight.w700, + ), + ), + const SizedBox(height: 8), + SegmentedButton( + segments: const [ + ButtonSegment( + value: ThemeMode.system, + icon: Icon(Icons.settings_suggest), + label: Text('System'), + ), + ButtonSegment( + value: ThemeMode.light, + icon: Icon(Icons.light_mode), + label: Text('Light'), + ), + ButtonSegment( + value: ThemeMode.dark, + icon: Icon(Icons.dark_mode), + label: Text('Dark'), + ), + ], + selected: {themeModeService.mode}, + onSelectionChanged: (selection) { + final mode = selection.first; + themeModeService.setMode(mode); + }, + ), + const SizedBox(height: 24), Text( 'API endpoint', style: Theme.of(context).textTheme.titleMedium?.copyWith( @@ -241,3 +327,54 @@ class _SettingsPageState extends State { ); } } + +class _AccentSwatchButton extends StatelessWidget { + const _AccentSwatchButton({ + required this.color, + required this.selected, + required this.onTap, + }); + + final Color color; + final bool selected; + final VoidCallback onTap; + + @override + Widget build(BuildContext context) { + final borderColor = selected + ? Theme.of(context).colorScheme.onSurface + : Colors.black26; + return InkWell( + onTap: onTap, + customBorder: const CircleBorder(), + child: Container( + width: 40, + height: 40, + decoration: BoxDecoration( + color: color, + shape: BoxShape.circle, + border: Border.all( + color: borderColor, + width: selected ? 3 : 1, + ), + boxShadow: [ + BoxShadow( + color: Colors.black.withValues(alpha: 0.15), + blurRadius: 4, + offset: const Offset(0, 2), + ), + ], + ), + child: selected + ? const Center( + child: Icon( + Icons.check, + size: 18, + color: Colors.white, + ), + ) + : null, + ), + ); + } +} diff --git a/lib/components/pages/traction/traction_page.dart b/lib/components/pages/traction/traction_page.dart index 6dd40ae..3835f7d 100644 --- a/lib/components/pages/traction/traction_page.dart +++ b/lib/components/pages/traction/traction_page.dart @@ -12,6 +12,7 @@ class TractionPage extends StatefulWidget { this.selectionMode = false, this.selectionSingle = false, this.replacementPendingLocoId, + this.transferFromLocoId, this.onSelect, this.selectedKeys = const {}, }); @@ -19,6 +20,7 @@ class TractionPage extends StatefulWidget { final bool selectionMode; final bool selectionSingle; final int? replacementPendingLocoId; + final int? transferFromLocoId; final ValueChanged? onSelect; final Set selectedKeys; @@ -33,6 +35,7 @@ class _TractionPageState extends State { final _nameController = TextEditingController(); bool _mileageFirst = true; bool _initialised = false; + int? get _transferFromLocoId => widget.transferFromLocoId; bool _showAdvancedFilters = false; String? _selectedClass; late Set _selectedKeys; @@ -1200,6 +1203,53 @@ class _TractionPageState extends State { } } + Future _confirmTransfer(LocoSummary target) async { + final fromId = _transferFromLocoId; + if (fromId == null) return; + final navContext = context; + final messenger = ScaffoldMessenger.of(navContext); + final confirmed = await showDialog( + context: navContext, + builder: (dialogContext) { + return AlertDialog( + title: const Text('Transfer allocations?'), + content: Text( + 'Transfer all allocations from this loco to ${target.locoClass} ${target.number}?', + ), + actions: [ + TextButton( + onPressed: () => Navigator.of(dialogContext).pop(false), + child: const Text('Cancel'), + ), + FilledButton( + onPressed: () => Navigator.of(dialogContext).pop(true), + child: const Text('Transfer'), + ), + ], + ); + }, + ); + if (confirmed != true) return; + if (!navContext.mounted) return; + try { + final data = navContext.read(); + await data.transferAllocations(fromLocoId: fromId, toLocoId: target.id); + if (navContext.mounted) { + messenger.showSnackBar( + const SnackBar(content: Text('Allocations transferred')), + ); + } + await _refreshTraction(preservePosition: true); + if (navContext.mounted) navContext.pop(); + } catch (e) { + if (navContext.mounted) { + messenger.showSnackBar( + SnackBar(content: Text('Failed to transfer allocations: $e')), + ); + } + } + } + bool _isSelected(LocoSummary loco) { final keyVal = '${loco.locoClass}-${loco.number}'; return _selectedKeys.contains(keyVal); @@ -1348,12 +1398,18 @@ class _TractionPageState extends State { widget.replacementPendingLocoId == null ? () => _toggleSelection(loco) : null, - onReplacePending: widget.selectionMode && - widget.selectionSingle && - widget.replacementPendingLocoId != null - ? () => _confirmReplacePending(loco) - : null, - ); + onReplacePending: widget.selectionMode && + widget.selectionSingle && + widget.replacementPendingLocoId != null + ? () => _confirmReplacePending(loco) + : null, + onTransferAllocations: widget.selectionMode && + widget.selectionSingle && + widget.transferFromLocoId != null && + widget.transferFromLocoId != loco.id + ? () => _confirmTransfer(loco) + : null, + ); } return Padding( diff --git a/lib/components/traction/traction_card.dart b/lib/components/traction/traction_card.dart index 52d6dce..cf16f51 100644 --- a/lib/components/traction/traction_card.dart +++ b/lib/components/traction/traction_card.dart @@ -20,6 +20,7 @@ class TractionCard extends StatelessWidget { this.onToggleSelect, this.onReplacePending, this.onActionComplete, + this.onTransferAllocations, }); final LocoSummary loco; @@ -31,6 +32,7 @@ class TractionCard extends StatelessWidget { final VoidCallback? onToggleSelect; final VoidCallback? onReplacePending; final Future Function()? onActionComplete; + final VoidCallback? onTransferAllocations; @override Widget build(BuildContext context) { @@ -145,23 +147,30 @@ class TractionCard extends StatelessWidget { ]; // Prefer replace action when picking a replacement loco. - final addButton = onReplacePending != null + final addButton = onTransferAllocations != null ? TextButton.icon( - onPressed: onReplacePending, + onPressed: onTransferAllocations, icon: const Icon(Icons.swap_horiz), - label: const Text('Replace'), + label: const Text('Transfer'), ) - : (!isRejected && selectionMode && onToggleSelect != null) + : onReplacePending != null ? TextButton.icon( - onPressed: onToggleSelect, - icon: Icon( - isSelected - ? Icons.remove_circle_outline - : Icons.add_circle_outline, - ), - label: Text(isSelected ? 'Remove' : 'Add to entry'), + onPressed: onReplacePending, + icon: const Icon(Icons.swap_horiz), + label: const Text('Replace'), ) - : null; + : (!isRejected && selectionMode && onToggleSelect != null) + ? TextButton.icon( + onPressed: onToggleSelect, + icon: Icon( + isSelected + ? Icons.remove_circle_outline + : Icons.add_circle_outline, + ), + label: + Text(isSelected ? 'Remove' : 'Add to entry'), + ) + : null; if (isNarrow) { return Column( @@ -551,6 +560,7 @@ Future showTractionDetails( LocoSummary loco, { Future Function()? onActionComplete, }) async { + final navContext = context; final hasMileageOrTrips = _hasMileageOrTrips(loco); final isVisibilityPending = (loco.visibility ?? '').toLowerCase().trim() == 'pending'; @@ -583,11 +593,11 @@ Future showTractionDetails( builder: (_, controller) { return Padding( padding: const EdgeInsets.all(16.0), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Row( - children: [ + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ IconButton( icon: const Icon(Icons.close), onPressed: () => Navigator.of(ctx).pop(), @@ -620,16 +630,37 @@ Future showTractionDetails( style: Theme.of(context).textTheme.bodyMedium, ), ), - const SizedBox(height: 4), - Expanded( - child: ListView( - controller: controller, - children: [ - if (isRejected && rejectedReason.isNotEmpty) - ...[ - _detailRow( - context, - 'Rejection reason', + const SizedBox(height: 4), + Expanded( + child: ListView( + controller: controller, + children: [ + FilledButton.icon( + onPressed: () { + Navigator.of(ctx).pop(); + navContext.push( + Uri( + path: '/traction', + queryParameters: { + 'selection': 'single', + 'transferFromLocoId': loco.id.toString(), + }, + ).toString(), + extra: { + 'selection': 'single', + 'transferFromLocoId': loco.id, + }, + ); + }, + icon: const Icon(Icons.swap_horiz), + label: const Text('Transfer allocations'), + ), + const SizedBox(height: 12), + if (isRejected && rejectedReason.isNotEmpty) + ...[ + _detailRow( + context, + 'Rejection reason', rejectedReason, ), const Divider(), diff --git a/lib/services/accent_color_service.dart b/lib/services/accent_color_service.dart new file mode 100644 index 0000000..2ae0e46 --- /dev/null +++ b/lib/services/accent_color_service.dart @@ -0,0 +1,51 @@ +import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +class AccentColorService extends ChangeNotifier { + static const _prefsKeyUseSystem = 'accent_use_system'; + static const _prefsKeySeed = 'accent_seed'; + static const Color defaultSeed = Colors.red; + + bool _useSystem = true; + Color _seedColor = defaultSeed; + bool _hasSavedSeed = false; + bool _loaded = false; + + bool get useSystem => _useSystem; + Color get seedColor => _seedColor; + bool get hasSavedSeed => _hasSavedSeed; + bool get isLoaded => _loaded; + + AccentColorService() { + _load(); + } + + Future _load() async { + final prefs = await SharedPreferences.getInstance(); + _useSystem = prefs.getBool(_prefsKeyUseSystem) ?? true; + final seedValue = prefs.getInt(_prefsKeySeed); + if (seedValue != null) { + _seedColor = Color(seedValue); + _hasSavedSeed = true; + } + _loaded = true; + notifyListeners(); + } + + Future setUseSystem(bool value) async { + _useSystem = value; + final prefs = await SharedPreferences.getInstance(); + await prefs.setBool(_prefsKeyUseSystem, _useSystem); + notifyListeners(); + } + + Future setSeedColor(Color color) async { + _seedColor = color; + _useSystem = false; + _hasSavedSeed = true; + final prefs = await SharedPreferences.getInstance(); + await prefs.setInt(_prefsKeySeed, color.toARGB32()); + await prefs.setBool(_prefsKeyUseSystem, _useSystem); + notifyListeners(); + } +} diff --git a/lib/services/data_service/data_service_friendships.dart b/lib/services/data_service/data_service_friendships.dart index 2183e26..253e32a 100644 --- a/lib/services/data_service/data_service_friendships.dart +++ b/lib/services/data_service/data_service_friendships.dart @@ -151,6 +151,8 @@ extension DataServiceFriendships on DataService { overrideAddressee: targetUser, ); _pendingOutgoing = [friendship, ..._pendingOutgoing]; + await fetchFriendships(); + await fetchPendingFriendships(); _notifyAsync(); return friendship; } @@ -159,6 +161,8 @@ extension DataServiceFriendships on DataService { final json = await api.post('/friendships/$friendshipId/accept', {}); final friendship = _parseAndUpsertFriendship(json, fallbackStatus: 'accepted'); _pendingIncoming = _pendingIncoming.where((f) => f.id != friendshipId).toList(); + await fetchFriendships(); + await fetchPendingFriendships(); _notifyAsync(); return friendship; } @@ -177,6 +181,8 @@ extension DataServiceFriendships on DataService { _parseAndRemoveFriendship(json, friendshipId, status: 'none'); _pendingOutgoing = _pendingOutgoing.where((f) => f.id != friendshipId).toList(); + await fetchFriendships(); + await fetchPendingFriendships(); _notifyAsync(); return friendship; } @@ -193,6 +199,8 @@ extension DataServiceFriendships on DataService { _pendingIncoming.where((f) => f.id != friendshipId).toList(); _pendingOutgoing = _pendingOutgoing.where((f) => f.id != friendshipId).toList(); + await fetchFriendships(); + await fetchPendingFriendships(); _notifyAsync(); } diff --git a/lib/services/data_service/data_service_traction.dart b/lib/services/data_service/data_service_traction.dart index 51de750..b80c0f8 100644 --- a/lib/services/data_service/data_service_traction.dart +++ b/lib/services/data_service/data_service_traction.dart @@ -466,6 +466,21 @@ extension DataServiceTraction on DataService { } } + Future transferAllocations({ + required int fromLocoId, + required int toLocoId, + }) async { + try { + await api.post('/loco/alloc/transfer', { + 'loco_id': fromLocoId, + 'to_loco_id': toLocoId, + }); + } catch (e) { + debugPrint('Failed to transfer allocations $fromLocoId -> $toLocoId: $e'); + rethrow; + } + } + Future adminDeleteLoco({required int locoId}) async { try { await api.delete('/loco/admin/delete/$locoId'); diff --git a/lib/services/theme_mode_service.dart b/lib/services/theme_mode_service.dart new file mode 100644 index 0000000..136fbb9 --- /dev/null +++ b/lib/services/theme_mode_service.dart @@ -0,0 +1,47 @@ +import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +class ThemeModeService extends ChangeNotifier { + static const _prefsKey = 'theme_mode_preference'; + + ThemeMode _mode = ThemeMode.system; + bool _loaded = false; + + ThemeMode get mode => _mode; + bool get isLoaded => _loaded; + + ThemeModeService() { + _load(); + } + + Future _load() async { + final prefs = await SharedPreferences.getInstance(); + final saved = prefs.getString(_prefsKey); + if (saved != null) { + switch (saved) { + case 'light': + _mode = ThemeMode.light; + break; + case 'dark': + _mode = ThemeMode.dark; + break; + default: + _mode = ThemeMode.system; + } + } + _loaded = true; + notifyListeners(); + } + + Future setMode(ThemeMode mode) async { + _mode = mode; + final prefs = await SharedPreferences.getInstance(); + final value = switch (mode) { + ThemeMode.light => 'light', + ThemeMode.dark => 'dark', + _ => 'system', + }; + await prefs.setString(_prefsKey, value); + notifyListeners(); + } +} diff --git a/lib/ui/app_shell.dart b/lib/ui/app_shell.dart index 75a81dc..8ce504a 100644 --- a/lib/ui/app_shell.dart +++ b/lib/ui/app_shell.dart @@ -27,8 +27,10 @@ import 'package:mileograph_flutter/components/widgets/leg_share_edit_notificatio import 'package:mileograph_flutter/components/widgets/leg_share_notification_card.dart'; import 'package:mileograph_flutter/objects/objects.dart'; import 'package:mileograph_flutter/services/authservice.dart'; +import 'package:mileograph_flutter/services/accent_color_service.dart'; import 'package:mileograph_flutter/services/data_service.dart'; import 'package:mileograph_flutter/services/navigation_guard.dart'; +import 'package:mileograph_flutter/services/theme_mode_service.dart'; import 'package:provider/provider.dart'; final GlobalKey _shellNavigatorKey = @@ -103,12 +105,6 @@ class _MyAppState extends State { late final GoRouter _router; bool _routerInitialized = false; - final ColorScheme defaultLight = ColorScheme.fromSeed(seedColor: Colors.red); - final ColorScheme defaultDark = ColorScheme.fromSeed( - seedColor: Colors.red, - brightness: Brightness.dark, - ); - @override void didChangeDependencies() { super.didChangeDependencies(); @@ -188,10 +184,23 @@ class _MyAppState extends State { '', ) : null; + final transferFromLocoIdStr = + state.uri.queryParameters['transferFromLocoId']; + final transferFromLocoId = transferFromLocoIdStr != null + ? int.tryParse(transferFromLocoIdStr) + : state.extra is Map + ? int.tryParse( + (state.extra as Map)['transferFromLocoId'] + ?.toString() ?? + '', + ) + : null; final selectionMode = (selectionParam != null && selectionParam.isNotEmpty) || - replacementPendingLocoId != null; + replacementPendingLocoId != null || + transferFromLocoId != null; final selectionSingle = replacementPendingLocoId != null || + transferFromLocoId != null || selectionParam?.toLowerCase() == 'single' || selectionParam == '1' || selectionParam?.toLowerCase() == 'true'; @@ -199,6 +208,7 @@ class _MyAppState extends State { selectionMode: selectionMode, selectionSingle: selectionSingle, replacementPendingLocoId: replacementPendingLocoId, + transferFromLocoId: transferFromLocoId, ); }, ), @@ -325,20 +335,34 @@ class _MyAppState extends State { @override Widget build(BuildContext context) { + final accent = context.watch(); + final themeModeService = context.watch(); + final seedColor = + accent.hasSavedSeed ? accent.seedColor : AccentColorService.defaultSeed; + final useSystemColors = accent.useSystem; return DynamicColorBuilder( builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) { + final colorSchemeLight = useSystemColors && lightDynamic != null + ? lightDynamic + : ColorScheme.fromSeed(seedColor: seedColor); + final colorSchemeDark = useSystemColors && darkDynamic != null + ? darkDynamic + : ColorScheme.fromSeed( + seedColor: seedColor, + brightness: Brightness.dark, + ); return MaterialApp.router( title: 'Mileograph', routerConfig: _router, theme: ThemeData( useMaterial3: true, - colorScheme: lightDynamic ?? defaultLight, + colorScheme: colorSchemeLight, ), darkTheme: ThemeData( useMaterial3: true, - colorScheme: darkDynamic ?? defaultDark, + colorScheme: colorSchemeDark, ), - themeMode: ThemeMode.system, + themeMode: themeModeService.mode, ); }, ); diff --git a/pubspec.yaml b/pubspec.yaml index e2027ba..596ef2c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 0.7.0+8 +version: 0.7.1+9 environment: sdk: ^3.8.1