TKK_E32232282/Source Code Mobile/monitoring_history_page.dart

258 lines
10 KiB
Dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/history_provider.dart';
import '../core/app_color.dart';
import 'control_history_page.dart';
class MonitoringHistoryPage extends StatefulWidget {
const MonitoringHistoryPage({super.key});
@override
State<MonitoringHistoryPage> createState() => _MonitoringHistoryPageState();
}
class _MonitoringHistoryPageState extends State<MonitoringHistoryPage> {
@override
void initState() {
super.initState();
context.read<HistoryProvider>().startMonitoringListener();
}
@override
Widget build(BuildContext context) {
final provider = context.watch<HistoryProvider>();
final history = provider.monitoringHistory;
return Scaffold(
backgroundColor: AppColor.background,
appBar: AppBar(
title: const Text('Histori Monitoring'),
actions: [
IconButton(
icon: const Icon(Icons.delete_outline, color: Colors.redAccent),
tooltip: 'Hapus Histori',
onPressed: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Hapus Histori'),
content: const Text('Apakah Anda yakin ingin menghapus semua data histori monitoring? Tindakan ini tidak dapat dibatalkan.'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Batal'),
),
TextButton(
onPressed: () async {
Navigator.pop(context);
try {
await context.read<HistoryProvider>().clearMonitoringHistory();
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Histori monitoring berhasil dihapus')),
);
}
} catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Gagal menghapus histori: $e')),
);
}
}
},
child: const Text('Hapus', style: TextStyle(color: Colors.red)),
),
],
),
);
},
),
],
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Table Header Title + Button to Control History
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'Riwayat (setiap 5 menit)',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.white70,
),
),
TextButton.icon(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (_) => const ControlHistoryPage()),
);
},
icon: const Icon(Icons.history, size: 16, color: Colors.blueAccent),
label: const Text(
'Histori Kontrol',
style: TextStyle(color: Colors.blueAccent, fontSize: 13, fontWeight: FontWeight.bold),
),
style: TextButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
),
),
],
),
const SizedBox(height: 12),
// Main Table Card
Expanded(
child: Card(
color: AppColor.cardBg,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
children: [
// Column Headers
Row(
children: const [
Expanded(
flex: 4,
child: Text(
'Waktu',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.grey,
fontSize: 12,
),
),
),
Expanded(
flex: 2,
child: Text(
'PM2.5',
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.grey,
fontSize: 12,
),
),
),
Expanded(
flex: 2,
child: Text(
'CO₂',
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.grey,
fontSize: 12,
),
),
),
Expanded(
flex: 3,
child: Text(
'Suhu / Lembap',
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.grey,
fontSize: 12,
),
),
),
],
),
const SizedBox(height: 10),
const Divider(color: Colors.white24, height: 1),
const SizedBox(height: 10),
// Rows List
Expanded(
child: history.isEmpty
? const Center(
child: Text(
'Belum ada data histori.',
style: TextStyle(color: Colors.grey),
),
)
: ListView.separated(
itemCount: history.length,
separatorBuilder: (context, i) => const Divider(
color: Colors.white12,
height: 16,
),
itemBuilder: (context, index) {
final item = history[index];
// Format timestamp for display (shorten year/seconds if needed)
String displayTime = item.timestamp;
if (displayTime.length > 16) {
displayTime = displayTime.substring(0, 16);
}
return Row(
children: [
Expanded(
flex: 4,
child: Text(
displayTime,
style: const TextStyle(
color: Colors.white,
fontSize: 11,
),
),
),
Expanded(
flex: 2,
child: Text(
'${item.pm25}',
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.white,
fontSize: 11,
),
),
),
Expanded(
flex: 2,
child: Text(
'${item.co2.round()}',
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.white,
fontSize: 11,
),
),
),
Expanded(
flex: 3,
child: Text(
'${item.temperature.round()}°C / ${item.humidity.round()}%',
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.white,
fontSize: 11,
),
),
),
],
);
},
),
),
],
),
),
),
),
],
),
),
);
}
}