Skip to content

Commit

Permalink
Added notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
xMadKing committed Dec 25, 2023
1 parent 80de341 commit 9929924
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 27 deletions.
6 changes: 3 additions & 3 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if (flutterVersionName == null) {

android {
namespace "com.example.smartspend"
compileSdkVersion flutter.compileSdkVersion
compileSdkVersion 34
ndkVersion flutter.ndkVersion

compileOptions {
Expand All @@ -45,8 +45,8 @@ android {
applicationId "com.example.smartspend"
// 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
targetSdkVersion flutter.targetSdkVersion
minSdkVersion 21
targetSdkVersion 34
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
2 changes: 2 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<application
android:label="smartspend"
android:name="${applicationName}"
Expand Down
32 changes: 32 additions & 0 deletions lib/backend/notifications.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:awesome_notifications/awesome_notifications.dart';

//This class is from the awesome_notifications documentation. It is how they
//suggest to use the plugin. We must define it to allows things to work, but we
//are not using anything here.

class NotificationController {

/// Use this method to detect when a new notification or a schedule is created
@pragma("vm:entry-point")
static Future <void> onNotificationCreatedMethod(ReceivedNotification receivedNotification) async {
//we dont really want to do anything
}

/// Use this method to detect every time that a new notification is displayed
@pragma("vm:entry-point")
static Future <void> onNotificationDisplayedMethod(ReceivedNotification receivedNotification) async {
//we dont really want to do anything
}

/// Use this method to detect if the user dismissed a notification
@pragma("vm:entry-point")
static Future <void> onDismissActionReceivedMethod(ReceivedAction receivedAction) async {
//we dont really want to do anything
}

/// Use this method to detect when the user taps on a notification or action button
@pragma("vm:entry-point")
static Future <void> onActionReceivedMethod(ReceivedAction receivedAction) async {
//we dont really want to do anything
}
}
56 changes: 52 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,41 @@ import 'package:smartspend/pages/onboardingpage.dart';
import 'package:smartspend/backend/wyrm/database.dart';
import 'package:smartspend/backend/user.dart';
import 'package:smartspend/backend/category.dart';
import 'package:awesome_notifications/awesome_notifications.dart';

import 'backend/notifications.dart';

void main() async {
Wyrm database = Wyrm();
User client = await initClient(database);
List<Category> categories = await initCategories(database, client);
await initCategories(database, client);
await initNotifications();
goFullscreen();
runApp(MyApp(client: client));
}

Future<void> initNotifications() async { //this code is based on the documentation from the package
AwesomeNotifications().initialize(
// set the icon to null if you want to use the default app icon
'assets/logo.png',
[
NotificationChannel(
channelGroupKey: 'smartspend_notification_group',
channelKey: 'smartspend_notification_channel',
channelName: 'Smartspend Notifications',
channelDescription: 'Notification channel for Smartspend app',
defaultColor: Color(0xFF9D50DD),
ledColor: Colors.white)
],
debug: true
);
AwesomeNotifications().isNotificationAllowed().then((isAllowed) {
if (!isAllowed) {
AwesomeNotifications().requestPermissionToSendNotifications();
}
});
}

void goFullscreen() async {
SystemChrome.setEnabledSystemUIMode(
SystemUiMode.immersive,
Expand All @@ -31,21 +57,43 @@ void goFullscreen() async {
}
}


class MyApp extends StatelessWidget {
class MyApp extends StatefulWidget {
User client;
static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

MyApp({super.key, required this.client});

@override
State<StatefulWidget> createState() => _MyApp();
}

class _MyApp extends State<MyApp> {


@override
void initState() {

// Only after at least the action method is set, the notification events are delivered
AwesomeNotifications().setListeners(
onActionReceivedMethod: NotificationController.onActionReceivedMethod,
onNotificationCreatedMethod: NotificationController.onNotificationCreatedMethod,
onNotificationDisplayedMethod: NotificationController.onNotificationDisplayedMethod,
onDismissActionReceivedMethod: NotificationController.onDismissActionReceivedMethod
);

super.initState();
}

@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
return MaterialApp(
navigatorKey: MyApp.navigatorKey,
home: BoardingPage(
client: client,
client: widget.client,
),
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
Expand Down
6 changes: 1 addition & 5 deletions lib/pages/homepage.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:smartspend/backend/category.dart';
Expand All @@ -8,7 +7,6 @@ import 'package:smartspend/widgets/categorywidget.dart';
import 'package:smartspend/pages/monthlybudgetpage.dart';
import 'package:smartspend/backend/user.dart';
import 'package:smartspend/backend/wyrm/database.dart';
import 'package:smartspend/pages/myaccountpage.dart';

class HomePage extends StatefulWidget {
const HomePage({super.key});
Expand Down Expand Up @@ -52,7 +50,6 @@ class _HomePageState extends State<HomePage>{
res.add(CategoryWidget(
name: element.categoryName,
color: Color(element.categoryColor),
bgColor: Colors.white,
number: element.currentSpending.toDouble(),
limit: element.spendingLimit.toDouble(),
));
Expand Down Expand Up @@ -209,8 +206,7 @@ class _HomePageState extends State<HomePage>{
margin: const EdgeInsets.only(right: 10),
alignment: Alignment.centerRight,
child: IconButton(
onPressed: () async {
List<Category> categories = await database.categories();
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder:
(context) => MonthlyBudgetPage(
Expand Down
78 changes: 63 additions & 15 deletions lib/widgets/categorywidget.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,74 @@
import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:flutter/material.dart';

class CategoryWidget extends StatelessWidget {

class CategoryWidget extends StatefulWidget{
final String name;
final Color color;
final double number;
final Color bgColor;
final double limit;
bool notificationSent = false;

CategoryWidget({
super.key,
required this.name,
required this.color,
required this.number,
required this.limit
});

@override
State<StatefulWidget> createState() => _CategoryWidget();

const CategoryWidget({super.key, required this.name, required this.color, required this.number,
required this.bgColor, required this.limit});
}

Color getFontColor(){
if (number > limit){
return Colors.red;
class _CategoryWidget extends State<CategoryWidget>{
static late Color bgColor;
static late Color fontColor;
static bool _loading = true;


Future<void> sendNotification() async {
if (widget.number > widget.limit && !widget.notificationSent){
widget.notificationSent = true;
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: 1,
channelKey: 'smartspend_notification_channel',
actionType: ActionType.Default,
title: 'SmartSpend: Limit Exceeded!',
body: 'The ${widget.name} budget has been exceeded! Current spending: ${widget.number}',
),
schedule: NotificationInterval(interval: 30,repeats: false)
);
}
return const Color(0xFF1E2038);
}

Future<void> initArgs() async {
await sendNotification();
setState(() {
_loading = false;
});
}

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

@override
Widget build(BuildContext context) {
Color fontColor = getFontColor();
if(_loading) {
return LinearProgressIndicator();
}
if (widget.number > widget.limit){
bgColor = Colors.red.shade400;
fontColor = Colors.white;
} else {
bgColor = Colors.white;
fontColor = Colors.black;
}
return GestureDetector(
onTap: () {
//we can maybe add future functionality here
Expand All @@ -41,17 +90,17 @@ class CategoryWidget extends StatelessWidget {
margin: const EdgeInsets.all(10),
child: Icon(
Icons.circle_rounded,
color: color,
color: widget.color,
size: 32,
)
),
Container(
margin: const EdgeInsets.only(left: 20),
alignment: Alignment.centerLeft,
child: Text(
name,
style: const TextStyle(
color: Color(0xFF1E2038),
widget.name,
style: TextStyle(
color: fontColor,
fontSize: 16,
fontWeight: FontWeight.w500,
fontFamily: 'Montserrat',
Expand All @@ -63,10 +112,9 @@ class CategoryWidget extends StatelessWidget {
alignment: Alignment.centerRight,
child: TextButton(
onPressed: () {

},
child: Text(
"$number",
"${widget.number}",
style: TextStyle(
color: fontColor,
fontSize: 16,
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.11.0"
awesome_notifications:
dependency: "direct main"
description:
name: awesome_notifications
sha256: "3eeb9e0cdfc72c7b4b83e9924da38ea86edd65a2af02bd5429aa41dce634ee90"
url: "https://pub.dev"
source: hosted
version: "0.8.3"
boolean_selector:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies:
flutter_launcher_icons: ^0.13.1
flutter_svg: ^2.0.9
quiver: ^3.2.1
awesome_notifications: any


dev_dependencies:
Expand Down

0 comments on commit 9929924

Please sign in to comment.