319 lines
11 KiB
Dart
319 lines
11 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../../../core/app_theme.dart';
|
|
import '../../../features/auth/auth_notifier.dart';
|
|
import '../../../features/auth/auth_state.dart';
|
|
import '../providers/control_notifier.dart';
|
|
import '../providers/monitoring_provider.dart';
|
|
import '../widgets/device_status_card.dart';
|
|
import '../widgets/device_control_section.dart';
|
|
import '../widgets/greeting_section.dart';
|
|
import '../widgets/schedule_section.dart';
|
|
|
|
import '../widgets/add_schedule_sheet.dart';
|
|
import '../widgets/operation_mode_card.dart';
|
|
|
|
class ControlPage extends ConsumerWidget {
|
|
const ControlPage({super.key});
|
|
|
|
void _onAddSchedule(BuildContext context, WidgetRef ref) {
|
|
showModalBottomSheet(
|
|
context: context,
|
|
isScrollControlled: true,
|
|
backgroundColor: Colors.transparent,
|
|
builder: (ctx) => AddScheduleSheet(
|
|
onSave: (time, days, durationS) {
|
|
ref.read(controlProvider.notifier).addSchedule(time, days, durationS);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showManualDurationDialog(BuildContext context, WidgetRef ref) {
|
|
int selectedDuration = 30; // default
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (ctx) {
|
|
return StatefulBuilder(
|
|
builder: (context, setState) {
|
|
return AlertDialog(
|
|
title: const Text('Lama Penyiraman'),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text('Pilih durasi pompa menyala secara manual:'),
|
|
const SizedBox(height: 16),
|
|
Wrap(
|
|
spacing: 8,
|
|
runSpacing: 8,
|
|
children: [
|
|
{'label': '15s', 'val': 15},
|
|
{'label': '30s', 'val': 30},
|
|
{'label': '1m', 'val': 60},
|
|
{'label': '2m', 'val': 120},
|
|
{'label': '5m', 'val': 300},
|
|
].map((opt) {
|
|
final val = opt['val'] as int;
|
|
final isSelected = selectedDuration == val;
|
|
return ChoiceChip(
|
|
label: Text(opt['label'] as String),
|
|
selected: isSelected,
|
|
onSelected: (bool selected) {
|
|
if (selected) {
|
|
setState(() => selectedDuration = val);
|
|
}
|
|
},
|
|
selectedColor: AppTheme.primary,
|
|
labelStyle: TextStyle(
|
|
color: isSelected ? Colors.white : AppTheme.textSecondary,
|
|
fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
|
|
),
|
|
backgroundColor: Colors.grey.shade200,
|
|
);
|
|
}).toList(),
|
|
),
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(ctx),
|
|
child: const Text('Batal'),
|
|
),
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(backgroundColor: AppTheme.primary),
|
|
onPressed: () {
|
|
Navigator.pop(ctx);
|
|
ref.read(controlProvider.notifier).togglePump(selectedDuration);
|
|
},
|
|
child: const Text('Mulai', style: TextStyle(color: Colors.white)),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
void _showStopConfirmationDialog(BuildContext context, WidgetRef ref) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Text('Hentikan Penyiraman?'),
|
|
content: const Text('Apakah Anda yakin ingin mematikan pompa saat ini?'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(ctx),
|
|
child: const Text('Batal'),
|
|
),
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(backgroundColor: AppTheme.statusDanger),
|
|
onPressed: () {
|
|
Navigator.pop(ctx);
|
|
ref.read(controlProvider.notifier).togglePump();
|
|
},
|
|
child: const Text('Matikan', style: TextStyle(color: Colors.white)),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _confirmDelete(BuildContext context, WidgetRef ref, String id) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Text('Hapus Jadwal?'),
|
|
content: const Text('Jadwal ini akan dihapus secara permanen.'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(ctx).pop(),
|
|
child: const Text('Batal'),
|
|
),
|
|
TextButton(
|
|
style: TextButton.styleFrom(foregroundColor: AppTheme.statusDanger),
|
|
onPressed: () {
|
|
Navigator.of(ctx).pop();
|
|
ref.read(controlProvider.notifier).removeSchedule(id);
|
|
},
|
|
child: const Text('Hapus'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
String _getUserName(WidgetRef ref) {
|
|
final authState = ref.watch(authProvider);
|
|
if (authState is AuthSuccess) return authState.user.name;
|
|
return 'Pengguna';
|
|
}
|
|
|
|
String _getDeviceCode(WidgetRef ref) {
|
|
final authState = ref.watch(authProvider);
|
|
if (authState is AuthSuccess) return authState.user.deviceId ?? '-';
|
|
return '-';
|
|
}
|
|
|
|
bool _checkIsOnline(WidgetRef ref) {
|
|
final monitoringState = ref.watch(monitoringProvider);
|
|
final latest = monitoringState.latest;
|
|
if (latest == null) return false;
|
|
|
|
// Jika data terakhir masuk dalam 5 menit, dianggap online
|
|
final diff = DateTime.now().difference(latest.timestamp);
|
|
return diff.inMinutes < 5;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
// ── Listener untuk Snackbar Error & Success ──
|
|
ref.listen(controlProvider, (prev, next) {
|
|
if (next.errorMessage != null) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(next.errorMessage!),
|
|
backgroundColor: AppTheme.statusDanger,
|
|
),
|
|
);
|
|
ref.read(controlProvider.notifier).clearMessage();
|
|
}
|
|
if (next.successMessage != null) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(next.successMessage!),
|
|
backgroundColor: AppTheme.statusNormal,
|
|
),
|
|
);
|
|
ref.read(controlProvider.notifier).clearMessage();
|
|
}
|
|
});
|
|
|
|
final control = ref.watch(controlProvider);
|
|
final notifier = ref.read(controlProvider.notifier);
|
|
|
|
final userName = _getUserName(ref);
|
|
final deviceCode = _getDeviceCode(ref);
|
|
final isOnline = _checkIsOnline(ref);
|
|
final authState = ref.watch(authProvider);
|
|
final hasDevice = authState is AuthSuccess && authState.user.deviceId != null;
|
|
|
|
return Scaffold(
|
|
backgroundColor: AppTheme.bgPage,
|
|
body: SafeArea(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
GreetingSection(
|
|
userName: userName,
|
|
subtitle: hasDevice ? 'Selamat Datang di Kontrol Panel' : 'Kontrol Panel Nonaktif',
|
|
),
|
|
const SizedBox(height: 20),
|
|
|
|
if (!hasDevice) ...[
|
|
const SizedBox(height: 40),
|
|
Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 36),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(24),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withOpacity(0.04),
|
|
blurRadius: 16,
|
|
offset: const Offset(0, 4),
|
|
),
|
|
],
|
|
),
|
|
child: const Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(
|
|
Icons.sensors_off_outlined,
|
|
size: 64,
|
|
color: Colors.grey,
|
|
),
|
|
SizedBox(height: 24),
|
|
Text(
|
|
'Kontrol Perangkat Dinonaktifkan',
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
color: AppTheme.textPrimary,
|
|
),
|
|
),
|
|
SizedBox(height: 12),
|
|
Text(
|
|
'Silakan hubungkan perangkat kumbung jamur Anda terlebih dahulu pada halaman Monitoring (tab pertama) dengan menekan tombol + dan memasukkan Claim Code Anda.',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
fontSize: 13,
|
|
color: AppTheme.textSecondary,
|
|
height: 1.5,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
] else ...[
|
|
DeviceStatusCard(
|
|
isOnline: isOnline,
|
|
deviceCode: deviceCode,
|
|
lastSeen: ref.watch(monitoringProvider).latest?.timestamp,
|
|
),
|
|
const SizedBox(height: 20),
|
|
|
|
const OperationModeCard(),
|
|
const SizedBox(height: 16),
|
|
|
|
DeviceControlSection(
|
|
isPumpOn: control.isPumpOn,
|
|
isOnline: isOnline,
|
|
suhuLimit: control.suhuLimit,
|
|
kelembapanLimit: control.kelembapanLimit,
|
|
activePumpDuration: control.activePumpDuration,
|
|
pumpTurnedOnAt: control.pumpTurnedOnAt,
|
|
onPumpToggle: () {
|
|
if (control.isPumpOn) {
|
|
_showStopConfirmationDialog(context, ref);
|
|
} else {
|
|
_showManualDurationDialog(context, ref);
|
|
}
|
|
},
|
|
onSaveThreshold: (suhu, kelembapan) =>
|
|
notifier.saveThreshold(suhu, kelembapan),
|
|
),
|
|
const SizedBox(height: 20),
|
|
|
|
// Jika sedang loading initial data, tampilkan loading utama
|
|
if (control.isLoading && control.schedules.isEmpty)
|
|
const Center(child: CircularProgressIndicator())
|
|
else ...[
|
|
// Jika loading tapi schedules sudah ada (misal proses tambah jadwal),
|
|
// tampilkan progress bar tipis di atas section
|
|
if (control.isLoading && control.schedules.isNotEmpty)
|
|
const LinearProgressIndicator(),
|
|
|
|
ScheduleSection(
|
|
schedules: control.schedules,
|
|
onAddSchedule: () => _onAddSchedule(context, ref),
|
|
onToggleSchedule: (id, _) => notifier.toggleSchedule(id),
|
|
onRemoveSchedule: (id) => _confirmDelete(context, ref, id),
|
|
),
|
|
],
|
|
const SizedBox(height: 20),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|