Files
oott/frontend/lib/utils/api/oott_api_devices.dart
T
rzuastiandClaude Opus 4.8 587e097291 Add permanent device deletion and refine frontend UI/snackbars
Backend:
- Add db::devices::delete to erase a device and its events atomically
- Expose DELETE /api/devices/{mac}/permanently, wired to OpenAPI
- Cover the new db method and endpoint with tests

Frontend:
- Delete action for not-registered devices (detail screen + list row)
  and an opt-in "permanently delete" checkbox in the Forget dialog
- Navigate to the devices list after deleting from the detail screen
- Refine button emphasis to M3: single filled primary, error-colored
  text buttons for destructive actions, Test demoted to filled-tonal
- Flash the backend-config Test button red on a failed connection test
- Render snackbars through a top-level ScaffoldMessenger host so they
  show above dialogs; keep the built-in SnackBar (with an Overlay host)

Docs:
- CLAUDE.md: rustfmt edition 2024, don't revert formatter-only changes,
  prefer built-in Flutter components, follow existing patterns + M3

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-12 09:49:11 -04:00

113 lines
3.0 KiB
Dart

part of '../oott_api.dart';
/// Device endpoints: lookup, listing, registration, updates and event history.
extension DeviceApi on BackendAPI {
Future<Device> getDevice(String macAddress) =>
_getModel('/devices/$macAddress', Device.fromJson);
Future<({List<Device> items, int totalCount})> listDevices({
bool? isRegistered,
String? owner,
DeviceType? deviceType,
String? sortBy,
bool? sortAscending,
int page = 0,
int perPage = 10,
CancelToken? cancelToken,
}) async {
final params = <String, dynamic>{};
if (isRegistered != null) params['is_registered'] = isRegistered;
if (owner != null && owner.isNotEmpty) params['owner'] = owner;
if (deviceType != null) {
params['device_type'] = deviceType == DeviceType.unknown
? ''
: deviceType.apiName;
}
if (sortBy != null) params['sort_by'] = sortBy;
if (sortAscending != null) {
params['sort_order'] = sortAscending ? 'asc' : 'desc';
}
params['page_offset'] = page * perPage;
params['page_limit'] = perPage;
final response = await _dio.get(
'/devices',
queryParameters: params,
cancelToken: cancelToken,
);
return _paginate(
response.data as Map<String, dynamic>,
(item) => Device.fromJson(item as Map<String, dynamic>),
);
}
Future<void> registerDevice(
String macAddress,
String owner,
String deviceType, {
String? name,
}) async {
await _dio.put(
'/devices',
data: {
'mac_address': macAddress,
'owner': owner,
'device_type': deviceType,
'name': ?name,
},
);
}
Future<void> updateDevice(
String macAddress,
String owner,
String deviceType,
String vendor, {
String? name,
}) async {
await _dio.put(
'/devices/$macAddress',
data: {
'owner': owner,
'device_type': deviceType,
'vendor': vendor,
'name': name,
},
);
}
Future<void> forgetDevice(String macAddress) async {
await _dio.delete('/devices/$macAddress');
}
/// Permanently deletes a device and all of its event history. This cannot be undone.
Future<void> deleteDevice(String macAddress) async {
await _dio.delete('/devices/$macAddress/permanently');
}
Future<List<DeviceEvent>> getDeviceEvents(
String macAddress, {
DateTime? createdFrom,
}) async {
final queryParams = <String, dynamic>{};
if (createdFrom != null) {
queryParams['created_from'] = createdFrom.toUtc().toIso8601String();
}
final response = await _dio.get(
'/devices/$macAddress/events',
queryParameters: queryParams.isEmpty ? null : queryParams,
);
return (response.data as List)
.map((item) => DeviceEvent.fromJson(item as Map<String, dynamic>))
.toList();
}
Future<DeviceSummary> getDeviceSummary({CancelToken? cancelToken}) =>
_getModel(
'/devices/summary',
DeviceSummary.fromJson,
cancelToken: cancelToken,
);
}