Skip to content

Commit

Permalink
Added request notification permission popup
Browse files Browse the repository at this point in the history
  • Loading branch information
guyluz11 committed Nov 20, 2024
1 parent 12f0341 commit 1d69076
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/domain/controllers/preferences_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ enum PreferenceKeys {
tipType,
sleepPermissionGranted,
finishedIntroduction,
notificationPermissionRequested,
}
1 change: 1 addition & 0 deletions lib/presentation/molecules/molecules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export 'package:infinite_horizons/presentation/molecules/alert_dialog_molecule.d
export 'package:infinite_horizons/presentation/molecules/back_to_home_alert_dialog_molecule.dart';
export 'package:infinite_horizons/presentation/molecules/bottom_navigation_bar_home_molecule.dart';
export 'package:infinite_horizons/presentation/molecules/energy_selection_molecule.dart';
export 'package:infinite_horizons/presentation/molecules/notification_permission_alert_dialog_molecule.dart';
export 'package:infinite_horizons/presentation/molecules/page_enclosure_molecule.dart';
export 'package:infinite_horizons/presentation/molecules/pdf_viewer_molecule.dart';
export 'package:infinite_horizons/presentation/molecules/progress_indicator_molecule.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:infinite_horizons/domain/controllers/controllers.dart';
import 'package:infinite_horizons/presentation/atoms/atoms.dart';
import 'package:infinite_horizons/presentation/molecules/molecules.dart';

void requestNotificationPermissionPopup(BuildContext context) {
openAlertDialog(
context,
_NotificationPopup(),
);
}

class _NotificationPopup extends StatefulWidget {
@override
State<_NotificationPopup> createState() => _NotificationPopupState();
}

class _NotificationPopupState extends State<_NotificationPopup> {
bool generalNotifications = false;
bool isLoading = true;

@override
void initState() {
super.initState();
checkPermissionsState();
}

Future checkPermissionsState() async {
generalNotifications = !Platform.isAndroid &&
await NotificationsController.instance.isPermissionGranted();

setState(() {
isLoading = false;
});
}

@override
Widget build(BuildContext context) {
return SizedBox(
height: 280,
child: PageEnclosureMolecule(
title: 'Notification Permission',
subTitle:
'Please approve the permission inorder for the app to work properly',
child: CardAtom(
child: isLoading
? const ProgressIndicatorAtom(totalDuration: Duration(seconds: 3))
: ToggleSwitchMolecule(
text: 'Notification Permission',
description:
'Get notify about timer status when the app is in the background',
offIcon: Icons.notifications_off_outlined,
onIcon: Icons.notifications_outlined,
onChange: (value) async {
await NotificationsController.instance.generalPermission();
await NotificationsController.instance
.preciseAlarmPermission();
},
initialValue: generalNotifications,
lockOnToggleOn: true,
),
),
),
);
}
}
20 changes: 20 additions & 0 deletions lib/presentation/pages/activity_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,30 @@ class _ActivityPageState extends State<ActivityPage>
PlayerController.instance.play(SoundType.startSession);
VibrationController.instance.vibrate(VibrationType.heavy);
TimerStateManager.iterateOverTimerStates();
notificationPermissionPopup();
}

AppLifecycleState currentAppState = AppLifecycleState.resumed;

Future notificationPermissionPopup() async {
final bool notificationPermissionRequested = PreferencesController.instance
.getBool(PreferenceKeys.notificationPermissionRequested) ??
false;
final bool notificationGranted =
await NotificationsController.instance.isPermissionGranted();
if (notificationPermissionRequested || notificationGranted) {
return;
}
await Future.delayed(const Duration(seconds: 5));

if (!mounted) {
return;
}
PreferencesController.instance
.setBool(PreferenceKeys.notificationPermissionRequested, true);
requestNotificationPermissionPopup(context);
}

Future<bool> onWillPop(bool didPop, dynamic result) async {
if (_currentTabNum == 0) {
backToHomePopup(context);
Expand Down

0 comments on commit 1d69076

Please sign in to comment.