Files
SDKmuta/lib/screens/calendar_screen.dart
T

661 lines
21 KiB
Dart

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../services/google_calendar_service.dart';
import '../utils/translations.dart';
enum EventType {
regular, // Dežurstvo and other regular events
competition, // Liga and Tekmovanje (League and Competition events)
sestanek, // Meetings
}
class CalendarScreen extends StatefulWidget {
const CalendarScreen({super.key});
@override
State<CalendarScreen> createState() => _CalendarScreenState();
}
class _CalendarScreenState extends State<CalendarScreen> {
final GoogleCalendarService _calendarService = GoogleCalendarService();
bool _isLoading = false;
List<CalendarEvent> _events = [];
String? _errorMessage;
String _bulletColor = 'Blue';
String _selectedLanguage = 'English';
@override
void initState() {
super.initState();
initializeDateFormatting('sl_SI', null); // Initialize Slovenian locale
_loadBulletColor();
_loadLanguage();
_loadEvents();
}
String _t(String key) {
return AppTranslations.translate(key, _selectedLanguage);
}
Future<void> _loadLanguage() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_selectedLanguage = prefs.getString('language') ?? 'English';
});
}
@override
void dispose() {
_calendarService.dispose();
super.dispose();
}
Future<void> _loadBulletColor() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_bulletColor = prefs.getString('bulletColor') ?? 'Blue';
});
}
Color _getAccentColor() {
switch (_bulletColor.toLowerCase()) {
case 'blue':
return Colors.blue;
case 'green':
return Colors.green;
case 'yellow':
return Colors.yellow;
case 'purple':
return Colors.purple;
case 'cyan':
return Colors.cyan;
case 'teal':
return Colors.teal;
case 'indigo':
return Colors.indigo;
case 'pink':
return Colors.pink;
case 'lime':
return Colors.lime;
case 'mint':
return Colors.greenAccent;
case 'orange':
return Colors.orange;
case 'violet':
return Colors.deepPurpleAccent;
case 'amber':
return Colors.amber;
case 'brown':
return Colors.brown;
case 'grey':
return Colors.grey;
default:
return Colors.blue;
}
}
bool _isValidEvent(CalendarEvent event) {
// Filter out events with empty summaries
if (event.summary == null || event.summary!.trim().isEmpty) {
return false;
}
final summary = event.summary!.trim();
// Only exclude very short single-character entries
// This allows short event names like "Meeting", "Training", etc.
if (summary.length < 2) {
return false;
}
return true;
}
EventType _getEventType(CalendarEvent event) {
if (event.summary == null) return EventType.regular;
final summary = event.summary!.toLowerCase();
if (summary.contains('liga') || summary.contains('tekmovanje') || summary.contains('tekma')) {
return EventType.competition;
} else if (summary.contains('sestanek')) {
return EventType.sestanek;
}
return EventType.regular;
}
Future<void> _loadEvents() async {
setState(() {
_isLoading = true;
_errorMessage = null;
});
try {
final events = await _calendarService.getEvents(
startDate: DateTime.now(),
endDate: DateTime.now().add(const Duration(days: 365)),
);
// Get today's date at midnight for comparison
final now = DateTime.now();
final today = DateTime(now.year, now.month, now.day);
// Filter: valid events AND events from today onwards
final filteredEvents = events.where((event) {
if (!_isValidEvent(event)) return false;
// Only include events that are today or in the future
if (event.startTime != null) {
final eventDate = DateTime(
event.startTime!.year,
event.startTime!.month,
event.startTime!.day,
);
return eventDate.isAtSameMomentAs(today) || eventDate.isAfter(today);
}
return false;
}).toList();
filteredEvents.sort((a, b) {
if (a.startTime == null) return 1;
if (b.startTime == null) return -1;
return a.startTime!.compareTo(b.startTime!);
});
if (mounted) {
setState(() {
_events = filteredEvents;
});
}
} catch (e) {
if (mounted) {
setState(() {
_errorMessage = 'Error loading events: $e';
});
}
} finally {
if (mounted) {
setState(() => _isLoading = false);
}
}
}
@override
Widget build(BuildContext context) {
final accentColor = _getAccentColor();
if (_isLoading && _events.isEmpty) {
return Scaffold(
body: Center(
child: CircularProgressIndicator(color: accentColor),
),
);
}
if (_errorMessage != null && _events.isEmpty) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.error_outline, size: 64, color: accentColor),
const SizedBox(height: 24),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 32),
child: Text(
_errorMessage!,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 16),
),
),
const SizedBox(height: 16),
ElevatedButton.icon(
onPressed: _loadEvents,
icon: const Icon(Icons.refresh),
label: Text(_t('Retry')),
style: ElevatedButton.styleFrom(
backgroundColor: accentColor,
foregroundColor: Colors.white,
),
),
],
),
),
);
}
return Scaffold(
backgroundColor: Colors.white,
body: RefreshIndicator(
onRefresh: _loadEvents,
color: accentColor,
child: _events.isEmpty
? ListView(
children: [
const SizedBox(height: 24),
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.event_available, size: 64, color: Colors.grey[400]),
const SizedBox(height: 16),
Text(
_t('No upcoming events'),
style: const TextStyle(fontSize: 16, color: Colors.grey),
),
const SizedBox(height: 8),
Text(
_t('Pull down to refresh'),
style: TextStyle(fontSize: 14, color: Colors.grey[400]),
),
],
),
),
const SizedBox(height: 32),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
_t('Example Event Preview'),
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.grey[700],
),
textAlign: TextAlign.center,
),
),
const SizedBox(height: 16),
_buildSampleEventCard(accentColor, EventType.regular),
const SizedBox(height: 8),
_buildSampleEventCard(accentColor, EventType.competition),
const SizedBox(height: 8),
_buildSampleEventCard(accentColor, EventType.sestanek),
],
)
: ListView.builder(
itemCount: _events.length,
padding: const EdgeInsets.all(8),
itemBuilder: (context, index) {
final event = _events[index];
return _buildEventCard(event, accentColor);
},
),
),
);
}
Widget _buildSampleEventCard(Color accentColor, EventType eventType) {
// Color coding for each event type
Color cardAccentColor;
Color backgroundColor = Colors.grey[300]!;
IconData eventIcon;
String title;
String time;
switch (eventType) {
case EventType.competition:
cardAccentColor = Colors.green;
eventIcon = Icons.emoji_events;
title = _selectedLanguage == 'Slovenian' ? 'Liga / Tekmovanje' : 'League / Competition';
time = '09:00 - 18:00';
break;
case EventType.sestanek:
cardAccentColor = Colors.purple;
eventIcon = Icons.groups;
title = _selectedLanguage == 'Slovenian' ? 'Sestanek ekipe' : 'Team Meeting';
time = '18:00 - 19:00';
break;
case EventType.regular:
cardAccentColor = Colors.blue;
eventIcon = Icons.event;
title = _selectedLanguage == 'Slovenian' ? 'Dežurstvo' : 'Duty Shift';
time = '14:00 - 16:00';
break;
}
return Container(
margin: const EdgeInsets.symmetric(vertical: 6, horizontal: 16),
decoration: BoxDecoration(
color: backgroundColor,
borderRadius: BorderRadius.circular(12),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Date header
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
color: cardAccentColor.withValues(alpha: 0.2),
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
),
),
child: Row(
children: [
// Small colored indicator
Container(
width: 4,
height: 16,
decoration: BoxDecoration(
color: cardAccentColor,
borderRadius: BorderRadius.circular(2),
),
),
const SizedBox(width: 8),
Text(
_t('Today'),
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: cardAccentColor,
),
),
const Spacer(),
// Event type badge - filled badges for all types
Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
decoration: BoxDecoration(
color: cardAccentColor,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: cardAccentColor.withValues(alpha: 0.3),
blurRadius: 4,
offset: const Offset(0, 2),
),
],
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(eventIcon, size: 12, color: Colors.white),
const SizedBox(width: 4),
Text(
eventType == EventType.competition
? (_selectedLanguage == 'Slovenian' ? 'TEKMOVANJE' : 'COMPETITION')
: eventType == EventType.sestanek
? (_selectedLanguage == 'Slovenian' ? 'SESTANEK' : 'MEETING')
: (_selectedLanguage == 'Slovenian' ? 'DEŽURSTVO' : 'DUTY'),
style: const TextStyle(
fontSize: 10,
fontWeight: FontWeight.bold,
color: Colors.white,
letterSpacing: 0.8,
),
),
],
),
),
],
),
),
// Main content
Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Title with icon
Row(
children: [
Icon(eventIcon, size: 20, color: cardAccentColor),
const SizedBox(width: 8),
Expanded(
child: Text(
title,
style: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.w600,
color: Colors.black87,
),
),
),
],
),
const SizedBox(height: 8),
// Time range
_buildDetailRowInline(
icon: Icons.access_time,
value: time,
accentColor: cardAccentColor,
),
const SizedBox(height: 6),
// Location
_buildDetailRowInline(
icon: Icons.location_on,
value: _selectedLanguage == 'Slovenian'
? 'Strelišče Ljubljana'
: 'Shooting Range',
accentColor: cardAccentColor,
),
],
),
),
],
),
);
}
Widget _buildEventCard(CalendarEvent event, Color accentColor) {
final startTime = event.startTime;
final isAllDay = event.isAllDay;
final eventType = _getEventType(event);
// Color coding for each event type
Color cardAccentColor;
Color backgroundColor;
IconData eventIcon;
switch (eventType) {
case EventType.competition:
cardAccentColor = Colors.green;
backgroundColor = Colors.grey[300]!;
eventIcon = Icons.emoji_events; // Trophy icon for competitions
break;
case EventType.sestanek:
cardAccentColor = Colors.purple;
backgroundColor = Colors.grey[300]!;
eventIcon = Icons.groups; // People icon for meetings
break;
case EventType.regular:
cardAccentColor = Colors.blue;
backgroundColor = Colors.grey[300]!;
eventIcon = Icons.event;
break;
}
// Format the date for display
String dateText = '';
bool isToday = false;
if (startTime != null) {
final now = DateTime.now();
final today = DateTime(now.year, now.month, now.day);
final eventDay = DateTime(startTime.year, startTime.month, startTime.day);
final difference = eventDay.difference(today).inDays;
if (difference == 0) {
dateText = _t('Today');
isToday = true;
} else if (difference == 1) {
dateText = _t('Tomorrow');
} else {
// Use locale based on selected language
final locale = _selectedLanguage == 'Slovenian' ? 'sl_SI' : 'en_US';
dateText = DateFormat('EEEE, MMMM d', locale).format(startTime);
// Capitalize first letter of day and month
if (dateText.isNotEmpty) {
dateText = dateText[0].toUpperCase() + dateText.substring(1);
}
}
}
return Container(
margin: const EdgeInsets.symmetric(vertical: 6, horizontal: 16),
decoration: BoxDecoration(
color: backgroundColor,
borderRadius: BorderRadius.circular(12),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Date header
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
color: isToday
? cardAccentColor.withValues(alpha: 0.2)
: cardAccentColor.withValues(alpha: 0.1),
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
),
),
child: Row(
children: [
// Small colored indicator
Container(
width: 4,
height: 16,
decoration: BoxDecoration(
color: cardAccentColor,
borderRadius: BorderRadius.circular(2),
),
),
const SizedBox(width: 8),
Text(
dateText,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: cardAccentColor,
),
),
const Spacer(),
// Event type badge - filled badges for all types
Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
decoration: BoxDecoration(
color: cardAccentColor,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: cardAccentColor.withValues(alpha: 0.3),
blurRadius: 4,
offset: const Offset(0, 2),
),
],
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(eventIcon, size: 12, color: Colors.white),
const SizedBox(width: 4),
Text(
eventType == EventType.competition
? (_selectedLanguage == 'Slovenian' ? 'TEKMOVANJE' : 'COMPETITION')
: eventType == EventType.sestanek
? (_selectedLanguage == 'Slovenian' ? 'SESTANEK' : 'MEETING')
: (_selectedLanguage == 'Slovenian' ? 'DEŽURSTVO' : 'DUTY'),
style: const TextStyle(
fontSize: 10,
fontWeight: FontWeight.bold,
color: Colors.white,
letterSpacing: 0.8,
),
),
],
),
),
],
),
),
// Main content
Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Title with icon
Row(
children: [
Icon(eventIcon, size: 20, color: cardAccentColor),
const SizedBox(width: 8),
Expanded(
child: Text(
event.summary ?? _t('Untitled Event'),
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w600,
color: Colors.black87,
),
),
),
],
),
const SizedBox(height: 8),
// Time range (if not all day)
if (!isAllDay && startTime != null && event.endTime != null) ...[
_buildDetailRowInline(
icon: Icons.access_time,
value: '${DateFormat('HH:mm').format(startTime.toLocal())} - ${DateFormat('HH:mm').format(event.endTime!.toLocal())}',
accentColor: cardAccentColor,
),
const SizedBox(height: 6),
],
if (isAllDay) ...[
_buildDetailRowInline(
icon: Icons.access_time,
value: _t('All day'),
accentColor: cardAccentColor,
),
const SizedBox(height: 6),
],
// Location
if (event.location != null && event.location!.isNotEmpty) ...[
_buildDetailRowInline(
icon: Icons.location_on,
value: event.location!,
accentColor: cardAccentColor,
),
],
],
),
),
],
),
);
}
Widget _buildDetailRowInline({
required IconData icon,
required String value,
required Color accentColor,
}) {
return Row(
children: [
Icon(icon, size: 16, color: accentColor),
const SizedBox(width: 6),
Expanded(
child: Text(
value,
style: const TextStyle(
fontSize: 13,
color: Colors.black87,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
],
);
}
}