2022-05-26 01:02:06 +00:00
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
2022-07-12 12:54:16 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/server.dart';
|
2022-05-26 01:02:06 +00:00
|
|
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
|
|
|
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/json/api_token.dart';
|
|
|
|
|
|
|
|
part 'devices_state.dart';
|
|
|
|
|
|
|
|
class ApiDevicesCubit
|
|
|
|
extends ServerInstallationDependendCubit<ApiDevicesState> {
|
2022-06-05 19:36:32 +00:00
|
|
|
ApiDevicesCubit(final ServerInstallationCubit serverInstallationCubit)
|
2022-05-26 01:02:06 +00:00
|
|
|
: super(serverInstallationCubit, const ApiDevicesState.initial());
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final ServerApi api = ServerApi();
|
2022-05-26 01:02:06 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void load() async {
|
|
|
|
if (serverInstallationCubit.state is ServerInstallationFinished) {
|
2022-06-27 07:07:11 +00:00
|
|
|
_refetch();
|
2022-05-26 01:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> refresh() async {
|
|
|
|
emit(const ApiDevicesState([], LoadingStatus.refreshing));
|
2022-06-27 07:07:11 +00:00
|
|
|
_refetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _refetch() async {
|
2022-06-05 19:36:32 +00:00
|
|
|
final List<ApiToken>? devices = await _getApiTokens();
|
2022-05-26 01:02:06 +00:00
|
|
|
if (devices != null) {
|
|
|
|
emit(ApiDevicesState(devices, LoadingStatus.success));
|
|
|
|
} else {
|
|
|
|
emit(const ApiDevicesState([], LoadingStatus.error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<ApiToken>?> _getApiTokens() async {
|
2022-06-05 19:36:32 +00:00
|
|
|
final ApiResponse<List<ApiToken>> response = await api.getApiTokens();
|
2022-05-26 01:02:06 +00:00
|
|
|
if (response.isSuccess) {
|
|
|
|
return response.data;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<void> deleteDevice(final ApiToken device) async {
|
|
|
|
final ApiResponse<void> response = await api.deleteApiToken(device.name);
|
2022-05-26 01:02:06 +00:00
|
|
|
if (response.isSuccess) {
|
2022-06-05 22:40:34 +00:00
|
|
|
emit(
|
|
|
|
ApiDevicesState(
|
2022-06-05 19:36:32 +00:00
|
|
|
state.devices.where((final d) => d.name != device.name).toList(),
|
2022-06-05 22:40:34 +00:00
|
|
|
LoadingStatus.success,
|
|
|
|
),
|
|
|
|
);
|
2022-05-26 01:02:06 +00:00
|
|
|
} else {
|
|
|
|
getIt<NavigationService>()
|
|
|
|
.showSnackBar(response.errorMessage ?? 'Error deleting device');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String?> getNewDeviceKey() async {
|
2022-06-05 19:36:32 +00:00
|
|
|
final ApiResponse<String> response = await api.createDeviceToken();
|
2022-05-26 01:02:06 +00:00
|
|
|
if (response.isSuccess) {
|
|
|
|
return response.data;
|
|
|
|
} else {
|
|
|
|
getIt<NavigationService>().showSnackBar(
|
2022-06-05 22:40:34 +00:00
|
|
|
response.errorMessage ?? 'Error getting new device key',
|
|
|
|
);
|
2022-05-26 01:02:06 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void clear() {
|
|
|
|
emit(const ApiDevicesState.initial());
|
|
|
|
}
|
|
|
|
}
|