Skip to content

Commit

Permalink
feature: added notifications feature
Browse files Browse the repository at this point in the history
  • Loading branch information
IamMuuo committed Apr 19, 2024
1 parent 219b711 commit a5cb372
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 27 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ android {
applicationId "com.dita.academia"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion flutter.minSdkVersion
minSdkVersion 21
targetSdkVersion flutter.targetSdkVersion
versionCode 6
versionName "2.0.0"
Expand Down
2 changes: 2 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@
android:name="flutterEmbedding"
android:value="2" />
</application>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

</manifest>
13 changes: 9 additions & 4 deletions lib/controllers/login_controller.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'package:academia/constants/common.dart';
import 'package:academia/constants/settings.dart';
import 'package:academia/pages/home_page.dart';
import 'package:academia/controllers/notifications_controller.dart';
import 'package:academia/exports/barrel.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:academia/controllers/controllers.dart';

class LoginController extends GetxController {
final TextEditingController usernameController = TextEditingController();
Expand Down Expand Up @@ -73,6 +71,13 @@ class LoginController extends GetxController {
await appDB.put("settings", settings);
}

final notificationsController = Get.find<NotificationsController>();
notificationsController.createInstantNotification(
"Hi ${userController.user.value?.name?.split(' ')[0].toString()}",
"Welcome to academia we have everything setup for you",
layout: NotificationLayout.Messaging,
);

// Navigate to home page
Get.off(
const HomePage(),
Expand Down
41 changes: 28 additions & 13 deletions lib/controllers/notifications_controller.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import "package:academia/exports/barrel.dart";
import 'package:get/get.dart';

Expand Down Expand Up @@ -35,20 +37,33 @@ class NotificationsController extends GetxController {
return [];
}

void scheduleNotificationForWeeklyClass(
String className, TimeOfDay classTime, int dayOfWeek) {
DateTime now = DateTime.now();
DateTime scheduledDate = DateTime(
now.year,
now.month,
now.day + (dayOfWeek - now.weekday + 7) % 7,
classTime.hour,
classTime.minute,
int _generateRandomIntId({int min = 100000, int max = 999999}) {
final random = Random();
return min + random.nextInt(max - min);
}

Future<void> createInstantNotification(
String title,
String content, {
NotificationLayout layout = NotificationLayout.BigPicture,
}) async {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: _generateRandomIntId(),
channelKey: 'basic_channel',
title: "${Emojis.building_school} $title",
body: content,
bigPicture: "asset://assets/images/bot_wave.png",
notificationLayout: layout,
),
);
}

// If the scheduled date is in the past, add a week to it
if (now.isAfter(scheduledDate)) {
scheduledDate = scheduledDate.add(const Duration(days: 7));
}
/// Cancel all scheduled schedules
///
/// Waring beware of cancelling all schedules it might result
/// to deleting all schedules which might be unintended
Future<void> cancelScheduledNotifications() async {
await AwesomeNotifications().cancelAllSchedules();
}
}
1 change: 1 addition & 0 deletions lib/exports/barrel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ export 'package:magnet/src/magnet_exams.dart';
export 'package:academia/tools/time_line/time_line_page.dart';
export 'package:academia/tools/stories/notifications_story_page.dart';
export 'package:academia/controllers/controllers.dart';
export 'package:awesome_notifications/awesome_notifications.dart';
72 changes: 64 additions & 8 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import 'package:academia/exports/barrel.dart';
import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:academia/services/services.dart';
import 'package:get/get.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();

/// Initialize the flutter notifications plugin
AwesomeNotifications().initialize(
'resource://drawable/app_icon',
[
NotificationChannel(
channelKey: "basic_channel",
channelName: "Basic Notifications",
channelDescription: "All Academia Notification Channel",
importance: NotificationImportance.High,
enableLights: true,
defaultColor: Colors.blueGrey,
playSound: true,
enableVibration: true,
channelShowBadge: true,
)
],
);

/// Init the type adapters for storage
await Hive.initFlutter();
Hive.registerAdapter(UserAdapter());
Expand All @@ -25,18 +44,55 @@ void main() async {
],
);

/// The user controller is injected seperately since its used in this file
/// and a reference to it is needed to check for login
final userController =
ControllerService().injectController(UserController()) as UserController;

ControllerService().injectController(UserController());
runApp(
GetMaterialApp(
home: userController.isLoggedIn.value
? const HomePage()
: const IntroPage(),
home: const Academia(),
theme: lightModeTheme,
darkTheme: darkModeTheme,
),
);
}

class Academia extends StatelessWidget {
const Academia({super.key});

@override
Widget build(BuildContext context) {
AwesomeNotifications().isNotificationAllowed().then((value) {
if ((!value) && (Platform.isAndroid || Platform.isIOS)) {
showDialog(
barrierDismissible: false,
context: context,
builder: (context) {
return AlertDialog(
title: const Text("Allow Notifications"),
content: const Text(
"Academia would like to send you notifications about classes and your school work",
),
actions: [
FilledButton(
onPressed: () {
AwesomeNotifications()
.requestPermissionToSendNotifications()
.then((value) => Navigator.pop(context));
},
child: const Text("Allow"),
),
OutlinedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text("No"),
),
],
);
});
}
});
final UserController userController = Get.find<UserController>();
return userController.isLoggedIn.value
? const HomePage()
: const IntroPage();
}
}
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ dependencies:
ionicons: ^0.2.2
google_fonts: ^6.2.1
flutter_timer_countdown: ^1.0.7
flutter_native_timezone: ^2.0.0
awesome_notifications_core: ^0.9.3
awesome_notifications: ^0.8.2



Expand Down

0 comments on commit a5cb372

Please sign in to comment.