Skip to content

Commit

Permalink
Change app move to background behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
AhsanSarwar45 committed May 11, 2024
1 parent 785a1a5 commit 1647baa
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 47 deletions.
18 changes: 12 additions & 6 deletions lib/alarm/logic/alarm_isolate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:clock_app/system/logic/initialize_isolate.dart';
import 'package:clock_app/timer/types/time_duration.dart';
import 'package:clock_app/timer/types/timer.dart';
import 'package:flutter/foundation.dart';
import 'package:get_storage/get_storage.dart';
import 'package:clock_app/alarm/logic/schedule_alarm.dart';
import 'package:clock_app/alarm/logic/update_alarms.dart';
import 'package:clock_app/alarm/types/alarm.dart';
Expand All @@ -19,6 +18,8 @@ import 'package:clock_app/alarm/utils/alarm_id.dart';
import 'package:clock_app/common/utils/time_of_day.dart';
import 'package:clock_app/timer/logic/update_timers.dart';
import 'package:clock_app/timer/utils/timer_id.dart';
import 'package:flutter/services.dart';
import 'package:receive_intent/receive_intent.dart';

const String stopAlarmPortName = "stopAlarmPort";
const String updatePortName = "updatePort";
Expand Down Expand Up @@ -51,6 +52,16 @@ void triggerScheduledNotification(int scheduleId, Json params) async {
stopScheduledNotification(message);
});

try {
final receivedIntent = await ReceiveIntent.getInitialIntent();
print("reeeeeeeeeeeeeeeeeeeeeeeeeee ${receivedIntent}");
// Validate receivedIntent and warn the user, if it is not correct,
// but keep in mind it could be `null` or "empty"(`receivedIntent.isNull`).
} on PlatformException {
// Handle exception
}


if (notificationType == ScheduledNotificationType.alarm) {
triggerAlarm(scheduleId, params);
} else if (notificationType == ScheduledNotificationType.timer) {
Expand Down Expand Up @@ -104,8 +115,6 @@ void triggerAlarm(int scheduleId, Json params) async {
return;
}

GetStorage().write("fullScreenNotificationRecentlyShown", true);

// Pause any currently ringing timers. We will continue them after the alarm
// is dismissed
if (RingingManager.isTimerRinging) {
Expand Down Expand Up @@ -168,9 +177,6 @@ void triggerTimer(int scheduleId, Json params) async {

await updateTimers("triggerTimer(): Updating all timers on trigger");

// Notify the front-end to update the timers
GetStorage().write("fullScreenNotificationRecentlyShown", true);

// Pause any currently ringing alarms. We will continue them after the timer
// is dismissed
if (RingingManager.isAlarmRinging) {
Expand Down
1 change: 1 addition & 0 deletions lib/navigation/data/fullscreen_intent.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const fullscreenIntentKey = "fullscreen_intent";
12 changes: 6 additions & 6 deletions lib/navigation/types/app_visibility.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'dart:async';

import 'package:clock_app/common/utils/list_storage.dart';
import 'package:clock_app/navigation/data/fullscreen_intent.dart';
import 'package:flutter_fgbg/flutter_fgbg.dart';
import 'package:get_storage/get_storage.dart';

class AppVisibility {
static StreamSubscription<FGBGType>? subscription;
Expand All @@ -15,12 +16,11 @@ class AppVisibility {
}

static void initialize() {
if (GetStorage().read<bool>("fullScreenNotificationRecentlyShown") ==
true) {
GetStorage().write("fullScreenNotificationRecentlyShown", false);
} else {
// if (loadTextFileSync(fullscreenIntentKey) == "true") {
// saveTextFile(fullscreenIntentKey, "false");
// } else {
setState(FGBGType.foreground);
}
// }

subscription = FGBGEvents.stream.listen((event) {
setState(event);
Expand Down
6 changes: 0 additions & 6 deletions lib/notifications/logic/notification_callbacks.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:clock_app/alarm/logic/alarm_reminder_notifications.dart';
import 'package:clock_app/alarm/logic/update_alarms.dart';
import 'package:clock_app/alarm/types/alarm.dart';
import 'package:clock_app/alarm/utils/alarm_id.dart';
import 'package:clock_app/common/types/notification_type.dart';
import 'package:clock_app/notifications/data/notification_channel.dart';
import 'package:clock_app/notifications/types/fullscreen_notification_data.dart';
import 'package:clock_app/notifications/types/fullscreen_notification_manager.dart';
import 'package:clock_app/settings/data/settings_schema.dart';
import 'package:clock_app/stopwatch/logic/update_stopwatch.dart';
import 'package:clock_app/system/logic/initialize_isolate.dart';
import 'package:clock_app/timer/logic/update_timers.dart';
Expand Down
40 changes: 26 additions & 14 deletions lib/notifications/types/fullscreen_notification_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ import 'package:clock_app/alarm/logic/alarm_isolate.dart';
import 'package:clock_app/alarm/logic/update_alarms.dart';
import 'package:clock_app/app.dart';
import 'package:clock_app/common/types/notification_type.dart';
import 'package:clock_app/common/utils/list_storage.dart';
import 'package:clock_app/navigation/data/fullscreen_intent.dart';
import 'package:clock_app/notifications/data/notification_channel.dart';
import 'package:clock_app/alarm/logic/schedule_alarm.dart';
import 'package:clock_app/navigation/types/app_visibility.dart';
import 'package:clock_app/navigation/types/routes.dart';
import 'package:clock_app/notifications/types/fullscreen_notification_data.dart';
import 'package:flutter/services.dart';
import 'package:flutter_fgbg/flutter_fgbg.dart';
import 'package:flutter_show_when_locked/flutter_show_when_locked.dart';
import 'package:get_storage/get_storage.dart';
import 'package:move_to_background/move_to_background.dart';
import 'package:receive_intent/receive_intent.dart';

class AlarmNotificationManager {
static const String _snoozeActionKey = "snooze";
static const String _dismissActionKey = "dismiss";

static FGBGType _appVisibilityWhenCreated = FGBGType.foreground;
static FGBGType appVisibilityWhenCreated = FGBGType.foreground;

static void showFullScreenNotification({
required ScheduledNotificationType type,
Expand Down Expand Up @@ -98,22 +102,29 @@ class AlarmNotificationManager {
static Future<void> closeNotification(ScheduledNotificationType type) async {
await removeNotification(type);

await GetStorage.init();
// await LockScreenFlagManager.clearLockScreenFlags();
await FlutterShowWhenLocked().hide();

// If we were on the alarm screen, pop it off the stack. Sometimes the system
// decides to show a heads up notification instead of a full screen one, so
// we can't always pop the top screen.
Routes.popIf(alarmNotificationData[type]?.route);

// If notification was created while app was in background, move app back
// to background when we close the notification
if (_appVisibilityWhenCreated == FGBGType.background &&

if (appVisibilityWhenCreated == FGBGType.background &&
AppVisibility.state == FGBGType.foreground) {
MoveToBackground.moveTaskToBack();
}
GetStorage().write("fullScreenNotificationRecentlyShown", false);

// try {
// final receivedIntent = await ReceiveIntent.getInitialIntent();
// print("reeeeeeeeeeeeeeeeeeeeeeeeeee ${receivedIntent}");
// // Validate receivedIntent and warn the user, if it is not correct,
// // but keep in mind it could be `null` or "empty"(`receivedIntent.isNull`).
// } on PlatformException {
// // Handle exception
// }

// If we were on the alarm screen, pop it off the stack. Sometimes the system
// decides to show a heads up notification instead of a full screen one, so
// we can't always pop the top screen.
Routes.popIf(alarmNotificationData[type]?.route);
}

static Future<void> snoozeAlarm(
Expand Down Expand Up @@ -149,7 +160,6 @@ class AlarmNotificationManager {
break;
}
await closeNotification(type);

}

static Future<void> stopAlarm(int scheduleId, ScheduledNotificationType type,
Expand All @@ -162,8 +172,7 @@ class AlarmNotificationManager {
}

static void handleNotificationCreated(ReceivedNotification notification) {
_appVisibilityWhenCreated = AppVisibility.state;
GetStorage().write("fullScreenNotificationRecentlyShown", false);
// _appVisibilityWhenCreated = AppVisibility.state;
}

static Future<void> openNotificationScreen(
Expand Down Expand Up @@ -197,7 +206,7 @@ class AlarmNotificationManager {
(json.decode((payload['scheduleIds'])!) as List<dynamic>).cast<int>();
if (scheduleIds.isEmpty) return;

if (tasksRequired && dismissType != AlarmDismissType.snooze){
if (tasksRequired && dismissType != AlarmDismissType.snooze) {
await openNotificationScreen(data, scheduleIds,
tasksOnly: true, dismissType: dismissType);
} else {
Expand All @@ -223,7 +232,10 @@ class AlarmNotificationManager {
break;

default:
/* print("ahsan is the besttttttttttttttttttttt ${AppVisibility.state}"); */

await openNotificationScreen(data, scheduleIds);

break;
}
}
Expand Down
19 changes: 4 additions & 15 deletions lib/settings/logic/initialize_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,12 @@ import 'package:clock_app/timer/data/default_timer_presets.dart';
import 'package:clock_app/timer/logic/update_timers.dart';
import 'package:clock_app/timer/types/timer.dart';
import 'package:clock_app/timer/types/timer_preset.dart';
import 'package:clock_app/widgets/logic/update_widgets.dart';
import 'package:flutter/foundation.dart';
import 'package:get_storage/get_storage.dart';


Future<void> _clearSettings() async {
// List<ClockTimer> timers = await loadList<ClockTimer>('timers');
// List<Alarm> alarms = await loadList<Alarm>('alarms');
// We need to remove all scheduled alarms and timers before clearing the data
// Otherwise, there would be no way to remove them in the future

// for (var timer in timers) {
// timer.reset();
// }
await cancelAllAlarms();
await cancelAllTimers();
await GetStorage().erase();
Expand All @@ -60,13 +52,10 @@ Future<void> initializeStorage([bool clearSettingsOnDebug = true]) async {
// Comment this out after the preferences are cleared
if (kDebugMode && clearSettingsOnDebug) await _clearSettings();

bool? firstLaunch = GetStorage().read('first_launch');
if (firstLaunch == null) {
// This is used to show alarm and timer edit animations
GetStorage().write('first_alarm_created', false);
GetStorage().write('first_timer_created', false);
}

// bool? firstLaunch = GetStorage().read('first_launch');
// if (firstLaunch == null) {
// }
//
await initList<Alarm>("alarms", []);
await initList<Tag>("tags", defaultTags);
await initList<AlarmEvent>("alarm_events", []);
Expand Down
6 changes: 6 additions & 0 deletions lib/system/logic/handle_intents.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:clock_app/alarm/types/alarm.dart';
import 'package:clock_app/alarm/types/schedules/weekly_alarm_schedule.dart';
import 'package:clock_app/common/types/notification_type.dart';
import 'package:clock_app/common/utils/list_storage.dart';
import 'package:clock_app/navigation/types/app_visibility.dart';
import 'package:clock_app/notifications/types/fullscreen_notification_manager.dart';
import 'package:clock_app/settings/types/listener_manager.dart';
import 'package:flutter/material.dart' hide Intent;
import 'package:receive_intent/receive_intent.dart';
Expand Down Expand Up @@ -92,6 +94,10 @@ void handleIntent(Intent? receivedIntent, BuildContext context, Function(Alarm)
break;
case "android.intent.action.VIEW_TIMERS":
break;
case "SELECT_NOTIFICATION":
AlarmNotificationManager.appVisibilityWhenCreated = AppVisibility.state;
// print("************************************** ${AppVisibility.state}");
break;
default:
break;
}
Expand Down

0 comments on commit 1647baa

Please sign in to comment.