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 '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