chore(notifications): fix conflig when show notification in android

This commit is contained in:
Tran Anh Tuan
2025-10-23 16:57:10 +07:00
parent 9bff11a0b1
commit 2afc71e24c
16 changed files with 567 additions and 422 deletions

View File

@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:developer';
import 'package:alarm/alarm.dart';
import 'package:flutter/material.dart';
import '../../product/shared/shared_loading_animation.dart';
@@ -27,7 +28,6 @@ class _HomeScreenState extends State<HomeScreen> {
bool isFunctionCall = false;
Timer? getAllDevicesTimer;
@override
void initState() {
super.initState();
@@ -35,14 +35,16 @@ class _HomeScreenState extends State<HomeScreen> {
const duration = Duration(seconds: 10);
WidgetsBinding.instance.addPostFrameCallback((_) {
// Code ở đây chạy sau khi giao diện render xong
getAllDevicesTimer =
Timer.periodic(duration, (Timer t) => homeBloc.getOwnerAndJoinedDevices(context));
getAllDevicesTimer = Timer.periodic(
duration, (Timer t) => homeBloc.getOwnerAndJoinedDevices(context));
// Ví dụ: gọi API, scroll tới vị trí nào đó, v.v.
});
}
Future<void> loadAlarms() async {
final alarms = await Alarm.getAlarms();
log("Alarms: $alarms");
}
@override
void dispose() {
@@ -54,196 +56,229 @@ class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return StreamBuilder<List<DeviceWithAlias>?>(
stream: homeBloc.streamAliasDevices,
builder: (context, aliasDevicesSnapshot) {
if(aliasDevicesSnapshot.data == null){
homeBloc.getOwnerAndJoinedDevices(context);
return const SharedLoadingAnimation();
}else{
homeBloc.getOwnerDeviceState(context, aliasDevicesSnapshot.data ?? []);
homeBloc.getDeviceStatusAliasMap(aliasDevicesSnapshot.data ?? []);
checkSettingDevice(aliasDevicesSnapshot.data ?? []);
return Scaffold(
body: Padding(
padding: context.paddingLow,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Row(
children: [
Text(
appLocalization(context).notification,
style: context.titleMediumTextStyle,
),
SizedBox(width: context.lowValue),
StreamBuilder<int>(
stream: homeBloc.streamCountNotification,
builder: (context, countSnapshot) {
if(countSnapshot.data == null){
homeBloc.getOwnerDeviceState(context, aliasDevicesSnapshot.data ?? []);
return const Text("0");
} else{
return Text(
"(${countSnapshot.data ?? 0})",
style: context.titleMediumTextStyle,
);
}
},
)
],
),
SizedBox(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: StreamBuilder<Map<String, List<DeviceWithAlias>>>(
stream: homeBloc.streamOwnerDevicesStatus,
builder: (context, ownerDevicesStatusSnapshot) {
if(ownerDevicesStatusSnapshot.data == null){
homeBloc.getOwnerDeviceState(context, aliasDevicesSnapshot.data ?? []);
return const SharedComponentLoadingAnimation();
}else{
return AnimatedSwitcher(
duration: context.lowDuration,
transitionBuilder:
(Widget child, Animation<double> animation) {
final offsetAnimation = Tween<Offset>(
begin: const Offset(0.0, 0.2),
end: Offset.zero,
).animate(CurvedAnimation(
parent: animation,
curve: Curves.easeInOut,
));
return FadeTransition(
opacity: animation,
child: SlideTransition(
position: offsetAnimation,
child: child,
),
);
},
child: ownerDevicesStatusSnapshot.data?['state'] != null ||
ownerDevicesStatusSnapshot.data?['battery'] != null
? ConstrainedBox(
key: const ValueKey('data'),
constraints: BoxConstraints(
minWidth:
MediaQuery.of(context).size.width),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
if (ownerDevicesStatusSnapshot.data?['state'] != null)
...ownerDevicesStatusSnapshot.data!['state']!
.map(
(item) => SizedBox(
width: context.dynamicWidth(0.95),
child: FutureBuilder<Widget>(
future: warningCard(
context, apiServices, item),
builder: (context,
warningCardSnapshot) {
if (warningCardSnapshot
.hasData) {
return warningCardSnapshot
.data!;
} else {
return const SizedBox
.shrink();
}
},
),
stream: homeBloc.streamAliasDevices,
builder: (context, aliasDevicesSnapshot) {
if (aliasDevicesSnapshot.data == null) {
homeBloc.getOwnerAndJoinedDevices(context);
return const SharedLoadingAnimation();
} else {
homeBloc.getOwnerDeviceState(
context, aliasDevicesSnapshot.data ?? []);
homeBloc.getDeviceStatusAliasMap(aliasDevicesSnapshot.data ?? []);
checkSettingDevice(aliasDevicesSnapshot.data ?? []);
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
loadAlarms();
},
child: const Icon(Icons.alarm),
),
body: Padding(
padding: context.paddingLow,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Row(
children: [
Text(
appLocalization(context).notification,
style: context.titleMediumTextStyle,
),
SizedBox(width: context.lowValue),
StreamBuilder<int>(
stream: homeBloc.streamCountNotification,
builder: (context, countSnapshot) {
if (countSnapshot.data == null) {
homeBloc.getOwnerDeviceState(
context, aliasDevicesSnapshot.data ?? []);
return const Text("0");
} else {
return Text(
"(${countSnapshot.data ?? 0})",
style: context.titleMediumTextStyle,
);
}
},
)
],
),
SizedBox(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child:
StreamBuilder<Map<String, List<DeviceWithAlias>>>(
stream: homeBloc.streamOwnerDevicesStatus,
builder: (context, ownerDevicesStatusSnapshot) {
if (ownerDevicesStatusSnapshot.data == null) {
homeBloc.getOwnerDeviceState(
context, aliasDevicesSnapshot.data ?? []);
return const SharedComponentLoadingAnimation();
} else {
return AnimatedSwitcher(
duration: context.lowDuration,
transitionBuilder: (Widget child,
Animation<double> animation) {
final offsetAnimation = Tween<Offset>(
begin: const Offset(0.0, 0.2),
end: Offset.zero,
).animate(CurvedAnimation(
parent: animation,
curve: Curves.easeInOut,
));
return FadeTransition(
opacity: animation,
child: SlideTransition(
position: offsetAnimation,
child: child,
),
);
},
child: ownerDevicesStatusSnapshot
.data?['state'] !=
null ||
ownerDevicesStatusSnapshot
.data?['battery'] !=
null
? ConstrainedBox(
key: const ValueKey('data'),
constraints: BoxConstraints(
minWidth: MediaQuery.of(context)
.size
.width),
child: Row(
mainAxisAlignment:
MainAxisAlignment.start,
children: [
if (ownerDevicesStatusSnapshot
.data?['state'] !=
null)
...ownerDevicesStatusSnapshot
.data!['state']!
.map(
(item) => SizedBox(
width: context
.dynamicWidth(0.95),
child: FutureBuilder<
Widget>(
future: warningCard(
context,
apiServices,
item),
builder: (context,
warningCardSnapshot) {
if (warningCardSnapshot
.hasData) {
return warningCardSnapshot
.data!;
} else {
return const SizedBox
.shrink();
}
},
),
),
)
.toList(),
if (ownerDevicesStatusSnapshot
.data?['battery'] !=
null)
...ownerDevicesStatusSnapshot
.data!['battery']!
.map(
(batteryItem) => SizedBox(
width: context
.dynamicWidth(0.95),
child: FutureBuilder<
Widget>(
future:
notificationCard(
context,
"lowBattery",
appLocalization(
context)
.low_battery_message,
batteryItem,
),
builder: (context,
warningCardSnapshot) {
if (warningCardSnapshot
.hasData) {
return warningCardSnapshot
.data!;
} else {
return const SizedBox
.shrink();
}
},
),
),
)
.toList(),
],
),
)
.toList(),
if (ownerDevicesStatusSnapshot.data?['battery'] != null)
...ownerDevicesStatusSnapshot.data!['battery']!
.map(
(batteryItem) => SizedBox(
width: context.dynamicWidth(0.95),
child: FutureBuilder<Widget>(
future: notificationCard(
context,
"lowBattery",
appLocalization(context)
.low_battery_message,
batteryItem,
),
builder: (context,
warningCardSnapshot) {
if (warningCardSnapshot
.hasData) {
return warningCardSnapshot
.data!;
} else {
return const SizedBox
.shrink();
}
},
: Padding(
key: const ValueKey('no_data'),
padding: context.paddingMedium,
child: Center(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(
Icons
.check_circle_outline_rounded,
size: 40,
color: Colors.green,
),
SizedBox(
width: context.lowValue),
Text(
appLocalization(context)
.notification_description,
maxLines: 2,
overflow:
TextOverflow.ellipsis,
softWrap: true,
textAlign: TextAlign.start,
),
],
),
),
)
.toList(),
],
),
)
: Padding(
key: const ValueKey('no_data'),
padding: context.paddingMedium,
child: Center(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(
Icons.check_circle_outline_rounded,
size: 40,
color: Colors.green,
),
SizedBox(width: context.lowValue),
Text(
appLocalization(context)
.notification_description,
maxLines: 2,
overflow: TextOverflow.ellipsis,
softWrap: true,
textAlign: TextAlign.start,
),
],
),
),
),
);
}
},
);
}
},
),
),
),
),
StreamBuilder<Map<String, List<DeviceWithAlias>>?>(
stream: homeBloc.streamAllDevicesAliasMap,
builder: (context, allDevicesAliasMapSnapshot) {
if(allDevicesAliasMapSnapshot.data == null){
homeBloc.getDeviceStatusAliasMap(aliasDevicesSnapshot.data ?? []);
return const SharedComponentLoadingAnimation();
}else{
final data = allDevicesAliasMapSnapshot.data!;
return OverviewCard(
isOwner: true,
total: data['all']?.length ?? 0,
active: data['online']?.length ?? 0,
inactive: data['offline']?.length ?? 0,
warning: data['warn']?.length ?? 0,
unused: data['not-use']?.length ?? 0,
showUnused: false,
);
}
},
),
],
StreamBuilder<Map<String, List<DeviceWithAlias>>?>(
stream: homeBloc.streamAllDevicesAliasMap,
builder: (context, allDevicesAliasMapSnapshot) {
if (allDevicesAliasMapSnapshot.data == null) {
homeBloc.getDeviceStatusAliasMap(
aliasDevicesSnapshot.data ?? []);
return const SharedComponentLoadingAnimation();
} else {
final data = allDevicesAliasMapSnapshot.data!;
return OverviewCard(
isOwner: true,
total: data['all']?.length ?? 0,
active: data['online']?.length ?? 0,
inactive: data['offline']?.length ?? 0,
warning: data['warn']?.length ?? 0,
unused: data['not-use']?.length ?? 0,
showUnused: false,
);
}
},
),
],
),
),
),
),
);
}
}
);
);
}
});
}
void checkSettingDevice(List<DeviceWithAlias> devices) async {

View File

@@ -76,11 +76,11 @@ class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
mainBloc.sinkThemeMode.add(isLight);
checkAndRequestPermission();
NotificationServices.requestNotificationPermission();
NotificationPermission.instance.checkNotificationPermission(context);
bool? notificationStatus = await NotificationPermission.instance.requestNotificationPermission();
if(!notificationStatus!){
showNoIconTopSnackBar(context, "Yêu cầu quyền thông báo không thành công", Colors.orange, Colors.white12);
}
// NotificationPermission.instance.checkNotificationPermission(context);
// bool? notificationStatus = await NotificationPermission.instance.requestNotificationPermission();
// if(notificationStatus == false){
// showNoIconTopSnackBar(context, "Yêu cầu quyền thông báo không thành công", Colors.orange, Colors.white12);
// }
}
@override