Add todos admin and 30s refresh

This commit is contained in:
kihong.kim
2026-01-24 23:45:22 +09:00
parent 2944b6d9b5
commit 6fb00fec5d
5 changed files with 617 additions and 276 deletions

View File

@@ -1,12 +1,66 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:intl/intl.dart';
import '../models/schedule_item.dart';
import '../services/schedule_service.dart';
class ScheduleListWidget extends StatelessWidget {
class ScheduleListWidget extends StatefulWidget {
const ScheduleListWidget({super.key});
@override
State<ScheduleListWidget> createState() => _ScheduleListWidgetState();
}
class _ScheduleListWidgetState extends State<ScheduleListWidget> {
Timer? _timer;
List<ScheduleItem> _schedules = [];
bool _isLoading = true;
String? _error;
@override
void initState() {
super.initState();
_fetchSchedules();
_startAutoRefresh();
}
void _startAutoRefresh() {
_timer = Timer.periodic(const Duration(seconds: 30), (timer) {
_fetchSchedules();
});
}
Future<void> _fetchSchedules() async {
try {
final data = await Provider.of<ScheduleService>(
context,
listen: false,
).fetchWeeklySchedules();
if (mounted) {
setState(() {
_schedules = data;
_isLoading = false;
_error = null;
});
}
} catch (e) {
if (mounted) {
setState(() {
_error = 'Failed to load schedules';
_isLoading = false;
});
}
}
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
@@ -21,109 +75,102 @@ class ScheduleListWidget extends StatelessWidget {
Text(
'Weekly Schedule',
style: Theme.of(context).textTheme.titleLarge?.copyWith(
color: Colors.white,
fontWeight: FontWeight.bold,
),
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 12),
Expanded(
child: FutureBuilder<List<ScheduleItem>>(
future: Provider.of<ScheduleService>(
context,
listen: false,
).fetchWeeklySchedules(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
if (snapshot.hasError) {
return const Center(
child: Text(
'Failed to load schedules',
style: TextStyle(color: Colors.white54),
),
);
}
if (!snapshot.hasData || snapshot.data!.isEmpty) {
return const Center(
child: Text(
'No schedules this week',
style: TextStyle(color: Colors.white54),
),
);
}
final schedules = snapshot.data!;
// Sort by date
schedules.sort((a, b) => a.startDate.compareTo(b.startDate));
return ListView.separated(
itemCount: schedules.length,
separatorBuilder: (context, index) =>
const Divider(color: Colors.white10),
itemBuilder: (context, index) {
final item = schedules[index];
final dateStr = DateFormat(
'E, MMM d',
).format(item.startDate);
final timeStr = item.isAllDay
? 'All Day'
: DateFormat('HH:mm').format(item.startDate);
return ListTile(
contentPadding: EdgeInsets.zero,
leading: Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 8,
),
decoration: BoxDecoration(
color: Colors.white10,
borderRadius: BorderRadius.circular(8),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Text(
DateFormat('d').format(item.startDate),
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
Text(
DateFormat('E').format(item.startDate),
style: const TextStyle(
color: Colors.white70,
fontSize: 12,
),
),
],
),
),
title: Text(
item.title,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.w500,
),
),
subtitle: Text(
timeStr,
style: const TextStyle(color: Colors.white54),
),
);
},
);
},
),
child: _buildContent(),
),
],
),
);
}
Widget _buildContent() {
if (_isLoading && _schedules.isEmpty) {
return const Center(child: CircularProgressIndicator());
}
if (_error != null && _schedules.isEmpty) {
return Center(
child: Text(
_error!,
style: const TextStyle(color: Colors.white54),
),
);
}
if (_schedules.isEmpty) {
return const Center(
child: Text(
'No schedules this week',
style: TextStyle(color: Colors.white54),
),
);
}
// Sort by date
final sortedSchedules = List<ScheduleItem>.from(_schedules);
sortedSchedules.sort((a, b) => a.startDate.compareTo(b.startDate));
return ListView.separated(
itemCount: sortedSchedules.length,
separatorBuilder: (context, index) =>
const Divider(color: Colors.white10),
itemBuilder: (context, index) {
final item = sortedSchedules[index];
final timeStr = item.isAllDay
? 'All Day'
: DateFormat('HH:mm').format(item.startDate);
return ListTile(
contentPadding: EdgeInsets.zero,
leading: Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 8,
),
decoration: BoxDecoration(
color: Colors.white10,
borderRadius: BorderRadius.circular(8),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Text(
DateFormat('d').format(item.startDate),
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
Text(
DateFormat('E').format(item.startDate),
style: const TextStyle(
color: Colors.white70,
fontSize: 12,
),
),
],
),
),
title: Text(
item.title,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.w500,
),
),
subtitle: Text(
timeStr,
style: const TextStyle(color: Colors.white54),
),
);
},
);
}
}