219 lines
6.0 KiB
Dart
219 lines
6.0 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class PenggunaScreen extends StatefulWidget {
|
|
const PenggunaScreen({
|
|
Key? key,
|
|
required this.token,
|
|
}) : super(key: key);
|
|
|
|
final String token;
|
|
|
|
@override
|
|
_PenggunaScreenState createState() => _PenggunaScreenState();
|
|
}
|
|
|
|
class _PenggunaScreenState extends State<PenggunaScreen> {
|
|
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
|
bool isLoading = true;
|
|
Map<String, dynamic>? roomData;
|
|
Map<String, dynamic>? monitoringData;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
fetchData();
|
|
}
|
|
|
|
Future<void> fetchData() async {
|
|
setState(() {
|
|
isLoading = true;
|
|
});
|
|
|
|
try {
|
|
// Fetch data from 'room' collection
|
|
QuerySnapshot roomSnapshot = await _firestore
|
|
.collection('room')
|
|
.where('token', isEqualTo: widget.token)
|
|
.get();
|
|
|
|
if (roomSnapshot.docs.isNotEmpty) {
|
|
roomData = roomSnapshot.docs[0].data() as Map<String, dynamic>?;
|
|
} else {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Data kamar tidak ditemukan'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
}
|
|
|
|
// Fetch data from 'monitoring' collection
|
|
DocumentSnapshot powerSnapshot = await _firestore
|
|
.collection('monitoring')
|
|
.doc('power')
|
|
.get();
|
|
DocumentSnapshot currentSnapshot = await _firestore
|
|
.collection('monitoring')
|
|
.doc('current')
|
|
.get();
|
|
DocumentSnapshot energySnapshot = await _firestore
|
|
.collection('monitoring')
|
|
.doc('energy')
|
|
.get();
|
|
DocumentSnapshot voltageSnapshot = await _firestore
|
|
.collection('monitoring')
|
|
.doc('voltage')
|
|
.get();
|
|
|
|
if (powerSnapshot.exists &&
|
|
currentSnapshot.exists &&
|
|
energySnapshot.exists &&
|
|
voltageSnapshot.exists) {
|
|
monitoringData = {
|
|
'power': powerSnapshot.data(),
|
|
'current': currentSnapshot.data(),
|
|
'energy': energySnapshot.data(),
|
|
'voltage': voltageSnapshot.data(),
|
|
};
|
|
} else {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Data monitoring tidak ditemukan'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Gagal mengambil data: $e'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
} finally {
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
bool isDeadlineValid = roomData != null && roomData!['deadline'] != null
|
|
? roomData!['deadline'].toDate().isAfter(DateTime.now())
|
|
: false;
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Column(
|
|
children: [
|
|
Text(
|
|
'Monitoring',
|
|
style:
|
|
TextStyle(color: Colors.white, fontWeight: FontWeight.w600),
|
|
),
|
|
Text(
|
|
'Pengguna Kost',
|
|
style: TextStyle(
|
|
fontSize: 14.0,
|
|
color: Colors.white,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
backgroundColor: isDeadlineValid ? Colors.grey : Colors.red,
|
|
centerTitle: true,
|
|
),
|
|
body: isLoading
|
|
? Center(child: CircularProgressIndicator())
|
|
: Container(
|
|
color: Colors.blue,
|
|
child: Column(
|
|
children: [
|
|
SizedBox(height: 20),
|
|
Text(
|
|
roomData != null ? '${roomData!['name']}' : '●●●●●',
|
|
style: TextStyle(
|
|
fontSize: 20.0,
|
|
color: Colors.black,
|
|
),
|
|
),
|
|
SizedBox(height: 20),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
children: <Widget>[
|
|
_buildMonitoringTile(
|
|
monitoringData != null
|
|
? monitoringData!['voltage']['voltage'].toString()
|
|
: '',
|
|
'V',
|
|
'Voltage'),
|
|
_buildMonitoringTile(
|
|
monitoringData != null
|
|
? monitoringData!['current']['current'].toString()
|
|
: '',
|
|
'A',
|
|
'Current'),
|
|
],
|
|
),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
children: <Widget>[
|
|
_buildMonitoringTile(
|
|
monitoringData != null
|
|
? monitoringData!['power']['power'].toString()
|
|
: '',
|
|
'W',
|
|
'Power'),
|
|
_buildMonitoringTile(
|
|
monitoringData != null
|
|
? monitoringData!['energy']['energy'].toString()
|
|
: '',
|
|
'kWh',
|
|
'Energy'),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildMonitoringTile(String value, String unit, String label) {
|
|
return Column(
|
|
children: <Widget>[
|
|
Container(
|
|
color: Colors.grey,
|
|
width: 150,
|
|
height: 100,
|
|
padding: EdgeInsets.all(16.0),
|
|
child: Center(
|
|
child: Column(
|
|
children: [
|
|
Text(
|
|
value,
|
|
style: TextStyle(
|
|
fontSize: 24,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
Text(
|
|
unit,
|
|
style: TextStyle(
|
|
fontSize: 20,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
SizedBox(height: 7),
|
|
Text(label),
|
|
SizedBox(height: 24),
|
|
],
|
|
);
|
|
}
|
|
}
|