310 lines
16 KiB
Dart
310 lines
16 KiB
Dart
import 'dart:async';
|
|
import 'dart:developer';
|
|
import 'package:alarm/alarm.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../product/shared/shared_loading_animation.dart';
|
|
import '../../product/shared/shared_component_loading_animation.dart';
|
|
import 'shared/alert_card.dart';
|
|
import 'shared/warning_card.dart';
|
|
import 'device_alias_model.dart';
|
|
import 'shared/overview_card.dart';
|
|
import '../settings/device_notification_settings/device_notification_settings_model.dart';
|
|
import '../../product/extension/context_extension.dart';
|
|
import '../../product/services/api_services.dart';
|
|
import '../../product/services/language_services.dart';
|
|
import '../../bloc/home_bloc.dart';
|
|
import '../../product/base/bloc/base_bloc.dart';
|
|
|
|
class HomeScreen extends StatefulWidget {
|
|
const HomeScreen({super.key});
|
|
@override
|
|
State<HomeScreen> createState() => _HomeScreenState();
|
|
}
|
|
|
|
class _HomeScreenState extends State<HomeScreen> {
|
|
late HomeBloc homeBloc;
|
|
APIServices apiServices = APIServices();
|
|
bool isFunctionCall = false;
|
|
Timer? getAllDevicesTimer;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
homeBloc = BlocProvider.of(context);
|
|
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));
|
|
// 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() {
|
|
getAllDevicesTimer?.cancel();
|
|
homeBloc.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@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(
|
|
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(),
|
|
],
|
|
),
|
|
)
|
|
: 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,
|
|
);
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
void checkSettingDevice(List<DeviceWithAlias> devices) async {
|
|
if (isFunctionCall) {
|
|
log("Ham check setting da duoc goi");
|
|
} else {
|
|
await apiServices.execute(context, () async {
|
|
List<DeviceNotificationSettings> list =
|
|
await apiServices.getAllSettingsNotificationOfDevices();
|
|
// log("List: $list");
|
|
Set<String> thingIdsInList =
|
|
list.map((device) => device.thingId!).toSet();
|
|
for (var device in devices) {
|
|
if (!thingIdsInList.contains(device.thingId)) {
|
|
log("Device with Thing ID ${device.thingId} is not in the notification settings list.");
|
|
await apiServices.execute(context, () async {
|
|
await apiServices.setupDeviceNotification(
|
|
device.thingId!, device.name!);
|
|
});
|
|
} else {
|
|
log("All devices are in the notification settings list.");
|
|
}
|
|
}
|
|
});
|
|
}
|
|
isFunctionCall = true;
|
|
}
|
|
}
|