Skip to content

Fix flip-to-snooze functionality to handle initial device orientation #780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 87 additions & 6 deletions lib/app/modules/alarmRing/controllers/alarm_ring_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class AlarmControlController extends GetxController {
late double initialVolume;
late Timer guardianTimer;
RxInt guardianCoundown = 120.obs;

bool wasFlipped = false;

RxBool isPreviewMode = false.obs;

getNextAlarm() async {
Expand Down Expand Up @@ -160,15 +163,89 @@ class AlarmControlController extends GetxController {
}
});
}
void startListeningToFlip() {
_sensorSubscription = accelerometerEvents.listen((event) {
if (event.z < -8) { // Device is flipped (screen down)
void startListeningToFlip() {
// Track initial device orientation to prevent false triggers
bool initialOrientationChecked = false;
bool initiallyFaceDown = false;

_sensorSubscription = accelerometerEvents.listen((event) {
// First, determine the initial orientation of the device
if (!initialOrientationChecked) {
initiallyFaceDown = event.z < -8;
initialOrientationChecked = true;

return; // Wait for next reading before processing flips
}

// If device was initially face down, wait until it's face up before tracking flips
if (initiallyFaceDown && event.z > -2) {
initiallyFaceDown = false;
print("Device now face up, will start tracking flips");
}

// Only process flip events if device wasn't initially face down or has been turned face up
if (!initiallyFaceDown) {
// Device is now face down (z < -8) and wasn't previously flipped
if (event.z < -8 && !wasFlipped) {
if (!isSnoozing.value && settingsController.isFlipToSnooze.value == true) {
startSnooze();

startFlipSnooze();
wasFlipped = true;
}
}
// Device is now face up (z > -2) and was previously flipped
else if (event.z > -2 && wasFlipped) {

wasFlipped = false;
}
});
}
});
}

void startFlipSnooze() async {

Vibration.cancel();
if (vibrationTimer != null) {
vibrationTimer!.cancel();
}

// Cancel guardian timer if it's active
if (currentlyRingingAlarm.value.isGuardian) {
guardianTimer.cancel();
}

isSnoozing.value = true;

String ringtoneName = currentlyRingingAlarm.value.ringtoneName;
AudioUtils.stopAlarm(ringtoneName: ringtoneName);

// Always use 1 minute as default snooze time for flip
minutes.value = 1;
seconds.value = 0;

if (_currentTimeTimer != null && _currentTimeTimer!.isActive) {
_currentTimeTimer!.cancel();
}

_currentTimeTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
if (minutes.value == 0 && seconds.value == 0) {
timer.cancel();
vibrationTimer =
Timer.periodic(const Duration(milliseconds: 3500), (Timer timer) {
Vibration.vibrate(pattern: [500, 3000]);
});

AudioUtils.playAlarm(alarmRecord: currentlyRingingAlarm.value);
startTimer();
} else if (seconds.value == 0) {
minutes.value--;
seconds.value = 59;
} else {
seconds.value--;
}
});
}


void showQuotePopup(Quote quote) {
Get.defaultDialog(
Expand Down Expand Up @@ -235,7 +312,11 @@ class AlarmControlController extends GetxController {
@override
void onInit() async {
super.onInit();
startListeningToFlip();
// Initialize wasFlipped before starting sensor listener
wasFlipped = false;
startListeningToFlip();



// Extract alarm and preview flag from arguments
final args = Get.arguments;
Expand Down