Implemented firebase and onesignal

This commit is contained in:
Abdullah Alassaf
2024-05-23 17:09:10 +03:00
parent f7db91f212
commit cda41ecf74
17 changed files with 312 additions and 64 deletions

View File

@ -1,8 +1,14 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:onesignal_flutter/onesignal_flutter.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
import 'package:syncrow_app/features/app_layout/view/widgets/app_bar_home_dropdown.dart';
import 'package:syncrow_app/features/auth/model/user_model.dart';
import 'package:syncrow_app/features/dashboard/view/dashboard_view.dart';
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
@ -23,6 +29,7 @@ part 'home_state.dart';
class HomeCubit extends Cubit<HomeState> {
HomeCubit._() : super(HomeInitial()) {
checkIfNotificationPermissionGranted();
if (selectedSpace == null) {
fetchUnitsByUserId().then((value) {
if (selectedSpace != null) {
@ -52,6 +59,9 @@ class HomeCubit extends Cubit<HomeState> {
selectedSpace = null;
selectedRoom = null;
pageIndex = 0;
OneSignal.User.pushSubscription.removeObserver((stateChanges) => oneSignalSubscriptionObserver);
OneSignal.Notifications.removePermissionObserver((permission) => oneSignalPermissionObserver);
OneSignal.Notifications.removeClickListener((event) => oneSignalClickListenerObserver);
return super.close();
}
@ -69,11 +79,67 @@ class HomeCubit extends Cubit<HomeState> {
var duration = const Duration(milliseconds: 300);
void oneSignalPermissionObserver;
void oneSignalSubscriptionObserver;
void oneSignalClickListenerObserver;
// selectSpace(SpaceModel space) async {
// selectedSpace = space;
// emit(SpaceSelected(space));
// }
checkIfNotificationPermissionGranted() async {
try {
OneSignal.initialize('762350c9-1e5d-4d95-a648-16d4dc8a25e1');
//Show native push notification dialog
if (Platform.isIOS) {
await OneSignal.Notifications.permissionNative();
} else {
await OneSignal.Notifications.requestPermission(true);
}
if (await Permission.notification.isGranted == false) {
return;
}
var userUuid = await const FlutterSecureStorage().read(key: UserModel.userUuidKey) ?? '';
if (userUuid.isNotEmpty) {
await OneSignal.login(userUuid);
}
//Enable push notifications
await OneSignal.User.pushSubscription.optIn();
//this function will be called once a user is subscribed
oneSignalSubscriptionObserver = OneSignal.User.pushSubscription.addObserver((state) async {
if (state.current.optedIn) {
await _sendSubscriptionId();
}
});
// Send the player id when a user allows notifications
oneSignalPermissionObserver = OneSignal.Notifications.addPermissionObserver((state) async {
await _sendSubscriptionId();
});
//check if the player id is sent, if not send it again
await _sendSubscriptionId();
oneSignalClickListenerObserver = OneSignal.Notifications.addClickListener((event) async {
//Once the user clicks on the notification
});
} catch (err) {
debugPrint("******* Error");
debugPrint(err.toString());
rethrow;
}
}
_sendSubscriptionId() async {
String? subscriptionId = OneSignal.User.pushSubscription.id ?? '';
//TODO send the subscription id to BE
}
changeSelectedSpace(SpaceModel space) {
selectedSpace = space;
emitSafe(SpaceSelected(space));

View File

@ -1,5 +1,7 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/devices/bloc/ceiling_bloc/ceiling_sensor_bloc.dart';
@ -157,19 +159,22 @@ class CeilingSensorInterface extends StatelessWidget {
mainAxisSize: MainAxisSize.min,
children: [
DefaultContainer(
padding:
const EdgeInsets.symmetric(vertical: 20, horizontal: 15),
padding: const EdgeInsets.symmetric(
vertical: 20,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Column(
mainAxisSize: MainAxisSize.min,
children: [
const BodySmall(text: 'Sports Para'),
BodyLarge(
text: '0',
style: context.bodyLarge.copyWith(
fontWeight: FontsManager.bold,
const Flexible(child: BodySmall(text: 'Sports Para')),
Flexible(
child: BodyLarge(
text: '0',
style: context.bodyLarge.copyWith(
fontWeight: FontsManager.bold,
),
),
),
],
@ -185,11 +190,18 @@ class CeilingSensorInterface extends StatelessWidget {
Column(
mainAxisSize: MainAxisSize.min,
children: [
const BodySmall(text: 'Detection Range'),
BodyLarge(
text: '0.0M',
style: context.bodyLarge.copyWith(
fontWeight: FontsManager.bold,
const Flexible(
child: BodySmall(
text: 'Detection Range',
textOverflow: TextOverflow.ellipsis,
)),
Flexible(
child: BodyLarge(
text: '0.0M',
textOverflow: TextOverflow.ellipsis,
style: context.bodyLarge.copyWith(
fontWeight: FontsManager.bold,
),
),
),
],
@ -205,11 +217,18 @@ class CeilingSensorInterface extends StatelessWidget {
Column(
mainAxisSize: MainAxisSize.min,
children: [
const BodySmall(text: 'Movement'),
BodyLarge(
text: 'none',
style: context.bodyLarge.copyWith(
fontWeight: FontsManager.bold,
const Flexible(
child: BodySmall(
text: 'Movement',
textOverflow: TextOverflow.ellipsis,
)),
Flexible(
child: BodyLarge(
text: 'none',
textOverflow: TextOverflow.ellipsis,
style: context.bodyLarge.copyWith(
fontWeight: FontsManager.bold,
),
),
),
],

View File

@ -4,16 +4,16 @@ import 'package:syncrow_app/utils/context_extension.dart';
import 'custom_text_widget.dart';
class BodyLarge extends StatelessWidget {
const BodyLarge({
required this.text,
super.key,
this.textAlign,
this.style,
this.height,
this.fontWeight,
this.fontColor,
this.fontSize,
});
const BodyLarge(
{required this.text,
super.key,
this.textAlign,
this.style,
this.height,
this.fontWeight,
this.fontColor,
this.fontSize,
this.textOverflow});
final String text;
final TextAlign? textAlign;
@ -28,16 +28,18 @@ class BodyLarge extends StatelessWidget {
final double? fontSize;
final TextOverflow? textOverflow;
@override
Widget build(BuildContext context) => CustomText(
text,
textAlign: textAlign,
style: style ??
context.bodyLarge.copyWith(
height: height,
fontWeight: fontWeight,
color: fontColor,
fontSize: fontSize,
),
height: height,
fontWeight: fontWeight,
color: fontColor,
fontSize: fontSize,
overflow: textOverflow),
);
}

View File

@ -11,6 +11,7 @@ class BodySmall extends StatelessWidget {
this.fontSize,
this.fontWeight,
this.textAlign,
this.textOverflow,
});
final String text;
@ -22,6 +23,7 @@ class BodySmall extends StatelessWidget {
final FontWeight? fontWeight;
final TextAlign? textAlign;
final TextOverflow? textOverflow;
@override
Widget build(BuildContext context) => CustomText(
@ -31,5 +33,6 @@ class BodySmall extends StatelessWidget {
fontSize: fontSize,
fontWeight: fontWeight,
textAlign: textAlign,
textOverflow: textOverflow,
);
}

View File

@ -9,6 +9,7 @@ class CustomText extends StatelessWidget {
this.minLines,
this.maxLines,
this.textDirection,
this.textOverflow,
this.fontSize,
this.fontColor,
this.fontWeight});
@ -20,6 +21,7 @@ class CustomText extends StatelessWidget {
final int? minLines;
final int? maxLines;
final TextDirection? textDirection;
final TextOverflow? textOverflow;
final double? fontSize;
final Color? fontColor;
@ -36,6 +38,7 @@ class CustomText extends StatelessWidget {
textAlign: textAlign,
// onTap: onTap,
// minLines: minLines,
overflow: textOverflow,
maxLines: maxLines,
textDirection: textDirection,
);