Files
syncrow-app/lib/services/api/home_management_api.dart
2024-10-30 19:16:10 +04:00

82 lines
2.4 KiB
Dart

import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/auth/model/user_model.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
import 'package:syncrow_app/services/api/http_service.dart';
class HomeManagementAPI {
static final HTTPService _httpService = HTTPService();
static Future<List<DeviceModel>> fetchDevicesByUserId() async {
var storage = const FlutterSecureStorage();
var userId = await storage.read(key: UserModel.userUuidKey) ?? '';
List<DeviceModel> list = [];
await _httpService.get(
path: ApiEndpoints.getDevicesByUserId.replaceAll("{userId}", userId),
showServerMessage: false,
expectedResponseModel: (json) {
json.forEach((value) {
list.add(DeviceModel.fromJson(value));
});
});
return list;
}
static Future<List<DeviceModel>> fetchDevicesByUnitId() async {
List<DeviceModel> list = [];
try {
// Retrieve selected space details
final selectedSpace = HomeCubit.getInstance().selectedSpace;
final communityUuid = selectedSpace?.community?.uuid ?? '';
final spaceUuid = selectedSpace?.id ?? '';
// Ensure both placeholders are replaced
final path = ApiEndpoints.spaceDevices
.replaceAll("{communityUuid}", communityUuid)
.replaceAll("{spaceUuid}", spaceUuid);
// Debugging: Log the path
print("Fetching devices with path: $path");
await _httpService.get(
path: path,
showServerMessage: false,
expectedResponseModel: (json) {
json.forEach((value) {
list.add(DeviceModel.fromJson(value));
});
},
);
// Debugging: Log successful fetch
print("Successfully fetched ${list.length} devices.");
} catch (e) {
// Log the error for debugging
print("Error fetching devices for the unit: $e");
}
return list;
}
static Future<Map<String, dynamic>> assignDeviceToRoom(
Map<String, String> body) async {
try {
final response = await _httpService.put(
path: ApiEndpoints.assignDeviceToRoom,
body: body,
expectedResponseModel: (json) {
return json;
},
);
return response;
} catch (e) {
rethrow;
}
}
}