@@ -20,6 +20,7 @@ import 'package:ultimate_alarm_clock/app/utils/audio_utils.dart';
2020
2121import 'package:ultimate_alarm_clock/app/utils/utils.dart' ;
2222import 'package:vibration/vibration.dart' ;
23+ import 'package:sensors_plus/sensors_plus.dart' ;
2324
2425import '../../home/controllers/home_controller.dart' ;
2526
@@ -33,6 +34,7 @@ class AlarmControlController extends GetxController {
3334 RxInt minutes = 1. obs;
3435 RxInt seconds = 0. obs;
3536 RxBool showButton = false .obs;
37+ StreamSubscription ? _sensorSubscription;
3638 HomeController homeController = Get .find <HomeController >();
3739 SettingsController settingsController = Get .find <SettingsController >();
3840 RxBool get is24HourFormat => settingsController.is24HrsEnabled;
@@ -47,15 +49,18 @@ class AlarmControlController extends GetxController {
4749 late Timer guardianTimer;
4850 RxInt guardianCoundown = 120. obs;
4951
52+
53+
54+
5055 getNextAlarm () async {
5156 UserModel ? _userModel = await SecureStorageProvider ().retrieveUserModel ();
5257 AlarmModel _alarmRecord = homeController.genFakeAlarmModel ();
5358 AlarmModel isarLatestAlarm =
54- await IsarDb .getLatestAlarm (_alarmRecord, true );
59+ await IsarDb .getLatestAlarm (_alarmRecord, true );
5560 AlarmModel firestoreLatestAlarm =
56- await FirestoreDb .getLatestAlarm (_userModel, _alarmRecord, true );
61+ await FirestoreDb .getLatestAlarm (_userModel, _alarmRecord, true );
5762 AlarmModel latestAlarm =
58- Utils .getFirstScheduledAlarm (isarLatestAlarm, firestoreLatestAlarm);
63+ Utils .getFirstScheduledAlarm (isarLatestAlarm, firestoreLatestAlarm);
5964 debugPrint ('LATEST : ${latestAlarm .alarmTime }' );
6065
6166 return latestAlarm;
@@ -81,8 +86,8 @@ class AlarmControlController extends GetxController {
8186 timer.cancel ();
8287 vibrationTimer =
8388 Timer .periodic (const Duration (milliseconds: 3500 ), (Timer timer) {
84- Vibration .vibrate (pattern: [500 , 3000 ]);
85- });
89+ Vibration .vibrate (pattern: [500 , 3000 ]);
90+ });
8691
8792 AudioUtils .playAlarm (alarmRecord: currentlyRingingAlarm.value);
8893
@@ -121,7 +126,7 @@ class AlarmControlController extends GetxController {
121126
122127 double vol = currentlyRingingAlarm.value.volMin / 10.0 ;
123128 double diff = (currentlyRingingAlarm.value.volMax -
124- currentlyRingingAlarm.value.volMin) /
129+ currentlyRingingAlarm.value.volMin) /
125130 10.0 ;
126131 int len = currentlyRingingAlarm.value.gradient * 1000 ;
127132 double steps = (diff / 0.01 ).abs ();
@@ -153,10 +158,21 @@ class AlarmControlController extends GetxController {
153158 }
154159 });
155160 }
161+ void startListeningToFlip () {
162+ _sensorSubscription = accelerometerEvents.listen ((event) {
163+ if (event.z < - 8 ) { // Device is flipped (screen down)
164+ if (! isSnoozing.value && settingsController.isFlipToSnooze.value == true ) {
165+ startSnooze ();
166+ }
167+ }
168+ });
169+ }
156170
157171 @override
158172 void onInit () async {
159173 super .onInit ();
174+ startListeningToFlip ();
175+
160176 currentlyRingingAlarm.value = Get .arguments;
161177 print ('hwyooo ${currentlyRingingAlarm .value .isGuardian }' );
162178 IsarDb ()
@@ -168,14 +184,16 @@ class AlarmControlController extends GetxController {
168184 currentlyRingingAlarm.value.isCall
169185 ? Utils .dialNumber (currentlyRingingAlarm.value.guardian)
170186 : Utils .sendSMS (currentlyRingingAlarm.value.guardian,
171- "Your Friend is not waking up \n - Ultimate Alarm Clock" );
187+ "Your Friend is not waking up \n - Ultimate Alarm Clock" );
172188 timer.cancel ();
173189 } else {
174190 guardianCoundown.value = guardianCoundown.value - 1 ;
175191 }
176192 });
177193 }
178194
195+
196+
179197 showButton.value = true ;
180198 initialVolume = await FlutterVolumeController .getVolume (
181199 stream: AudioStream .alarm,
@@ -197,8 +215,8 @@ class AlarmControlController extends GetxController {
197215 }
198216 vibrationTimer =
199217 Timer .periodic (const Duration (milliseconds: 3500 ), (Timer timer) {
200- Vibration .vibrate (pattern: [500 , 3000 ]);
201- });
218+ Vibration .vibrate (pattern: [500 , 3000 ]);
219+ });
202220
203221 // Preventing app from being minimized!
204222 _subscription = FGBGEvents .stream.listen ((event) {
@@ -261,16 +279,16 @@ class AlarmControlController extends GetxController {
261279 // Finding the next alarm to ring
262280 AlarmModel latestAlarm = await getNextAlarm ();
263281 TimeOfDay latestAlarmTimeOfDay =
264- Utils .stringToTimeOfDay (latestAlarm.alarmTime);
282+ Utils .stringToTimeOfDay (latestAlarm.alarmTime);
265283
266284 // }
267285 // This condition will never satisfy because this will only
268286 // occur if fake model is returned as latest alarm
269287 if (latestAlarm.isEnabled == false ) {
270288 debugPrint (
271289 'STOPPED IF CONDITION with latest = '
272- '${latestAlarmTimeOfDay .toString ()} and '
273- 'current = ${currentTime .toString ()}' ,
290+ '${latestAlarmTimeOfDay .toString ()} and '
291+ 'current = ${currentTime .toString ()}' ,
274292 );
275293
276294 await alarmChannel.invokeMethod ('cancelAllScheduledAlarms' );
@@ -305,7 +323,10 @@ class AlarmControlController extends GetxController {
305323 initialVolume,
306324 stream: AudioStream .alarm,
307325 );
326+
327+
308328 _subscription.cancel ();
309329 _currentTimeTimer? .cancel ();
330+ _sensorSubscription? .cancel ();
310331 }
311332}
0 commit comments