Files
Tracker/lib/screens/settings/privacy_settings_screen.dart

141 lines
4.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:health/health.dart';
import '../../providers/user_provider.dart';
import '../../services/health_service.dart';
class PrivacySettingsScreen extends ConsumerStatefulWidget {
const PrivacySettingsScreen({super.key});
@override
ConsumerState<PrivacySettingsScreen> createState() =>
_PrivacySettingsScreenState();
}
class _PrivacySettingsScreenState extends ConsumerState<PrivacySettingsScreen> {
bool _hasPermissions = false;
final HealthService _healthService = HealthService();
@override
void initState() {
super.initState();
_checkPermissions();
}
Future<void> _checkPermissions() async {
final hasPermissions = await _healthService.hasPermissions(_healthService.menstruationDataTypes);
setState(() {
_hasPermissions = hasPermissions;
});
}
Future<void> _requestPermissions() async {
final authorized = await _healthService.requestAuthorization(_healthService.menstruationDataTypes);
if (authorized) {
_hasPermissions = true;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Health app access granted!')),
);
} else {
_hasPermissions = false;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Health app access denied.')),
);
}
setState(() {}); // Rebuild to update UI
}
Future<void> _syncPeriodDays(bool sync) async {
if (sync) {
if (!_hasPermissions) {
// Request permissions if not already granted
final authorized = await _healthService.requestAuthorization(_healthService.menstruationDataTypes);
if (!authorized) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Cannot sync without health app permissions.')),
);
}
return;
}
_hasPermissions = true;
}
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Syncing period data...')),
);
}
final userProfile = ref.read(userProfileProvider);
final cycleEntries = ref.read(cycleEntriesProvider);
if (userProfile != null) {
final success = await _healthService.writeMenstruationData(cycleEntries);
if (mounted) {
if (success) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Period data synced successfully!')),
);
// Optionally store a flag in userProfile if sync is active
// userProfile.copyWith(syncPeriodToHealth: true)
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Failed to sync period data.')),
);
}
}
}
} else {
// Logic to disable sync (e.g., revoke permissions if Health package supports it,
// or just stop writing data in future)
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Period data sync disabled.')),
);
}
}
setState(() {}); // Rebuild to update UI
}
@override
Widget build(BuildContext context) {
// This value would ideally come from userProfile.syncPeriodToHealth
bool syncPeriodToHealth = _hasPermissions;
return Scaffold(
appBar: AppBar(
title: const Text('Privacy Settings'),
),
body: ListView(
padding: const EdgeInsets.all(16.0),
children: [
ListTile(
title: const Text('Health App Integration'),
subtitle: _hasPermissions
? const Text('Connected to Health App. Period data can be synced.')
: const Text('Not connected. Tap to grant access.'),
trailing: _hasPermissions ? const Icon(Icons.check_circle, color: Colors.green) : const Icon(Icons.warning, color: Colors.orange),
onTap: _requestPermissions,
),
SwitchListTile(
title: const Text('Sync Period Days'),
subtitle: const Text('Automatically sync your period start and end dates to your health app.'),
value: syncPeriodToHealth,
onChanged: _hasPermissions ? (value) async {
if (value) {
await _syncPeriodDays(true);
} else {
await _syncPeriodDays(false);
}
setState(() {
syncPeriodToHealth = value; // Update local state for toggle
});
} : null,
),
// TODO: Add more privacy settings if needed
],
),
);
}
}