mirror of
https://github.com/SyncrowIOT/web.git
synced 2026-10-23 09:37:56 +00:00
Refactor LivingRoomBloc to integrate new service dependencies and utilize a factory for instantiation. Enhanced event handling methods for improved error management and state updates, including real-time status listening from Firebase.
This commit is contained in:
@@ -1,12 +1,14 @@
|
|||||||
// ignore_for_file: invalid_use_of_visible_for_testing_member
|
|
||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:bloc/bloc.dart';
|
import 'dart:developer';
|
||||||
|
|
||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
import 'package:firebase_database/firebase_database.dart';
|
import 'package:firebase_database/firebase_database.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
|
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/all_devices/models/factory_reset_model.dart';
|
import 'package:syncrow_web/pages/device_managment/all_devices/models/factory_reset_model.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/three_gang_switch/models/living_room_model.dart';
|
import 'package:syncrow_web/pages/device_managment/three_gang_switch/models/living_room_model.dart';
|
||||||
|
import 'package:syncrow_web/services/batch_control_devices_service.dart';
|
||||||
|
import 'package:syncrow_web/services/control_device_service.dart';
|
||||||
import 'package:syncrow_web/services/devices_mang_api.dart';
|
import 'package:syncrow_web/services/devices_mang_api.dart';
|
||||||
|
|
||||||
part 'living_room_event.dart';
|
part 'living_room_event.dart';
|
||||||
@@ -15,9 +17,14 @@ part 'living_room_state.dart';
|
|||||||
class LivingRoomBloc extends Bloc<LivingRoomEvent, LivingRoomState> {
|
class LivingRoomBloc extends Bloc<LivingRoomEvent, LivingRoomState> {
|
||||||
late LivingRoomStatusModel deviceStatus;
|
late LivingRoomStatusModel deviceStatus;
|
||||||
final String deviceId;
|
final String deviceId;
|
||||||
Timer? _timer;
|
final ControlDeviceService controlDeviceService;
|
||||||
|
final BatchControlDevicesService batchControlDevicesService;
|
||||||
|
|
||||||
LivingRoomBloc({required this.deviceId}) : super(LivingRoomInitial()) {
|
LivingRoomBloc({
|
||||||
|
required this.deviceId,
|
||||||
|
required this.controlDeviceService,
|
||||||
|
required this.batchControlDevicesService,
|
||||||
|
}) : super(LivingRoomInitial()) {
|
||||||
on<LivingRoomFetchDeviceStatusEvent>(_onFetchDeviceStatus);
|
on<LivingRoomFetchDeviceStatusEvent>(_onFetchDeviceStatus);
|
||||||
on<LivingRoomControl>(_livingRoomControl);
|
on<LivingRoomControl>(_livingRoomControl);
|
||||||
on<LivingRoomBatchControl>(_livingRoomBatchControl);
|
on<LivingRoomBatchControl>(_livingRoomBatchControl);
|
||||||
@@ -26,156 +33,108 @@ class LivingRoomBloc extends Bloc<LivingRoomEvent, LivingRoomState> {
|
|||||||
on<StatusUpdated>(_onStatusUpdated);
|
on<StatusUpdated>(_onStatusUpdated);
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onFetchDeviceStatus(LivingRoomFetchDeviceStatusEvent event,
|
Future<void> _onFetchDeviceStatus(
|
||||||
Emitter<LivingRoomState> emit) async {
|
LivingRoomFetchDeviceStatusEvent event,
|
||||||
|
Emitter<LivingRoomState> emit,
|
||||||
|
) async {
|
||||||
emit(LivingRoomDeviceStatusLoading());
|
emit(LivingRoomDeviceStatusLoading());
|
||||||
try {
|
try {
|
||||||
final status =
|
final status = await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
||||||
await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
|
||||||
deviceStatus =
|
|
||||||
LivingRoomStatusModel.fromJson(event.deviceId, status.status);
|
|
||||||
_listenToChanges(deviceId);
|
_listenToChanges(deviceId);
|
||||||
|
deviceStatus = LivingRoomStatusModel.fromJson(event.deviceId, status.status);
|
||||||
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(LivingRoomDeviceManagementError(e.toString()));
|
emit(LivingRoomDeviceManagementError(e.toString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _livingRoomControl(
|
void _listenToChanges(String deviceId) {
|
||||||
LivingRoomControl event, Emitter<LivingRoomState> emit) async {
|
try {
|
||||||
final oldValue = _getValueByCode(event.code);
|
final ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
|
||||||
|
ref.onValue.listen((event) {
|
||||||
|
final eventsMap = event.snapshot.value as Map<dynamic, dynamic>;
|
||||||
|
|
||||||
|
List<Status> statusList = [];
|
||||||
|
eventsMap['status'].forEach((element) {
|
||||||
|
statusList.add(
|
||||||
|
Status(code: element['code'], value: element['value']),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
deviceStatus = LivingRoomStatusModel.fromJson(deviceId, statusList);
|
||||||
|
add(StatusUpdated(deviceStatus));
|
||||||
|
});
|
||||||
|
} catch (_) {
|
||||||
|
log('Error listening to changes');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onStatusUpdated(
|
||||||
|
StatusUpdated event,
|
||||||
|
Emitter<LivingRoomState> emit,
|
||||||
|
) {
|
||||||
|
deviceStatus = event.deviceStatus;
|
||||||
|
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _livingRoomControl(
|
||||||
|
LivingRoomControl event,
|
||||||
|
Emitter<LivingRoomState> emit,
|
||||||
|
) async {
|
||||||
|
emit(LivingRoomDeviceStatusLoading());
|
||||||
_updateLocalValue(event.code, event.value);
|
_updateLocalValue(event.code, event.value);
|
||||||
|
|
||||||
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
||||||
|
|
||||||
await _runDebounce(
|
try {
|
||||||
deviceId: event.deviceId,
|
await controlDeviceService.controlDevice(
|
||||||
code: event.code,
|
deviceUuid: event.deviceId,
|
||||||
value: event.value,
|
status: Status(code: event.code, value: event.value),
|
||||||
oldValue: oldValue,
|
);
|
||||||
emit: emit,
|
} catch (e) {
|
||||||
isBatch: false,
|
_updateLocalValue(event.code, !event.value);
|
||||||
);
|
emit(LivingRoomDeviceManagementError(e.toString()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _runDebounce({
|
Future<void> _livingRoomBatchControl(
|
||||||
required dynamic deviceId,
|
LivingRoomBatchControl event,
|
||||||
required String code,
|
Emitter<LivingRoomState> emit,
|
||||||
required dynamic value,
|
) async {
|
||||||
required dynamic oldValue,
|
emit(LivingRoomDeviceStatusLoading());
|
||||||
required Emitter<LivingRoomState> emit,
|
_updateLocalValue(event.code, event.value);
|
||||||
required bool isBatch,
|
|
||||||
}) async {
|
|
||||||
late String id;
|
|
||||||
|
|
||||||
if (deviceId is List) {
|
|
||||||
id = deviceId.first;
|
|
||||||
} else {
|
|
||||||
id = deviceId;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_timer != null) {
|
|
||||||
_timer!.cancel();
|
|
||||||
}
|
|
||||||
_timer = Timer(const Duration(seconds: 1), () async {
|
|
||||||
try {
|
|
||||||
late bool response;
|
|
||||||
if (isBatch) {
|
|
||||||
response = await DevicesManagementApi()
|
|
||||||
.deviceBatchControl(deviceId, code, value);
|
|
||||||
} else {
|
|
||||||
response = await DevicesManagementApi()
|
|
||||||
.deviceControl(deviceId, Status(code: code, value: value));
|
|
||||||
}
|
|
||||||
if (!response) {
|
|
||||||
_revertValueAndEmit(id, code, oldValue, emit);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
_revertValueAndEmit(id, code, oldValue, emit);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
void _revertValueAndEmit(String deviceId, String code, dynamic oldValue,
|
|
||||||
Emitter<LivingRoomState> emit) {
|
|
||||||
_updateLocalValue(code, oldValue);
|
|
||||||
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
||||||
}
|
|
||||||
|
|
||||||
void _updateLocalValue(String code, dynamic value) {
|
try {
|
||||||
switch (code) {
|
await batchControlDevicesService.batchControlDevices(
|
||||||
case 'switch_1':
|
uuids: event.devicesIds,
|
||||||
if (value is bool) {
|
code: event.code,
|
||||||
deviceStatus = deviceStatus.copyWith(switch1: value);
|
value: event.value,
|
||||||
}
|
);
|
||||||
break;
|
} catch (e) {
|
||||||
case 'switch_2':
|
_updateLocalValue(event.code, !event.value);
|
||||||
if (value is bool) {
|
emit(LivingRoomDeviceManagementError(e.toString()));
|
||||||
deviceStatus = deviceStatus.copyWith(switch2: value);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'switch_3':
|
|
||||||
if (value is bool) {
|
|
||||||
deviceStatus = deviceStatus.copyWith(switch3: value);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
|
||||||
}
|
|
||||||
|
|
||||||
dynamic _getValueByCode(String code) {
|
|
||||||
switch (code) {
|
|
||||||
case 'switch_1':
|
|
||||||
return deviceStatus.switch1;
|
|
||||||
case 'switch_2':
|
|
||||||
return deviceStatus.switch2;
|
|
||||||
case 'switch_3':
|
|
||||||
return deviceStatus.switch3;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _livingRoomFetchBatchControl(
|
Future<void> _livingRoomFetchBatchControl(
|
||||||
LivingRoomFetchBatchEvent event, Emitter<LivingRoomState> emit) async {
|
LivingRoomFetchBatchEvent event,
|
||||||
|
Emitter<LivingRoomState> emit,
|
||||||
|
) async {
|
||||||
emit(LivingRoomDeviceStatusLoading());
|
emit(LivingRoomDeviceStatusLoading());
|
||||||
try {
|
try {
|
||||||
final status =
|
final status = await DevicesManagementApi().getBatchStatus(event.devicesIds);
|
||||||
await DevicesManagementApi().getBatchStatus(event.devicesIds);
|
|
||||||
deviceStatus =
|
deviceStatus =
|
||||||
LivingRoomStatusModel.fromJson(event.devicesIds.first, status.status);
|
LivingRoomStatusModel.fromJson(event.devicesIds.first, status.status);
|
||||||
// for (var deviceId in event.devicesIds) {
|
|
||||||
// _listenToChanges(deviceId);
|
|
||||||
// }
|
|
||||||
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(LivingRoomDeviceManagementError(e.toString()));
|
emit(LivingRoomDeviceManagementError(e.toString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _livingRoomBatchControl(
|
Future<void> _livingRoomFactoryReset(
|
||||||
LivingRoomBatchControl event, Emitter<LivingRoomState> emit) async {
|
LivingRoomFactoryResetEvent event,
|
||||||
final oldValue = _getValueByCode(event.code);
|
Emitter<LivingRoomState> emit,
|
||||||
|
) async {
|
||||||
_updateLocalValue(event.code, event.value);
|
|
||||||
|
|
||||||
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
|
||||||
|
|
||||||
await _runDebounce(
|
|
||||||
deviceId: event.devicesIds,
|
|
||||||
code: event.code,
|
|
||||||
value: event.value,
|
|
||||||
oldValue: oldValue,
|
|
||||||
emit: emit,
|
|
||||||
isBatch: true,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
FutureOr<void> _livingRoomFactoryReset(
|
|
||||||
LivingRoomFactoryResetEvent event, Emitter<LivingRoomState> emit) async {
|
|
||||||
emit(LivingRoomDeviceStatusLoading());
|
emit(LivingRoomDeviceStatusLoading());
|
||||||
try {
|
try {
|
||||||
final response = await DevicesManagementApi().factoryReset(
|
final response = await DevicesManagementApi().factoryReset(
|
||||||
@@ -183,42 +142,28 @@ class LivingRoomBloc extends Bloc<LivingRoomEvent, LivingRoomState> {
|
|||||||
event.uuid,
|
event.uuid,
|
||||||
);
|
);
|
||||||
if (!response) {
|
if (!response) {
|
||||||
emit(const LivingRoomDeviceManagementError('Failed'));
|
emit(const LivingRoomDeviceManagementError('Failed to reset device'));
|
||||||
} else {
|
} else {
|
||||||
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
add(LivingRoomFetchDeviceStatusEvent(event.uuid));
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(LivingRoomDeviceManagementError(e.toString()));
|
emit(LivingRoomDeviceManagementError(e.toString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_listenToChanges(deviceId) {
|
void _updateLocalValue(String code, dynamic value) {
|
||||||
try {
|
if (value is! bool) return;
|
||||||
DatabaseReference ref =
|
|
||||||
FirebaseDatabase.instance.ref('device-status/$deviceId');
|
|
||||||
Stream<DatabaseEvent> stream = ref.onValue;
|
|
||||||
|
|
||||||
stream.listen((DatabaseEvent event) {
|
switch (code) {
|
||||||
Map<dynamic, dynamic> usersMap =
|
case 'switch_1':
|
||||||
event.snapshot.value as Map<dynamic, dynamic>;
|
deviceStatus = deviceStatus.copyWith(switch1: value);
|
||||||
|
break;
|
||||||
List<Status> statusList = [];
|
case 'switch_2':
|
||||||
usersMap['status'].forEach((element) {
|
deviceStatus = deviceStatus.copyWith(switch2: value);
|
||||||
statusList
|
break;
|
||||||
.add(Status(code: element['code'], value: element['value']));
|
case 'switch_3':
|
||||||
});
|
deviceStatus = deviceStatus.copyWith(switch3: value);
|
||||||
|
break;
|
||||||
deviceStatus =
|
}
|
||||||
LivingRoomStatusModel.fromJson(usersMap['productUuid'], statusList);
|
|
||||||
if (!isClosed) {
|
|
||||||
add(StatusUpdated(deviceStatus));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (_) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
void _onStatusUpdated(StatusUpdated event, Emitter<LivingRoomState> emit) {
|
|
||||||
deviceStatus = event.deviceStatus;
|
|
||||||
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import 'package:syncrow_web/pages/device_managment/factories/device_bloc_dependencies_factory.dart';
|
||||||
|
import 'package:syncrow_web/pages/device_managment/three_gang_switch/bloc/living_room_bloc.dart';
|
||||||
|
|
||||||
|
abstract final class LivingRoomBlocFactory {
|
||||||
|
const LivingRoomBlocFactory._();
|
||||||
|
|
||||||
|
static LivingRoomBloc create({
|
||||||
|
required String deviceId,
|
||||||
|
}) {
|
||||||
|
return LivingRoomBloc(
|
||||||
|
deviceId: deviceId,
|
||||||
|
controlDeviceService:
|
||||||
|
DeviceBlocDependenciesFactory.createControlDeviceService(),
|
||||||
|
batchControlDevicesService:
|
||||||
|
DeviceBlocDependenciesFactory.createBatchControlDevicesService(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import 'package:syncrow_web/pages/device_managment/all_devices/models/factory_re
|
|||||||
import 'package:syncrow_web/pages/device_managment/shared/batch_control/factory_reset.dart';
|
import 'package:syncrow_web/pages/device_managment/shared/batch_control/factory_reset.dart';
|
||||||
// import 'package:syncrow_web/pages/device_managment/shared/batch_control/firmware_update.dart';
|
// import 'package:syncrow_web/pages/device_managment/shared/batch_control/firmware_update.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/three_gang_switch/bloc/living_room_bloc.dart';
|
import 'package:syncrow_web/pages/device_managment/three_gang_switch/bloc/living_room_bloc.dart';
|
||||||
|
import 'package:syncrow_web/pages/device_managment/three_gang_switch/factories/living_room_bloc_factory.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/three_gang_switch/models/living_room_model.dart';
|
import 'package:syncrow_web/pages/device_managment/three_gang_switch/models/living_room_model.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/shared/toggle_widget.dart';
|
import 'package:syncrow_web/pages/device_managment/shared/toggle_widget.dart';
|
||||||
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';
|
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';
|
||||||
@@ -17,7 +18,7 @@ class LivingRoomBatchControlsView extends StatelessWidget with HelperResponsiveL
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocProvider(
|
return BlocProvider(
|
||||||
create: (context) =>
|
create: (context) =>
|
||||||
LivingRoomBloc(deviceId: deviceIds.first)..add(LivingRoomFetchBatchEvent(deviceIds)),
|
LivingRoomBlocFactory.create(deviceId: deviceIds.first)..add(LivingRoomFetchBatchEvent(deviceIds)),
|
||||||
child: BlocBuilder<LivingRoomBloc, LivingRoomState>(
|
child: BlocBuilder<LivingRoomBloc, LivingRoomState>(
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
if (state is LivingRoomDeviceStatusLoading) {
|
if (state is LivingRoomDeviceStatusLoading) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/three_gang_switch/bloc/living_room_bloc.dart';
|
import 'package:syncrow_web/pages/device_managment/three_gang_switch/bloc/living_room_bloc.dart';
|
||||||
|
import 'package:syncrow_web/pages/device_managment/three_gang_switch/factories/living_room_bloc_factory.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/three_gang_switch/models/living_room_model.dart';
|
import 'package:syncrow_web/pages/device_managment/three_gang_switch/models/living_room_model.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/shared/toggle_widget.dart';
|
import 'package:syncrow_web/pages/device_managment/shared/toggle_widget.dart';
|
||||||
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';
|
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';
|
||||||
@@ -14,7 +15,7 @@ class LivingRoomDeviceControlsView extends StatelessWidget
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocProvider(
|
return BlocProvider(
|
||||||
create: (context) => LivingRoomBloc(deviceId: deviceId)
|
create: (context) => LivingRoomBlocFactory.create(deviceId: deviceId)
|
||||||
..add(LivingRoomFetchDeviceStatusEvent(deviceId)),
|
..add(LivingRoomFetchDeviceStatusEvent(deviceId)),
|
||||||
child: BlocBuilder<LivingRoomBloc, LivingRoomState>(
|
child: BlocBuilder<LivingRoomBloc, LivingRoomState>(
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
|
|||||||
Reference in New Issue
Block a user