water leak

This commit is contained in:
mohammad
2024-10-08 10:11:08 +03:00
parent 754d869fac
commit 75b3036e03
10 changed files with 264 additions and 249 deletions

View File

@ -0,0 +1,3 @@
<svg width="15" height="15" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
<circle cx="7.5" cy="7.5" r="5" stroke="#FF4756" stroke-width="5"/>
</svg>

After

Width:  |  Height:  |  Size: 171 B

View File

@ -0,0 +1,3 @@
<svg width="15" height="15" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
<circle cx="7.5" cy="7.5" r="5" stroke="#023DFE" stroke-opacity="0.6" stroke-width="5"/>
</svg>

After

Width:  |  Height:  |  Size: 192 B

View File

@ -28,7 +28,7 @@ class WaterLeakBloc extends Bloc<WaterLeakEvent, WaterLeakState> {
bool closingReminder = false;
bool waterAlarm = false;
WaterLeakModel deviceStatus =
WaterLeakModel(waterContactState: false, batteryPercentage: 0);
WaterLeakModel(waterContactState: 'normal', batteryPercentage: 0);
void _fetchStatus(
WaterLeakInitial event, Emitter<WaterLeakState> emit) async {
@ -43,6 +43,7 @@ class WaterLeakBloc extends Bloc<WaterLeakEvent, WaterLeakState> {
statusModelList,
);
emit(UpdateState(waterSensor: deviceStatus));
Future.delayed(const Duration(milliseconds: 500));
_listenToChanges();
} catch (e) {
@ -115,39 +116,32 @@ class WaterLeakBloc extends Bloc<WaterLeakEvent, WaterLeakState> {
}
}
DeviceReport recordGroups = DeviceReport(
startTime: '0',
endTime: '0',
data: [DeviceEvent(code: '111', eventTime: 23456789, value: 'tyuio')]);
DeviceReport recordGroups =
DeviceReport(startTime: '0', endTime: '0', data: []);
Future<void> fetchLogsForLastMonth(
ReportLogsInitial event, Emitter<WaterLeakState> emit) async {
// Get the current date and time
DateTime now = DateTime.now();
// Calculate the date one month ago
DateTime lastMonth = DateTime(now.year, now.month - 1, now.day);
// Convert the date to milliseconds since epoch (Unix timestamp in milliseconds)
int startTime = lastMonth.millisecondsSinceEpoch;
int endTime = now.millisecondsSinceEpoch;
try {
emit(WaterLeakLoadingState());
// var response = await DevicesAPI.getReportLogs(
// startTime:
// startTime.toString(), // Convert to String if the API expects it
// endTime: endTime.toString(), // Convert to String if the API expects it
// deviceUuid: WLId,
// code: 'watercontact_state',
// );
// print('response======${response}');
// recordGroups = response;
// Process response here
var response = await DevicesAPI.getReportLogs(
startTime: startTime.toString(),
endTime: endTime.toString(),
deviceUuid: WLId,
code: 'watercontact_state',
);
recordGroups = response;
emit(UpdateState(waterSensor: deviceStatus));
} on DioException catch (e) {
final errorData = e.response!.data;
String errorMessage = errorData['message'];
// Handle error
emit(WaterLeakFailedState(errorMessage: errorMessage));
print('Error fetching logs: ${errorMessage}');
}
}

View File

@ -10,7 +10,7 @@ abstract class WaterLeakEvent extends Equatable {
class WaterLeakLoading extends WaterLeakEvent {}
class WaterLeakSwitch extends WaterLeakEvent {
final bool switchD;
final String switchD;
final String deviceId;
final String productId;
const WaterLeakSwitch({required this.switchD, this.deviceId = '', this.productId = ''});

View File

@ -1,7 +1,7 @@
import 'package:syncrow_app/features/devices/model/status_model.dart';
class WaterLeakModel {
bool waterContactState;
String waterContactState;
int batteryPercentage;
WaterLeakModel({
@ -10,7 +10,7 @@ class WaterLeakModel {
});
factory WaterLeakModel.fromJson(List<StatusModel> jsonList) {
late bool _waterContactState;
late String _waterContactState;
late int _batteryPercentage;
for (int i = 0; i < jsonList.length; i++) {

View File

@ -8,6 +8,7 @@ import 'package:syncrow_app/features/devices/bloc/water_leak_bloc/water_leak_sta
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_large.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
class NotificationSettingsPage extends StatelessWidget {
@ -32,56 +33,61 @@ class NotificationSettingsPage extends StatelessWidget {
: Column(
children: [
DefaultContainer(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 50,
child: ListTile(
contentPadding: EdgeInsets.zero,
leading: const BodyLarge(
text: 'Low Battery Alarm',
fontWeight: FontWeight.normal,
child: Padding(
padding: const EdgeInsets.only(left: 10, right: 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 50,
child: ListTile(
contentPadding: EdgeInsets.zero,
leading: const BodyMedium(
text: 'Low Battery Alarm',
fontWeight: FontWeight.w400,
fontSize: 15,
),
trailing: Transform.scale(
scale: .8,
child: CupertinoSwitch(
value: waterLeakBloc.lowBattery,
onChanged: (value) {
context.read<WaterLeakBloc>().add(
ToggleLowBatteryEvent(value));
},
applyTheme: true,
)),
),
trailing: Transform.scale(
scale: .8,
child: CupertinoSwitch(
value: waterLeakBloc.lowBattery,
onChanged: (value) {
context.read<WaterLeakBloc>().add(
ToggleLowBatteryEvent(value));
},
applyTheme: true,
)),
),
),
const Divider(
color: ColorsManager.graysColor,
),
SizedBox(
height: 50,
child: ListTile(
contentPadding: EdgeInsets.zero,
leading: const BodyLarge(
text: 'Water Leakage Alarm',
fontWeight: FontWeight.normal,
const Divider(
color: ColorsManager.graysColor,
),
SizedBox(
height: 50,
child: ListTile(
contentPadding: EdgeInsets.zero,
leading: const BodyLarge(
text: 'Water Leakage Alarm',
fontWeight: FontWeight.w400,
fontSize: 15,
),
trailing: Transform.scale(
scale: .8,
child: CupertinoSwitch(
value: waterLeakBloc.closingReminder,
onChanged: (value) {
context.read<WaterLeakBloc>().add(
ToggleClosingReminderEvent(
value));
},
applyTheme: true,
)),
),
trailing: Transform.scale(
scale: .8,
child: CupertinoSwitch(
value: waterLeakBloc.closingReminder,
onChanged: (value) {
context.read<WaterLeakBloc>().add(
ToggleClosingReminderEvent(
value));
},
applyTheme: true,
)),
),
),
],
],
),
),
),
],

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart';
import 'package:intl/intl.dart';
import 'package:syncrow_app/features/devices/bloc/water_leak_bloc/water_leak_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/water_leak_bloc/water_leak_event.dart';
@ -8,212 +9,214 @@ import 'package:syncrow_app/features/devices/model/device_report_model.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
class WaterLeakRecordsScreen extends StatelessWidget {
class WaterLeakRecordsScreen extends StatefulWidget {
final String WLId;
const WaterLeakRecordsScreen({super.key, required this.WLId});
@override
State<WaterLeakRecordsScreen> createState() => _WaterLeakRecordsScreenState();
}
class _WaterLeakRecordsScreenState extends State<WaterLeakRecordsScreen> {
int _selectedIndex = 0; // Track the selected tab index
@override
Widget build(BuildContext context) {
return DefaultScaffold(
title: 'Records',
child: BlocProvider(
create: (context) =>
WaterLeakBloc(WLId: WLId ?? '')..add(const ReportLogsInitial()),
WaterLeakBloc(WLId: widget.WLId)..add(const ReportLogsInitial()),
child: BlocBuilder<WaterLeakBloc, WaterLeakState>(
builder: (context, state) {
final waterSensorBloc = BlocProvider.of<WaterLeakBloc>(context);
final Map<String, List<DeviceEvent>> groupedRecords = {};
builder: (context, state) {
final waterSensorBloc = BlocProvider.of<WaterLeakBloc>(context);
final Map<String, List<DeviceEvent>> groupedRecords = {};
if (state is WaterLeakLoadingState) {
return const Center(
child: DefaultContainer(
width: 50, height: 50, child: CircularProgressIndicator()),
);
} else if (state is UpdateState) {
// Group records by formatted date
if (state is WaterLeakLoadingState) {
return const Center(
child: DefaultContainer(
width: 50, height: 50, child: CircularProgressIndicator()),
);
} else if (state is UpdateState) {
for (var record in waterSensorBloc.recordGroups.data!) {
final DateTime eventDateTime =
DateTime.fromMillisecondsSinceEpoch(record.eventTime!);
final String formattedDate =
DateFormat('EEEE, dd/MM/yyyy').format(eventDateTime);
// Iterate over the data list in DeviceReport
for (var record in waterSensorBloc.recordGroups.data!) {
final DateTime eventDateTime =
DateTime.fromMillisecondsSinceEpoch(record.eventTime!);
final String formattedDate =
DateFormat('EEEE, dd/MM/yyyy').format(eventDateTime);
// Group by formatted date
if (groupedRecords.containsKey(formattedDate)) {
groupedRecords[formattedDate]!.add(record);
} else {
groupedRecords[formattedDate] = [record];
if (groupedRecords.containsKey(formattedDate)) {
groupedRecords[formattedDate]!.add(record);
} else {
groupedRecords[formattedDate] = [record];
}
}
}
}
return DefaultTabController(
// Wrap with DefaultTabController
length: 2,
child: Column(
children: [
Container(
width: MediaQuery.of(context).size.width,
decoration: const ShapeDecoration(
color: ColorsManager.onPrimaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(30)),
),
),
child: TabBar(
onTap: (value) {
// if (value == 0) {
// if (oneTouchBloc.createSchedule == true) {
// // oneTouchBloc.toggleCreateSchedule();
// oneTouchBloc
// .add(const ToggleCreateScheduleEvent(index: 0));
// }
// oneTouchBloc.add(const ToggleSelectedEvent(index: 0));
// } else {
// oneTouchBloc.add(const ToggleSelectedEvent(index: 1));
// }
},
indicatorColor: Colors.white, // Customize the indicator
dividerHeight: 0,
indicatorSize: TabBarIndicatorSize.tab,
indicator: const ShapeDecoration(
color: ColorsManager.slidingBlueColor,
return DefaultTabController(
length: 2,
child: Column(
children: [
Container(
width: MediaQuery.of(context).size.width,
decoration: const ShapeDecoration(
color: ColorsManager.onPrimaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
borderRadius: BorderRadius.all(Radius.circular(30)),
),
),
tabs: [
Tab(
child: Container(
padding: const EdgeInsets.symmetric(vertical: 10),
child: BodySmall(
text: 'Record',
style: context.bodySmall.copyWith(
color: ColorsManager.blackColor,
fontSize: 12,
fontWeight: FontWeight.w400,
child: TabBar(
onTap: (value) {
setState(() {
_selectedIndex = value; // Update selected index
});
},
indicatorColor: Colors.white,
dividerHeight: 0,
indicatorSize: TabBarIndicatorSize.tab,
indicator: const ShapeDecoration(
color: ColorsManager.slidingBlueColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
),
),
tabs: [
Tab(
child: Container(
padding: const EdgeInsets.symmetric(vertical: 10),
child: BodySmall(
text: 'Record',
style: context.bodySmall.copyWith(
color: _selectedIndex == 0
? Colors.white
: ColorsManager.blackColor,
fontSize: 12,
fontWeight: FontWeight.w400,
),
),
),
),
),
Tab(
child: Container(
padding: const EdgeInsets.symmetric(vertical: 10),
child: Text(
'Automation Record',
style: context.bodySmall.copyWith(
color: ColorsManager.blackColor,
fontSize: 12,
fontWeight: FontWeight.w400,
Tab(
child: Container(
padding: const EdgeInsets.symmetric(vertical: 10),
child: Text(
'Automation Record',
style: context.bodySmall.copyWith(
color: _selectedIndex == 1
? Colors.white
: ColorsManager.blackColor,
fontSize: 12,
fontWeight: FontWeight.w400,
),
),
),
),
),
],
],
),
),
),
Expanded(
Expanded(
child: TabBarView(
physics:
const NeverScrollableScrollPhysics(), // Disable swiping
physics: const NeverScrollableScrollPhysics(),
children: [
groupedRecords.isEmpty
? const Center(child: Text('No data available.'))
: ListView.builder(
itemCount: groupedRecords.length,
itemBuilder: (context, index) {
final String date =
groupedRecords.keys.elementAt(index);
final List<DeviceEvent> recordsForDate =
groupedRecords[date]!;
children: [
// Build the ListView with grouped data
ListView.builder(
itemCount: groupedRecords.length,
itemBuilder: (context, index) {
final String date =
groupedRecords.keys.elementAt(index);
final List<DeviceEvent> recordsForDate =
groupedRecords[date]!;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Date header
Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
date,
style: const TextStyle(
color: ColorsManager.grayColor,
fontSize: 13,
fontWeight: FontWeight.w700,
),
),
),
// List of records for the specific date
DefaultContainer(
child: Column(
children: [
...recordsForDate
.asMap()
.entries
.map((entry) {
final int idx = entry.key;
final DeviceEvent record = entry.value;
final DateTime eventDateTime =
DateTime.fromMillisecondsSinceEpoch(
record.eventTime!);
final String formattedTime =
DateFormat('HH:mm:ss')
.format(eventDateTime);
return Column(
children: [
Container(
child: ListTile(
leading: Icon(
record.value == 'true'
? Icons.radio_button_checked
: Icons
.radio_button_unchecked,
color: record.value == 'true'
? Colors.blue
: Colors.grey,
),
title: Text(
record.value == 'true'
? "Opened"
: "Closed",
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
subtitle: Text('$formattedTime'),
),
return Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
date,
style: const TextStyle(
color: ColorsManager.grayColor,
fontSize: 13,
fontWeight: FontWeight.w700,
),
// Only add Divider if it's not the last item
if (idx != recordsForDate.length - 1)
const Divider(
color: ColorsManager.graysColor,
),
],
);
}).toList(),
],
),
),
),
DefaultContainer(
child: Column(
children: [
...recordsForDate
.asMap()
.entries
.map((entry) {
final int idx = entry.key;
final DeviceEvent record =
entry.value;
final DateTime eventDateTime =
DateTime
.fromMillisecondsSinceEpoch(
record.eventTime!);
final String formattedTime =
DateFormat('HH:mm:ss')
.format(eventDateTime);
return Column(
children: [
ListTile(
leading: SvgPicture.asset(
record.value == 'true'
? Assets
.leakDetectedIcon
: Assets
.leakNormalIcon,
height: 25,
width: 25,
),
title: Text(
record.value == 'true'
? "Normal"
: "Leak Detected",
style: const TextStyle(
fontWeight:
FontWeight.bold,
fontSize: 18,
),
),
subtitle:
Text('$formattedTime'),
),
if (idx !=
recordsForDate.length - 1)
const Divider(
color: ColorsManager
.graysColor,
),
],
);
}).toList(),
],
),
),
],
);
},
),
],
);
},
),
Container(
height: 10,
width: 20,
)
]))
],
),
);
}
// return const Center(child: Text('No data available.'));
// },
),
Container(
height: 10,
width: 20,
),
],
),
),
],
),
);
},
),
),
);
}

View File

@ -29,13 +29,14 @@ class WaterLeakScreen extends StatelessWidget {
child: BlocBuilder<WaterLeakBloc, WaterLeakState>(
builder: (context, state) {
final waterLeakBloc = BlocProvider.of<WaterLeakBloc>(context);
WaterLeakModel model =
WaterLeakModel(batteryPercentage: 0, waterContactState: false);
WaterLeakModel model = WaterLeakModel(
batteryPercentage: 0, waterContactState: 'normal');
if (state is LoadingNewSate) {
model = state.waterSensor;
} else if (state is UpdateState) {
model = state.waterSensor;
}
print(model.batteryPercentage);
return state is WaterLeakLoadingState
? const Center(
child: DefaultContainer(
@ -97,7 +98,7 @@ class WaterLeakScreen extends StatelessWidget {
],
),
child: SvgPicture.asset(
model.waterContactState
model.waterContactState == 'normal'
? Assets.normalWaterLeak
: Assets.detectedWaterLeak,
fit: BoxFit.fill,
@ -116,7 +117,8 @@ class WaterLeakScreen extends StatelessWidget {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>
WaterLeakRecordsScreen(WLId: device!.uuid!)),
WaterLeakRecordsScreen(
WLId: device!.uuid!)),
);
},
child: Column(
@ -138,7 +140,6 @@ class WaterLeakScreen extends StatelessWidget {
child: FittedBox(
child: BodySmall(
text: 'Records',
// doorLockButtons.keys.elementAt(index),
textAlign: TextAlign.center,
),
),
@ -155,7 +156,8 @@ class WaterLeakScreen extends StatelessWidget {
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => NotificationSettingsPage()),
builder: (context) =>
NotificationSettingsPage()),
);
},
child: Column(

View File

@ -1076,4 +1076,7 @@ class Assets {
"assets/icons/detected_water_leak.svg";
static const String waterLeakIcon = "assets/icons/waterleak_icon.svg";
static const String leakDetectedIcon = "assets/icons/leak_detected.svg";
static const String leakNormalIcon = "assets/icons/leak_normal_icon.svg";
//leakNormalIcon
}

View File

@ -77,6 +77,7 @@ class DevicesAPI {
.replaceAll('{deviceUuid}', deviceId),
showServerMessage: false,
expectedResponseModel: (json) {
print(json);
return json;
},
);