Commit 530725cc authored by ayodyabanuka's avatar ayodyabanuka

update schedule

parent d0dda92c
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cron/cron.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:ifarm/AB/Utils/Colors.dart';
import 'package:ifarm/AB/Utils/NotificationService.dart';
import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';
class FoodBucketSchedule extends StatefulWidget {
FoodBucketSchedule({Key key}) : super(key: key);
......@@ -14,20 +17,25 @@ class FoodBucketSchedule extends StatefulWidget {
TextEditingController dateController = TextEditingController();
TextEditingController timeController = TextEditingController();
List dateList = [];
List timeList = [];
List<String> timeList = [];
List buckets = [];
List bucketsMap = [];
class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
@override
void initState() {
gettimelist();
cancelSheduleTask();
scheduleDispens();
buckets.clear();
print(buckets);
// TODO: implement initState
super.initState();
}
final cron = Cron();
ScheduledTask scheduledTask;
@override
Widget build(BuildContext context) {
final Stream<QuerySnapshot> bucketref =
......@@ -40,7 +48,7 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
child: Text(
"Schedule your Food dispense Here!",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold, fontSize: 20),
color: Colors.white, fontWeight: FontWeight.bold, fontSize: 30),
)),
const SizedBox(
height: 40,
......@@ -59,46 +67,6 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
const SizedBox(
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(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15), color: cardColor),
......@@ -143,7 +111,6 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
onTap: () {
setState(() {
timeController.clear();
dateController.clear();
});
},
child: Container(
......@@ -162,16 +129,15 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
),
)),
const SizedBox(
width: 10,
width: 30,
),
GestureDetector(
onTap: () {
if (dateController.text.isNotEmpty &&
timeController.text.isNotEmpty) {
if (timeController.text.isNotEmpty) {
setState(() {
dateList.add(dateController.text);
timeList.add(timeController.text);
dateController.clear();
timeList.add(timeController.text.toString());
savetimelist();
scheduleDispens();
timeController.clear();
});
}
......@@ -211,7 +177,7 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
height: 10,
),
Expanded(
child: dateList.isEmpty
child: timeList.isEmpty
? const Center(
child: Text(
"No Scheduled Data",
......@@ -219,7 +185,7 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
),
)
: ListView.builder(
itemCount: dateList.length,
itemCount: timeList.length,
itemBuilder: (context, index) {
return Container(
margin: const EdgeInsets.symmetric(vertical: 5),
......@@ -231,14 +197,18 @@ class _FoodBucketScheduleState extends State<FoodBucketSchedule> {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Date: ${dateList[index]} Time: ${timeList[index]}",
"Time: ${timeList[index]}",
style: const TextStyle(color: Colors.white),
),
IconButton(
onPressed: () {
setState(() {
dateList.remove(dateList[index]);
cancelSheduleTask();
timeList.remove(timeList[index]);
scheduleDispens();
savetimelist();
NotificationService()
.cancelNotifications(index);
});
},
icon: const Icon(
......@@ -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> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(
height: 40,
),
Container(
padding: const EdgeInsets.all(25),
decoration: BoxDecoration(
......@@ -246,65 +249,65 @@ class _HomeState extends State<Home> {
),
],
),
GestureDetector(
onTap: () {
NotificationService()
.showNotification(1, "title", "body", 3);
},
child: Container(
height: 40,
width: 200,
color: Colors.green,
child: Center(
child: Text("Show Notification"),
),
),
),
GestureDetector(
onTap: () {
NotificationService().cancelAllNotifications();
},
child: Container(
height: 40,
width: 200,
color: Colors.red,
child: Center(
child: Text(
"Cancel All Notifications",
),
),
),
),
GestureDetector(
onTap: () {
scheduleTask();
},
child: Container(
height: 40,
width: 200,
color: Color.fromARGB(255, 255, 255, 255),
child: Center(
child: Text(
"Schedule",
),
),
),
),
GestureDetector(
onTap: () {
cancelSheduleTask();
},
child: Container(
height: 40,
width: 200,
color: Color.fromARGB(255, 255, 255, 255),
child: Center(
child: Text(
"Cancel Schedule",
),
),
),
),
// GestureDetector(
// onTap: () {
// NotificationService()
// .showNotification(1, "title", "body", 3);
// },
// child: Container(
// height: 40,
// width: 200,
// color: Colors.green,
// child: Center(
// child: Text("Show Notification"),
// ),
// ),
// ),
// GestureDetector(
// onTap: () {
// NotificationService().cancelAllNotifications();
// },
// child: Container(
// height: 40,
// width: 200,
// color: Colors.red,
// child: Center(
// child: Text(
// "Cancel All Notifications",
// ),
// ),
// ),
// ),
// GestureDetector(
// onTap: () {
// scheduleTask();
// },
// child: Container(
// height: 40,
// width: 200,
// color: Color.fromARGB(255, 255, 255, 255),
// child: Center(
// child: Text(
// "Schedule",
// ),
// ),
// ),
// ),
// GestureDetector(
// onTap: () {
// cancelSheduleTask();
// },
// child: Container(
// height: 40,
// width: 200,
// color: Color.fromARGB(255, 255, 255, 255),
// child: Center(
// child: Text(
// "Cancel Schedule",
// ),
// ),
// ),
// ),
],
),
],
......@@ -312,15 +315,15 @@ class _HomeState extends State<Home> {
);
}
void scheduleTask() async {
scheduledTask = cron.schedule(Schedule.parse("*/1 * * * *"), () async {
NotificationService().showNotification(2, "Food Dispensing",
"Food dispensing now ${DateTime.now().toString()}", 1);
});
}
// void scheduleTask() async {
// scheduledTask = cron.schedule(Schedule.parse("*/1 * * * *"), () async {
// NotificationService().showNotification(2, "Food Dispensing",
// "Food dispensing now ${DateTime.now().toString()}", 1);
// });
// }
void cancelSheduleTask() async {
scheduledTask.cancel();
print("cancel");
}
// void cancelSheduleTask() async {
// scheduledTask.cancel();
// print("cancel");
// }
}
......@@ -37,29 +37,35 @@ class NotificationService {
Future<void> showNotification(
int id, String title, String body, int seconds) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
tz.TZDateTime.now(tz.local).add(Duration(seconds: seconds)),
const NotificationDetails(
android: AndroidNotificationDetails(
'main_channel', 'Main Channel', 'Main channel notifications',
importance: Importance.max,
priority: Priority.max,
icon: '@drawable/ifarm_icon_blue'),
iOS: IOSNotificationDetails(
presentAlert: true,
presentBadge: true,
presentSound: true,
id,
title,
body,
tz.TZDateTime.now(tz.local).add(Duration(seconds: seconds)),
const NotificationDetails(
android: AndroidNotificationDetails(
'main_channel', 'Main Channel', 'Main channel notifications',
importance: Importance.max,
priority: Priority.max,
icon: '@drawable/ifarm_icon_blue'),
iOS: IOSNotificationDetails(
presentAlert: true,
presentBadge: true,
presentSound: true,
),
),
),
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
androidAllowWhileIdle: true,
);
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
androidAllowWhileIdle: true,
payload: "Start Dispense");
}
Future selectNotification(String payload) async {}
Future<void> cancelAllNotifications() async {
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