@@ -20,6 +20,7 @@ import 'package:ultimate_alarm_clock/app/utils/audio_utils.dart';
20
20
21
21
import 'package:ultimate_alarm_clock/app/utils/utils.dart' ;
22
22
import 'package:vibration/vibration.dart' ;
23
+ import 'package:sensors_plus/sensors_plus.dart' ;
23
24
24
25
import '../../home/controllers/home_controller.dart' ;
25
26
@@ -33,6 +34,7 @@ class AlarmControlController extends GetxController {
33
34
RxInt minutes = 1. obs;
34
35
RxInt seconds = 0. obs;
35
36
RxBool showButton = false .obs;
37
+ StreamSubscription ? _sensorSubscription;
36
38
HomeController homeController = Get .find <HomeController >();
37
39
SettingsController settingsController = Get .find <SettingsController >();
38
40
RxBool get is24HourFormat => settingsController.is24HrsEnabled;
@@ -47,15 +49,18 @@ class AlarmControlController extends GetxController {
47
49
late Timer guardianTimer;
48
50
RxInt guardianCoundown = 120. obs;
49
51
52
+
53
+
54
+
50
55
getNextAlarm () async {
51
56
UserModel ? _userModel = await SecureStorageProvider ().retrieveUserModel ();
52
57
AlarmModel _alarmRecord = homeController.genFakeAlarmModel ();
53
58
AlarmModel isarLatestAlarm =
54
- await IsarDb .getLatestAlarm (_alarmRecord, true );
59
+ await IsarDb .getLatestAlarm (_alarmRecord, true );
55
60
AlarmModel firestoreLatestAlarm =
56
- await FirestoreDb .getLatestAlarm (_userModel, _alarmRecord, true );
61
+ await FirestoreDb .getLatestAlarm (_userModel, _alarmRecord, true );
57
62
AlarmModel latestAlarm =
58
- Utils .getFirstScheduledAlarm (isarLatestAlarm, firestoreLatestAlarm);
63
+ Utils .getFirstScheduledAlarm (isarLatestAlarm, firestoreLatestAlarm);
59
64
debugPrint ('LATEST : ${latestAlarm .alarmTime }' );
60
65
61
66
return latestAlarm;
@@ -81,8 +86,8 @@ class AlarmControlController extends GetxController {
81
86
timer.cancel ();
82
87
vibrationTimer =
83
88
Timer .periodic (const Duration (milliseconds: 3500 ), (Timer timer) {
84
- Vibration .vibrate (pattern: [500 , 3000 ]);
85
- });
89
+ Vibration .vibrate (pattern: [500 , 3000 ]);
90
+ });
86
91
87
92
AudioUtils .playAlarm (alarmRecord: currentlyRingingAlarm.value);
88
93
@@ -121,7 +126,7 @@ class AlarmControlController extends GetxController {
121
126
122
127
double vol = currentlyRingingAlarm.value.volMin / 10.0 ;
123
128
double diff = (currentlyRingingAlarm.value.volMax -
124
- currentlyRingingAlarm.value.volMin) /
129
+ currentlyRingingAlarm.value.volMin) /
125
130
10.0 ;
126
131
int len = currentlyRingingAlarm.value.gradient * 1000 ;
127
132
double steps = (diff / 0.01 ).abs ();
@@ -153,10 +158,21 @@ class AlarmControlController extends GetxController {
153
158
}
154
159
});
155
160
}
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
+ }
156
170
157
171
@override
158
172
void onInit () async {
159
173
super .onInit ();
174
+ startListeningToFlip ();
175
+
160
176
currentlyRingingAlarm.value = Get .arguments;
161
177
print ('hwyooo ${currentlyRingingAlarm .value .isGuardian }' );
162
178
IsarDb ()
@@ -168,14 +184,16 @@ class AlarmControlController extends GetxController {
168
184
currentlyRingingAlarm.value.isCall
169
185
? Utils .dialNumber (currentlyRingingAlarm.value.guardian)
170
186
: 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" );
172
188
timer.cancel ();
173
189
} else {
174
190
guardianCoundown.value = guardianCoundown.value - 1 ;
175
191
}
176
192
});
177
193
}
178
194
195
+
196
+
179
197
showButton.value = true ;
180
198
initialVolume = await FlutterVolumeController .getVolume (
181
199
stream: AudioStream .alarm,
@@ -197,8 +215,8 @@ class AlarmControlController extends GetxController {
197
215
}
198
216
vibrationTimer =
199
217
Timer .periodic (const Duration (milliseconds: 3500 ), (Timer timer) {
200
- Vibration .vibrate (pattern: [500 , 3000 ]);
201
- });
218
+ Vibration .vibrate (pattern: [500 , 3000 ]);
219
+ });
202
220
203
221
// Preventing app from being minimized!
204
222
_subscription = FGBGEvents .stream.listen ((event) {
@@ -261,16 +279,16 @@ class AlarmControlController extends GetxController {
261
279
// Finding the next alarm to ring
262
280
AlarmModel latestAlarm = await getNextAlarm ();
263
281
TimeOfDay latestAlarmTimeOfDay =
264
- Utils .stringToTimeOfDay (latestAlarm.alarmTime);
282
+ Utils .stringToTimeOfDay (latestAlarm.alarmTime);
265
283
266
284
// }
267
285
// This condition will never satisfy because this will only
268
286
// occur if fake model is returned as latest alarm
269
287
if (latestAlarm.isEnabled == false ) {
270
288
debugPrint (
271
289
'STOPPED IF CONDITION with latest = '
272
- '${latestAlarmTimeOfDay .toString ()} and '
273
- 'current = ${currentTime .toString ()}' ,
290
+ '${latestAlarmTimeOfDay .toString ()} and '
291
+ 'current = ${currentTime .toString ()}' ,
274
292
);
275
293
276
294
await alarmChannel.invokeMethod ('cancelAllScheduledAlarms' );
@@ -305,7 +323,10 @@ class AlarmControlController extends GetxController {
305
323
initialVolume,
306
324
stream: AudioStream .alarm,
307
325
);
326
+
327
+
308
328
_subscription.cancel ();
309
329
_currentTimeTimer? .cancel ();
330
+ _sensorSubscription? .cancel ();
310
331
}
311
332
}
0 commit comments