Initial commit: TimeReg Flutter app med Firebase backend

- Opprettet Flutter-prosjekt med alle nødvendige avhengigheter
- Implementert datamodeller (User, TimeRegistration, TariffProfile, Deviation, AuditLog)
- Implementert tjenester (AuthService, TimeService)
- Implementert Riverpod providers for state management
- Opprettet autentiseringsskjermer (login, signup, reset password)
- Opprettet hjemmeskjerm med timer-funksjonalitet
- Opprettet placeholder-skjermer for historikk, rapporter og profil
- Lagt til norsk dokumentasjon i README
This commit is contained in:
steinhelge
2025-11-24 20:52:27 +01:00
commit c829f78984
148 changed files with 8462 additions and 0 deletions
+202
View File
@@ -0,0 +1,202 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../providers/auth_provider.dart';
import 'signup_screen.dart';
import 'reset_password_screen.dart';
class LoginScreen extends ConsumerStatefulWidget {
const LoginScreen({super.key});
@override
ConsumerState<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends ConsumerState<LoginScreen> {
final _formKey = GlobalKey<FormState>();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
bool _isLoading = false;
bool _obscurePassword = true;
@override
void dispose() {
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
Future<void> _handleLogin() async {
if (!_formKey.currentState!.validate()) return;
setState(() => _isLoading = true);
try {
final login = ref.read(loginProvider);
await login(
email: _emailController.text.trim(),
password: _passwordController.text,
);
// Navigation håndteres automatisk av authStateProvider i main.dart
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(e.toString()),
backgroundColor: Colors.red,
),
);
}
} finally {
if (mounted) {
setState(() => _isLoading = false);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24.0),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Logo/Ikon
Icon(
Icons.access_time_rounded,
size: 80,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(height: 16),
// Tittel
Text(
'TimeReg',
style: Theme.of(context).textTheme.headlineLarge?.copyWith(
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
Text(
'Registrer arbeidstid med AML-kontroll',
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Colors.grey,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 48),
// E-post felt
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
labelText: 'E-post',
prefixIcon: Icon(Icons.email_outlined),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Vennligst skriv inn e-post';
}
if (!value.contains('@')) {
return 'Vennligst skriv inn en gyldig e-post';
}
return null;
},
),
const SizedBox(height: 16),
// Passord felt
TextFormField(
controller: _passwordController,
obscureText: _obscurePassword,
decoration: InputDecoration(
labelText: 'Passord',
prefixIcon: const Icon(Icons.lock_outlined),
suffixIcon: IconButton(
icon: Icon(
_obscurePassword
? Icons.visibility_outlined
: Icons.visibility_off_outlined,
),
onPressed: () {
setState(() {
_obscurePassword = !_obscurePassword;
});
},
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Vennligst skriv inn passord';
}
return null;
},
),
const SizedBox(height: 8),
// Glemt passord
Align(
alignment: Alignment.centerRight,
child: TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const ResetPasswordScreen(),
),
);
},
child: const Text('Glemt passord?'),
),
),
const SizedBox(height: 24),
// Logg inn knapp
FilledButton(
onPressed: _isLoading ? null : _handleLogin,
style: FilledButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
child: _isLoading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('Logg inn'),
),
const SizedBox(height: 16),
// Opprett konto
OutlinedButton(
onPressed: _isLoading
? null
: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SignUpScreen(),
),
);
},
style: OutlinedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
child: const Text('Opprett konto'),
),
],
),
),
),
),
),
);
}
}
+116
View File
@@ -0,0 +1,116 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../providers/auth_provider.dart';
class ResetPasswordScreen extends ConsumerStatefulWidget {
const ResetPasswordScreen({super.key});
@override
ConsumerState<ResetPasswordScreen> createState() => _ResetPasswordScreenState();
}
class _ResetPasswordScreenState extends ConsumerState<ResetPasswordScreen> {
final _formKey = GlobalKey<FormState>();
final _emailController = TextEditingController();
bool _isLoading = false;
@override
void dispose() {
_emailController.dispose();
super.dispose();
}
Future<void> _handleResetPassword() async {
if (!_formKey.currentState!.validate()) return;
setState(() => _isLoading = true);
try {
final resetPassword = ref.read(resetPasswordProvider);
await resetPassword(_emailController.text.trim());
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('E-post for tilbakestilling av passord er sendt'),
backgroundColor: Colors.green,
),
);
Navigator.pop(context);
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(e.toString()),
backgroundColor: Colors.red,
),
);
}
} finally {
if (mounted) {
setState(() => _isLoading = false);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Tilbakestill passord'),
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text(
'Skriv inn e-postadressen din, så sender vi deg en lenke for å tilbakestille passordet.',
style: TextStyle(fontSize: 16),
),
const SizedBox(height: 24),
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
labelText: 'E-post',
prefixIcon: Icon(Icons.email_outlined),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Vennligst skriv inn e-post';
}
if (!value.contains('@')) {
return 'Vennligst skriv inn en gyldig e-post';
}
return null;
},
),
const SizedBox(height: 24),
FilledButton(
onPressed: _isLoading ? null : _handleResetPassword,
style: FilledButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
child: _isLoading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('Send tilbakestillingslenke'),
),
],
),
),
),
),
);
}
}
+218
View File
@@ -0,0 +1,218 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../providers/auth_provider.dart';
class SignUpScreen extends ConsumerStatefulWidget {
const SignUpScreen({super.key});
@override
ConsumerState<SignUpScreen> createState() => _SignUpScreenState();
}
class _SignUpScreenState extends ConsumerState<SignUpScreen> {
final _formKey = GlobalKey<FormState>();
final _nameController = TextEditingController();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
final _confirmPasswordController = TextEditingController();
final _organizationIdController = TextEditingController();
bool _isLoading = false;
bool _obscurePassword = true;
bool _obscureConfirmPassword = true;
@override
void dispose() {
_nameController.dispose();
_emailController.dispose();
_passwordController.dispose();
_confirmPasswordController.dispose();
_organizationIdController.dispose();
super.dispose();
}
Future<void> _handleSignUp() async {
if (!_formKey.currentState!.validate()) return;
setState(() => _isLoading = true);
try {
final signUp = ref.read(signUpProvider);
await signUp(
email: _emailController.text.trim(),
password: _passwordController.text,
displayName: _nameController.text.trim(),
organizationId: _organizationIdController.text.trim(),
);
if (mounted) {
Navigator.pop(context);
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(e.toString()),
backgroundColor: Colors.red,
),
);
}
} finally {
if (mounted) {
setState(() => _isLoading = false);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Opprett konto'),
),
body: SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Navn
TextFormField(
controller: _nameController,
decoration: const InputDecoration(
labelText: 'Fullt navn',
prefixIcon: Icon(Icons.person_outlined),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Vennligst skriv inn navn';
}
return null;
},
),
const SizedBox(height: 16),
// E-post
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
labelText: 'E-post',
prefixIcon: Icon(Icons.email_outlined),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Vennligst skriv inn e-post';
}
if (!value.contains('@')) {
return 'Vennligst skriv inn en gyldig e-post';
}
return null;
},
),
const SizedBox(height: 16),
// Organisasjons-ID
TextFormField(
controller: _organizationIdController,
decoration: const InputDecoration(
labelText: 'Organisasjons-ID',
prefixIcon: Icon(Icons.business_outlined),
helperText: 'Kontakt administrator for organisasjons-ID',
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Vennligst skriv inn organisasjons-ID';
}
return null;
},
),
const SizedBox(height: 16),
// Passord
TextFormField(
controller: _passwordController,
obscureText: _obscurePassword,
decoration: InputDecoration(
labelText: 'Passord',
prefixIcon: const Icon(Icons.lock_outlined),
suffixIcon: IconButton(
icon: Icon(
_obscurePassword
? Icons.visibility_outlined
: Icons.visibility_off_outlined,
),
onPressed: () {
setState(() {
_obscurePassword = !_obscurePassword;
});
},
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Vennligst skriv inn passord';
}
if (value.length < 6) {
return 'Passordet må være minst 6 tegn';
}
return null;
},
),
const SizedBox(height: 16),
// Bekreft passord
TextFormField(
controller: _confirmPasswordController,
obscureText: _obscureConfirmPassword,
decoration: InputDecoration(
labelText: 'Bekreft passord',
prefixIcon: const Icon(Icons.lock_outlined),
suffixIcon: IconButton(
icon: Icon(
_obscureConfirmPassword
? Icons.visibility_outlined
: Icons.visibility_off_outlined,
),
onPressed: () {
setState(() {
_obscureConfirmPassword = !_obscureConfirmPassword;
});
},
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Vennligst bekreft passord';
}
if (value != _passwordController.text) {
return 'Passordene matcher ikke';
}
return null;
},
),
const SizedBox(height: 32),
// Opprett konto knapp
FilledButton(
onPressed: _isLoading ? null : _handleSignUp,
style: FilledButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
child: _isLoading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('Opprett konto'),
),
],
),
),
),
),
);
}
}
+17
View File
@@ -0,0 +1,17 @@
import 'package:flutter/material.dart';
class HistoryScreen extends StatelessWidget {
const HistoryScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Historikk'),
),
body: const Center(
child: Text('Historikk kommer her'),
),
);
}
}
+311
View File
@@ -0,0 +1,311 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import '../../providers/auth_provider.dart';
import '../../providers/time_provider.dart';
import '../../services/time_service.dart';
import '../../models/time_registration.dart';
import '../history/history_screen.dart';
import '../reports/reports_screen.dart';
import '../profile/profile_screen.dart';
import '../../widgets/timer_widget.dart';
class HomeScreen extends ConsumerStatefulWidget {
const HomeScreen({super.key});
@override
ConsumerState<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends ConsumerState<HomeScreen> {
int _selectedIndex = 0;
@override
Widget build(BuildContext context) {
final List<Widget> screens = [
const HomeTab(),
const HistoryScreen(),
const ReportsScreen(),
const ProfileScreen(),
];
return Scaffold(
body: screens[_selectedIndex],
bottomNavigationBar: NavigationBar(
selectedIndex: _selectedIndex,
onDestinationSelected: (index) {
setState(() {
_selectedIndex = index;
});
},
destinations: const [
NavigationDestination(
icon: Icon(Icons.home_outlined),
selectedIcon: Icon(Icons.home),
label: 'Hjem',
),
NavigationDestination(
icon: Icon(Icons.history_outlined),
selectedIcon: Icon(Icons.history),
label: 'Historikk',
),
NavigationDestination(
icon: Icon(Icons.bar_chart_outlined),
selectedIcon: Icon(Icons.bar_chart),
label: 'Rapporter',
),
NavigationDestination(
icon: Icon(Icons.person_outlined),
selectedIcon: Icon(Icons.person),
label: 'Profil',
),
],
),
);
}
}
class HomeTab extends ConsumerWidget {
const HomeTab({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final userData = ref.watch(userDataProvider);
final todayRegistrations = ref.watch(todayRegistrationsProvider);
return Scaffold(
appBar: AppBar(
title: const Text('TimeReg'),
actions: [
IconButton(
icon: const Icon(Icons.notifications_outlined),
onPressed: () {
// TODO: Implementer varsler
},
),
],
),
body: SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Velkomst
userData.when(
data: (user) => user != null
? Text(
'Hei, ${user.displayName}!',
style: Theme.of(context).textTheme.headlineSmall,
)
: const SizedBox.shrink(),
loading: () => const SizedBox.shrink(),
error: (_, __) => const SizedBox.shrink(),
),
const SizedBox(height: 8),
Text(
DateFormat('EEEE d. MMMM yyyy', 'nb_NO').format(DateTime.now()),
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Colors.grey,
),
),
const SizedBox(height: 24),
// Timer widget
const TimerWidget(),
const SizedBox(height: 24),
// Dagens registreringer
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'I dag',
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
),
),
todayRegistrations.when(
data: (registrations) {
final totalMinutes = registrations
.where((r) => r.endTime != null)
.fold<int>(0, (sum, r) => sum + r.netWorkMinutes);
final hours = totalMinutes ~/ 60;
final minutes = totalMinutes % 60;
return Text(
'${hours}t ${minutes}m',
style: Theme.of(context).textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.primary,
),
);
},
loading: () => const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
),
error: (_, __) => const Text('--'),
),
],
),
const SizedBox(height: 16),
todayRegistrations.when(
data: (registrations) {
if (registrations.isEmpty) {
return const Text('Ingen registreringer i dag');
}
return Column(
children: registrations.map((reg) {
return ListTile(
contentPadding: EdgeInsets.zero,
leading: Icon(
_getIconForType(reg.type),
color: Theme.of(context).colorScheme.primary,
),
title: Text(
'${DateFormat('HH:mm').format(reg.startTime)} - ${reg.endTime != null ? DateFormat('HH:mm').format(reg.endTime!) : 'Pågår'}',
),
subtitle: reg.comment != null
? Text(reg.comment!)
: null,
trailing: reg.endTime != null
? Text(
'${reg.netWorkMinutes ~/ 60}t ${reg.netWorkMinutes % 60}m',
style: const TextStyle(
fontWeight: FontWeight.bold,
),
)
: const Icon(Icons.play_arrow, color: Colors.green),
);
}).toList(),
);
},
loading: () => const Center(
child: CircularProgressIndicator(),
),
error: (error, _) => Text('Feil: $error'),
),
],
),
),
),
const SizedBox(height: 16),
// Ukens oversikt
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Denne uken',
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 16),
_WeekSummary(),
],
),
),
),
],
),
),
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () {
// TODO: Implementer manuell registrering
},
icon: const Icon(Icons.add),
label: const Text('Manuell registrering'),
),
);
}
IconData _getIconForType(RegistrationType type) {
switch (type) {
case RegistrationType.ordinary:
return Icons.work_outline;
case RegistrationType.overtime:
return Icons.access_time_filled;
case RegistrationType.oncall:
return Icons.phone_in_talk;
case RegistrationType.travel:
return Icons.directions_car;
}
}
}
class _WeekSummary extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final now = DateTime.now();
final startOfWeek = now.subtract(Duration(days: now.weekday - 1));
final endOfWeek = startOfWeek.add(const Duration(days: 6, hours: 23, minutes: 59));
final dateRange = DateRange(start: startOfWeek, end: endOfWeek);
final totalHours = ref.watch(totalHoursProvider(dateRange));
return totalHours.when(
data: (hours) {
final totalMinutes = hours['total'] ?? 0;
final ordinaryMinutes = hours['ordinary'] ?? 0;
final overtimeMinutes = hours['overtime'] ?? 0;
return Column(
children: [
_SummaryRow(
label: 'Total arbeidstid',
value: '${totalMinutes ~/ 60}t ${totalMinutes % 60}m',
),
const SizedBox(height: 8),
_SummaryRow(
label: 'Ordinær tid',
value: '${ordinaryMinutes ~/ 60}t ${ordinaryMinutes % 60}m',
),
const SizedBox(height: 8),
_SummaryRow(
label: 'Overtid',
value: '${overtimeMinutes ~/ 60}t ${overtimeMinutes % 60}m',
),
],
);
},
loading: () => const Center(child: CircularProgressIndicator()),
error: (error, _) => Text('Feil: $error'),
);
}
}
class _SummaryRow extends StatelessWidget {
final String label;
final String value;
const _SummaryRow({
required this.label,
required this.value,
});
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(label),
Text(
value,
style: const TextStyle(fontWeight: FontWeight.bold),
),
],
);
}
}
+90
View File
@@ -0,0 +1,90 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../providers/auth_provider.dart';
class ProfileScreen extends ConsumerWidget {
const ProfileScreen({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final userData = ref.watch(userDataProvider);
final signOut = ref.read(signOutProvider);
return Scaffold(
appBar: AppBar(
title: const Text('Profil'),
),
body: userData.when(
data: (user) {
if (user == null) {
return const Center(child: Text('Ingen brukerdata'));
}
return ListView(
padding: const EdgeInsets.all(16.0),
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
CircleAvatar(
radius: 40,
child: Text(
user.displayName.isNotEmpty
? user.displayName[0].toUpperCase()
: '?',
style: const TextStyle(fontSize: 32),
),
),
const SizedBox(height: 16),
Text(
user.displayName,
style: Theme.of(context).textTheme.titleLarge,
),
Text(
user.email,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Colors.grey,
),
),
const SizedBox(height: 8),
Chip(
label: Text(
user.role.name == 'employee'
? 'Ansatt'
: user.role.name == 'admin'
? 'Administrator'
: 'Systemadministrator',
),
),
],
),
),
),
const SizedBox(height: 16),
ListTile(
leading: const Icon(Icons.business),
title: const Text('Organisasjon'),
subtitle: Text(user.organizationId),
),
const Divider(),
ListTile(
leading: const Icon(Icons.logout),
title: const Text('Logg ut'),
onTap: () async {
await signOut();
},
),
],
);
},
loading: () => const Center(child: CircularProgressIndicator()),
error: (error, _) => Center(child: Text('Feil: $error')),
),
);
}
}
+17
View File
@@ -0,0 +1,17 @@
import 'package:flutter/material.dart';
class ReportsScreen extends StatelessWidget {
const ReportsScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Rapporter'),
),
body: const Center(
child: Text('Rapporter kommer her'),
),
);
}
}