Skip to content

Commit

Permalink
Notification with daily bug
Browse files Browse the repository at this point in the history
  • Loading branch information
HossamElghamry committed Jul 2, 2019
1 parent 0d4ff74 commit 8603728
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 162 deletions.
4 changes: 3 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.medicine_reminder">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
Expand Down
2 changes: 1 addition & 1 deletion lib/src/global_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class GlobalBloc {
BehaviorSubject<List<Medicine>> get medicineList$ => _medicineList$;

GlobalBloc() {
_medicineList$ = BehaviorSubject<List<Medicine>>.seeded([]);
makeMedicineList();
// _selectedDay$ = BehaviorSubject<Day>.seeded(Day.Saturday);
_selectedPeriod$ = BehaviorSubject<Period>.seeded(Period.Week);
_medicineList$ = BehaviorSubject<List<Medicine>>.seeded([]);
}

// void updateSelectedDay(Day day) {
Expand Down
2 changes: 0 additions & 2 deletions lib/src/models/medicine.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'package:medicine_reminder/src/models/medicine_type.dart';

class Medicine {
final String medicineName;
final int dosage;
Expand Down
10 changes: 5 additions & 5 deletions lib/src/ui/homepage/homepage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class MedicineCard extends StatelessWidget {
Hero makeIcon(double size) {
if (type == "Bottle") {
return Hero(
tag: type,
tag: medicine.medicineName + type,
child: Icon(
IconData(0xe900, fontFamily: "Ic"),
color: Color(0xFF3EB16F),
Expand All @@ -289,7 +289,7 @@ class MedicineCard extends StatelessWidget {
);
} else if (type == "Pill") {
return Hero(
tag: type,
tag: medicine.medicineName + type,
child: Icon(
IconData(0xe901, fontFamily: "Ic"),
color: Color(0xFF3EB16F),
Expand All @@ -298,7 +298,7 @@ class MedicineCard extends StatelessWidget {
);
} else if (type == "Syringe") {
return Hero(
tag: type,
tag: medicine.medicineName + type,
child: Icon(
IconData(0xe902, fontFamily: "Ic"),
color: Color(0xFF3EB16F),
Expand All @@ -307,7 +307,7 @@ class MedicineCard extends StatelessWidget {
);
} else if (type == "Tablet") {
return Hero(
tag: type,
tag: medicine.medicineName + type,
child: Icon(
IconData(0xe903, fontFamily: "Ic"),
color: Color(0xFF3EB16F),
Expand All @@ -316,7 +316,7 @@ class MedicineCard extends StatelessWidget {
);
}
return Hero(
tag: type,
tag: medicine.medicineName + type,
child: Icon(
Icons.error,
color: Color(0xFF3EB16F),
Expand Down
10 changes: 5 additions & 5 deletions lib/src/ui/medicine_details/medicine_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class MainSection extends StatelessWidget {
Hero makeIcon(double size) {
if (type == "Bottle") {
return Hero(
tag: type,
tag: medicine.medicineName + type,
child: Icon(
IconData(0xe900, fontFamily: "Ic"),
color: Color(0xFF3EB16F),
Expand All @@ -89,7 +89,7 @@ class MainSection extends StatelessWidget {
);
} else if (type == "Pill") {
return Hero(
tag: type,
tag: medicine.medicineName + type,
child: Icon(
IconData(0xe901, fontFamily: "Ic"),
color: Color(0xFF3EB16F),
Expand All @@ -98,7 +98,7 @@ class MainSection extends StatelessWidget {
);
} else if (type == "Syringe") {
return Hero(
tag: type,
tag: medicine.medicineName + type,
child: Icon(
IconData(0xe902, fontFamily: "Ic"),
color: Color(0xFF3EB16F),
Expand All @@ -107,7 +107,7 @@ class MainSection extends StatelessWidget {
);
} else if (type == "Tablet") {
return Hero(
tag: type,
tag: medicine.medicineName + type,
child: Icon(
IconData(0xe903, fontFamily: "Ic"),
color: Color(0xFF3EB16F),
Expand All @@ -116,7 +116,7 @@ class MainSection extends StatelessWidget {
);
}
return Hero(
tag: type,
tag: medicine.medicineName + type,
child: Icon(
Icons.error,
color: Color(0xFF3EB16F),
Expand Down
226 changes: 80 additions & 146 deletions lib/src/ui/new_entry/new_entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import 'package:medicine_reminder/src/common/convert_time.dart';
import 'package:medicine_reminder/src/global_bloc.dart';
import 'package:medicine_reminder/src/models/medicine.dart';
import 'package:medicine_reminder/src/models/medicine_type.dart';
import 'package:medicine_reminder/src/ui/homepage/homepage.dart';
import 'package:medicine_reminder/src/ui/new_entry/new_entry_bloc.dart';
import 'package:provider/provider.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

class NewEntry extends StatefulWidget {
@override
_NewEntryState createState() => _NewEntryState();
Expand All @@ -22,25 +25,57 @@ class _NewEntryState extends State<NewEntry> {
dosageController.dispose();
}

// void initState() {
// FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
// new FlutterLocalNotificationsPlugin();
// var android = new AndroidInitializationSettings('@mipmap/ic_launcher');
// var iOS = new IOSInitializationSettings();
// var initializationSettings = new InitializationSettings(android, iOS);
// flutterLocalNotificationsPlugin.initialize(initializationSettings,
// onSelectNotification: onSelectNotification);
// }
void initState() {
super.initState();
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
var initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = IOSInitializationSettings();
var initializationSettings = InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
}

// Future onSelectNotification(String payload) async {
// if (payload != null) {
// debugPrint('notification payload: ' + payload);
// }
// await Navigator.push(
// context,
// new MaterialPageRoute(builder: (context) => new SecondScreen(payload)),
// );
// }
Future onSelectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
await Navigator.push(
context,
new MaterialPageRoute(builder: (context) => HomePage()),
);
}

Future<void> scheduleNotification(Medicine medicine) async {
var hour = int.parse(medicine.startTime[0] + medicine.startTime[1]);
var ogValue = hour;
var minute = int.parse(medicine.startTime[2] + medicine.startTime[3]);

var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'repeatDailyAtTime channel id',
'repeatDailyAtTime channel name',
'repeatDailyAtTime description',
importance: Importance.Max,
priority: Priority.High);
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
for (int i = 0; i < (24 / medicine.interval).floor(); i++) {
if ((hour + (medicine.interval * i) > 24)) {
hour = hour + (medicine.interval * i) - 24;
} else {
hour = hour + (medicine.interval * i);
}
await flutterLocalNotificationsPlugin.showDailyAtTime(
medicine.dosage + medicine.interval + i,
'Medicine Reminder: ${medicine.medicineName}',
'It is time to take your ${medicine.medicineName + " " + medicine.medicineType.substring(13)}, according to schedule',
Time(hour, minute, 0),
platformChannelSpecifics);
hour = ogValue;
}
}

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -182,6 +217,7 @@ class _NewEntryState extends State<NewEntry> {
startTime: _newEntryBloc.selectedTimeOfDay$.value,
);
_globalBloc.updateMedicineList(newEntryMedicine);
scheduleNotification(newEntryMedicine);
Navigator.of(context).pop();
},
child: Center(
Expand Down Expand Up @@ -211,32 +247,33 @@ class IntervalSelection extends StatefulWidget {

class _IntervalSelectionState extends State<IntervalSelection> {
var _intervals = [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24
// 1,
// 2,
// 3,
// 4,
// 5,
// 6,
// 7,
// 8,
// 9,
// 10,
// 11,
// 12,
// 13,
// 14,
// 15,
// 16,
// 17,
// 18,
// 19,
// 20,
// 21,
// 22,
// 23,
// 24,
6, 8, 12, 24,
];
var _selected = 1;
var _selected = 6;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -430,109 +467,6 @@ class MedicineTypeColumn extends StatelessWidget {
}
}

// class ScheduleCheckBoxes extends StatelessWidget {
// @override
// Widget build(BuildContext context) {
// final NewEntryBloc _newEntryBloc = Provider.of<NewEntryBloc>(context);
// return StreamBuilder<List<Day>>(
// stream: _newEntryBloc.checkedDays$,
// builder: (context, snapshot) {
// if (!snapshot.hasData) {
// return CircularProgressIndicator();
// } else {
// return Padding(
// padding: EdgeInsets.only(top: 12.0),
// child: Row(
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
// children: <Widget>[
// BuildDay(
// text: "Sat",
// day: Day.Saturday,
// isSelected:
// snapshot.data.contains(Day.Saturday) ? true : false,
// ),
// BuildDay(
// text: "Sun",
// day: Day.Sunday,
// isSelected:
// snapshot.data.contains(Day.Sunday) ? true : false,
// ),
// BuildDay(
// text: "Mon",
// day: Day.Monday,
// isSelected:
// snapshot.data.contains(Day.Monday) ? true : false,
// ),
// BuildDay(
// text: "Tue",
// day: Day.Tuesday,
// isSelected:
// snapshot.data.contains(Day.Tuesday) ? true : false,
// ),
// BuildDay(
// text: "Wed",
// day: Day.Wednesday,
// isSelected:
// snapshot.data.contains(Day.Wednesday) ? true : false,
// ),
// BuildDay(
// text: "Thu",
// day: Day.Thursday,
// isSelected:
// snapshot.data.contains(Day.Thursday) ? true : false,
// ),
// BuildDay(
// text: "Fri",
// day: Day.Friday,
// isSelected:
// snapshot.data.contains(Day.Friday) ? true : false,
// ),
// ]),
// );
// }
// });
// }
// }

// class BuildDay extends StatelessWidget {
// final String text;
// final Day day;
// final bool isSelected;

// BuildDay(
// {Key key,
// @required this.text,
// @required this.day,
// @required this.isSelected})
// : super(key: key);

// @override
// Widget build(BuildContext context) {
// final NewEntryBloc _newEntryBloc = Provider.of<NewEntryBloc>(context);
// return Column(
// mainAxisAlignment: MainAxisAlignment.center,
// children: <Widget>[
// Text(
// text,
// style: TextStyle(
// color: Color(0xFF3EB16F),
// fontSize: 14,
// fontWeight: FontWeight.w700,
// ),
// ),
// Checkbox(
// value: isSelected,
// activeColor: Color(0xFF3EB16F),
// onChanged: (value) {
// _newEntryBloc.addtoDays(day);
// },
// ),
// ],
// );
// ;
// }
// }

class PanelTitle extends StatelessWidget {
final String title;
final bool isRequired;
Expand Down
1 change: 0 additions & 1 deletion lib/src/ui/new_entry/new_entry_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:medicine_reminder/src/models/day.dart';
import 'package:medicine_reminder/src/models/medicine_type.dart';
import 'package:rxdart/rxdart.dart';

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies:
rxdart: ^0.22.0
provider: ^3.0.0+1
shared_preferences: 0.5.2
flutter_local_notifications: ^0.7.1+3
flutter_local_notifications:

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 8603728

Please sign in to comment.