112 lines
2.9 KiB
Dart
112 lines
2.9 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:didvan/models/ai/bots_model.dart';
|
|
import 'package:didvan/models/ai/chats_model.dart';
|
|
import 'package:didvan/models/enums.dart';
|
|
import 'package:didvan/models/view/alert_data.dart';
|
|
import 'package:didvan/providers/core.dart';
|
|
import 'package:didvan/services/network/request.dart';
|
|
import 'package:didvan/services/network/request_helper.dart';
|
|
import 'package:didvan/utils/action_sheet.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
class HistoryAiChatState extends CoreProvier {
|
|
final List<ChatsModel> chats = [];
|
|
final List<int> chatsToDelete = [];
|
|
final List<BotsModel> bots = [];
|
|
ValueNotifier<bool> loadingBots = ValueNotifier(false);
|
|
Timer? timer;
|
|
String search = '';
|
|
|
|
Future<void> getChats() async {
|
|
final service = RequestService(
|
|
RequestHelper.aiChats(),
|
|
);
|
|
await service.httpGet();
|
|
if (service.isSuccess) {
|
|
chats.clear();
|
|
final messages = service.result['chats'];
|
|
for (var i = 0; i < messages.length; i++) {
|
|
chats.add(ChatsModel.fromJson(messages[i]));
|
|
}
|
|
appState = AppState.idle;
|
|
update();
|
|
return;
|
|
}
|
|
appState = AppState.failed;
|
|
update();
|
|
}
|
|
|
|
Future<void> getBots() async {
|
|
loadingBots.value = true;
|
|
final service = RequestService(
|
|
RequestHelper.aiBots(),
|
|
);
|
|
await service.httpGet();
|
|
if (service.isSuccess) {
|
|
bots.clear();
|
|
final messages = service.result['bots'];
|
|
for (var i = 0; i < messages.length; i++) {
|
|
bots.add(BotsModel.fromJson(messages[i]));
|
|
}
|
|
appState = AppState.idle;
|
|
loadingBots.value = false;
|
|
update();
|
|
return;
|
|
}
|
|
appState = AppState.failed;
|
|
loadingBots.value = false;
|
|
update();
|
|
}
|
|
|
|
Future<void> getSearchBots(String q) async {
|
|
loadingBots.value = true;
|
|
final service = RequestService(
|
|
RequestHelper.aiSearchBots(q),
|
|
);
|
|
await service.httpGet();
|
|
if (service.isSuccess) {
|
|
bots.clear();
|
|
final messages = service.result['bots'];
|
|
for (var i = 0; i < messages.length; i++) {
|
|
bots.add(BotsModel.fromJson(messages[i]));
|
|
}
|
|
appState = AppState.idle;
|
|
loadingBots.value = false;
|
|
update();
|
|
|
|
return;
|
|
}
|
|
appState = AppState.failed;
|
|
loadingBots.value = false;
|
|
update();
|
|
}
|
|
|
|
Future<void> addChatToDelete() async {
|
|
final service = RequestService(RequestHelper.aiDeleteChats(),
|
|
body: {"ids": chatsToDelete});
|
|
await service.delete();
|
|
if (service.isSuccess) {
|
|
final List<ChatsModel> cs = [];
|
|
for (var chat in chats) {
|
|
if (!chatsToDelete.contains(chat.id)) {
|
|
cs.add(chat);
|
|
}
|
|
}
|
|
chatsToDelete.clear();
|
|
chats.clear();
|
|
chats.addAll(cs);
|
|
|
|
appState = AppState.idle;
|
|
update();
|
|
|
|
return;
|
|
}
|
|
appState = AppState.failed;
|
|
await ActionSheetUtils.showAlert(AlertData(
|
|
message: 'خطا در برقراری ارتباط', aLertType: ALertType.error));
|
|
|
|
update();
|
|
}
|
|
}
|