mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-12-08 06:02:34 +00:00
- Implemented SettingsProvider to manage user preferences for theme options and time ranges for reminders. - Added persistent reminder settings with configurable retry intervals and maximum attempts. - Created UI for settings screen to allow users to customize their preferences. - Integrated shared_preferences for persistent storage of user settings. feat: Introduce Ingredient model - Created Ingredient model to represent nutritional components with properties for id, name, amount, and unit. - Added methods for serialization and deserialization of Ingredient objects. feat: Develop Archived Supplements Screen - Implemented ArchivedSupplementsScreen to display archived supplements with options to unarchive or delete. - Added UI components for listing archived supplements and handling user interactions. chore: Update dependencies in pubspec.yaml and pubspec.lock - Updated shared_preferences dependency to the latest version. - Removed flutter_datetime_picker_plus dependency and added file dependency. - Updated Flutter SDK constraint to >=3.27.0.
469 lines
18 KiB
Dart
469 lines
18 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:intl/intl.dart';
|
|
import '../providers/supplement_provider.dart';
|
|
|
|
class HistoryScreen extends StatefulWidget {
|
|
const HistoryScreen({super.key});
|
|
|
|
@override
|
|
State<HistoryScreen> createState() => _HistoryScreenState();
|
|
}
|
|
|
|
class _HistoryScreenState extends State<HistoryScreen> with SingleTickerProviderStateMixin {
|
|
late TabController _tabController;
|
|
DateTime _selectedDate = DateTime.now();
|
|
int _selectedMonth = DateTime.now().month;
|
|
int _selectedYear = DateTime.now().year;
|
|
int _refreshKey = 0; // Add this to force FutureBuilder refresh
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_tabController = TabController(length: 2, vsync: this);
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth);
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Intake History'),
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
|
bottom: TabBar(
|
|
controller: _tabController,
|
|
tabs: const [
|
|
Tab(text: 'Daily View'),
|
|
Tab(text: 'Monthly View'),
|
|
],
|
|
),
|
|
),
|
|
body: TabBarView(
|
|
controller: _tabController,
|
|
children: [
|
|
_buildDailyView(),
|
|
_buildMonthlyView(),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildDailyView() {
|
|
return Column(
|
|
children: [
|
|
Container(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
IconButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
_selectedDate = _selectedDate.subtract(const Duration(days: 1));
|
|
});
|
|
},
|
|
icon: const Icon(Icons.chevron_left),
|
|
),
|
|
InkWell(
|
|
onTap: _selectDate,
|
|
child: Text(
|
|
DateFormat('EEEE, MMM d, yyyy').format(_selectedDate),
|
|
style: const TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
),
|
|
IconButton(
|
|
onPressed: _selectedDate.isBefore(DateTime.now())
|
|
? () {
|
|
setState(() {
|
|
_selectedDate = _selectedDate.add(const Duration(days: 1));
|
|
});
|
|
}
|
|
: null,
|
|
icon: const Icon(Icons.chevron_right),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Expanded(
|
|
child: FutureBuilder<List<Map<String, dynamic>>>(
|
|
key: ValueKey('daily_view_$_refreshKey'), // Use refresh key to force rebuild
|
|
future: context.read<SupplementProvider>().getIntakesForDate(_selectedDate),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
if (!snapshot.hasData || snapshot.data!.isEmpty) {
|
|
return Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(
|
|
Icons.event_note,
|
|
size: 64,
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
const SizedBox(height: 16),
|
|
Text(
|
|
'No supplements taken on this day',
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
final intakes = snapshot.data!;
|
|
|
|
// Group intakes by supplement
|
|
final Map<String, List<Map<String, dynamic>>> groupedIntakes = {};
|
|
for (final intake in intakes) {
|
|
final supplementName = intake['supplementName'] as String;
|
|
groupedIntakes.putIfAbsent(supplementName, () => []);
|
|
groupedIntakes[supplementName]!.add(intake);
|
|
}
|
|
|
|
return ListView.builder(
|
|
padding: const EdgeInsets.all(16),
|
|
itemCount: groupedIntakes.length,
|
|
itemBuilder: (context, index) {
|
|
final supplementName = groupedIntakes.keys.elementAt(index);
|
|
final supplementIntakes = groupedIntakes[supplementName]!;
|
|
|
|
// Calculate totals
|
|
double totalUnits = 0;
|
|
final firstIntake = supplementIntakes.first;
|
|
|
|
for (final intake in supplementIntakes) {
|
|
totalUnits += (intake['unitsTaken'] as num?)?.toDouble() ?? 1.0;
|
|
}
|
|
|
|
return Card(
|
|
margin: const EdgeInsets.only(bottom: 12),
|
|
child: ExpansionTile(
|
|
leading: CircleAvatar(
|
|
backgroundColor: Theme.of(context).colorScheme.primary,
|
|
child: Icon(Icons.medication, color: Theme.of(context).colorScheme.onPrimary),
|
|
),
|
|
title: Text(
|
|
supplementName,
|
|
style: const TextStyle(fontWeight: FontWeight.w600),
|
|
),
|
|
subtitle: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'${totalUnits.toStringAsFixed(totalUnits % 1 == 0 ? 0 : 1)} ${firstIntake['supplementUnitType'] ?? 'units'} total',
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.w500,
|
|
color: Theme.of(context).colorScheme.primary,
|
|
),
|
|
),
|
|
Text(
|
|
'${supplementIntakes.length} intake${supplementIntakes.length > 1 ? 's' : ''}',
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
children: supplementIntakes.map((intake) {
|
|
final takenAt = DateTime.parse(intake['takenAt']);
|
|
final units = (intake['unitsTaken'] as num?)?.toDouble() ?? 1.0;
|
|
|
|
return ListTile(
|
|
contentPadding: const EdgeInsets.only(left: 72, right: 8),
|
|
title: Text(
|
|
'${units.toStringAsFixed(units % 1 == 0 ? 0 : 1)} ${intake['supplementUnitType'] ?? 'units'}',
|
|
style: const TextStyle(fontSize: 14),
|
|
),
|
|
subtitle: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'${units.toStringAsFixed(units % 1 == 0 ? 0 : 1)} ${intake['supplementUnitType'] ?? 'units'} at ${DateFormat('HH:mm').format(takenAt)}',
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
if (intake['notes'] != null && intake['notes'].toString().isNotEmpty)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 4),
|
|
child: Text(
|
|
intake['notes'],
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
fontStyle: FontStyle.italic,
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
trailing: IconButton(
|
|
icon: Icon(
|
|
Icons.delete_outline,
|
|
color: Colors.red.shade400,
|
|
size: 20,
|
|
),
|
|
onPressed: () => _deleteIntake(context, intake['id'], intake['supplementName']),
|
|
padding: EdgeInsets.zero,
|
|
constraints: const BoxConstraints(
|
|
minWidth: 32,
|
|
minHeight: 32,
|
|
),
|
|
),
|
|
);
|
|
}).toList(),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildMonthlyView() {
|
|
return Column(
|
|
children: [
|
|
Container(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
IconButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
if (_selectedMonth == 1) {
|
|
_selectedMonth = 12;
|
|
_selectedYear--;
|
|
} else {
|
|
_selectedMonth--;
|
|
}
|
|
});
|
|
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth);
|
|
},
|
|
icon: const Icon(Icons.chevron_left),
|
|
),
|
|
Text(
|
|
DateFormat('MMMM yyyy').format(DateTime(_selectedYear, _selectedMonth)),
|
|
style: const TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
IconButton(
|
|
onPressed: () {
|
|
final now = DateTime.now();
|
|
if (_selectedYear < now.year || (_selectedYear == now.year && _selectedMonth < now.month)) {
|
|
setState(() {
|
|
if (_selectedMonth == 12) {
|
|
_selectedMonth = 1;
|
|
_selectedYear++;
|
|
} else {
|
|
_selectedMonth++;
|
|
}
|
|
});
|
|
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth);
|
|
}
|
|
},
|
|
icon: const Icon(Icons.chevron_right),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Expanded(
|
|
child: Consumer<SupplementProvider>(
|
|
builder: (context, provider, child) {
|
|
if (provider.monthlyIntakes.isEmpty) {
|
|
return Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(
|
|
Icons.calendar_month,
|
|
size: 64,
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
const SizedBox(height: 16),
|
|
Text(
|
|
'No supplements taken this month',
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
// Group intakes by date
|
|
final groupedIntakes = <String, List<Map<String, dynamic>>>{};
|
|
for (final intake in provider.monthlyIntakes) {
|
|
final date = DateTime.parse(intake['takenAt']);
|
|
final dateKey = DateFormat('yyyy-MM-dd').format(date);
|
|
groupedIntakes.putIfAbsent(dateKey, () => []);
|
|
groupedIntakes[dateKey]!.add(intake);
|
|
}
|
|
|
|
final sortedDates = groupedIntakes.keys.toList()
|
|
..sort((a, b) => b.compareTo(a)); // Most recent first
|
|
|
|
return ListView.builder(
|
|
padding: const EdgeInsets.all(16),
|
|
itemCount: sortedDates.length,
|
|
itemBuilder: (context, index) {
|
|
final dateKey = sortedDates[index];
|
|
final dayIntakes = groupedIntakes[dateKey]!;
|
|
final date = DateTime.parse(dateKey);
|
|
|
|
return Card(
|
|
margin: const EdgeInsets.only(bottom: 16),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
DateFormat('EEEE, MMM d').format(date),
|
|
style: const TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
...dayIntakes.map((intake) {
|
|
final takenAt = DateTime.parse(intake['takenAt']);
|
|
final units = (intake['unitsTaken'] as num?)?.toDouble() ?? 1.0;
|
|
return Padding(
|
|
padding: const EdgeInsets.only(bottom: 4),
|
|
child: Row(
|
|
children: [
|
|
Icon(
|
|
Icons.circle,
|
|
size: 8,
|
|
color: Theme.of(context).colorScheme.primary,
|
|
),
|
|
const SizedBox(width: 8),
|
|
Expanded(
|
|
child: Text(
|
|
'${intake['supplementName']} - ${units.toStringAsFixed(units % 1 == 0 ? 0 : 1)} ${intake['supplementUnitType'] ?? 'units'} at ${DateFormat('HH:mm').format(takenAt)}',
|
|
style: const TextStyle(fontSize: 14),
|
|
),
|
|
),
|
|
IconButton(
|
|
icon: Icon(
|
|
Icons.delete_outline,
|
|
color: Colors.red.shade400,
|
|
size: 18,
|
|
),
|
|
onPressed: () => _deleteIntake(context, intake['id'], intake['supplementName']),
|
|
padding: EdgeInsets.zero,
|
|
constraints: const BoxConstraints(
|
|
minWidth: 24,
|
|
minHeight: 24,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}),
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
'${dayIntakes.length} supplement${dayIntakes.length != 1 ? 's' : ''} taken',
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
void _selectDate() async {
|
|
final picked = await showDatePicker(
|
|
context: context,
|
|
initialDate: _selectedDate,
|
|
firstDate: DateTime(2020),
|
|
lastDate: DateTime.now(),
|
|
);
|
|
if (picked != null) {
|
|
setState(() {
|
|
_selectedDate = picked;
|
|
});
|
|
}
|
|
}
|
|
|
|
void _deleteIntake(BuildContext context, int intakeId, String supplementName) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Delete Intake'),
|
|
content: Text('Are you sure you want to delete this $supplementName intake?'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: const Text('Cancel'),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: () async {
|
|
await context.read<SupplementProvider>().deleteIntake(intakeId);
|
|
Navigator.of(context).pop();
|
|
|
|
// Force refresh of the UI
|
|
setState(() {
|
|
_refreshKey++; // This will force FutureBuilder to rebuild
|
|
});
|
|
|
|
// Force refresh of the current view data
|
|
if (_tabController.index == 1) {
|
|
// Monthly view - refresh monthly intakes
|
|
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth);
|
|
}
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('$supplementName intake deleted'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
},
|
|
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
|
|
child: const Text('Delete'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_tabController.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|