Calendar implementation

This commit is contained in:
2025-10-10 16:57:11 +02:00
parent ff0d6b7796
commit dc86955721
17 changed files with 1194 additions and 103 deletions
+520 -40
View File
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../services/google_calendar_service.dart';
class CalendarScreen extends StatefulWidget {
const CalendarScreen({super.key});
@@ -9,59 +11,537 @@ class CalendarScreen extends StatefulWidget {
}
class _CalendarScreenState extends State<CalendarScreen> {
late final WebViewController _controller;
bool _isLoading = true;
bool _hasError = false;
final GoogleCalendarService _calendarService = GoogleCalendarService();
bool _isLoading = false;
List<CalendarEvent> _events = [];
String? _errorMessage;
String _bulletColor = 'Blue';
@override
void initState() {
super.initState();
_controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setNavigationDelegate(
NavigationDelegate(
onPageStarted: (_) => setState(() {
_isLoading = true;
_hasError = false;
}),
onPageFinished: (_) => setState(() => _isLoading = false),
onWebResourceError: (error) => setState(() {
_hasError = true;
_isLoading = false;
}),
),
)
..loadRequest(Uri.parse(
'https://calendar.google.com/calendar/u/0/embed?src=b9c267252d5be63d5cdc880be499885a88421eefd0e34e1392890daba0d8471e@group.calendar.google.com&ctz=Europe/Belgrade'));
_loadBulletColor();
_loadEvents();
}
@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 that are just single names or too short
if (event.summary == null || event.summary!.trim().isEmpty) {
return false;
}
final summary = event.summary!.trim();
// Exclude if it's just a single word (likely just a name)
if (!summary.contains(' ') && summary.length < 15) {
return false;
}
return true;
}
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!);
});
setState(() {
_events = filteredEvents;
});
} catch (e) {
setState(() {
_errorMessage = 'Error loading events: $e';
});
} finally {
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: const Text('Retry'),
style: ElevatedButton.styleFrom(
backgroundColor: accentColor,
foregroundColor: Colors.white,
),
),
],
),
),
);
}
return Scaffold(
body: Stack(
children: [
WebViewWidget(controller: _controller),
if (_isLoading)
const Center(child: CircularProgressIndicator()),
if (_hasError)
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
backgroundColor: Colors.white,
body: RefreshIndicator(
onRefresh: _loadEvents,
color: accentColor,
child: _events.isEmpty
? ListView(
children: [
const Text(
"Failed to load Google Calendar.",
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () => _controller.reload(),
child: const Text("Retry"),
SizedBox(height: MediaQuery.of(context).size.height * 0.3),
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.event_available, size: 64, color: Colors.grey[400]),
const SizedBox(height: 16),
const Text(
'No upcoming events',
style: TextStyle(fontSize: 16, color: Colors.grey),
),
const SizedBox(height: 8),
Text(
'Pull down to refresh',
style: TextStyle(fontSize: 14, color: Colors.grey[400]),
),
],
),
),
],
)
: ListView.builder(
itemCount: _events.length,
padding: const EdgeInsets.all(8),
itemBuilder: (context, index) {
final event = _events[index];
return _buildEventCard(event, accentColor);
},
),
),
],
),
);
}
Widget _buildEventCard(CalendarEvent event, Color accentColor) {
final startTime = event.startTime;
final isAllDay = event.isAllDay;
String timeText;
if (isAllDay) {
timeText = 'All day';
} else if (startTime != null) {
timeText = DateFormat('HH:mm').format(startTime.toLocal());
} else {
timeText = '';
}
// 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 = 'Today';
isToday = true;
} else if (difference == 1) {
dateText = 'Tomorrow';
} else {
dateText = DateFormat('EEEE, MMMM d').format(startTime);
}
}
return Container(
margin: const EdgeInsets.symmetric(vertical: 6, horizontal: 16),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.black.withValues(alpha: 0.05),
blurRadius: 4,
offset: const Offset(0, 2),
),
],
),
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: () => _showEventDetails(event, accentColor),
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
? accentColor.withValues(alpha: 0.15)
: Colors.grey[50],
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
),
),
child: Row(
children: [
Container(
width: 4,
height: 16,
decoration: BoxDecoration(
color: accentColor,
borderRadius: BorderRadius.circular(2),
),
),
const SizedBox(width: 8),
Text(
dateText,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: isToday ? accentColor : Colors.black87,
),
),
if (isToday) ...[
const SizedBox(width: 8),
Container(
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
decoration: BoxDecoration(
color: accentColor,
borderRadius: BorderRadius.circular(8),
),
child: const Text(
'NOW',
style: TextStyle(
fontSize: 10,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
],
],
),
),
// Time and title
Padding(
padding: const EdgeInsets.all(16),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
// Time indicator with accent color
Container(
width: 60,
height: 60,
decoration: BoxDecoration(
color: accentColor.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(12),
),
child: Center(
child: Text(
timeText,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: isAllDay ? 12 : 14,
fontWeight: FontWeight.bold,
color: accentColor,
),
),
),
),
const SizedBox(width: 16),
// Title
Expanded(
child: Text(
event.summary ?? 'Untitled Event',
style: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.w600,
color: Colors.black87,
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
),
const SizedBox(width: 8),
// Arrow icon
Icon(
Icons.arrow_forward_ios,
size: 16,
color: Colors.grey[400],
),
],
),
),
],
),
),
),
);
}
void _showEventDetails(CalendarEvent event, Color accentColor) {
final startTime = event.startTime;
final endTime = event.endTime;
final isAllDay = event.isAllDay;
showDialog(
context: context,
builder: (context) => Dialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Container(
constraints: const BoxConstraints(maxWidth: 400),
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Container(
width: 4,
height: 24,
decoration: BoxDecoration(
color: accentColor,
borderRadius: BorderRadius.circular(2),
),
),
const SizedBox(width: 12),
Expanded(
child: Text(
event.summary ?? 'Event Details',
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.black87,
),
),
),
],
),
const SizedBox(height: 24),
if (startTime != null) ...[
_buildDetailRow(
icon: Icons.calendar_today,
label: 'Date',
value: DateFormat('EEEE, MMMM d, y').format(startTime),
accentColor: accentColor,
),
const SizedBox(height: 16),
],
if (!isAllDay && startTime != null && endTime != null) ...[
_buildDetailRow(
icon: Icons.access_time,
label: 'Time',
value:
'${DateFormat('HH:mm').format(startTime.toLocal())} - ${DateFormat('HH:mm').format(endTime.toLocal())}',
accentColor: accentColor,
),
const SizedBox(height: 16),
],
if (isAllDay) ...[
_buildDetailRow(
icon: Icons.access_time,
label: 'Duration',
value: 'All day event',
accentColor: accentColor,
),
const SizedBox(height: 16),
],
if (event.location != null && event.location!.isNotEmpty) ...[
_buildDetailRow(
icon: Icons.location_on,
label: 'Location',
value: event.location!,
accentColor: accentColor,
),
const SizedBox(height: 16),
],
if (event.description != null && event.description!.isNotEmpty) ...[
_buildDetailRow(
icon: Icons.notes,
label: 'Notes',
value: event.description!,
accentColor: accentColor,
),
const SizedBox(height: 16),
],
const SizedBox(height: 8),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () => Navigator.pop(context),
style: ElevatedButton.styleFrom(
backgroundColor: accentColor,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: const Text(
'Close',
style: TextStyle(fontWeight: FontWeight.w600),
),
),
),
],
),
),
),
);
}
Widget _buildDetailRow({
required IconData icon,
required String label,
required String value,
required Color accentColor,
}) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: accentColor.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: Icon(icon, size: 20, color: accentColor),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: Colors.grey[600],
),
),
const SizedBox(height: 4),
Text(
value,
style: const TextStyle(
fontSize: 14,
color: Colors.black87,
),
),
],
),
),
],
);
}
}
+154
View File
@@ -0,0 +1,154 @@
import 'package:http/http.dart' as http;
class CalendarEvent {
final String? summary;
final String? description;
final String? location;
final DateTime? startTime;
final DateTime? endTime;
final bool isAllDay;
CalendarEvent({
this.summary,
this.description,
this.location,
this.startTime,
this.endTime,
this.isAllDay = false,
});
}
class GoogleCalendarService {
static const String calendarId =
'b9c267252d5be63d5cdc880be499885a88421eefd0e34e1392890daba0d8471e@group.calendar.google.com';
final http.Client _httpClient = http.Client();
Future<List<CalendarEvent>> getEvents({
DateTime? startDate,
DateTime? endDate,
}) async {
try {
// Use the public iCal feed URL
final url = Uri.parse(
'https://calendar.google.com/calendar/ical/$calendarId/public/basic.ics');
final response = await _httpClient.get(url);
if (response.statusCode == 200) {
return _parseICalendar(response.body);
} else {
throw Exception('Failed to load calendar: ${response.statusCode}');
}
} catch (e) {
throw Exception('Error fetching events: $e');
}
}
List<CalendarEvent> _parseICalendar(String icalData) {
final events = <CalendarEvent>[];
final lines = icalData.split('\n');
String? summary;
String? description;
String? location;
DateTime? startTime;
DateTime? endTime;
bool isAllDay = false;
for (var i = 0; i < lines.length; i++) {
var line = lines[i].trim();
// Handle line continuations
while (i + 1 < lines.length && lines[i + 1].startsWith(' ')) {
i++;
line += lines[i].trim();
}
if (line.startsWith('BEGIN:VEVENT')) {
summary = null;
description = null;
location = null;
startTime = null;
endTime = null;
isAllDay = false;
} else if (line.startsWith('SUMMARY:')) {
summary = _unescapeText(line.substring(8));
} else if (line.startsWith('DESCRIPTION:')) {
description = _unescapeText(line.substring(12));
} else if (line.startsWith('LOCATION:')) {
location = _unescapeText(line.substring(9));
} else if (line.startsWith('DTSTART')) {
final dateStr = line.split(':')[1];
if (line.contains('VALUE=DATE')) {
isAllDay = true;
startTime = _parseDate(dateStr);
} else {
startTime = _parseDateTime(dateStr);
}
} else if (line.startsWith('DTEND')) {
final dateStr = line.split(':')[1];
if (line.contains('VALUE=DATE')) {
endTime = _parseDate(dateStr);
} else {
endTime = _parseDateTime(dateStr);
}
} else if (line.startsWith('END:VEVENT')) {
events.add(CalendarEvent(
summary: summary,
description: description,
location: location,
startTime: startTime,
endTime: endTime,
isAllDay: isAllDay,
));
}
}
return events;
}
String _unescapeText(String text) {
return text
.replaceAll('\\n', '\n')
.replaceAll('\\,', ',')
.replaceAll('\\;', ';')
.replaceAll('\\\\', '\\');
}
DateTime? _parseDate(String dateStr) {
try {
// Format: YYYYMMDD
final year = int.parse(dateStr.substring(0, 4));
final month = int.parse(dateStr.substring(4, 6));
final day = int.parse(dateStr.substring(6, 8));
return DateTime(year, month, day);
} catch (e) {
return null;
}
}
DateTime? _parseDateTime(String dateStr) {
try {
// Format: YYYYMMDDTHHMMSSZ or YYYYMMDDTHHMMSS
final year = int.parse(dateStr.substring(0, 4));
final month = int.parse(dateStr.substring(4, 6));
final day = int.parse(dateStr.substring(6, 8));
final hour = int.parse(dateStr.substring(9, 11));
final minute = int.parse(dateStr.substring(11, 13));
final second = int.parse(dateStr.substring(13, 15));
if (dateStr.endsWith('Z')) {
return DateTime.utc(year, month, day, hour, minute, second);
} else {
return DateTime(year, month, day, hour, minute, second);
}
} catch (e) {
return null;
}
}
void dispose() {
_httpClient.close();
}
}