pembaruan firebase service untuk manual mode

This commit is contained in:
aarsyah0 2025-06-19 03:03:47 +07:00
parent b4ac6fcaa8
commit e0a3d3cf25
2 changed files with 121 additions and 61 deletions

View File

@ -18,6 +18,7 @@ class _DashboardPageState extends State<DashboardPage>
double humidity = 0.0; double humidity = 0.0;
bool fanOn = false; bool fanOn = false;
bool pumpOn = false; bool pumpOn = false;
bool isManual = false;
int _selectedIndex = 0; int _selectedIndex = 0;
late AnimationController _controller; late AnimationController _controller;
late Animation<double> _fadeAnim; late Animation<double> _fadeAnim;
@ -54,6 +55,8 @@ class _DashboardPageState extends State<DashboardPage>
setState(() { setState(() {
fanOn = status; fanOn = status;
}); });
// Sinkronkan ke control/kipas
_firebaseService.setControlFanFromStatus(status);
} }
}); });
@ -63,6 +66,17 @@ class _DashboardPageState extends State<DashboardPage>
setState(() { setState(() {
pumpOn = status; pumpOn = status;
}); });
// Sinkronkan ke control/pompa
_firebaseService.setControlPumpFromStatus(status);
}
});
// Listen to manual mode
_firebaseService.getManualMode().listen((manual) {
if (mounted) {
setState(() {
isManual = manual;
});
} }
}); });
@ -128,74 +142,78 @@ class _DashboardPageState extends State<DashboardPage>
required String label, required String label,
required IconData icon, required IconData icon,
required Color color, required Color color,
required bool enabled,
}) { }) {
return Material( return Material(
color: Colors.transparent, color: Colors.transparent,
child: InkWell( child: InkWell(
onTap: onTap, onTap: enabled ? onTap : null,
borderRadius: BorderRadius.circular(5), borderRadius: BorderRadius.circular(5),
child: Container( child: Opacity(
decoration: BoxDecoration( opacity: enabled ? 1.0 : 0.5,
color: Colors.white, child: Container(
borderRadius: BorderRadius.circular(5), decoration: BoxDecoration(
boxShadow: [ color: Colors.white,
BoxShadow( borderRadius: BorderRadius.circular(5),
color: Colors.black.withOpacity(0.05), boxShadow: [
blurRadius: 10, BoxShadow(
offset: Offset(0, 3), color: Colors.black.withOpacity(0.05),
), blurRadius: 10,
], offset: Offset(0, 3),
),
child: Padding(
padding: EdgeInsets.all(12),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: EdgeInsets.all(12),
decoration: BoxDecoration(
color: color.withOpacity(0.1),
borderRadius: BorderRadius.circular(5),
),
child: Icon(icon, color: color, size: 28),
),
SizedBox(height: 8),
Text(
label,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: color,
),
),
SizedBox(height: 8),
AnimatedContainer(
duration: Duration(milliseconds: 200),
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 6),
decoration: BoxDecoration(
color: isOn ? color : Colors.grey[200],
borderRadius: BorderRadius.circular(5),
boxShadow: [
BoxShadow(
color: (isOn ? color : Colors.grey[300]!).withOpacity(
0.3,
),
blurRadius: 4,
offset: Offset(0, 2),
),
],
),
child: Text(
isOn ? 'ON' : 'OFF',
style: TextStyle(
color: isOn ? Colors.white : Colors.grey[600],
fontSize: 12,
fontWeight: FontWeight.w600,
),
),
), ),
], ],
), ),
child: Padding(
padding: EdgeInsets.all(12),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: EdgeInsets.all(12),
decoration: BoxDecoration(
color: color.withOpacity(0.1),
borderRadius: BorderRadius.circular(5),
),
child: Icon(icon, color: color, size: 28),
),
SizedBox(height: 8),
Text(
label,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: color,
),
),
SizedBox(height: 8),
AnimatedContainer(
duration: Duration(milliseconds: 200),
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 6),
decoration: BoxDecoration(
color: isOn ? color : Colors.grey[200],
borderRadius: BorderRadius.circular(5),
boxShadow: [
BoxShadow(
color: (isOn ? color : Colors.grey[300]!).withOpacity(
0.3,
),
blurRadius: 4,
offset: Offset(0, 2),
),
],
),
child: Text(
isOn ? 'ON' : 'OFF',
style: TextStyle(
color: isOn ? Colors.white : Colors.grey[600],
fontSize: 12,
fontWeight: FontWeight.w600,
),
),
),
],
),
),
), ),
), ),
), ),
@ -434,6 +452,26 @@ class _DashboardPageState extends State<DashboardPage>
), ),
], ],
), ),
// Tambahkan switch manual mode di sini
Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Mode Manual',
style: TextStyle(fontWeight: FontWeight.bold),
),
Switch(
value: isManual,
onChanged: (val) {
_firebaseService.setManualMode(val);
},
activeColor: greenPrimary,
),
],
),
),
SizedBox(height: gridPadding), SizedBox(height: gridPadding),
// Grid 2x2 // Grid 2x2
Expanded( Expanded(
@ -444,7 +482,7 @@ class _DashboardPageState extends State<DashboardPage>
mainAxisSpacing: gridSpacing, mainAxisSpacing: gridSpacing,
crossAxisSpacing: gridSpacing, crossAxisSpacing: gridSpacing,
childAspectRatio: 1, childAspectRatio: 1,
physics: const NeverScrollableScrollPhysics(), physics: const AlwaysScrollableScrollPhysics(),
children: [ children: [
// Temperature // Temperature
_buildSensorCard( _buildSensorCard(
@ -469,6 +507,7 @@ class _DashboardPageState extends State<DashboardPage>
label: 'Kipas', label: 'Kipas',
icon: Icons.ac_unit_rounded, icon: Icons.ac_unit_rounded,
color: greenPrimary, color: greenPrimary,
enabled: isManual,
), ),
// Pump Control // Pump Control
_buildControlButton( _buildControlButton(
@ -477,6 +516,7 @@ class _DashboardPageState extends State<DashboardPage>
label: 'Pompa', label: 'Pompa',
icon: Icons.water_rounded, icon: Icons.water_rounded,
color: greenPrimary, color: greenPrimary,
enabled: isManual,
), ),
], ],
), ),

View File

@ -80,4 +80,24 @@ class FirebaseService {
Future<void> deleteHistory(String id) async { Future<void> deleteHistory(String id) async {
await _database.child('riwayat').child(id).remove(); await _database.child('riwayat').child(id).remove();
} }
// Stream untuk mendapatkan mode manual
Stream<bool> getManualMode() {
return _database.child('control/manual').onValue.map((event) {
final data = event.snapshot.value as bool?;
return data ?? false;
});
}
Future<void> setManualMode(bool value) async {
await _database.child('control/manual').set(value);
}
Future<void> setControlFanFromStatus(bool status) async {
await _database.child('control/kipas').set(status ? "ON" : "OFF");
}
Future<void> setControlPumpFromStatus(bool status) async {
await _database.child('control/pompa').set(status ? "ON" : "OFF");
}
} }