Commit 844069e5 authored by ayodyabanuka's avatar ayodyabanuka

updated

parent 530725cc
......@@ -35,6 +35,7 @@ class _LoginState extends State<Login> {
Size size = MediaQuery.of(context).size;
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
backgroundColor: backgroundColor,
elevation: 0,
......
......@@ -20,6 +20,7 @@ class _registerState extends State<register> {
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: const Color(0xff024F8E),
appBar: AppBar(
backgroundColor: const Color(0xff024F8E),
......@@ -117,6 +118,9 @@ class _registerState extends State<register> {
),
),
)),
const SizedBox(
width: 20,
),
GestureDetector(
onTap: () {
print(emailController.text);
......
......@@ -160,14 +160,16 @@ class _foodbucketsState extends State<foodbuckets> {
if (!mounted) return;
setState(() {
getResult = qrCode;
if (buckets.contains(qrCode) || qrCode == '-1') {
} else {
buckets.add(qrCode);
saveQRCodes();
}
});
if (mounted) {
setState(() {
getResult = qrCode;
if (buckets.contains(qrCode) || qrCode == '-1') {
} else {
buckets.add(qrCode);
saveQRCodes();
}
});
}
print("QRCode_Result:-");
print(qrCode);
} on PlatformException {
......@@ -178,9 +180,11 @@ class _foodbucketsState extends State<foodbuckets> {
saveQRCodes() {
final user = FirebaseAuth.instance.currentUser;
if (user != null) {
setState(() {
name = user.displayName.toString();
});
if (mounted) {
setState(() {
name = user.displayName.toString();
});
}
}
FirestoreServices.savefoodbuckets(buckets, name);
}
......
import 'dart:async';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:ifarm/AB/Utils/NotificationService.dart';
import 'package:liquid_progress_indicator/liquid_progress_indicator.dart';
import 'package:switcher_button/switcher_button.dart';
......@@ -15,6 +16,7 @@ class SingleFoodBucketData extends StatefulWidget {
String d = '100';
String m = '250';
int s = 0;
String err = "2";
bool svalue = false;
class _SingleFoodBucketDataState extends State<SingleFoodBucketData> {
......@@ -33,17 +35,27 @@ class _SingleFoodBucketDataState extends State<SingleFoodBucketData> {
getData();
servovalueinbool();
});
timer = Timer.periodic(const Duration(seconds: 30), (_) {
if (err == "1") {
NotificationService().showNotification(
1, "Error On Bucket", "Error on bucket: ${widget.bucketId}", 2);
}
});
}
servovalueinbool() {
if (s == 0) {
setState(() {
svalue = false;
});
if (mounted) {
setState(() {
svalue = false;
});
}
} else {
setState(() {
svalue = true;
});
if (mounted) {
setState(() {
svalue = true;
});
}
}
}
......@@ -53,27 +65,44 @@ class _SingleFoodBucketDataState extends State<SingleFoodBucketData> {
.child('Food/' + widget.bucketId + '/Moist/foodMoist')
.onValue
.listen((event) {
setState(() {
m = event.snapshot.value.toString();
});
if (mounted) {
setState(() {
m = event.snapshot.value.toString();
});
}
});
FirebaseDatabase.instance
.ref()
.child('Food/' + widget.bucketId + '/SERVO')
.onValue
.listen((event) {
setState(() {
s = int.parse(event.snapshot.value.toString());
});
if (mounted) {
setState(() {
s = int.parse(event.snapshot.value.toString());
});
}
});
FirebaseDatabase.instance
.ref()
.child('Food/' + widget.bucketId + '/dis')
.onValue
.listen((event) {
setState(() {
d = event.snapshot.value.toString();
});
if (mounted) {
setState(() {
d = event.snapshot.value.toString();
});
}
});
FirebaseDatabase.instance
.ref()
.child('Food/' + widget.bucketId + '/ERROR')
.onValue
.listen((event) {
if (mounted) {
setState(() {
err = event.snapshot.value.toString();
});
}
});
}
......@@ -84,8 +113,12 @@ class _SingleFoodBucketDataState extends State<SingleFoodBucketData> {
leading: IconButton(
icon: const Icon(Icons.arrow_back, color: Colors.white),
onPressed: () {
if (mounted) {
setState(() {
timer.cancel();
});
}
Navigator.pop(context);
timer.cancel();
},
),
backgroundColor: const Color(0xff024F8E),
......@@ -101,21 +134,53 @@ class _SingleFoodBucketDataState extends State<SingleFoodBucketData> {
const SizedBox(
height: 20,
),
const Text(
'Bucket',
style: TextStyle(
color: Color.fromARGB(255, 255, 255, 255),
fontSize: 30,
fontFamily: 'poppins',
fontWeight: FontWeight.bold),
),
Text(
widget.bucketId,
style: const TextStyle(
color: Color.fromARGB(136, 255, 255, 255),
fontSize: 25,
fontFamily: 'poppins',
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Bucket',
style: TextStyle(
color: Color.fromARGB(255, 255, 255, 255),
fontSize: 30,
fontFamily: 'poppins',
fontWeight: FontWeight.bold),
),
Text(
widget.bucketId,
style: const TextStyle(
color: Color.fromARGB(136, 255, 255, 255),
fontSize: 25,
fontFamily: 'poppins',
),
),
],
),
Row(
children: [
const Text(
"Bucket Status",
style: TextStyle(color: Colors.white),
),
const SizedBox(
width: 10,
),
Container(
width: 10,
height: 10,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: err == "2"
? Colors.green
: err == "1"
? Colors.red
: null),
),
],
)
],
),
const SizedBox(
height: 30,
......@@ -138,15 +203,21 @@ class _SingleFoodBucketDataState extends State<SingleFoodBucketData> {
children: [
Text(
d,
style: const TextStyle(
fontSize: 20,
color: Color.fromARGB(255, 0, 0, 0),
style: TextStyle(
fontSize: 40,
color: int.parse(d) > 50
? Colors.white
: const Color.fromARGB(
255, 0, 0, 0),
fontWeight: FontWeight.bold),
),
const Text(
Text(
'%',
style: TextStyle(
color: Color.fromARGB(255, 0, 0, 0),
color: int.parse(d) > 50
? Colors.white
: const Color.fromARGB(
255, 0, 0, 0),
fontWeight: FontWeight.bold),
),
],
......@@ -156,10 +227,11 @@ class _SingleFoodBucketDataState extends State<SingleFoodBucketData> {
value: double.parse(d) / 100,
valueColor: AlwaysStoppedAnimation(
double.parse(d) >= 0.0 && double.parse(d) <= 20.0
? Color.fromARGB(255, 255, 0, 0)
? const Color.fromARGB(255, 255, 0, 0)
: Colors.green,
),
backgroundColor: Color.fromARGB(255, 255, 255, 255),
backgroundColor:
const Color.fromARGB(255, 255, 255, 255),
direction: Axis.vertical,
shapePath: _buildBoatPath(),
),
......@@ -191,7 +263,7 @@ class _SingleFoodBucketDataState extends State<SingleFoodBucketData> {
height: 10,
),
const Text(
'Moisture',
'Food quality',
style: TextStyle(
fontSize: 20,
color: Colors.white70,
......@@ -202,7 +274,7 @@ class _SingleFoodBucketDataState extends State<SingleFoodBucketData> {
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
m,
int.parse(m) > 1 ? "Error" : "Good",
style: const TextStyle(
color: Colors.white,
fontSize: 40,
......@@ -246,12 +318,14 @@ class _SingleFoodBucketDataState extends State<SingleFoodBucketData> {
offColor: Colors.black,
value: svalue,
onChange: (value) {
setState(() {
svalue = value;
if (mounted) {
setState(() {
svalue = value;
updateone(value);
print(value);
});
updateone(value);
print(value);
});
}
},
)
],
......
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cron/cron.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:ifarm/AB/Utils/Colors.dart';
......@@ -17,16 +18,15 @@ class FoodBucketSchedule extends StatefulWidget {
TextEditingController dateController = TextEditingController();
TextEditingController timeController = TextEditingController();
List<String> timeList = [];
List buckets = [];
List bucketsMap = [];
List<String> timeList = [];
class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
@override
void initState() {
gettimelist();
cancelSheduleTask();
scheduleDispens();
buckets.clear();
......@@ -80,9 +80,11 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
print('change ${DateFormat('kk:mm').format(time)}');
}, onConfirm: (time) {
print('confirm ${DateFormat('kk:mm').format(time)}');
setState(() {
timeController.text = DateFormat('kk:mm').format(time);
});
if (mounted) {
setState(() {
timeController.text = DateFormat('kk:mm').format(time);
});
}
}, currentTime: DateTime.now(), locale: LocaleType.en);
},
controller: timeController,
......@@ -109,9 +111,11 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
children: [
GestureDetector(
onTap: () {
setState(() {
timeController.clear();
});
if (mounted) {
setState(() {
timeController.clear();
});
}
},
child: Container(
padding: const EdgeInsets.symmetric(
......@@ -134,12 +138,14 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
GestureDetector(
onTap: () {
if (timeController.text.isNotEmpty) {
setState(() {
timeList.add(timeController.text.toString());
savetimelist();
scheduleDispens();
timeController.clear();
});
if (mounted) {
setState(() {
timeList.add(timeController.text.toString());
savetimelist();
timeController.clear();
});
}
}
},
child: Container(
......@@ -177,7 +183,7 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
height: 10,
),
Expanded(
child: timeList.isEmpty
child: timeList == null
? const Center(
child: Text(
"No Scheduled Data",
......@@ -202,14 +208,12 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
),
IconButton(
onPressed: () {
setState(() {
cancelSheduleTask();
timeList.remove(timeList[index]);
scheduleDispens();
savetimelist();
NotificationService()
.cancelNotifications(index);
});
if (mounted) {
setState(() {
timeList.remove(timeList[index]);
savetimelist();
});
}
},
icon: const Icon(
Icons.delete,
......@@ -264,6 +268,52 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
});
},
)),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
GestureDetector(
onTap: () {
cancelSheduleTask();
},
child: Container(
padding:
const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
decoration: BoxDecoration(
border: Border.all(color: yellowButton),
borderRadius: BorderRadius.circular(10)),
height: 40,
child: const Center(
child: Text(
"Stop Schedule",
style: TextStyle(color: Colors.white),
),
),
),
),
Visibility(
visible: timeList != null,
child: GestureDetector(
onTap: () {
scheduleDispens();
},
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 20, vertical: 10),
decoration: BoxDecoration(
color: yellowButton,
borderRadius: BorderRadius.circular(10)),
height: 40,
child: const Center(
child: Text(
"Schedule Start",
style: TextStyle(color: Colors.black),
),
),
),
),
),
],
)
],
),
......@@ -271,27 +321,61 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
}
scheduleDispens() async {
if (timeList.isEmpty) {
if (timeList == null) {
cancelSheduleTask();
NotificationService().cancelAllNotifications();
} else {
print(timeList);
print(DateFormat('kk:mm').format(DateTime.now()));
for (var i = 0; i < timeList.length; i++) {
String hours = timeList[i].toString().substring(0, 2);
String mins = timeList[i].toString().substring(3, 5);
scheduledTask =
cron.schedule(Schedule.parse("*/$mins */$hours * * *"), () async {
final servoref1 = FirebaseDatabase.instance.ref();
for (var k = 0; k < buckets.length; k++) {
print("servo run");
servoref1.update({
"Food/" + buckets[k] + "/SERVO": 1,
});
Future.delayed(const Duration(seconds: 20), () async {
print("servo closed");
await servoref1.update({
"Food/" + buckets[k] + "/SERVO": 0,
});
});
}
NotificationService().showNotification(
i,
1,
"Food Dispensing",
"Food dispensing now ${DateFormat('kk:mm').format(DateTime.now()).toString()}",
1);
});
print(hours + mins);
}
print("start");
}
}
dispense() async {
print('start dispensing');
for (var k = 0; k < buckets.length; k++) {
final servoref1 = FirebaseDatabase.instance.ref();
print("servo run");
servoref1.update({
"Food/" + buckets[k] + "/SERVO": 1,
});
Future.delayed(const Duration(seconds: 20), () async {
await servoref1.update({
"Food/" + buckets[k] + "/SERVO": 0,
});
});
}
}
void cancelSheduleTask() async {
if (scheduledTask != null) {
scheduledTask.cancel();
......@@ -308,9 +392,10 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
gettimelist() async {
SharedPreferences timelist = await SharedPreferences.getInstance();
setState(() {
timeList = timelist.getStringList("timelist");
scheduleDispens();
});
if (mounted) {
setState(() {
timeList = timelist.getStringList("timelist");
});
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment