update(MainScreen): Fix Timer cannot cancel when switching tabs
This commit is contained in:
@@ -1,10 +1,17 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import '../feature/home/device_alias_model.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:sfm_app/product/extension/context_extension.dart';
|
||||||
|
|
||||||
|
import '../product/services/api_services.dart';
|
||||||
|
import '../feature/home/device_alias_model.dart';
|
||||||
import '../product/base/bloc/base_bloc.dart';
|
import '../product/base/bloc/base_bloc.dart';
|
||||||
|
import '../product/services/language_services.dart';
|
||||||
|
import '../product/utils/device_utils.dart';
|
||||||
|
|
||||||
class HomeBloc extends BlocBase {
|
class HomeBloc extends BlocBase {
|
||||||
|
APIServices apiServices = APIServices();
|
||||||
|
|
||||||
final allDevicesAliasMap =
|
final allDevicesAliasMap =
|
||||||
StreamController<Map<String, List<DeviceWithAlias>>>.broadcast();
|
StreamController<Map<String, List<DeviceWithAlias>>>.broadcast();
|
||||||
StreamSink<Map<String, List<DeviceWithAlias>>> get sinkAllDevicesAliasMap =>
|
StreamSink<Map<String, List<DeviceWithAlias>>> get sinkAllDevicesAliasMap =>
|
||||||
@@ -12,12 +19,12 @@ class HomeBloc extends BlocBase {
|
|||||||
Stream<Map<String, List<DeviceWithAlias>>> get streamAllDevicesAliasMap =>
|
Stream<Map<String, List<DeviceWithAlias>>> get streamAllDevicesAliasMap =>
|
||||||
allDevicesAliasMap.stream;
|
allDevicesAliasMap.stream;
|
||||||
|
|
||||||
final allDevicesAliasJoinedMap =
|
// final allDevicesAliasJoinedMap =
|
||||||
StreamController<Map<String, List<DeviceWithAlias>>>.broadcast();
|
// StreamController<Map<String, List<DeviceWithAlias>>>.broadcast();
|
||||||
StreamSink<Map<String, List<DeviceWithAlias>>>
|
// StreamSink<Map<String, List<DeviceWithAlias>>>
|
||||||
get sinkAllDevicesAliasJoinedMap => allDevicesAliasJoinedMap.sink;
|
// get sinkAllDevicesAliasJoinedMap => allDevicesAliasJoinedMap.sink;
|
||||||
Stream<Map<String, List<DeviceWithAlias>>>
|
// Stream<Map<String, List<DeviceWithAlias>>>
|
||||||
get streamAllDevicesAliasJoinedMap => allDevicesAliasJoinedMap.stream;
|
// get streamAllDevicesAliasJoinedMap => allDevicesAliasJoinedMap.stream;
|
||||||
|
|
||||||
final countNotification = StreamController<int>.broadcast();
|
final countNotification = StreamController<int>.broadcast();
|
||||||
StreamSink<int> get sinkCountNotification => countNotification.sink;
|
StreamSink<int> get sinkCountNotification => countNotification.sink;
|
||||||
@@ -34,12 +41,104 @@ class HomeBloc extends BlocBase {
|
|||||||
Stream<Map<String, List<DeviceWithAlias>>> get streamOwnerDevicesStatus =>
|
Stream<Map<String, List<DeviceWithAlias>>> get streamOwnerDevicesStatus =>
|
||||||
ownerDevicesStatus.stream;
|
ownerDevicesStatus.stream;
|
||||||
|
|
||||||
|
|
||||||
|
final aliasDevices = StreamController<List<DeviceWithAlias>?>.broadcast();
|
||||||
|
StreamSink<List<DeviceWithAlias>?> get sinkAliasDevices => aliasDevices.sink;
|
||||||
|
Stream<List<DeviceWithAlias>?> get streamAliasDevices => aliasDevices.stream;
|
||||||
|
|
||||||
|
void getOwnerAndJoinedDevices(BuildContext context) async {
|
||||||
|
|
||||||
|
await apiServices.execute(context, () async {
|
||||||
|
List<DeviceWithAlias> devices = await apiServices.getDashBoardDevices().handleApiError();
|
||||||
|
List<DeviceWithAlias> publicDevices = [];
|
||||||
|
for (var device in devices) {
|
||||||
|
if (device.visibility == "PUBLIC") {
|
||||||
|
publicDevices.add(device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sinkAliasDevices.add(publicDevices);
|
||||||
|
if (!context.mounted) return;
|
||||||
|
getOwnerDeviceState(context, publicDevices);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
void getOwnerDeviceState(BuildContext context,List<DeviceWithAlias> allDevices) async {
|
||||||
|
// int notificationCount = 0;
|
||||||
|
Map<String, List<DeviceWithAlias>> ownerDevicesStatus = {};
|
||||||
|
List<String> ownerDevicesState = [];
|
||||||
|
|
||||||
|
if (!context.mounted) return;
|
||||||
|
sinkOwnerDevicesStatus.add(ownerDevicesStatus);
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
for (var device in allDevices) {
|
||||||
|
// if (device.isOwner != true) continue;
|
||||||
|
|
||||||
|
if (!context.mounted) return;
|
||||||
|
Map<String, dynamic> sensorMap = DeviceUtils.instance
|
||||||
|
.getDeviceSensors(context, device.status?.sensors ?? []);
|
||||||
|
|
||||||
|
if (device.state == 1 || device.state == 3) {
|
||||||
|
ownerDevicesStatus["state"] ??= [];
|
||||||
|
ownerDevicesStatus["state"]!.add(device);
|
||||||
|
if (!context.mounted) return;
|
||||||
|
sinkOwnerDevicesStatus.add(ownerDevicesStatus);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
final noDataMessage = appLocalization(context).no_data_message;
|
||||||
|
if (sensorMap['sensorBattery'] != noDataMessage) {
|
||||||
|
if (double.parse(sensorMap['sensorBattery']) <= 20) {
|
||||||
|
ownerDevicesStatus['battery'] ??= [];
|
||||||
|
ownerDevicesStatus['battery']!.add(device);
|
||||||
|
if (!context.mounted) return;
|
||||||
|
sinkOwnerDevicesStatus.add(ownerDevicesStatus);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!context.mounted) return;
|
||||||
|
sinkCountNotification.add(count);
|
||||||
|
}
|
||||||
|
|
||||||
|
void getDeviceStatusAliasMap(List<DeviceWithAlias> devices) {
|
||||||
|
Map<String, List<DeviceWithAlias>> allDevicesAliasMap = {};
|
||||||
|
for (var key in ['all', 'online', 'offline', 'warning', 'not-use']) {
|
||||||
|
allDevicesAliasMap[key] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (DeviceWithAlias device in devices) {
|
||||||
|
allDevicesAliasMap['all']!.add(device);
|
||||||
|
if (device.state == 0 || device.state == 1) {
|
||||||
|
allDevicesAliasMap['online']!.add(device);
|
||||||
|
}
|
||||||
|
if (device.state == -1) {
|
||||||
|
allDevicesAliasMap['offline']!.add(device);
|
||||||
|
}
|
||||||
|
if (device.state == 1) {
|
||||||
|
allDevicesAliasMap['warning']!.add(device);
|
||||||
|
}
|
||||||
|
if (device.state == -2) {
|
||||||
|
allDevicesAliasMap['not-use']!.add(device);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
sinkAllDevicesAliasMap.add(allDevicesAliasMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
allDevicesAliasMap.close();
|
allDevicesAliasMap.close();
|
||||||
allDevicesAliasJoinedMap.close();
|
// allDevicesAliasJoinedMap.close();
|
||||||
countNotification.close();
|
countNotification.close();
|
||||||
hasJoinedDevice.close();
|
hasJoinedDevice.close();
|
||||||
ownerDevicesStatus.close();
|
ownerDevicesStatus.close();
|
||||||
|
aliasDevices.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:developer';
|
import 'dart:developer';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:persistent_bottom_nav_bar/persistent_bottom_nav_bar.dart';
|
|
||||||
|
|
||||||
|
import '../../product/shared/shared_loading_animation.dart';
|
||||||
import '../../product/shared/shared_component_loading_animation.dart';
|
import '../../product/shared/shared_component_loading_animation.dart';
|
||||||
import '../../product/utils/app_logger_utils.dart';
|
|
||||||
import 'shared/alert_card.dart';
|
import 'shared/alert_card.dart';
|
||||||
import 'shared/warning_card.dart';
|
import 'shared/warning_card.dart';
|
||||||
import '../../product/utils/device_utils.dart';
|
|
||||||
import 'device_alias_model.dart';
|
import 'device_alias_model.dart';
|
||||||
import 'shared/overview_card.dart';
|
import 'shared/overview_card.dart';
|
||||||
import '../settings/device_notification_settings/device_notification_settings_model.dart';
|
import '../settings/device_notification_settings/device_notification_settings_model.dart';
|
||||||
@@ -18,8 +16,7 @@ import '../../bloc/home_bloc.dart';
|
|||||||
import '../../product/base/bloc/base_bloc.dart';
|
import '../../product/base/bloc/base_bloc.dart';
|
||||||
|
|
||||||
class HomeScreen extends StatefulWidget {
|
class HomeScreen extends StatefulWidget {
|
||||||
const HomeScreen({super.key, required this.persistentTabController});
|
const HomeScreen({super.key});
|
||||||
final PersistentTabController persistentTabController;
|
|
||||||
@override
|
@override
|
||||||
State<HomeScreen> createState() => _HomeScreenState();
|
State<HomeScreen> createState() => _HomeScreenState();
|
||||||
}
|
}
|
||||||
@@ -27,24 +24,17 @@ class HomeScreen extends StatefulWidget {
|
|||||||
class _HomeScreenState extends State<HomeScreen> {
|
class _HomeScreenState extends State<HomeScreen> {
|
||||||
late HomeBloc homeBloc;
|
late HomeBloc homeBloc;
|
||||||
APIServices apiServices = APIServices();
|
APIServices apiServices = APIServices();
|
||||||
Map<String, List<DeviceWithAlias>> allDevicesAliasMap = {};
|
|
||||||
List<DeviceWithAlias> devices = [];
|
|
||||||
bool isFunctionCall = false;
|
bool isFunctionCall = false;
|
||||||
Timer? getAllDevicesTimer;
|
Timer? getAllDevicesTimer;
|
||||||
int notificationCount = 0;
|
|
||||||
Map<String, List<DeviceWithAlias>> ownerDevicesStatus = {};
|
|
||||||
List<String> ownerDevicesState = [];
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
homeBloc = BlocProvider.of(context);
|
homeBloc = BlocProvider.of(context);
|
||||||
getAllDevicesTimer?.cancel();
|
|
||||||
AppLoggerUtils.debug("Init State HomeScreen");
|
|
||||||
getOwnerAndJoinedDevices();
|
|
||||||
const duration = Duration(seconds: 10);
|
const duration = Duration(seconds: 10);
|
||||||
getAllDevicesTimer =
|
getAllDevicesTimer =
|
||||||
Timer.periodic(duration, (Timer t) => getOwnerAndJoinedDevices());
|
Timer.periodic(duration, (Timer t) => homeBloc.getOwnerAndJoinedDevices(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -56,255 +46,200 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return StreamBuilder<List<DeviceWithAlias>?>(
|
||||||
body: Padding(
|
stream: homeBloc.streamAliasDevices,
|
||||||
padding: context.paddingLow,
|
builder: (context, aliasDevicesSnapshot) {
|
||||||
child: SingleChildScrollView(
|
if(aliasDevicesSnapshot.data == null){
|
||||||
child: Column(
|
homeBloc.getOwnerAndJoinedDevices(context);
|
||||||
children: <Widget>[
|
return const SharedLoadingAnimation();
|
||||||
Row(
|
}else{
|
||||||
children: [
|
log("Goi else");
|
||||||
Text(
|
homeBloc.getOwnerDeviceState(context, aliasDevicesSnapshot.data ?? []);
|
||||||
appLocalization(context).notification,
|
homeBloc.getDeviceStatusAliasMap(aliasDevicesSnapshot.data ?? []);
|
||||||
style: context.titleMediumTextStyle,
|
checkSettingDevice(aliasDevicesSnapshot.data ?? []);
|
||||||
),
|
return Scaffold(
|
||||||
SizedBox(width: context.lowValue),
|
body: Padding(
|
||||||
StreamBuilder<int>(
|
padding: context.paddingLow,
|
||||||
stream: homeBloc.streamCountNotification,
|
child: SingleChildScrollView(
|
||||||
builder: (context, countSnapshot) {
|
child: Column(
|
||||||
return Text(
|
children: <Widget>[
|
||||||
"(${countSnapshot.data ?? 0})",
|
Row(
|
||||||
style: context.titleMediumTextStyle,
|
children: [
|
||||||
);
|
Text(
|
||||||
},
|
appLocalization(context).notification,
|
||||||
)
|
style: context.titleMediumTextStyle,
|
||||||
],
|
),
|
||||||
),
|
SizedBox(width: context.lowValue),
|
||||||
SizedBox(
|
StreamBuilder<int>(
|
||||||
child: SingleChildScrollView(
|
stream: homeBloc.streamCountNotification,
|
||||||
scrollDirection: Axis.horizontal,
|
builder: (context, countSnapshot) {
|
||||||
child: StreamBuilder<Map<String, List<DeviceWithAlias>>>(
|
if(countSnapshot.data == null){
|
||||||
stream: homeBloc.streamOwnerDevicesStatus,
|
homeBloc.getOwnerDeviceState(context, aliasDevicesSnapshot.data ?? []);
|
||||||
builder: (context, snapshot) {
|
return const Text("0");
|
||||||
return AnimatedSwitcher(
|
} else{
|
||||||
duration: context.lowDuration,
|
return Text(
|
||||||
transitionBuilder:
|
"(${countSnapshot.data ?? 0})",
|
||||||
(Widget child, Animation<double> animation) {
|
style: context.titleMediumTextStyle,
|
||||||
final offsetAnimation = Tween<Offset>(
|
);
|
||||||
begin: const Offset(0.0, 0.2),
|
}
|
||||||
end: Offset.zero,
|
},
|
||||||
).animate(CurvedAnimation(
|
)
|
||||||
parent: animation,
|
],
|
||||||
curve: Curves.easeInOut,
|
),
|
||||||
));
|
SizedBox(
|
||||||
return FadeTransition(
|
child: SingleChildScrollView(
|
||||||
opacity: animation,
|
scrollDirection: Axis.horizontal,
|
||||||
child: SlideTransition(
|
child: StreamBuilder<Map<String, List<DeviceWithAlias>>>(
|
||||||
position: offsetAnimation,
|
stream: homeBloc.streamOwnerDevicesStatus,
|
||||||
child: child,
|
builder: (context, ownerDevicesStatusSnapshot) {
|
||||||
),
|
if(ownerDevicesStatusSnapshot.data == null){
|
||||||
);
|
homeBloc.getOwnerDeviceState(context, aliasDevicesSnapshot.data ?? []);
|
||||||
},
|
return const SharedComponentLoadingAnimation();
|
||||||
child: snapshot.data?['state'] != null ||
|
}else{
|
||||||
snapshot.data?['battery'] != null
|
return AnimatedSwitcher(
|
||||||
? ConstrainedBox(
|
duration: context.lowDuration,
|
||||||
key: const ValueKey('data'),
|
transitionBuilder:
|
||||||
constraints: BoxConstraints(
|
(Widget child, Animation<double> animation) {
|
||||||
minWidth:
|
final offsetAnimation = Tween<Offset>(
|
||||||
MediaQuery.of(context).size.width),
|
begin: const Offset(0.0, 0.2),
|
||||||
child: Row(
|
end: Offset.zero,
|
||||||
mainAxisAlignment: MainAxisAlignment.start,
|
).animate(CurvedAnimation(
|
||||||
children: [
|
parent: animation,
|
||||||
if (snapshot.data?['state'] != null)
|
curve: Curves.easeInOut,
|
||||||
...snapshot.data!['state']!
|
));
|
||||||
.map(
|
return FadeTransition(
|
||||||
(item) => SizedBox(
|
opacity: animation,
|
||||||
width: context.dynamicWidth(0.95),
|
child: SlideTransition(
|
||||||
child: FutureBuilder<Widget>(
|
position: offsetAnimation,
|
||||||
future: warningCard(
|
child: child,
|
||||||
context, apiServices, item),
|
),
|
||||||
builder: (context,
|
);
|
||||||
warningCardSnapshot) {
|
},
|
||||||
if (warningCardSnapshot
|
child: ownerDevicesStatusSnapshot.data?['state'] != null ||
|
||||||
.hasData) {
|
ownerDevicesStatusSnapshot.data?['battery'] != null
|
||||||
return warningCardSnapshot
|
? ConstrainedBox(
|
||||||
.data!;
|
key: const ValueKey('data'),
|
||||||
} else {
|
constraints: BoxConstraints(
|
||||||
return const SizedBox
|
minWidth:
|
||||||
.shrink();
|
MediaQuery.of(context).size.width),
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.toList(),
|
|
||||||
if (snapshot.data?['battery'] != null)
|
|
||||||
...snapshot.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(),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
)
|
|
||||||
: Padding(
|
|
||||||
key: const ValueKey('no_data'),
|
|
||||||
padding: context.paddingMedium,
|
|
||||||
child: Center(
|
|
||||||
child: Row(
|
child: Row(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisAlignment: MainAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
const Icon(
|
if (ownerDevicesStatusSnapshot.data?['state'] != null)
|
||||||
Icons.check_circle_outline_rounded,
|
...ownerDevicesStatusSnapshot.data!['state']!
|
||||||
size: 40,
|
.map(
|
||||||
color: Colors.green,
|
(item) => SizedBox(
|
||||||
),
|
width: context.dynamicWidth(0.95),
|
||||||
SizedBox(width: context.lowValue),
|
child: FutureBuilder<Widget>(
|
||||||
Text(
|
future: warningCard(
|
||||||
appLocalization(context)
|
context, apiServices, item),
|
||||||
.notification_description,
|
builder: (context,
|
||||||
maxLines: 2,
|
warningCardSnapshot) {
|
||||||
overflow: TextOverflow.ellipsis,
|
if (warningCardSnapshot
|
||||||
softWrap: true,
|
.hasData) {
|
||||||
textAlign: TextAlign.start,
|
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(),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
)
|
||||||
|
: 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){
|
}
|
||||||
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 getOwnerAndJoinedDevices() async {
|
|
||||||
await apiServices.execute(context, () async {
|
|
||||||
devices = await apiServices.getDashBoardDevices().handleApiError();
|
|
||||||
List<DeviceWithAlias> publicDevices = [];
|
|
||||||
for (var device in devices) {
|
|
||||||
if (device.visibility == "PUBLIC") {
|
|
||||||
publicDevices.add(device);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
getOwnerDeviceState(publicDevices);
|
|
||||||
checkSettingDevice(publicDevices);
|
|
||||||
getDeviceStatusAliasMap(publicDevices);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
void getOwnerDeviceState(List<DeviceWithAlias> allDevices) async {
|
|
||||||
ownerDevicesState.clear();
|
|
||||||
ownerDevicesStatus.clear();
|
|
||||||
|
|
||||||
if (!mounted) return;
|
|
||||||
homeBloc.sinkOwnerDevicesStatus.add(ownerDevicesStatus);
|
|
||||||
|
|
||||||
int count = 0;
|
|
||||||
for (var device in allDevices) {
|
|
||||||
// if (device.isOwner != true) continue;
|
|
||||||
|
|
||||||
if (!mounted) return;
|
|
||||||
Map<String, dynamic> sensorMap = DeviceUtils.instance
|
|
||||||
.getDeviceSensors(context, device.status?.sensors ?? []);
|
|
||||||
|
|
||||||
if (device.state == 1 || device.state == 3) {
|
|
||||||
ownerDevicesStatus["state"] ??= [];
|
|
||||||
ownerDevicesStatus["state"]!.add(device);
|
|
||||||
if (!mounted) return;
|
|
||||||
homeBloc.sinkOwnerDevicesStatus.add(ownerDevicesStatus);
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
final noDataMessage = appLocalization(context).no_data_message;
|
|
||||||
if (sensorMap['sensorBattery'] != noDataMessage) {
|
|
||||||
if (double.parse(sensorMap['sensorBattery']) <= 20) {
|
|
||||||
ownerDevicesStatus['battery'] ??= [];
|
|
||||||
ownerDevicesStatus['battery']!.add(device);
|
|
||||||
if (!mounted) return;
|
|
||||||
homeBloc.sinkOwnerDevicesStatus.add(ownerDevicesStatus);
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
notificationCount = count;
|
|
||||||
if (!mounted) return;
|
|
||||||
homeBloc.sinkCountNotification.add(notificationCount);
|
|
||||||
}
|
|
||||||
|
|
||||||
void getDeviceStatusAliasMap(List<DeviceWithAlias> devices) {
|
|
||||||
allDevicesAliasMap.clear();
|
|
||||||
for (var key in ['all', 'online', 'offline', 'warning', 'not-use']) {
|
|
||||||
allDevicesAliasMap[key] = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (DeviceWithAlias device in devices) {
|
|
||||||
allDevicesAliasMap['all']!.add(device);
|
|
||||||
if (device.state == 0 || device.state == 1) {
|
|
||||||
allDevicesAliasMap['online']!.add(device);
|
|
||||||
}
|
|
||||||
if (device.state == -1) {
|
|
||||||
allDevicesAliasMap['offline']!.add(device);
|
|
||||||
}
|
|
||||||
if (device.state == 1) {
|
|
||||||
allDevicesAliasMap['warning']!.add(device);
|
|
||||||
}
|
|
||||||
if (device.state == -2) {
|
|
||||||
allDevicesAliasMap['not-use']!.add(device);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
homeBloc.sinkAllDevicesAliasMap.add(allDevicesAliasMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
void checkSettingDevice(List<DeviceWithAlias> devices) async {
|
void checkSettingDevice(List<DeviceWithAlias> devices) async {
|
||||||
if (isFunctionCall) {
|
if (isFunctionCall) {
|
||||||
log("Ham check setting da duoc goi");
|
log("Ham check setting da duoc goi");
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:badges/badges.dart' as badges;
|
import 'package:badges/badges.dart' as badges;
|
||||||
import 'package:persistent_bottom_nav_bar/persistent_bottom_nav_bar.dart';
|
import 'package:persistent_bottom_nav_bar_v2/persistent_bottom_nav_bar_v2.dart';
|
||||||
|
|
||||||
import '../../product/utils/permission_handler.dart';
|
import '../../product/utils/permission_handler.dart';
|
||||||
import '../../product/permission/notification_permission.dart';
|
import '../../product/permission/notification_permission.dart';
|
||||||
@@ -147,79 +147,75 @@ class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
|
|||||||
WidgetsBinding.instance.removeObserver(this);
|
WidgetsBinding.instance.removeObserver(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<PersistentBottomNavBarItem> _navBarsItems() {
|
|
||||||
return [
|
|
||||||
PersistentBottomNavBarItem(
|
|
||||||
icon: IconConstants.instance.getMaterialIcon(Icons.home),
|
|
||||||
title: appLocalization(context).home_page_destination,
|
|
||||||
activeColorPrimary: Colors.blue,
|
|
||||||
inactiveColorPrimary: Colors.grey,
|
|
||||||
inactiveIcon:
|
|
||||||
IconConstants.instance.getMaterialIcon(Icons.home_outlined),
|
|
||||||
),
|
|
||||||
PersistentBottomNavBarItem(
|
|
||||||
icon: IconConstants.instance.getMaterialIcon(Icons.settings),
|
|
||||||
title: appLocalization(context).manager_page_destination,
|
|
||||||
activeColorPrimary: Colors.blue,
|
|
||||||
inactiveColorPrimary: Colors.grey,
|
|
||||||
inactiveIcon:
|
|
||||||
IconConstants.instance.getMaterialIcon(Icons.settings_outlined),
|
|
||||||
),
|
|
||||||
PersistentBottomNavBarItem(
|
|
||||||
icon: IconConstants.instance.getMaterialIcon(Icons.location_on),
|
|
||||||
title: appLocalization(context).map_page_destination,
|
|
||||||
activeColorPrimary: Colors.blue,
|
|
||||||
inactiveColorPrimary: Colors.grey,
|
|
||||||
inactiveIcon:
|
|
||||||
IconConstants.instance.getMaterialIcon(Icons.location_on_outlined),
|
|
||||||
),
|
|
||||||
PersistentBottomNavBarItem(
|
|
||||||
icon: IconConstants.instance.getMaterialIcon(Icons.history),
|
|
||||||
title: appLocalization(context).history_page_destination,
|
|
||||||
activeColorPrimary: Colors.blue,
|
|
||||||
inactiveColorPrimary: Colors.grey,
|
|
||||||
inactiveIcon:
|
|
||||||
IconConstants.instance.getMaterialIcon(Icons.history_outlined),
|
|
||||||
),
|
|
||||||
PersistentBottomNavBarItem(
|
|
||||||
icon: IconConstants.instance.getMaterialIcon(Icons.group),
|
|
||||||
title: appLocalization(context).group_page_destination,
|
|
||||||
activeColorPrimary: Colors.blue,
|
|
||||||
inactiveColorPrimary: Colors.grey,
|
|
||||||
inactiveIcon:
|
|
||||||
IconConstants.instance.getMaterialIcon(Icons.group_outlined),
|
|
||||||
),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Widget> _buildScreens() {
|
|
||||||
return [
|
|
||||||
BlocProvider(
|
|
||||||
child: HomeScreen(
|
|
||||||
persistentTabController: controller,
|
|
||||||
),
|
|
||||||
blocBuilder: () => HomeBloc(),
|
|
||||||
),
|
|
||||||
BlocProvider(
|
|
||||||
child: const DevicesManagerScreen(),
|
|
||||||
blocBuilder: () => DevicesManagerBloc()),
|
|
||||||
BlocProvider(
|
|
||||||
child: const MapScreen(),
|
|
||||||
blocBuilder: () => MapBloc(),
|
|
||||||
),
|
|
||||||
BlocProvider(
|
|
||||||
child: const DeviceLogsScreen(),
|
|
||||||
blocBuilder: () => DeviceLogsBloc(),
|
|
||||||
),
|
|
||||||
BlocProvider(
|
|
||||||
child: const InterFamilyScreen(),
|
|
||||||
blocBuilder: () => InterFamilyBloc(),
|
|
||||||
),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
List<PersistentTabConfig> tabs = [
|
||||||
|
PersistentTabConfig(
|
||||||
|
screen: BlocProvider(
|
||||||
|
child: const HomeScreen(),
|
||||||
|
blocBuilder: () => HomeBloc(),
|
||||||
|
),
|
||||||
|
item: ItemConfig(
|
||||||
|
icon: IconConstants.instance.getMaterialIcon(Icons.home),
|
||||||
|
title: appLocalization(context).home_page_destination,
|
||||||
|
inactiveIcon:
|
||||||
|
IconConstants.instance.getMaterialIcon(Icons.home_outlined),
|
||||||
|
iconSize: 30,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
PersistentTabConfig(
|
||||||
|
screen: BlocProvider(
|
||||||
|
child: const DevicesManagerScreen(),
|
||||||
|
blocBuilder: () => DevicesManagerBloc(),
|
||||||
|
),
|
||||||
|
item: ItemConfig(
|
||||||
|
icon: IconConstants.instance.getMaterialIcon(Icons.settings),
|
||||||
|
title: appLocalization(context).manager_page_destination,
|
||||||
|
inactiveIcon:
|
||||||
|
IconConstants.instance.getMaterialIcon(Icons.settings_outlined),
|
||||||
|
iconSize: 30,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
PersistentTabConfig(
|
||||||
|
screen: BlocProvider(
|
||||||
|
child: const MapScreen(),
|
||||||
|
blocBuilder: () => MapBloc(),
|
||||||
|
),
|
||||||
|
item: ItemConfig(
|
||||||
|
icon: IconConstants.instance.getMaterialIcon(Icons.location_on),
|
||||||
|
title: appLocalization(context).map_page_destination,
|
||||||
|
inactiveIcon: IconConstants.instance
|
||||||
|
.getMaterialIcon(Icons.location_on_outlined),
|
||||||
|
iconSize: 30,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
PersistentTabConfig(
|
||||||
|
screen: BlocProvider(
|
||||||
|
child: const DeviceLogsScreen(),
|
||||||
|
blocBuilder: () => DeviceLogsBloc(),
|
||||||
|
),
|
||||||
|
item: ItemConfig(
|
||||||
|
icon: IconConstants.instance.getMaterialIcon(Icons.history),
|
||||||
|
title: appLocalization(context).history_page_destination,
|
||||||
|
inactiveIcon:
|
||||||
|
IconConstants.instance.getMaterialIcon(Icons.history_outlined),
|
||||||
|
iconSize: 30,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
PersistentTabConfig(
|
||||||
|
screen: BlocProvider(
|
||||||
|
child: const InterFamilyScreen(),
|
||||||
|
blocBuilder: () => InterFamilyBloc(),
|
||||||
|
),
|
||||||
|
item: ItemConfig(
|
||||||
|
icon: IconConstants.instance.getMaterialIcon(Icons.group),
|
||||||
|
title: appLocalization(context).group_page_destination,
|
||||||
|
inactiveIcon:
|
||||||
|
IconConstants.instance.getMaterialIcon(Icons.group_outlined),
|
||||||
|
iconSize: 30,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
];
|
||||||
return StreamBuilder<bool>(
|
return StreamBuilder<bool>(
|
||||||
stream: mainBloc.streamThemeMode,
|
stream: mainBloc.streamThemeMode,
|
||||||
initialData: isLight,
|
initialData: isLight,
|
||||||
@@ -368,32 +364,27 @@ class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
body: PersistentTabView(
|
body: PersistentTabView(
|
||||||
context,
|
|
||||||
controller: controller,
|
|
||||||
screens: _buildScreens(),
|
|
||||||
items: _navBarsItems(),
|
|
||||||
handleAndroidBackButtonPress: true,
|
|
||||||
resizeToAvoidBottomInset: true,
|
|
||||||
stateManagement: false,
|
stateManagement: false,
|
||||||
|
controller: controller,
|
||||||
|
tabs: tabs,
|
||||||
|
navBarBuilder: (navBarConfig) => Style6BottomNavBar(
|
||||||
|
navBarConfig: navBarConfig,
|
||||||
|
navBarDecoration: NavBarDecoration(
|
||||||
|
color: themeModeSnapshot.data! ? Colors.white : Colors.black,
|
||||||
|
borderRadius: BorderRadius.circular(context.mediumValue),
|
||||||
|
padding: const EdgeInsets.all(10)),
|
||||||
|
),
|
||||||
backgroundColor:
|
backgroundColor:
|
||||||
themeModeSnapshot.data! ? Colors.white : Colors.black,
|
themeModeSnapshot.data! ? Colors.white : Colors.black,
|
||||||
decoration: NavBarDecoration(
|
navBarOverlap: const NavBarOverlap.none(),
|
||||||
borderRadius: BorderRadius.circular(30.0),
|
// margin: EdgeInsets.only(
|
||||||
colorBehindNavBar:
|
// left: context.lowValue,
|
||||||
themeModeSnapshot.data! ? Colors.white : Colors.black,
|
// bottom: context.dynamicHeight(0.02),
|
||||||
|
// right: context.lowValue),
|
||||||
|
screenTransitionAnimation: const ScreenTransitionAnimation(
|
||||||
|
curve: Curves.bounceInOut,
|
||||||
|
duration: Duration(milliseconds: 200),
|
||||||
),
|
),
|
||||||
animationSettings: const NavBarAnimationSettings(
|
|
||||||
navBarItemAnimation: ItemAnimationSettings(
|
|
||||||
duration: Duration(milliseconds: 200),
|
|
||||||
curve: Curves.bounceInOut,
|
|
||||||
),
|
|
||||||
screenTransitionAnimation: ScreenTransitionAnimationSettings(
|
|
||||||
animateTabTransition: true,
|
|
||||||
curve: Curves.bounceInOut,
|
|
||||||
duration: Duration(milliseconds: 200),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
navBarStyle: NavBarStyle.style13,
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
import 'dart:developer';
|
|
||||||
|
|
||||||
import 'package:firebase_core/firebase_core.dart';
|
import 'package:firebase_core/firebase_core.dart';
|
||||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
|
|||||||
@@ -25,5 +25,5 @@ class ApplicationConstants {
|
|||||||
static const PARTICIPANT_GROUP = "participant";
|
static const PARTICIPANT_GROUP = "participant";
|
||||||
static const NO_DATA = "no_data";
|
static const NO_DATA = "no_data";
|
||||||
static const LOADING = "loading";
|
static const LOADING = "loading";
|
||||||
static int CALL_API_TIMEOUT = 15;
|
static int CALL_API_TIMEOUT = 30;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import 'dart:math' as math;
|
|||||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||||
import 'package:persistent_bottom_nav_bar/persistent_bottom_nav_bar.dart';
|
import 'package:persistent_bottom_nav_bar_v2/persistent_bottom_nav_bar_v2.dart';
|
||||||
|
|
||||||
class NotificationServices {
|
class NotificationServices {
|
||||||
FirebaseMessaging messaging = FirebaseMessaging.instance;
|
FirebaseMessaging messaging = FirebaseMessaging.instance;
|
||||||
@@ -30,12 +30,8 @@ class NotificationServices {
|
|||||||
void firebaseInit(BuildContext context) {
|
void firebaseInit(BuildContext context) {
|
||||||
FirebaseMessaging.onMessage.listen((message) {
|
FirebaseMessaging.onMessage.listen((message) {
|
||||||
dev.log("Foreground message payload: ${message.toMap()}");
|
dev.log("Foreground message payload: ${message.toMap()}");
|
||||||
if (WidgetsBinding.instance != null) {
|
showNotification(message);
|
||||||
showNotification(message);
|
});
|
||||||
} else {
|
|
||||||
dev.log("App is in background, skipping foreground notification");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<String> getDeviceToken() async {
|
Future<String> getDeviceToken() async {
|
||||||
|
|||||||
12
pubspec.lock
12
pubspec.lock
@@ -417,10 +417,10 @@ packages:
|
|||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: go_router
|
name: go_router
|
||||||
sha256: b465e99ce64ba75e61c8c0ce3d87b66d8ac07f0b35d0a7e0263fcfc10f99e836
|
sha256: "02ff498f6279470ff7f60c998a69b872f26696ceec237c8402e63a2133868ddf"
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "13.2.5"
|
version: "15.2.3"
|
||||||
google_maps:
|
google_maps:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -693,14 +693,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.2.1"
|
version: "0.2.1"
|
||||||
persistent_bottom_nav_bar:
|
persistent_bottom_nav_bar_v2:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: persistent_bottom_nav_bar
|
name: persistent_bottom_nav_bar_v2
|
||||||
sha256: "6aa9b97ced1abd92c90cedd1997d34ea0b35c3ded762ac6063baccc299b0c4c5"
|
sha256: "1d6347d86512ef028341eaecf2e8870af5dd41462fe77ec5b3126da29b4d7a8d"
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "6.2.1"
|
version: "6.1.0"
|
||||||
petitparser:
|
petitparser:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ dependencies:
|
|||||||
permission_handler: ^11.0.1
|
permission_handler: ^11.0.1
|
||||||
# flex_color_scheme: ^7.2.0
|
# flex_color_scheme: ^7.2.0
|
||||||
flex_color_scheme: ^8.2.0
|
flex_color_scheme: ^8.2.0
|
||||||
go_router: ^13.1.0
|
go_router: ^15.2.3
|
||||||
http: ^1.3.0
|
http: ^1.3.0
|
||||||
top_snackbar_flutter: ^3.1.0
|
top_snackbar_flutter: ^3.1.0
|
||||||
badges: ^3.1.2
|
badges: ^3.1.2
|
||||||
@@ -40,8 +40,7 @@ dependencies:
|
|||||||
flutter_polyline_points: ^2.1.0
|
flutter_polyline_points: ^2.1.0
|
||||||
simple_ripple_animation: ^0.1.0
|
simple_ripple_animation: ^0.1.0
|
||||||
fl_chart: ^0.64.0
|
fl_chart: ^0.64.0
|
||||||
# persistent_bottom_nav_bar_v2: ^4.2.8
|
persistent_bottom_nav_bar_v2: ^6.1.0
|
||||||
persistent_bottom_nav_bar: ^6.2.1
|
|
||||||
win32: ^5.10.0
|
win32: ^5.10.0
|
||||||
google_maps_flutter: ^2.12.2
|
google_maps_flutter: ^2.12.2
|
||||||
data_table_2: ^2.5.18
|
data_table_2: ^2.5.18
|
||||||
|
|||||||
Reference in New Issue
Block a user