Commit 530725cc authored by ayodyabanuka's avatar ayodyabanuka

update schedule

parent d0dda92c
import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cron/cron.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart'; import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:ifarm/AB/Utils/Colors.dart'; import 'package:ifarm/AB/Utils/Colors.dart';
import 'package:ifarm/AB/Utils/NotificationService.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';
class FoodBucketSchedule extends StatefulWidget { class FoodBucketSchedule extends StatefulWidget {
FoodBucketSchedule({Key key}) : super(key: key); FoodBucketSchedule({Key key}) : super(key: key);
...@@ -14,20 +17,25 @@ class FoodBucketSchedule extends StatefulWidget { ...@@ -14,20 +17,25 @@ class FoodBucketSchedule extends StatefulWidget {
TextEditingController dateController = TextEditingController(); TextEditingController dateController = TextEditingController();
TextEditingController timeController = TextEditingController(); TextEditingController timeController = TextEditingController();
List dateList = []; List<String> timeList = [];
List timeList = [];
List buckets = []; List buckets = [];
List bucketsMap = []; List bucketsMap = [];
class _FoodBucketScheduleState extends State<FoodBucketSchedule> { class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
@override @override
void initState() { void initState() {
gettimelist();
cancelSheduleTask();
scheduleDispens();
buckets.clear(); buckets.clear();
print(buckets);
// TODO: implement initState // TODO: implement initState
super.initState(); super.initState();
} }
final cron = Cron();
ScheduledTask scheduledTask;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final Stream<QuerySnapshot> bucketref = final Stream<QuerySnapshot> bucketref =
...@@ -40,7 +48,7 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> { ...@@ -40,7 +48,7 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
child: Text( child: Text(
"Schedule your Food dispense Here!", "Schedule your Food dispense Here!",
style: TextStyle( style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold, fontSize: 20), color: Colors.white, fontWeight: FontWeight.bold, fontSize: 30),
)), )),
const SizedBox( const SizedBox(
height: 40, height: 40,
...@@ -59,46 +67,6 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> { ...@@ -59,46 +67,6 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
const SizedBox( const SizedBox(
height: 10, height: 10,
), ),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15), color: cardColor),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: TextFormField(
readOnly: true,
onTap: () {
DatePicker.showDatePicker(context,
showTitleActions: true,
minTime: DateTime(2022, 1, 1),
maxTime: DateTime(2030, 12, 31), onChanged: (date) {
print('change ${DateFormat('yyyy-MM-dd').format(date)}');
}, onConfirm: (date) {
print('confirm ${DateFormat('yyyy-MM-dd').format(date)}');
setState(() {
dateController.text =
DateFormat('yyyy-MM-dd').format(date);
});
}, currentTime: DateTime.now(), locale: LocaleType.en);
},
controller: dateController,
style:
const TextStyle(color: Color.fromARGB(255, 255, 255, 255)),
cursorColor: const Color.fromARGB(255, 0, 0, 0),
decoration: const InputDecoration(
suffixIcon: Icon(
Icons.calendar_month,
color: Colors.white,
),
hintText: "Pick Date",
border: InputBorder.none,
hintStyle:
TextStyle(color: Color.fromARGB(255, 255, 255, 255))),
),
),
),
const SizedBox(
height: 10,
),
Container( Container(
decoration: BoxDecoration( decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15), color: cardColor), borderRadius: BorderRadius.circular(15), color: cardColor),
...@@ -143,7 +111,6 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> { ...@@ -143,7 +111,6 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
onTap: () { onTap: () {
setState(() { setState(() {
timeController.clear(); timeController.clear();
dateController.clear();
}); });
}, },
child: Container( child: Container(
...@@ -162,16 +129,15 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> { ...@@ -162,16 +129,15 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
), ),
)), )),
const SizedBox( const SizedBox(
width: 10, width: 30,
), ),
GestureDetector( GestureDetector(
onTap: () { onTap: () {
if (dateController.text.isNotEmpty && if (timeController.text.isNotEmpty) {
timeController.text.isNotEmpty) {
setState(() { setState(() {
dateList.add(dateController.text); timeList.add(timeController.text.toString());
timeList.add(timeController.text); savetimelist();
dateController.clear(); scheduleDispens();
timeController.clear(); timeController.clear();
}); });
} }
...@@ -211,7 +177,7 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> { ...@@ -211,7 +177,7 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
height: 10, height: 10,
), ),
Expanded( Expanded(
child: dateList.isEmpty child: timeList.isEmpty
? const Center( ? const Center(
child: Text( child: Text(
"No Scheduled Data", "No Scheduled Data",
...@@ -219,7 +185,7 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> { ...@@ -219,7 +185,7 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
), ),
) )
: ListView.builder( : ListView.builder(
itemCount: dateList.length, itemCount: timeList.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
return Container( return Container(
margin: const EdgeInsets.symmetric(vertical: 5), margin: const EdgeInsets.symmetric(vertical: 5),
...@@ -231,14 +197,18 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> { ...@@ -231,14 +197,18 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [ children: [
Text( Text(
"Date: ${dateList[index]} Time: ${timeList[index]}", "Time: ${timeList[index]}",
style: const TextStyle(color: Colors.white), style: const TextStyle(color: Colors.white),
), ),
IconButton( IconButton(
onPressed: () { onPressed: () {
setState(() { setState(() {
dateList.remove(dateList[index]); cancelSheduleTask();
timeList.remove(timeList[index]); timeList.remove(timeList[index]);
scheduleDispens();
savetimelist();
NotificationService()
.cancelNotifications(index);
}); });
}, },
icon: const Icon( icon: const Icon(
...@@ -299,4 +269,48 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> { ...@@ -299,4 +269,48 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
), ),
); );
} }
scheduleDispens() async {
if (timeList.isEmpty) {
cancelSheduleTask();
NotificationService().cancelAllNotifications();
} else {
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 {
NotificationService().showNotification(
i,
"Food Dispensing",
"Food dispensing now ${DateFormat('kk:mm').format(DateTime.now()).toString()}",
1);
});
print(hours + mins);
}
print("start");
}
}
void cancelSheduleTask() async {
if (scheduledTask != null) {
scheduledTask.cancel();
}
print("cancel");
}
savetimelist() async {
SharedPreferences timelist = await SharedPreferences.getInstance();
timelist.setStringList("timelist", timeList);
}
gettimelist() async {
SharedPreferences timelist = await SharedPreferences.getInstance();
setState(() {
timeList = timelist.getStringList("timelist");
scheduleDispens();
});
}
} }
...@@ -50,6 +50,9 @@ class _HomeState extends State<Home> { ...@@ -50,6 +50,9 @@ class _HomeState extends State<Home> {
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
const SizedBox(
height: 40,
),
Container( Container(
padding: const EdgeInsets.all(25), padding: const EdgeInsets.all(25),
decoration: BoxDecoration( decoration: BoxDecoration(
...@@ -246,65 +249,65 @@ class _HomeState extends State<Home> { ...@@ -246,65 +249,65 @@ class _HomeState extends State<Home> {
), ),
], ],
), ),
GestureDetector( // GestureDetector(
onTap: () { // onTap: () {
NotificationService() // NotificationService()
.showNotification(1, "title", "body", 3); // .showNotification(1, "title", "body", 3);
}, // },
child: Container( // child: Container(
height: 40, // height: 40,
width: 200, // width: 200,
color: Colors.green, // color: Colors.green,
child: Center( // child: Center(
child: Text("Show Notification"), // child: Text("Show Notification"),
), // ),
), // ),
), // ),
GestureDetector( // GestureDetector(
onTap: () { // onTap: () {
NotificationService().cancelAllNotifications(); // NotificationService().cancelAllNotifications();
}, // },
child: Container( // child: Container(
height: 40, // height: 40,
width: 200, // width: 200,
color: Colors.red, // color: Colors.red,
child: Center( // child: Center(
child: Text( // child: Text(
"Cancel All Notifications", // "Cancel All Notifications",
), // ),
), // ),
), // ),
), // ),
GestureDetector( // GestureDetector(
onTap: () { // onTap: () {
scheduleTask(); // scheduleTask();
}, // },
child: Container( // child: Container(
height: 40, // height: 40,
width: 200, // width: 200,
color: Color.fromARGB(255, 255, 255, 255), // color: Color.fromARGB(255, 255, 255, 255),
child: Center( // child: Center(
child: Text( // child: Text(
"Schedule", // "Schedule",
), // ),
), // ),
), // ),
), // ),
GestureDetector( // GestureDetector(
onTap: () { // onTap: () {
cancelSheduleTask(); // cancelSheduleTask();
}, // },
child: Container( // child: Container(
height: 40, // height: 40,
width: 200, // width: 200,
color: Color.fromARGB(255, 255, 255, 255), // color: Color.fromARGB(255, 255, 255, 255),
child: Center( // child: Center(
child: Text( // child: Text(
"Cancel Schedule", // "Cancel Schedule",
), // ),
), // ),
), // ),
), // ),
], ],
), ),
], ],
...@@ -312,15 +315,15 @@ class _HomeState extends State<Home> { ...@@ -312,15 +315,15 @@ class _HomeState extends State<Home> {
); );
} }
void scheduleTask() async { // void scheduleTask() async {
scheduledTask = cron.schedule(Schedule.parse("*/1 * * * *"), () async { // scheduledTask = cron.schedule(Schedule.parse("*/1 * * * *"), () async {
NotificationService().showNotification(2, "Food Dispensing", // NotificationService().showNotification(2, "Food Dispensing",
"Food dispensing now ${DateTime.now().toString()}", 1); // "Food dispensing now ${DateTime.now().toString()}", 1);
}); // });
} // }
void cancelSheduleTask() async { // void cancelSheduleTask() async {
scheduledTask.cancel(); // scheduledTask.cancel();
print("cancel"); // print("cancel");
} // }
} }
...@@ -56,10 +56,16 @@ class NotificationService { ...@@ -56,10 +56,16 @@ class NotificationService {
uiLocalNotificationDateInterpretation: uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime, UILocalNotificationDateInterpretation.absoluteTime,
androidAllowWhileIdle: true, androidAllowWhileIdle: true,
); payload: "Start Dispense");
} }
Future selectNotification(String payload) async {}
Future<void> cancelAllNotifications() async { Future<void> cancelAllNotifications() async {
await flutterLocalNotificationsPlugin.cancelAll(); await flutterLocalNotificationsPlugin.cancelAll();
} }
Future<void> cancelNotifications(int id) async {
await flutterLocalNotificationsPlugin.cancel(id);
}
} }
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