From df47ffaa9b358016b0cbdd65f8163ac84224e379 Mon Sep 17 00:00:00 2001 From: iqfareez Date: Fri, 22 Jan 2021 00:46:12 +0800 Subject: [PATCH] Added limit on nofication --- lib/CONSTANTS.dart | 3 + lib/main.dart | 3 + lib/utils/isolate_handler_notification.dart | 19 +- lib/views/GetPrayerTime.dart | 20 +- lib/views/Settings part/AboutPage.dart | 2 +- .../NotificationSettingPage.dart | 187 ++++++++++-------- lib/views/ZoneChooser.dart | 2 + 7 files changed, 149 insertions(+), 87 deletions(-) diff --git a/lib/CONSTANTS.dart b/lib/CONSTANTS.dart index 91a2a0f..3cd784f 100644 --- a/lib/CONSTANTS.dart +++ b/lib/CONSTANTS.dart @@ -9,6 +9,9 @@ const kStoredTimeIs12 = "storedTimeFormat"; const kStoredShowOtherPrayerTime = "storedshowothertime"; const kStoredApiPrayerCall = "storedApiPrayCall"; const kStoredApiLocationCall = "storedApiLocCall"; +const kStoredShouldUpdateNotif = "storedShouldUpdateNotif"; +const kStoredLastUpdateNotif = "storedLastUpdateNotif"; +const kStoredNotificationLimit = "storedNotificationLimit"; //Network image const kAppIconUrl = diff --git a/lib/main.dart b/lib/main.dart index 8c2ab70..a7c35d5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -104,6 +104,9 @@ void initGetStorage() { GetStorage().writeIfNull(kStoredFirstRun, true); GetStorage().writeIfNull(kStoredTimeIs12, true); GetStorage().writeIfNull(kStoredShowOtherPrayerTime, false); + GetStorage().writeIfNull(kStoredShouldUpdateNotif, true); + GetStorage().writeIfNull(kStoredLastUpdateNotif, 0); + GetStorage().writeIfNull(kStoredNotificationLimit, false); } Future _configureLocalTimeZone() async { diff --git a/lib/utils/isolate_handler_notification.dart b/lib/utils/isolate_handler_notification.dart index 4e94a75..58c94c8 100644 --- a/lib/utils/isolate_handler_notification.dart +++ b/lib/utils/isolate_handler_notification.dart @@ -21,7 +21,19 @@ void schedulePrayNotification(List times) async { var currentTime = DateTime.now().millisecondsSinceEpoch; - for (int i = 0; i < times.length; i++) { + var howMuchToSchedule; + + if (GetStorage().read(kStoredNotificationLimit)) { + //should limit to 7 + //TODO: Check this logic wether work on not + howMuchToSchedule = times.length < 7 ? times.length : 7; + } else { + howMuchToSchedule = times.length; + } + + print('howMuchToSchedule is $howMuchToSchedule'); + + for (int i = 0; i < howMuchToSchedule; i++) { //i denotes the day relative for today int subuhTimeEpoch = times[i][0] * 1000; int syurukTimeEpoch = times[i][1] * 1000; @@ -93,7 +105,7 @@ void schedulePrayNotification(List times) async { DateTime.fromMillisecondsSinceEpoch(isyakTimeEpoch), tz.local)); } - print('Notification scheduled #$i'); + print('Notification scheduled #${i + 1}'); print('Subuh @ $subuhTimeEpoch'); print('Syuruk @ $syurukTimeEpoch'); print('Zohor @ $zuhrTimeEpoch'); @@ -114,6 +126,9 @@ void schedulePrayNotification(List times) async { ); print('DONE SCHEDULING NOTIFS'); + //This timestamp is later used to determine wether notification should be updated or not + GetStorage() + .write(kStoredLastUpdateNotif, DateTime.now().millisecondsSinceEpoch); killCurrentScheduleNotifications(); } diff --git a/lib/views/GetPrayerTime.dart b/lib/views/GetPrayerTime.dart index d00a3c1..2124c23 100644 --- a/lib/views/GetPrayerTime.dart +++ b/lib/views/GetPrayerTime.dart @@ -1,3 +1,4 @@ +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_spinkit/flutter_spinkit.dart'; @@ -39,6 +40,19 @@ class _GetPrayerTimeState extends State { .getMptLocationCode(GetStorage().read(kStoredGlobalIndex)); prayerBloc = Mpti906PrayerBloc(location); print('$location'); + //If less than 3 days, since the last notif is scheduled, do not rescehdule + if ((DateTime.now().millisecondsSinceEpoch - + GetStorage().read(kStoredLastUpdateNotif)) < + 259200000) { + print('Notification should not update'); + //TODO: Rremove when release, toast is for debug purposes + Fluttertoast.showToast(msg: 'Notification should not update'); + GetStorage().write(kStoredShouldUpdateNotif, false); + } else { + GetStorage().write(kStoredLastUpdateNotif, true); + print('Notification should update'); + Fluttertoast.showToast(msg: 'Notification should update'); + } } @override @@ -97,8 +111,10 @@ class _PrayTimeListState extends State { Widget build(BuildContext context) { var prayerTimeData = widget.prayerTime.data; handler = PrayDataHandler(prayerTimeData.times); - schedulePrayNotification( - handler.getPrayDataCurrentDateOnwards()); //schedule notification + if (!kIsWeb && GetStorage().read(kStoredShouldUpdateNotif)) { + schedulePrayNotification( + handler.getPrayDataCurrentDateOnwards()); //schedule notification + } return Container(child: Consumer( builder: (context, setting, child) { diff --git a/lib/views/Settings part/AboutPage.dart b/lib/views/Settings part/AboutPage.dart index 04e711e..72c2602 100644 --- a/lib/views/Settings part/AboutPage.dart +++ b/lib/views/Settings part/AboutPage.dart @@ -135,7 +135,7 @@ class AboutAppPage extends StatelessWidget { ), ), Text( - '\n© Copyright 2020 Fareez Iqmal\n', + '\n© Copyright 2020-2021 Fareez Iqmal\n', textAlign: TextAlign.center, ), Container( diff --git a/lib/views/Settings part/NotificationSettingPage.dart b/lib/views/Settings part/NotificationSettingPage.dart index 278d286..8914000 100644 --- a/lib/views/Settings part/NotificationSettingPage.dart +++ b/lib/views/Settings part/NotificationSettingPage.dart @@ -1,7 +1,8 @@ -// at the moment, this feature is not readily implemented import 'package:app_settings/app_settings.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:get_storage/get_storage.dart'; +import 'package:waktusolatmalaysia/CONSTANTS.dart'; import 'package:waktusolatmalaysia/utils/cupertinoSwitchListTile.dart'; import 'package:waktusolatmalaysia/utils/prayerName.dart'; @@ -21,96 +22,118 @@ class _NotificationPageSettingState extends State { appBar: AppBar( title: Text('Notification Settings'), centerTitle: true, - actions: [ - FlatButton( - shape: CircleBorder(side: BorderSide(color: Colors.transparent)), - textColor: Colors.white, - onPressed: !isAnythingChanged - ? null - : () { - print('Save button clicked'); - print(prayerNotification); - Navigator.pop(context); - }, - child: Text( - 'SAVE', - ), - ) - ], + // actions: [ + // FlatButton( + // shape: CircleBorder(side: BorderSide(color: Colors.transparent)), + // textColor: Colors.white, + // onPressed: !isAnythingChanged + // ? null + // : () { + // print('Save button clicked'); + // print(prayerNotification); + // Navigator.pop(context); + // }, + // child: Text( + // 'SAVE', + // ), + // ) + // ], ), body: ListView( padding: EdgeInsets.all(16), children: [ + //Turned of for a while + // Text('Basic'), + // Card( + // child: Column(children: [ + // CupertinoSwitchListTile( + // title: Text(PrayerName.prayerName[0]), + // value: prayerNotification[0], + // onChanged: (bool value) { + // setState(() { + // isAnythingChanged = true; + // prayerNotification[0] = value; + // }); + // }), + // CupertinoSwitchListTile( + // title: Text(PrayerName.prayerName[1]), + // value: prayerNotification[1], + // onChanged: (bool value) { + // setState(() { + // isAnythingChanged = true; + // prayerNotification[1] = value; + // }); + // }), + // CupertinoSwitchListTile( + // title: Text(PrayerName.prayerName[2]), + // value: prayerNotification[2], + // onChanged: (bool value) { + // setState(() { + // isAnythingChanged = true; + // prayerNotification[2] = value; + // }); + // }), + // CupertinoSwitchListTile( + // title: Text(PrayerName.prayerName[3]), + // value: prayerNotification[3], + // onChanged: (bool value) { + // setState(() { + // isAnythingChanged = true; + // prayerNotification[3] = value; + // }); + // }), + // CupertinoSwitchListTile( + // title: Text(PrayerName.prayerName[4]), + // value: prayerNotification[4], + // onChanged: (bool value) { + // setState(() { + // isAnythingChanged = true; + // prayerNotification[4] = value; + // }); + // }), + // CupertinoSwitchListTile( + // title: Text(PrayerName.prayerName[5]), + // value: prayerNotification[5], + // onChanged: (bool value) { + // setState(() { + // isAnythingChanged = true; + // prayerNotification[5] = value; + // }); + // }), + // ]), + // ), Text('Basic'), Card( - child: Column(children: [ - CupertinoSwitchListTile( - title: Text(PrayerName.prayerName[0]), - value: prayerNotification[0], - onChanged: (bool value) { - setState(() { - isAnythingChanged = true; - prayerNotification[0] = value; - }); - }), - CupertinoSwitchListTile( - title: Text(PrayerName.prayerName[1]), - value: prayerNotification[1], - onChanged: (bool value) { - setState(() { - isAnythingChanged = true; - prayerNotification[1] = value; - }); - }), - CupertinoSwitchListTile( - title: Text(PrayerName.prayerName[2]), - value: prayerNotification[2], - onChanged: (bool value) { - setState(() { - isAnythingChanged = true; - prayerNotification[2] = value; - }); - }), - CupertinoSwitchListTile( - title: Text(PrayerName.prayerName[3]), - value: prayerNotification[3], - onChanged: (bool value) { - setState(() { - isAnythingChanged = true; - prayerNotification[3] = value; - }); - }), - CupertinoSwitchListTile( - title: Text(PrayerName.prayerName[4]), - value: prayerNotification[4], - onChanged: (bool value) { - setState(() { - isAnythingChanged = true; - prayerNotification[4] = value; - }); - }), - CupertinoSwitchListTile( - title: Text(PrayerName.prayerName[5]), - value: prayerNotification[5], - onChanged: (bool value) { - setState(() { - isAnythingChanged = true; - prayerNotification[5] = value; - }); - }), - ]), + child: ListTile( + title: Text('App notification System Setting'), + subtitle: + Text('Change sound, vibration of prayer notification etc.'), + trailing: Icon(Icons.launch_rounded), + onTap: () async { + await AppSettings.openNotificationSettings(); + }, + ), + ), + SizedBox( + height: 10, ), Text('Advanced'), Card( - child: ListTile( - title: Text('App notification System Setting'), - subtitle: - Text('Change sound, vibration of prayer notification etc.'), - trailing: Icon(Icons.launch_rounded), - onTap: () async { - await AppSettings.openNotificationSettings(); - }, - )) + child: Container( + padding: const EdgeInsets.symmetric(vertical: 8.0), + child: CupertinoSwitchListTile( + title: Text('Limit notification scheduling'), + subtitle: Text( + 'Only you are experiencing extreme slowdown in app. Notification will schedule weekly basis. Default is OFF (monthly).'), + value: GetStorage().read(kStoredNotificationLimit), + onChanged: (value) { + // print(value); + setState(() { + GetStorage().write(kStoredNotificationLimit, value); + }); + }), + ), + ) ], ), ); diff --git a/lib/views/ZoneChooser.dart b/lib/views/ZoneChooser.dart index add5648..4c908a6 100644 --- a/lib/views/ZoneChooser.dart +++ b/lib/views/ZoneChooser.dart @@ -63,6 +63,8 @@ class _LocationChooserState extends State { var shortCode = locationDatabase.getJakimCode(globalIndex); void _updateUI() { + GetStorage().write(kStoredShouldUpdateNotif, + true); //if zone changes, update the notification //this setState will be called when user select a new location, this will update the Text short code setState(() { shortCode = locationDatabase.getJakimCode(globalIndex);