132 lines
3.3 KiB
Dart
132 lines
3.3 KiB
Dart
import 'package:didvan/models/ai/bot_assistants_model.dart';
|
|
import 'package:didvan/models/ai/bot_assistants_req_model.dart';
|
|
import 'package:didvan/models/ai/bots_model.dart';
|
|
import 'package:didvan/models/ai/tools_model.dart';
|
|
import 'package:didvan/models/enums.dart';
|
|
import 'package:didvan/providers/core.dart';
|
|
import 'package:didvan/services/network/request.dart';
|
|
import 'package:didvan/services/network/request_helper.dart';
|
|
|
|
class CreateBotAssistantsState extends CoreProvier {
|
|
List<BotsModel> imageBots = [];
|
|
bool loadingImageBots = false;
|
|
bool loadingCreate = false;
|
|
bool loadingName = false;
|
|
bool successName = false;
|
|
bool loading = false;
|
|
BotAssistants? assistant;
|
|
|
|
void getImageToolsBots() async {
|
|
loadingImageBots = true;
|
|
|
|
final service = RequestService(
|
|
RequestHelper.tools(),
|
|
);
|
|
await service.httpGet();
|
|
if (service.isSuccess) {
|
|
final ToolsModel toolsModel = ToolsModel.fromJson(service.result);
|
|
if (toolsModel.tools != null) {
|
|
imageBots = toolsModel.tools!.first.bots ?? [];
|
|
} else {
|
|
imageBots = [];
|
|
}
|
|
|
|
appState = AppState.idle;
|
|
loadingImageBots = false;
|
|
update();
|
|
return;
|
|
}
|
|
appState = AppState.failed;
|
|
loadingImageBots = false;
|
|
update();
|
|
}
|
|
|
|
Future createAssistants(
|
|
{required final BotAssistantsReqModel data, final int? id}) async {
|
|
loadingCreate = true;
|
|
update();
|
|
|
|
final service = RequestService(
|
|
(id != null
|
|
? RequestHelper.updateAssistants(id)
|
|
: RequestHelper.createAssistants()),
|
|
body: data.toJson());
|
|
await service.multipartFilesCreateAssismants(
|
|
files: data.files,
|
|
image: data.image,
|
|
method: id != null ? 'PUT' : 'POST');
|
|
if (service.isSuccess) {
|
|
appState = AppState.idle;
|
|
loadingCreate = false;
|
|
update();
|
|
return;
|
|
}
|
|
appState = AppState.failed;
|
|
loadingCreate = false;
|
|
update();
|
|
}
|
|
|
|
Future getAnAssistant({required final int id}) async {
|
|
loading = true;
|
|
update();
|
|
|
|
final service = RequestService(
|
|
RequestHelper.getAssistant(id),
|
|
);
|
|
await service.httpGet();
|
|
if (service.isSuccess) {
|
|
assistant = BotAssistants.fromJson(service.result['bot']);
|
|
appState = AppState.idle;
|
|
loading = false;
|
|
update();
|
|
return;
|
|
}
|
|
appState = AppState.failed;
|
|
loading = false;
|
|
update();
|
|
}
|
|
|
|
Future deleteAssistants({required final int id}) async {
|
|
loadingCreate = true;
|
|
update();
|
|
|
|
final service = RequestService(RequestHelper.getAssistant(id));
|
|
await service.delete();
|
|
if (service.isSuccess) {
|
|
appState = AppState.idle;
|
|
loadingCreate = false;
|
|
update();
|
|
return;
|
|
}
|
|
appState = AppState.failed;
|
|
loadingCreate = false;
|
|
update();
|
|
}
|
|
|
|
Future getAssistantsName({required final String name}) async {
|
|
loadingName = true;
|
|
update();
|
|
|
|
final service =
|
|
RequestService(RequestHelper.nameOfAssistant(), body: {'name': name});
|
|
await service.post();
|
|
if (service.isSuccess) {
|
|
appState = AppState.idle;
|
|
loadingName = false;
|
|
if (service.result['available'] as bool) {
|
|
successName = true;
|
|
} else {
|
|
successName = false;
|
|
}
|
|
|
|
update();
|
|
return;
|
|
}
|
|
appState = AppState.failed;
|
|
loadingName = false;
|
|
successName = false;
|
|
|
|
update();
|
|
}
|
|
}
|