diff --git a/lib/app/modules/alarmRingIgnore/bindings/alarm_ring_ignore_binding.dart b/lib/app/modules/alarmRingIgnore/bindings/alarm_ring_ignore_binding.dart deleted file mode 100644 index f9079b87..00000000 --- a/lib/app/modules/alarmRingIgnore/bindings/alarm_ring_ignore_binding.dart +++ /dev/null @@ -1,17 +0,0 @@ -import 'package:get/get.dart'; -import 'package:ultimate_alarm_clock/app/modules/settings/controllers/settings_controller.dart'; -import 'package:ultimate_alarm_clock/app/modules/settings/controllers/theme_controller.dart'; - -import '../controllers/alarm_ring_ignore_controller.dart'; - -class AlarmControlIgnoreBinding extends Bindings { - @override - void dependencies() { - Get.lazyPut( - () => AlarmControlIgnoreController(), - ); - Get.lazyPut( - () => SettingsController(), - ); - } -} diff --git a/lib/app/modules/alarmRingIgnore/controllers/alarm_ring_ignore_controller.dart b/lib/app/modules/alarmRingIgnore/controllers/alarm_ring_ignore_controller.dart deleted file mode 100644 index 4d475304..00000000 --- a/lib/app/modules/alarmRingIgnore/controllers/alarm_ring_ignore_controller.dart +++ /dev/null @@ -1,113 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; - -import 'package:get/get.dart'; -import 'package:ultimate_alarm_clock/app/data/models/alarm_model.dart'; -import 'package:ultimate_alarm_clock/app/data/models/user_model.dart'; -import 'package:ultimate_alarm_clock/app/data/providers/firestore_provider.dart'; -import 'package:ultimate_alarm_clock/app/data/providers/isar_provider.dart'; -import 'package:ultimate_alarm_clock/app/data/providers/secure_storage_provider.dart'; -import 'package:ultimate_alarm_clock/app/utils/utils.dart'; - -class AlarmControlIgnoreController extends GetxController { - MethodChannel alarmChannel = MethodChannel('ulticlock'); - - final formattedDate = Utils.getFormattedDate(DateTime.now()).obs; - final timeNow = - Utils.convertTo12HourFormat(Utils.timeOfDayToString(TimeOfDay.now())).obs; - final Rx currentlyRingingAlarm = Utils.genFakeAlarmModel().obs; - getCurrentlyRingingAlarm() async { - UserModel? _userModel = await SecureStorageProvider().retrieveUserModel(); - AlarmModel _alarmRecord = Utils.genFakeAlarmModel(); - AlarmModel isarLatestAlarm = - await IsarDb.getLatestAlarm(_alarmRecord, false); - AlarmModel firestoreLatestAlarm = - await FirestoreDb.getLatestAlarm(_userModel, _alarmRecord, false); - AlarmModel latestAlarm = - Utils.getFirstScheduledAlarm(isarLatestAlarm, firestoreLatestAlarm); - debugPrint('CURRENT RINGING : ${latestAlarm.alarmTime}'); - - return latestAlarm; - } - - getNextAlarm() async { - UserModel? _userModel = await SecureStorageProvider().retrieveUserModel(); - AlarmModel _alarmRecord = Utils.genFakeAlarmModel(); - AlarmModel isarLatestAlarm = - await IsarDb.getLatestAlarm(_alarmRecord, true); - AlarmModel firestoreLatestAlarm = - await FirestoreDb.getLatestAlarm(_userModel, _alarmRecord, true); - AlarmModel latestAlarm = - Utils.getFirstScheduledAlarm(isarLatestAlarm, firestoreLatestAlarm); - debugPrint('LATEST : ${latestAlarm.alarmTime}'); - - return latestAlarm; - } - - @override - void onInit() async { - super.onInit(); - - currentlyRingingAlarm.value = await getCurrentlyRingingAlarm(); - // If the alarm is set to NEVER repeat, then it will be chosen as - // the next alarm to ring by default as it would ring the next day - if (currentlyRingingAlarm.value.days.every((element) => element == false)) { - currentlyRingingAlarm.value.isEnabled = false; - - if (currentlyRingingAlarm.value.isSharedAlarmEnabled == false) { - IsarDb.updateAlarm(currentlyRingingAlarm.value); - } else { - FirestoreDb.updateAlarm( - currentlyRingingAlarm.value.ownerId, - currentlyRingingAlarm.value, - ); - } - } else if (currentlyRingingAlarm.value.isOneTime == true) { - // If the alarm has to repeat on one day, but ring just once, - // we will keep seting its days to false until it will never ring - int currentDay = DateTime.now().weekday - 1; - currentlyRingingAlarm.value.days[currentDay] = false; - - if (currentlyRingingAlarm.value.days - .every((element) => element == false)) { - currentlyRingingAlarm.value.isEnabled = false; - } - - if (currentlyRingingAlarm.value.isSharedAlarmEnabled == false) { - IsarDb.updateAlarm(currentlyRingingAlarm.value); - } else { - FirestoreDb.updateAlarm( - currentlyRingingAlarm.value.ownerId, - currentlyRingingAlarm.value, - ); - } - } - - AlarmModel latestAlarm = await getNextAlarm(); - - TimeOfDay latestAlarmTimeOfDay = - Utils.stringToTimeOfDay(latestAlarm.alarmTime); -// This condition will never satisfy because this will only occur if fake mode -// is returned as latest alarm - if (latestAlarm.isEnabled == false) { - debugPrint('STOPPED IF CONDITION with latest = ' - '${latestAlarmTimeOfDay.toString()} and '); - await alarmChannel.invokeMethod('cancelAllScheduledAlarms'); - } else { - int intervaltoAlarm = Utils.getMillisecondsToAlarm( - DateTime.now(), - Utils.timeOfDayToDateTime(latestAlarmTimeOfDay), - ); - - try { - await alarmChannel - .invokeMethod('scheduleAlarm', {'milliSeconds': intervaltoAlarm}); - print("Scheduled..."); - } on PlatformException catch (e) { - print("Failed to schedule alarm: ${e.message}"); - } - } - - alarmChannel.invokeMethod('minimizeApp'); - } -} diff --git a/lib/app/modules/alarmRingIgnore/views/alarm_ring_ignore_view.dart b/lib/app/modules/alarmRingIgnore/views/alarm_ring_ignore_view.dart deleted file mode 100644 index 7b8528c2..00000000 --- a/lib/app/modules/alarmRingIgnore/views/alarm_ring_ignore_view.dart +++ /dev/null @@ -1,102 +0,0 @@ -import 'package:flutter/material.dart'; - -import 'package:get/get.dart'; -import 'package:ultimate_alarm_clock/app/modules/settings/controllers/theme_controller.dart'; -import 'package:ultimate_alarm_clock/app/utils/constants.dart'; -import 'package:ultimate_alarm_clock/app/utils/utils.dart'; - -import '../controllers/alarm_ring_ignore_controller.dart'; - -class AlarmControlIgnoreView extends GetView { - AlarmControlIgnoreView({Key? key}) : super(key: key); - - ThemeController themeController = Get.find(); - - @override - Widget build(BuildContext context) { - var width = Get.width; - var height = Get.height; - return SafeArea( - child: Scaffold( - floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, - floatingActionButton: Padding( - padding: const EdgeInsets.all(18.0), - child: SizedBox( - height: height * 0.06, - width: width * 0.8, - child: TextButton( - style: ButtonStyle( - backgroundColor: MaterialStateProperty.all(kprimaryColor), - ), - child: Text( - 'Dismiss', - style: Theme.of(context).textTheme.displaySmall!.copyWith( - color: themeController.isLightMode.value - ? kLightPrimaryTextColor - : ksecondaryTextColor, - ), - ), - onPressed: () { - Utils.hapticFeedback(); - Get.offNamed('/home'); - }, - ), - ), - ), - body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.spaceAround, - children: [ - Obx( - () => Column( - children: [ - Text( - controller.formattedDate.value, - style: Theme.of(context).textTheme.bodyLarge, - ), - const SizedBox( - height: 10, - width: 0, - ), - Text( - '${controller.timeNow[0]} ${controller.timeNow[1]}', - style: Theme.of(context) - .textTheme - .displayLarge! - .copyWith(fontSize: 50), - ), - ], - ), - ), - SizedBox( - height: height * 0.055, - width: width * 0.25, - child: TextButton( - style: ButtonStyle( - backgroundColor: MaterialStateProperty.all( - themeController.isLightMode.value - ? kLightSecondaryBackgroundColor - : ksecondaryBackgroundColor, - ), - ), - child: Text( - 'Snooze', - style: Theme.of(context).textTheme.bodyMedium!.copyWith( - color: themeController.isLightMode.value - ? kLightPrimaryTextColor - : kprimaryTextColor, - fontWeight: FontWeight.w600, - ), - ), - onPressed: () { - Utils.hapticFeedback(); - }, - ), - ), - ], - ), - ), - ), - ); - } -} diff --git a/lib/app/modules/splashScreen/controllers/splash_screen_controller.dart b/lib/app/modules/splashScreen/controllers/splash_screen_controller.dart index 26ccb63d..e3787c3a 100644 --- a/lib/app/modules/splashScreen/controllers/splash_screen_controller.dart +++ b/lib/app/modules/splashScreen/controllers/splash_screen_controller.dart @@ -18,6 +18,8 @@ class SplashScreenController extends GetxController { bool shouldAlarmRing = true; bool shouldNavigate = true; + final Rx currentlyRingingAlarm = Utils.genFakeAlarmModel().obs; + getCurrentlyRingingAlarm() async { UserModel? _userModel = await SecureStorageProvider().retrieveUserModel(); AlarmModel _alarmRecord = Utils.genFakeAlarmModel(); @@ -32,6 +34,20 @@ class SplashScreenController extends GetxController { return latestAlarm; } + getNextAlarm() async { + UserModel? _userModel = await SecureStorageProvider().retrieveUserModel(); + AlarmModel _alarmRecord = Utils.genFakeAlarmModel(); + AlarmModel isarLatestAlarm = + await IsarDb.getLatestAlarm(_alarmRecord, true); + AlarmModel firestoreLatestAlarm = + await FirestoreDb.getLatestAlarm(_userModel, _alarmRecord, true); + AlarmModel latestAlarm = + Utils.getFirstScheduledAlarm(isarLatestAlarm, firestoreLatestAlarm); + debugPrint('LATEST : ${latestAlarm.alarmTime}'); + + return latestAlarm; + } + Future checkWeatherCondition( LatLng location, List weatherTypeInt, @@ -137,7 +153,70 @@ class SplashScreenController extends GetxController { if (shouldAlarmRing) { Get.offNamed('/alarm-ring'); } else { - Get.offNamed('/alarm-ring-ignore'); + currentlyRingingAlarm.value = await getCurrentlyRingingAlarm(); + // If the alarm is set to NEVER repeat, then it will be chosen as + // the next alarm to ring by default as it would ring the next day + if (currentlyRingingAlarm.value.days + .every((element) => element == false)) { + currentlyRingingAlarm.value.isEnabled = false; + + if (currentlyRingingAlarm.value.isSharedAlarmEnabled == false) { + IsarDb.updateAlarm(currentlyRingingAlarm.value); + } else { + FirestoreDb.updateAlarm( + currentlyRingingAlarm.value.ownerId, + currentlyRingingAlarm.value, + ); + } + } else if (currentlyRingingAlarm.value.isOneTime == true) { + // If the alarm has to repeat on one day, but ring just once, + // we will keep seting its days to false until it will never ring + int currentDay = DateTime.now().weekday - 1; + currentlyRingingAlarm.value.days[currentDay] = false; + + if (currentlyRingingAlarm.value.days + .every((element) => element == false)) { + currentlyRingingAlarm.value.isEnabled = false; + } + + if (currentlyRingingAlarm.value.isSharedAlarmEnabled == false) { + IsarDb.updateAlarm(currentlyRingingAlarm.value); + } else { + FirestoreDb.updateAlarm( + currentlyRingingAlarm.value.ownerId, + currentlyRingingAlarm.value, + ); + } + } + + AlarmModel latestAlarm = await getNextAlarm(); + + TimeOfDay latestAlarmTimeOfDay = + Utils.stringToTimeOfDay(latestAlarm.alarmTime); +// This condition will never satisfy because this will only occur if fake mode +// is returned as latest alarm + if (latestAlarm.isEnabled == false) { + debugPrint('STOPPED IF CONDITION with latest = ' + '${latestAlarmTimeOfDay.toString()} and '); + await alarmChannel.invokeMethod('cancelAllScheduledAlarms'); + } else { + int intervaltoAlarm = Utils.getMillisecondsToAlarm( + DateTime.now(), + Utils.timeOfDayToDateTime(latestAlarmTimeOfDay), + ); + + try { + await alarmChannel.invokeMethod( + 'scheduleAlarm', {'milliSeconds': intervaltoAlarm}); + print("Scheduled..."); + } on PlatformException catch (e) { + print("Failed to schedule alarm: ${e.message}"); + } + } + + Get.offNamed('/home'); + + alarmChannel.invokeMethod('minimizeApp'); } } } diff --git a/lib/app/routes/app_pages.dart b/lib/app/routes/app_pages.dart index a3e24feb..8bd0d686 100644 --- a/lib/app/routes/app_pages.dart +++ b/lib/app/routes/app_pages.dart @@ -10,8 +10,6 @@ import '../modules/alarmChallenge/bindings/alarm_challenge_binding.dart'; import '../modules/alarmChallenge/views/alarm_challenge_view.dart'; import '../modules/alarmRing/bindings/alarm_ring_binding.dart'; import '../modules/alarmRing/views/alarm_ring_view.dart'; -import '../modules/alarmRingIgnore/bindings/alarm_ring_ignore_binding.dart'; -import '../modules/alarmRingIgnore/views/alarm_ring_ignore_view.dart'; import '../modules/home/bindings/home_binding.dart'; import '../modules/home/views/home_view.dart'; import '../modules/settings/bindings/settings_binding.dart'; @@ -45,11 +43,6 @@ class AppPages { page: () => AlarmControlView(), binding: AlarmControlBinding(), ), - GetPage( - name: _Paths.ALARM_RING_IGNORE, - page: () => AlarmControlIgnoreView(), - binding: AlarmControlIgnoreBinding(), - ), GetPage( name: _Paths.SETTINGS, page: () => SettingsView(), @@ -71,6 +64,5 @@ class AppPages { page: () => AboutView(), binding: AboutBinding(), ), - ]; } diff --git a/lib/app/routes/app_routes.dart b/lib/app/routes/app_routes.dart index c7ea8f1d..7c38f157 100644 --- a/lib/app/routes/app_routes.dart +++ b/lib/app/routes/app_routes.dart @@ -7,7 +7,6 @@ abstract class Routes { static const HOME = _Paths.HOME; static const ADD_ALARM = _Paths.ADD_OR_UPDATE_ALARM; static const ALARM_RING = _Paths.ALARM_RING; - static const ALARM_RING_IGNORE = _Paths.ALARM_RING_IGNORE; static const SETTINGS = _Paths.SETTINGS; static const ALARM_CHALLENGE = _Paths.ALARM_CHALLENGE; static const ABOUT = _Paths.ABOUT; @@ -19,7 +18,6 @@ abstract class _Paths { static const HOME = '/home'; static const ADD_OR_UPDATE_ALARM = '/add-update-alarm'; static const ALARM_RING = '/alarm-ring'; - static const ALARM_RING_IGNORE = '/alarm-ring-ignore'; static const SETTINGS = '/settings'; static const ALARM_CHALLENGE = '/alarm-challenge'; static const ABOUT = '/about';