218 lines
7.2 KiB
Dart
218 lines
7.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../providers/app_state.dart';
|
|
import '../models/journal_entry.dart';
|
|
import '../components/journal_entry_dialog.dart';
|
|
|
|
class JournalScreen extends StatelessWidget {
|
|
const JournalScreen({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Consumer<AppState>(
|
|
builder: (context, appState, child) {
|
|
if (appState.isLoading) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
return Scaffold(
|
|
body: appState.journalEntries.isEmpty
|
|
? const Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(Icons.book, size: 64, color: Colors.grey),
|
|
SizedBox(height: 16),
|
|
Text('No journal entries yet'),
|
|
Text('Tap the + button to add your first entry'),
|
|
],
|
|
),
|
|
)
|
|
: ListView.builder(
|
|
padding: const EdgeInsets.all(16),
|
|
itemCount: appState.journalEntries.length,
|
|
itemBuilder: (context, index) {
|
|
final entry = appState.journalEntries[index];
|
|
return _buildJournalCard(context, entry);
|
|
},
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: () => _showAddJournalDialog(context),
|
|
child: const Icon(Icons.add),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _buildJournalCard(BuildContext context, JournalEntry entry) {
|
|
return Card(
|
|
margin: const EdgeInsets.only(bottom: 16),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: Text(
|
|
_formatDate(entry.date),
|
|
style: Theme.of(context).textTheme.headlineSmall,
|
|
),
|
|
),
|
|
if (entry.weather != null)
|
|
Chip(
|
|
label: Text(entry.weather!),
|
|
backgroundColor: Colors.blue.withAlpha(51),
|
|
),
|
|
PopupMenuButton(
|
|
itemBuilder: (context) => [
|
|
const PopupMenuItem(
|
|
value: 'edit',
|
|
child: Row(
|
|
children: [
|
|
Icon(Icons.edit),
|
|
SizedBox(width: 8),
|
|
Text('Edit'),
|
|
],
|
|
),
|
|
),
|
|
const PopupMenuItem(
|
|
value: 'delete',
|
|
child: Row(
|
|
children: [
|
|
Icon(Icons.delete, color: Colors.red),
|
|
SizedBox(width: 8),
|
|
Text('Delete', style: TextStyle(color: Colors.red)),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
onSelected: (value) {
|
|
if (value == 'edit') {
|
|
_showEditJournalDialog(context, entry);
|
|
} else if (value == 'delete') {
|
|
_showDeleteJournalDialog(context, entry);
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
entry.drink.name,
|
|
style: Theme.of(context).textTheme.titleMedium,
|
|
),
|
|
const SizedBox(height: 4),
|
|
Row(
|
|
children: [
|
|
...List.generate(
|
|
5,
|
|
(index) => Icon(
|
|
index < entry.drink.rating ? Icons.star : Icons.star_border,
|
|
size: 16,
|
|
color: Colors.amber,
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
Text('${entry.drink.rating}/5'),
|
|
],
|
|
),
|
|
if (entry.mood != null) ...[
|
|
const SizedBox(height: 8),
|
|
Row(
|
|
children: [
|
|
const Icon(Icons.mood, size: 16),
|
|
const SizedBox(width: 4),
|
|
Text('Mood: ${entry.mood}'),
|
|
],
|
|
),
|
|
],
|
|
if (entry.notes != null) ...[
|
|
const SizedBox(height: 12),
|
|
Text('Notes:', style: Theme.of(context).textTheme.titleSmall),
|
|
const SizedBox(height: 4),
|
|
Text(entry.notes!, style: Theme.of(context).textTheme.bodyMedium),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
String _formatDate(DateTime date) {
|
|
final now = DateTime.now();
|
|
final today = DateTime(now.year, now.month, now.day);
|
|
final yesterday = today.subtract(const Duration(days: 1));
|
|
final entryDay = DateTime(date.year, date.month, date.day);
|
|
|
|
if (entryDay == today) {
|
|
return 'Today - ${_formatTime(date)}';
|
|
} else if (entryDay == yesterday) {
|
|
return 'Yesterday - ${_formatTime(date)}';
|
|
} else {
|
|
return '${date.day}/${date.month}/${date.year} - ${_formatTime(date)}';
|
|
}
|
|
}
|
|
|
|
String _formatTime(DateTime date) {
|
|
final hour = date.hour.toString().padLeft(2, '0');
|
|
final minute = date.minute.toString().padLeft(2, '0');
|
|
return '$hour:$minute';
|
|
}
|
|
|
|
void _showAddJournalDialog(BuildContext context) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => const JournalEntryDialog(),
|
|
);
|
|
}
|
|
|
|
void _showEditJournalDialog(BuildContext context, JournalEntry entry) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => JournalEntryDialog(journalEntry: entry),
|
|
);
|
|
}
|
|
|
|
void _showDeleteJournalDialog(BuildContext context, JournalEntry entry) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Delete Journal Entry'),
|
|
content: Text('Are you sure you want to delete the entry for "${entry.drink.name}"?'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: const Text('Cancel'),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: () async {
|
|
try {
|
|
await Provider.of<AppState>(context, listen: false)
|
|
.deleteJournalEntry(entry.id);
|
|
if (context.mounted) {
|
|
Navigator.of(context).pop();
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Journal entry deleted successfully!')),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
if (context.mounted) {
|
|
Navigator.of(context).pop();
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Error deleting journal entry: $e')),
|
|
);
|
|
}
|
|
}
|
|
},
|
|
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
|
|
child: const Text('Delete'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|