Skip to content

Commit

Permalink
Merge pull request #72 from IamMuuo/dev
Browse files Browse the repository at this point in the history
Notifications
  • Loading branch information
IamMuuo authored Apr 19, 2024
2 parents 6afcb88 + bc735c4 commit a371310
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 295 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ 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 5
versionCode 6
versionName "2.0.0"
}
signingConfigs {
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>
15 changes: 3 additions & 12 deletions lib/controllers/login_controller.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import 'package:academia/constants/common.dart';
import 'package:academia/constants/settings.dart';
import 'package:academia/notifications/notification_service.dart';
import 'package:academia/pages/home_page.dart';
import 'package:academia/controllers/notifications_controller.dart';
import 'package:academia/exports/barrel.dart';
import 'package:awesome_notifications/awesome_notifications.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,13 +71,6 @@ class LoginController extends GetxController {
if (!appDB.containsKey("settings")) {
await appDB.put("settings", settings);
}

// Say hello to the user
NotificationService().showNotification(
title: "Hello,👋👋",
body: "With academia, your fun has just began!",
id: notifications["greetings"]!,
);
// Navigate to home page
Get.off(
const HomePage(),
Expand Down
58 changes: 29 additions & 29 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 All @@ -9,14 +11,7 @@ class NotificationsController extends GetxController {
@override
void onInit() async {
super.onInit();
if (Platform.isIOS || Platform.isAndroid) {
NotificationService().scheduleNotification(
0,
"Happy Birthday",
"We wish you a happy birthday and prosperous new year",
DateTime.now().add(const Duration(seconds: 10)),
);
}
if (Platform.isIOS || Platform.isAndroid) {}

notifications = await magnet.fetchNotifications();
hasNotifications.value = notifications.isNotEmpty;
Expand All @@ -42,28 +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);
}

// If the scheduled date is in the past, add a week to it
if (now.isAfter(scheduledDate)) {
scheduledDate = scheduledDate.add(const Duration(days: 7));
}
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,
),
);
}

// Schedule the notification
// NotificationService().scheduleNotification(
// notifications["course"]!,
// "Class reminder⏱️⏱️",
// "$className is about to begin",
// scheduledDate,
// );
/// 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();
}
}
3 changes: 1 addition & 2 deletions lib/exports/barrel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export 'package:academia/models/courses.dart';
export 'package:academia/models/schedule.dart';
export 'package:academia/models/tasks.dart';
export 'package:academia/models/user.dart';
export 'package:academia/notifications/notification_service.dart';
export 'package:academia/pages/home_page.dart';
export 'package:academia/pages/intro_page.dart';
export 'package:academia/themes/theme.dart';
Expand All @@ -29,7 +28,6 @@ export 'package:academia/controllers/courses_page_controller.dart';
export 'package:academia/controllers/dashboard_controller.dart';
export 'package:academia/tools/attendance_page/attendance_page.dart';
export 'package:flutter_carousel_widget/flutter_carousel_widget.dart';
export 'package:connectivity_plus/connectivity_plus.dart';
export 'package:academia/tools/exam_timetable/exams_timetable_page.dart';
export 'package:academia/tools/fees/fees_page.dart';
export 'package:academia/pages/pdf_viewer.dart';
Expand Down Expand Up @@ -58,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';
88 changes: 76 additions & 12 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:academia/services/services.dart';
import 'package:get/get.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await NotificationService().initNotifications();
// Init flutter

/// 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());
Hive.registerAdapter(ScheduleAdapter());
Expand All @@ -13,21 +32,66 @@ void main() async {
Hive.registerAdapter(ExamAdapter());
appDB = await Hive.openBox(dbName);

// Init settings controller
final userController = Get.put(UserController());
Get.put(SettingsController());
//Get.put(NotificationsController());

// Init TasksManager controller
Get.put(TaskManagerController());
// Initialize the various controllers
// once you append the controller onto the list don't inject it again
// since it will be placed in the context
ControllerService().injectMultipleControllers(
<GetxController>[
SettingsController(),
NotificationsController(),
TaskManagerController(),
],
);

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();
}
}
140 changes: 0 additions & 140 deletions lib/notifications/notification_service.dart

This file was deleted.

Loading

0 comments on commit a371310

Please sign in to comment.