feat: Add auto-sync, fix partner linking UI, update sharing settings

- Add 10-second periodic auto-sync to CycleEntriesNotifier
- Fix husband_devotional_screen: use partnerId for isConnected check, navigate to SharingSettingsScreen instead of legacy mock dialog
- Remove obsolete _showConnectDialog method and mock data import
- Update husband_settings_screen: show 'Partner Settings' with linked partner name when connected
- Add SharingSettingsScreen: Pad Supplies toggle (disabled when pad tracking off), Intimacy always enabled
- Add CORS OPTIONS handler to backend server
- Add _ensureServerRegistration for reliable partner linking
- Add copy button to Invite Partner dialog
- Dynamic base URL for web (uses window.location.hostname)
This commit is contained in:
2026-01-09 17:20:49 -06:00
parent d28898cb81
commit 1c2c56e9e2
21 changed files with 1690 additions and 493 deletions

View File

@@ -1,163 +1,238 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:google_fonts/google_fonts.dart';
import '../../theme/app_theme.dart';
import '../../providers/user_provider.dart';
import '../../services/sync_service.dart';
class SharingSettingsScreen extends ConsumerWidget {
class SharingSettingsScreen extends ConsumerStatefulWidget {
const SharingSettingsScreen({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final userProfile = ref.watch(userProfileProvider);
ConsumerState<SharingSettingsScreen> createState() =>
_SharingSettingsScreenState();
}
if (userProfile == null) {
return Scaffold(
appBar: AppBar(
title: const Text('Sharing Settings'),
),
body: const Center(child: CircularProgressIndicator()),
);
class _SharingSettingsScreenState extends ConsumerState<SharingSettingsScreen> {
final _partnerIdController = TextEditingController();
bool _isLoading = false;
String? _errorText;
@override
void initState() {
super.initState();
final user = ref.read(userProfileProvider);
if (user != null && user.partnerId != null) {
_partnerIdController.text = user.partnerId!;
}
}
@override
void dispose() {
_partnerIdController.dispose();
super.dispose();
}
Future<void> _savePartnerId() async {
final input = _partnerIdController.text.trim();
if (input.isEmpty) return;
final user = ref.read(userProfileProvider);
if (user != null) {
setState(() {
_isLoading = true;
_errorText = null;
});
try {
// 1. Preview first
final result = await SyncService().previewPartnerId(input);
final partnerName = result['partnerName'];
if (!mounted) return;
// 2. Show Confirmation Dialog
final confirm = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Confirm Partner'),
content: Text(
'Found partner: $partnerName.\n\nDo you want to link with them?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Cancel'),
),
ElevatedButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('Confirm Link'),
),
],
),
);
if (confirm != true) {
if (mounted) setState(() => _isLoading = false);
return;
}
// 3. Perform Link
final verifyResult =
await SyncService().verifyPartnerId(user.id, input);
if (!mounted) return;
await ref.read(userProfileProvider.notifier).updateProfile(
user.copyWith(
partnerId: input,
partnerName: verifyResult['partnerName'],
),
);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Connected to ${verifyResult['partnerName']}!')),
);
setState(() {
_isLoading = false;
});
}
} catch (e) {
if (mounted) {
setState(() {
_isLoading = false;
_errorText = e.toString().replaceAll('Exception:', '').trim();
});
}
}
}
}
@override
Widget build(BuildContext context) {
final user = ref.watch(userProfileProvider);
if (user == null) return const Scaffold();
return Scaffold(
appBar: AppBar(
title: const Text('Sharing Settings'),
),
appBar: AppBar(title: const Text('Sharing & Partner')),
body: ListView(
padding: const EdgeInsets.all(16.0),
children: [
ListTile(
leading: const Icon(Icons.link),
title: const Text('Link with Husband'),
subtitle: Text(userProfile.partnerName != null
? 'Linked to ${userProfile.partnerName}'
: 'Not linked'),
trailing: const Icon(Icons.chevron_right),
onTap: () => _showShareDialog(context, ref),
),
_buildPartnerSection(user),
const Divider(),
SwitchListTile(
title: const Text('Share Moods'),
value: userProfile.shareMoods,
onChanged: (value) {
ref
.read(userProfileProvider.notifier)
.updateProfile(userProfile.copyWith(shareMoods: value));
},
),
SwitchListTile(
title: const Text('Share Symptoms'),
value: userProfile.shareSymptoms,
onChanged: (value) {
ref
.read(userProfileProvider.notifier)
.updateProfile(userProfile.copyWith(shareSymptoms: value));
},
),
SwitchListTile(
title: const Text('Share Cravings'),
value: userProfile.shareCravings,
onChanged: (value) {
ref
.read(userProfileProvider.notifier)
.updateProfile(userProfile.copyWith(shareCravings: value));
},
),
SwitchListTile(
title: const Text('Share Energy Levels'),
value: userProfile.shareEnergyLevels,
onChanged: (value) {
ref.read(userProfileProvider.notifier).updateProfile(
userProfile.copyWith(shareEnergyLevels: value));
},
),
SwitchListTile(
title: const Text('Share Sleep Data'),
value: userProfile.shareSleep,
onChanged: (value) {
ref
.read(userProfileProvider.notifier)
.updateProfile(userProfile.copyWith(shareSleep: value));
},
),
SwitchListTile(
title: const Text('Share Intimacy Details'),
value: userProfile.shareIntimacy,
onChanged: (value) {
ref
.read(userProfileProvider.notifier)
.updateProfile(userProfile.copyWith(shareIntimacy: value));
},
),
_buildSharingToggles(user),
],
),
);
}
void _showShareDialog(BuildContext context, WidgetRef ref) {
// Generate a simple pairing code
final userProfile = ref.read(userProfileProvider);
final pairingCode =
userProfile?.id.substring(0, 6).toUpperCase() ?? 'ABC123';
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Row(
children: [
Icon(Icons.share_outlined, color: AppColors.navyBlue),
SizedBox(width: 8),
Text('Share with Husband'),
],
),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Share this code with your husband so he can connect to your cycle data:',
style:
GoogleFonts.outfit(fontSize: 14, color: AppColors.warmGray),
),
const SizedBox(height: 24),
Container(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
decoration: BoxDecoration(
color: AppColors.navyBlue.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: AppColors.navyBlue.withValues(alpha: 0.3)),
),
child: SelectableText(
pairingCode,
style: GoogleFonts.outfit(
fontSize: 32,
fontWeight: FontWeight.bold,
letterSpacing: 4,
color: AppColors.navyBlue,
Widget _buildPartnerSection(user) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Partner Connection',
style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 16),
// My ID
Text('My ID (Share this with your partner):',
style: Theme.of(context).textTheme.bodySmall),
Row(
children: [
Expanded(
child: SelectableText(
user.id,
style: const TextStyle(fontWeight: FontWeight.bold),
),
),
),
const SizedBox(height: 16),
Text(
'He can enter this in his app under Settings > Connect with Wife.',
style:
GoogleFonts.outfit(fontSize: 12, color: AppColors.warmGray),
textAlign: TextAlign.center,
),
],
),
actions: [
ElevatedButton(
onPressed: () => Navigator.pop(context),
style: ElevatedButton.styleFrom(
backgroundColor: AppColors.navyBlue,
foregroundColor: Colors.white,
),
child: const Text('Done'),
IconButton(
icon: const Icon(Icons.copy),
onPressed: () {
Clipboard.setData(ClipboardData(text: user.id));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('ID copied to clipboard')),
);
},
),
],
),
const SizedBox(height: 24),
// Partner ID Input
TextField(
controller: _partnerIdController,
decoration: InputDecoration(
labelText: 'Enter Partner ID',
border: const OutlineInputBorder(),
hintText: 'Paste partner ID here',
errorText: _errorText,
),
enabled: !_isLoading,
),
const SizedBox(height: 8),
if (_isLoading)
const Padding(
padding: EdgeInsets.all(8.0),
child: Center(child: CircularProgressIndicator()),
)
else
ElevatedButton(
onPressed: _savePartnerId,
child: const Text('Link Partner'),
),
],
),
);
}
Widget _buildSharingToggles(user) {
final notifier = ref.read(userProfileProvider.notifier);
final isPadTrackingEnabled = user.isPadTrackingEnabled ?? false;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Text('What to Share',
style: Theme.of(context).textTheme.titleMedium),
),
SwitchListTile(
title: const Text('Share Moods'),
value: user.shareMoods,
onChanged: (val) =>
notifier.updateProfile(user.copyWith(shareMoods: val)),
),
SwitchListTile(
title: const Text('Share Symptoms'),
value: user.shareSymptoms,
onChanged: (val) =>
notifier.updateProfile(user.copyWith(shareSymptoms: val)),
),
SwitchListTile(
title: const Text('Share Energy Levels'),
value: user.shareEnergyLevels,
onChanged: (val) =>
notifier.updateProfile(user.copyWith(shareEnergyLevels: val)),
),
SwitchListTile(
title: const Text('Share Pad Supplies'),
subtitle: !isPadTrackingEnabled
? const Text('Enable Pad Tracking to share supplies')
: null,
value: isPadTrackingEnabled && (user.sharePadSupplies ?? true),
onChanged: isPadTrackingEnabled
? (val) =>
notifier.updateProfile(user.copyWith(sharePadSupplies: val))
: null,
),
SwitchListTile(
title: const Text('Share Intimacy'),
subtitle: const Text('Always shared with your husband'),
value: true, // Always true
onChanged: null, // Disabled - always on
),
],
);
}
}