mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
Created a bloc for loading and managing the state of device location data.
This commit is contained in:
@ -0,0 +1,50 @@
|
|||||||
|
import 'package:bloc/bloc.dart';
|
||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
import 'package:syncrow_web/pages/analytics/models/device_location_info.dart';
|
||||||
|
import 'package:syncrow_web/pages/analytics/params/get_device_location_data_param.dart';
|
||||||
|
import 'package:syncrow_web/pages/analytics/services/device_location/device_location_service.dart';
|
||||||
|
|
||||||
|
part 'device_location_event.dart';
|
||||||
|
part 'device_location_state.dart';
|
||||||
|
|
||||||
|
class DeviceLocationBloc extends Bloc<DeviceLocationEvent, DeviceLocationState> {
|
||||||
|
DeviceLocationBloc(
|
||||||
|
this._deviceLocationService,
|
||||||
|
) : super(const DeviceLocationState()) {
|
||||||
|
on<LoadDeviceLocationEvent>(_onLoadDeviceLocation);
|
||||||
|
on<ClearDeviceLocationEvent>(_onClearDeviceLocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
final DeviceLocationService _deviceLocationService;
|
||||||
|
|
||||||
|
Future<void> _onLoadDeviceLocation(
|
||||||
|
LoadDeviceLocationEvent event,
|
||||||
|
Emitter<DeviceLocationState> emit,
|
||||||
|
) async {
|
||||||
|
emit(const DeviceLocationState(status: DeviceLocationStatus.loading));
|
||||||
|
|
||||||
|
try {
|
||||||
|
final locationInfo = await _deviceLocationService.get(event.param);
|
||||||
|
emit(
|
||||||
|
DeviceLocationState(
|
||||||
|
status: DeviceLocationStatus.success,
|
||||||
|
locationInfo: locationInfo,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
emit(
|
||||||
|
DeviceLocationState(
|
||||||
|
status: DeviceLocationStatus.failure,
|
||||||
|
errorMessage: e.toString(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onClearDeviceLocation(
|
||||||
|
ClearDeviceLocationEvent event,
|
||||||
|
Emitter<DeviceLocationState> emit,
|
||||||
|
) {
|
||||||
|
emit(const DeviceLocationState());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
part of 'device_location_bloc.dart';
|
||||||
|
|
||||||
|
sealed class DeviceLocationEvent extends Equatable {
|
||||||
|
const DeviceLocationEvent();
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [];
|
||||||
|
}
|
||||||
|
|
||||||
|
class LoadDeviceLocationEvent extends DeviceLocationEvent {
|
||||||
|
const LoadDeviceLocationEvent(this.param);
|
||||||
|
|
||||||
|
final GetDeviceLocationDataParam param;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [param];
|
||||||
|
}
|
||||||
|
|
||||||
|
class ClearDeviceLocationEvent extends DeviceLocationEvent {
|
||||||
|
const ClearDeviceLocationEvent();
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
part of 'device_location_bloc.dart';
|
||||||
|
|
||||||
|
enum DeviceLocationStatus { initial, loading, success, failure }
|
||||||
|
|
||||||
|
class DeviceLocationState extends Equatable {
|
||||||
|
const DeviceLocationState({
|
||||||
|
this.status = DeviceLocationStatus.initial,
|
||||||
|
this.locationInfo,
|
||||||
|
this.errorMessage,
|
||||||
|
});
|
||||||
|
|
||||||
|
final DeviceLocationStatus status;
|
||||||
|
final DeviceLocationInfo? locationInfo;
|
||||||
|
final String? errorMessage;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [status, locationInfo, errorMessage];
|
||||||
|
}
|
Reference in New Issue
Block a user