Files
sure/mobile/lib/screens/main_navigation_screen.dart
ghost 401cd6ab08 feat(mobile): privacy mode to mask money values (#2386)
* feat(mobile): add privacy mode to mask money values

Adds an app-wide "privacy mode" so users can hide monetary amounts from
over-the-shoulder view.

- PrivacyProvider (ChangeNotifier) backed by PreferencesService, so the
  choice persists across launches and every money widget rebuilds on
  toggle.
- MoneyMasker.mask() collapses an amount's numeric portion into a short
  fixed run of bullets while keeping the currency symbol and sign
  (e.g. CA$1,234.56 -> CA$••••). A fixed run avoids leaking the value's
  magnitude and reads cleanly without stray separators. Currency- and
  locale-agnostic — it operates on already-formatted strings.
- Masking applied at every money render site: net worth + per-currency
  totals + breakdown sheet (NetWorthCard), account balances (AccountCard,
  AccountDetailHeader, transaction form account selector), and transaction
  amounts (transactions list, recent transactions, calendar).
- Two entry points: a "Hide amounts" switch in Settings -> Security, and a
  quick eye toggle in the top bar (visible on every tab).

Tests: MoneyMasker unit tests (fixed-run mask, magnitude hidden, symbol/
sign kept, passthrough, idempotent) + a widget test asserting the net
worth masks/unmasks as the provider flips; account_card_test updated to
provide the new provider. flutter analyze: no new issues; full suite
(123) green.

* fix(mobile): address privacy-mode review feedback

- Startup masking (Codex P1): read the privacy preference in main() before
  runApp and seed PrivacyProvider with it, so the first frame already has
  the correct value — money is never briefly rendered unmasked for a user
  who enabled "Hide amounts". Provider stays fail-closed otherwise: starts
  masked, SureApp's no-arg default is masked, and a failed read keeps it
  masked. A late-completing initial load no longer clobbers an explicit
  user toggle.
- setHidden() reverts the in-memory state (and logs) if persistence fails,
  keeping the UI consistent with what's actually stored.
- Mask the cash-balance detail chip in AccountDetailHeader (was leaking
  the cash position in privacy mode).
- Privacy top-bar toggle gets a "Toggle privacy" tooltip + icon semantic
  label for accessibility (kept as an InkWell to match the adjacent
  settings control).
- Tests: assert fail-closed initial state; assert the exact masked count;
  test the persistence round-trip (set -> reload); add
  PreferencesService.resetForTest() and reset between tests so the cached
  singleton can't leak state.

125 tests pass; flutter analyze: no new issues.

* refactor(mobile): thread hideAmounts through calendar tiles

Per review: the calendar tile builders read PrivacyProvider via
context.read, relying implicitly on the parent build()'s context.watch to
rebuild them — fragile if a tile is later extracted or wrapped in a
RepaintBoundary. Pass hideAmounts down explicitly instead, matching the
recent_transactions_screen pattern:

- build() (context.watch) -> _buildCalendar -> _buildDayCell
- _showTransactionsDialog reads once when the modal opens ->
  _buildTransactionTile

No more context.read inside tile methods. 125 tests pass; analyze clean.

* fix(mobile): watch PrivacyProvider inside calendar dialog builder

Moving the hideAmounts read inside the showDialog builder and switching
from context.read to context.watch ensures the dialog re-masks transaction
amounts if the user toggles privacy mode while the dialog is open.
2026-06-30 06:49:05 +02:00

274 lines
7.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/auth_provider.dart';
import '../providers/privacy_provider.dart';
import '../widgets/sure_logo.dart';
import 'chat_list_screen.dart';
import 'dashboard_screen.dart';
import 'intro_screen.dart';
import 'more_screen.dart';
import 'settings_screen.dart';
import '../l10n/app_localizations.dart';
class MainNavigationScreen extends StatefulWidget {
const MainNavigationScreen({super.key});
@override
State<MainNavigationScreen> createState() => _MainNavigationScreenState();
}
class _MainNavigationScreenState extends State<MainNavigationScreen> {
int _currentIndex = 0;
final _dashboardKey = GlobalKey<DashboardScreenState>();
List<Widget> _buildScreens(bool introLayout, VoidCallback? onStartChat) {
final screens = <Widget>[];
if (!introLayout) {
screens.add(DashboardScreen(key: _dashboardKey));
}
if (introLayout) {
screens.add(IntroScreen(onStartChat: onStartChat));
}
screens.add(const ChatListScreen());
if (!introLayout) {
screens.add(const MoreScreen());
}
screens.add(const SettingsScreen());
return screens;
}
Future<void> _handleDestinationSelected(
int index,
AuthProvider authProvider,
bool introLayout,
) async {
const chatIndex = 1;
if (index == chatIndex && !authProvider.aiEnabled) {
final enabled = await _showEnableAiPrompt();
if (!enabled) {
return;
}
}
if (mounted) {
setState(() {
_currentIndex = index;
});
if (!introLayout && index == 0) {
_dashboardKey.currentState?.reloadPreferences();
}
}
}
Future<void> _handleSelectSettings(AuthProvider authProvider, bool introLayout) async {
final settingsIndex = introLayout ? 2 : 3;
await _handleDestinationSelected(settingsIndex, authProvider, introLayout);
}
List<NavigationDestination> _buildDestinations(bool introLayout, AppLocalizations l) {
final destinations = <NavigationDestination>[];
if (!introLayout) {
destinations.add(
NavigationDestination(
icon: const Icon(Icons.home_outlined),
selectedIcon: const Icon(Icons.home),
label: l.navHome,
),
);
}
if (introLayout) {
destinations.add(
NavigationDestination(
icon: const Icon(Icons.auto_awesome_outlined),
selectedIcon: const Icon(Icons.auto_awesome),
label: l.navIntro,
),
);
}
destinations.add(
NavigationDestination(
icon: const Icon(Icons.chat_bubble_outline),
selectedIcon: const Icon(Icons.chat_bubble),
label: l.navAssistant,
),
);
if (!introLayout) {
destinations.add(
NavigationDestination(
icon: const Icon(Icons.more_horiz),
selectedIcon: const Icon(Icons.more_horiz),
label: l.navMore,
),
);
}
return destinations;
}
PreferredSizeWidget _buildTopBar(AuthProvider authProvider, bool introLayout) {
return AppBar(
automaticallyImplyLeading: false,
toolbarHeight: 60,
elevation: 0,
titleSpacing: 0,
centerTitle: false,
actionsPadding: EdgeInsets.zero,
title: Container(
width: 60,
height: 60,
alignment: Alignment.topLeft,
child: const Padding(
padding: EdgeInsets.only(top: 12, left: 12),
child: SureLogo(),
),
),
actions: [
Padding(
padding: const EdgeInsets.only(right: 12),
child: Center(
child: Tooltip(
message: 'Toggle privacy',
child: InkWell(
onTap: () => context.read<PrivacyProvider>().toggle(),
child: SizedBox(
width: 36,
height: 36,
child: Icon(
context.watch<PrivacyProvider>().hidden
? Icons.visibility_off_outlined
: Icons.visibility_outlined,
semanticLabel: 'Toggle privacy',
),
),
),
),
),
),
Padding(
padding: const EdgeInsets.only(right: 12),
child: Center(
child: InkWell(
onTap: () {
_handleSelectSettings(authProvider, introLayout);
},
child: const SizedBox(
width: 36,
height: 36,
child: Icon(Icons.settings_outlined),
),
),
),
),
],
);
}
Future<bool> _showEnableAiPrompt() async {
final l = AppLocalizations.of(context);
final authProvider = Provider.of<AuthProvider>(context, listen: false);
final shouldEnable = await showDialog<bool>(
context: context,
builder: (context) {
final dl = AppLocalizations.of(context);
return AlertDialog(
title: Text(dl.navEnableAiChatTitle),
content: Text(dl.navEnableAiChatContent),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: Text(dl.navEnableAiChatNotNow),
),
FilledButton(
onPressed: () => Navigator.of(context).pop(true),
child: Text(dl.navEnableAiChatConfirm),
),
],
);
},
);
if (shouldEnable != true) {
return false;
}
final enabled = await authProvider.enableAi();
if (!enabled && mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(authProvider.errorMessage ?? l.navEnableAiChatFailed),
backgroundColor: Colors.red,
),
);
}
return enabled;
}
int _resolveBottomSelectedIndex(List<NavigationDestination> destinations) {
if (destinations.isEmpty) {
return 0;
}
if (_currentIndex < 0) {
return 0;
}
if (_currentIndex >= destinations.length) {
return destinations.length - 1;
}
return _currentIndex;
}
@override
Widget build(BuildContext context) {
final l = AppLocalizations.of(context);
return Consumer<AuthProvider>(
builder: (context, authProvider, _) {
final introLayout = authProvider.isIntroLayout;
const chatIndex = 1;
final screens = _buildScreens(
introLayout,
() => _handleDestinationSelected(chatIndex, authProvider, introLayout),
);
final destinations = _buildDestinations(introLayout, l);
final bottomNavIndex = _resolveBottomSelectedIndex(destinations);
if (_currentIndex >= screens.length) {
_currentIndex = 0;
}
return Scaffold(
appBar: _buildTopBar(authProvider, introLayout),
body: IndexedStack(
index: _currentIndex,
children: screens,
),
bottomNavigationBar: NavigationBar(
selectedIndex: bottomNavIndex,
onDestinationSelected: (index) {
_handleDestinationSelected(index, authProvider, introLayout);
},
destinations: destinations,
),
);
},
);
}
}