Implementert historikk og manuell timeregistrering

This commit is contained in:
steinhelge
2025-11-24 21:04:15 +01:00
parent f76b2e5c72
commit feda00ac83
5 changed files with 839 additions and 18 deletions
+234 -3
View File
@@ -1,17 +1,248 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:table_calendar/table_calendar.dart';
import 'package:intl/intl.dart';
import '../../providers/time_provider.dart';
import '../../models/time_registration.dart';
import 'registration_detail_screen.dart';
class HistoryScreen extends StatelessWidget {
class HistoryScreen extends ConsumerStatefulWidget {
const HistoryScreen({super.key});
@override
ConsumerState<HistoryScreen> createState() => _HistoryScreenState();
}
class _HistoryScreenState extends ConsumerState<HistoryScreen> {
CalendarFormat _calendarFormat = CalendarFormat.week;
DateTime _focusedDay = DateTime.now();
DateTime? _selectedDay;
@override
void initState() {
super.initState();
_selectedDay = _focusedDay;
}
@override
Widget build(BuildContext context) {
// Hent registreringer for valgt dag
final selectedDate = _selectedDay ?? DateTime.now();
final dateRange = DateRange(
start: DateTime(selectedDate.year, selectedDate.month, selectedDate.day),
end: DateTime(selectedDate.year, selectedDate.month, selectedDate.day, 23, 59, 59),
);
final registrationsAsync = ref.watch(registrationsProvider(dateRange));
final totalHoursAsync = ref.watch(totalHoursProvider(dateRange));
return Scaffold(
appBar: AppBar(
title: const Text('Historikk'),
),
body: const Center(
child: Text('Historikk kommer her'),
body: Column(
children: [
TableCalendar(
locale: 'nb_NO',
firstDay: DateTime.utc(2020, 1, 1),
lastDay: DateTime.utc(2030, 12, 31),
focusedDay: _focusedDay,
calendarFormat: _calendarFormat,
selectedDayPredicate: (day) {
return isSameDay(_selectedDay, day);
},
onDaySelected: (selectedDay, focusedDay) {
if (!isSameDay(_selectedDay, selectedDay)) {
setState(() {
_selectedDay = selectedDay;
_focusedDay = focusedDay;
});
}
},
onFormatChanged: (format) {
if (_calendarFormat != format) {
setState(() {
_calendarFormat = format;
});
}
},
onPageChanged: (focusedDay) {
_focusedDay = focusedDay;
},
calendarStyle: CalendarStyle(
selectedDecoration: BoxDecoration(
color: Theme.of(context).colorScheme.primary,
shape: BoxShape.circle,
),
todayDecoration: BoxDecoration(
color: Theme.of(context).colorScheme.primary.withOpacity(0.5),
shape: BoxShape.circle,
),
),
),
const Divider(),
// Dagens oppsummering
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: totalHoursAsync.when(
data: (hours) {
final total = hours['total'] ?? 0;
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Totalt denne dagen:',
style: Theme.of(context).textTheme.titleMedium,
),
Text(
'${total ~/ 60}t ${total % 60}m',
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.primary,
),
),
],
);
},
loading: () => const SizedBox(height: 20),
error: (_, __) => const SizedBox.shrink(),
),
),
const Divider(),
// Liste over registreringer
Expanded(
child: registrationsAsync.when(
data: (registrations) {
if (registrations.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.event_note,
size: 64,
color: Colors.grey[300],
),
const SizedBox(height: 16),
Text(
'Ingen registreringer denne dagen',
style: TextStyle(color: Colors.grey[600]),
),
],
),
);
}
return ListView.builder(
itemCount: registrations.length,
itemBuilder: (context, index) {
final reg = registrations[index];
return Card(
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
child: ListTile(
leading: CircleAvatar(
backgroundColor: _getColorForType(reg.type).withOpacity(0.2),
child: Icon(
_getIconForType(reg.type),
color: _getColorForType(reg.type),
size: 20,
),
),
title: Text(
'${DateFormat('HH:mm').format(reg.startTime)} - ${reg.endTime != null ? DateFormat('HH:mm').format(reg.endTime!) : 'Pågår'}',
style: const TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(_getTypeDisplayName(reg.type)),
if (reg.comment != null && reg.comment!.isNotEmpty)
Text(
reg.comment!,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(fontStyle: FontStyle.italic),
),
],
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (reg.deviations.isNotEmpty)
const Padding(
padding: EdgeInsets.only(right: 8.0),
child: Icon(Icons.warning_amber, color: Colors.orange),
),
Text(
reg.endTime != null
? '${reg.netWorkMinutes ~/ 60}t ${reg.netWorkMinutes % 60}m'
: '...',
style: const TextStyle(fontWeight: FontWeight.bold),
),
const Icon(Icons.chevron_right),
],
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => RegistrationDetailScreen(registration: reg),
),
);
},
),
);
},
);
},
loading: () => const Center(child: CircularProgressIndicator()),
error: (error, _) => Center(child: Text('Feil: $error')),
),
),
],
),
);
}
Color _getColorForType(RegistrationType type) {
switch (type) {
case RegistrationType.ordinary:
return Colors.blue;
case RegistrationType.overtime:
return Colors.orange;
case RegistrationType.oncall:
return Colors.purple;
case RegistrationType.travel:
return Colors.green;
}
}
IconData _getIconForType(RegistrationType type) {
switch (type) {
case RegistrationType.ordinary:
return Icons.work;
case RegistrationType.overtime:
return Icons.access_time_filled;
case RegistrationType.oncall:
return Icons.phone_in_talk;
case RegistrationType.travel:
return Icons.directions_car;
}
}
String _getTypeDisplayName(RegistrationType type) {
switch (type) {
case RegistrationType.ordinary:
return 'Ordinær';
case RegistrationType.overtime:
return 'Overtid';
case RegistrationType.oncall:
return 'Beredskap';
case RegistrationType.travel:
return 'Reisetid';
}
}
}
@@ -0,0 +1,297 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import '../../models/time_registration.dart';
import '../../providers/time_provider.dart';
class RegistrationDetailScreen extends ConsumerWidget {
final TimeRegistration registration;
const RegistrationDetailScreen({
super.key,
required this.registration,
});
@override
Widget build(BuildContext context, WidgetRef ref) {
final timeService = ref.read(timeServiceProvider);
return Scaffold(
appBar: AppBar(
title: const Text('Detaljer'),
actions: [
IconButton(
icon: const Icon(Icons.edit),
onPressed: () {
// TODO: Implementer redigering
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Redigering kommer snart')),
);
},
),
IconButton(
icon: const Icon(Icons.delete),
onPressed: () async {
final confirm = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Slett registrering?'),
content: const Text('Er du sikker på at du vil slette denne registreringen?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Avbryt'),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
style: TextButton.styleFrom(foregroundColor: Colors.red),
child: const Text('Slett'),
),
],
),
);
if (confirm == true) {
try {
await timeService.deleteRegistration(registration.id);
if (context.mounted) {
Navigator.pop(context);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Registrering slettet')),
);
// Invalidate providers to refresh lists
ref.invalidate(todayRegistrationsProvider);
}
} catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Feil: $e')),
);
}
}
}
},
),
],
),
body: ListView(
padding: const EdgeInsets.all(16.0),
children: [
// Hovedinfo kort
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
_DetailRow(
icon: Icons.calendar_today,
label: 'Dato',
value: DateFormat('EEEE d. MMMM yyyy', 'nb_NO').format(registration.startTime),
),
const Divider(),
_DetailRow(
icon: Icons.schedule,
label: 'Tidsrom',
value: '${DateFormat('HH:mm').format(registration.startTime)} - ${registration.endTime != null ? DateFormat('HH:mm').format(registration.endTime!) : 'Pågår'}',
),
const Divider(),
_DetailRow(
icon: Icons.timer,
label: 'Varighet',
value: registration.endTime != null
? '${registration.netWorkMinutes ~/ 60}t ${registration.netWorkMinutes % 60}m'
: '...',
),
const Divider(),
_DetailRow(
icon: _getIconForType(registration.type),
label: 'Type',
value: _getTypeDisplayName(registration.type),
valueColor: _getColorForType(registration.type),
),
],
),
),
),
const SizedBox(height: 16),
// Prosjekt/Kunde info
if (registration.projectId != null || registration.customerId != null)
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
if (registration.customerId != null)
_DetailRow(
icon: Icons.business,
label: 'Kunde',
value: registration.customerId!,
),
if (registration.customerId != null && registration.projectId != null)
const Divider(),
if (registration.projectId != null)
_DetailRow(
icon: Icons.folder,
label: 'Prosjekt',
value: registration.projectId!,
),
],
),
),
),
if (registration.projectId != null || registration.customerId != null)
const SizedBox(height: 16),
// Kommentar
if (registration.comment != null && registration.comment!.isNotEmpty)
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Icon(Icons.comment, size: 20, color: Colors.grey),
const SizedBox(width: 8),
Text(
'Kommentar',
style: Theme.of(context).textTheme.titleSmall?.copyWith(
color: Colors.grey,
),
),
],
),
const SizedBox(height: 8),
Text(registration.comment!),
],
),
),
),
// Avvik
if (registration.deviations.isNotEmpty) ...[
const SizedBox(height: 16),
const Text(
'Avvik',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.red,
),
),
const SizedBox(height: 8),
// Her ville vi normalt hentet avviksdetaljer fra Firestore
// For nå viser vi bare at det finnes avvik
Card(
color: Colors.red.shade50,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
children: [
const Icon(Icons.warning, color: Colors.red),
const SizedBox(width: 16),
Expanded(
child: Text(
'Denne registreringen har ${registration.deviations.length} registrerte avvik.',
style: const TextStyle(color: Colors.red),
),
),
],
),
),
),
],
],
),
);
}
Color _getColorForType(RegistrationType type) {
switch (type) {
case RegistrationType.ordinary:
return Colors.blue;
case RegistrationType.overtime:
return Colors.orange;
case RegistrationType.oncall:
return Colors.purple;
case RegistrationType.travel:
return Colors.green;
}
}
IconData _getIconForType(RegistrationType type) {
switch (type) {
case RegistrationType.ordinary:
return Icons.work;
case RegistrationType.overtime:
return Icons.access_time_filled;
case RegistrationType.oncall:
return Icons.phone_in_talk;
case RegistrationType.travel:
return Icons.directions_car;
}
}
String _getTypeDisplayName(RegistrationType type) {
switch (type) {
case RegistrationType.ordinary:
return 'Ordinær';
case RegistrationType.overtime:
return 'Overtid';
case RegistrationType.oncall:
return 'Beredskap';
case RegistrationType.travel:
return 'Reisetid';
}
}
}
class _DetailRow extends StatelessWidget {
final IconData icon;
final String label;
final String value;
final Color? valueColor;
const _DetailRow({
required this.icon,
required this.label,
required this.value,
this.valueColor,
});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Row(
children: [
Icon(icon, size: 20, color: Colors.grey),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Colors.grey,
),
),
Text(
value,
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
color: valueColor,
fontWeight: valueColor != null ? FontWeight.bold : null,
),
),
],
),
),
],
),
);
}
}