194 lines
5.5 KiB
Dart
194 lines
5.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:insparkspokalen_ui/info/informationPage.dart';
|
|
import 'package:insparkspokalen_ui/info/AdminInformationPage.dart';
|
|
import 'package:insparkspokalen_ui/ProfileInfoTab/ProfilePage.dart';
|
|
import 'package:insparkspokalen_ui/ProfileInfoTab/ProfilePageAdmin.dart';
|
|
import 'package:insparkspokalen_ui/services/backend/informationService.dart';
|
|
import 'package:insparkspokalen_ui/models/informationModel.dart';
|
|
|
|
class BaseScaffold extends StatefulWidget {
|
|
final String title;
|
|
final int selectedIndex;
|
|
final Function(int) onTap;
|
|
final Widget body;
|
|
final String role;
|
|
|
|
const BaseScaffold({
|
|
super.key,
|
|
required this.title,
|
|
required this.selectedIndex,
|
|
required this.onTap,
|
|
required this.body,
|
|
required this.role,
|
|
});
|
|
|
|
@override
|
|
State<BaseScaffold> createState() => _BaseScaffoldState();
|
|
}
|
|
|
|
class _BaseScaffoldState extends State<BaseScaffold> {
|
|
bool _hasNewInformation = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_checkForNewInformation();
|
|
}
|
|
|
|
Future<void> _checkForNewInformation() async {
|
|
try {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final lastSeenTimestamp = prefs.getInt('last_seen_information') ?? 0;
|
|
final lastSeenDate = DateTime.fromMillisecondsSinceEpoch(lastSeenTimestamp);
|
|
|
|
final informationList = await InformationService().fetchInformation();
|
|
|
|
// Check if there's any information newer than last seen
|
|
final hasNewInfo = informationList.any((info) =>
|
|
info.createdAt.isAfter(lastSeenDate)
|
|
);
|
|
|
|
if (mounted) {
|
|
setState(() {
|
|
_hasNewInformation = hasNewInfo;
|
|
});
|
|
}
|
|
} catch (e) {
|
|
print("Error checking for new information: $e");
|
|
}
|
|
}
|
|
|
|
Future<void> _markInformationAsSeen() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setInt('last_seen_information', DateTime.now().millisecondsSinceEpoch);
|
|
|
|
if (mounted) {
|
|
setState(() {
|
|
_hasNewInformation = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
Widget _buildInfoIconWithBadge() {
|
|
return Stack(
|
|
children: [
|
|
IconButton(
|
|
icon: const Icon(Icons.info, color: Color(0xFFDAA520)),
|
|
onPressed: () async {
|
|
// Mark as seen when user opens the information page
|
|
await _markInformationAsSeen();
|
|
|
|
if (widget.role == 'ADMIN') {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => const AdminInformationPage(),
|
|
),
|
|
);
|
|
} else {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => const InformationPage(),
|
|
),
|
|
);
|
|
}
|
|
},
|
|
),
|
|
if (_hasNewInformation)
|
|
Positioned(
|
|
right: 8,
|
|
top: 8,
|
|
child: Container(
|
|
width: 10,
|
|
height: 10,
|
|
decoration: const BoxDecoration(
|
|
color: Colors.red,
|
|
shape: BoxShape.circle,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget build(BuildContext context) {
|
|
final items = <BottomNavigationBarItem>[
|
|
const BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Feed'),
|
|
const BottomNavigationBarItem(
|
|
icon: Icon(Icons.calendar_month),
|
|
label: 'Events',
|
|
),
|
|
const BottomNavigationBarItem(
|
|
icon: Icon(Icons.leaderboard),
|
|
label: 'Poängställning',
|
|
),
|
|
const BottomNavigationBarItem(
|
|
icon: Icon(Icons.description),
|
|
label: 'Uppdrag',
|
|
),
|
|
];
|
|
|
|
if (widget.role == 'ADMIN') {
|
|
items.add(
|
|
const BottomNavigationBarItem(
|
|
icon: Icon(Icons.group),
|
|
label: 'Grupper',
|
|
),
|
|
);
|
|
}
|
|
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFF2E2E2E),
|
|
appBar: AppBar(
|
|
backgroundColor: const Color(0xFF2E2E2E),
|
|
centerTitle: true,
|
|
iconTheme: const IconThemeData(color: Color(0xFFD6D6D6)),
|
|
title: Text(
|
|
widget.title,
|
|
style: const TextStyle(
|
|
color: Color(0xFFDAA520),
|
|
fontSize: 20,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
actions: [
|
|
_buildInfoIconWithBadge(),
|
|
IconButton(
|
|
icon: const Icon(Icons.person_outline, color: Color(0xFFDAA520)),
|
|
onPressed: () {
|
|
if (widget.role == 'ADMIN') {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => const ProfilePageAdmin(),
|
|
),
|
|
);
|
|
} else {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => const ProfilePage()),
|
|
);
|
|
}
|
|
},
|
|
),
|
|
const SizedBox(width: 12),
|
|
],
|
|
),
|
|
body: widget.body,
|
|
bottomNavigationBar: BottomNavigationBar(
|
|
backgroundColor: const Color(0xFF2E2E2E),
|
|
type: BottomNavigationBarType.fixed,
|
|
selectedItemColor: const Color(0xFFDAA520),
|
|
unselectedItemColor: Colors.grey,
|
|
currentIndex: widget.selectedIndex,
|
|
onTap: (index) {
|
|
if (widget.role != 'ADMIN' && index >= items.length) return;
|
|
widget.onTap(index);
|
|
},
|
|
items: items,
|
|
),
|
|
);
|
|
}
|
|
} |