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 chats = []; final List archivedChats = []; // final List chatsToDelete = []; final List bots = []; BotsModel? bot; ValueNotifier loadingBots = ValueNotifier(false); bool loadingchangeTitle = false; bool loadingdeleteAll = false; bool refresh = false; Timer? timer; String search = ''; Future getChats({final bool? archived}) async { appState = AppState.busy; update(); final service = RequestService( archived != null && archived ? RequestHelper.aiArchived() : RequestHelper.aiChats(), ); await service.httpGet(); if (service.isSuccess) { archived != null && archived ? archivedChats.clear() : chats.clear(); final messages = service.result['chats']; for (var i = 0; i < messages.length; i++) { archived != null && archived ? archivedChats.add(ChatsModel.fromJson(messages[i])) : chats.add(ChatsModel.fromJson(messages[i])); } appState = AppState.idle; update(); return; } appState = AppState.failed; update(); } Future getSearchChats( {required final String q, final bool? archived}) async { final service = RequestService( archived != null && archived ? RequestHelper.aiSearchArchived(q) : RequestHelper.aiSearchChats(q), ); await service.httpGet(); if (service.isSuccess) { chats.clear(); final ch = service.result['chats']; for (var i = 0; i < ch.length; i++) { chats.add(ChatsModel.fromJson(ch[i])); } appState = AppState.idle; update(); return; } appState = AppState.failed; update(); } Future 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; bot = bots.first; update(); return; } appState = AppState.failed; loadingBots.value = false; update(); } Future 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 addChatToDelete() async { // final service = RequestService(RequestHelper.aiDeleteChats(), // body: {"ids": chatsToDelete}); // await service.delete(); // if (service.isSuccess) { // final List 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(); // } Future changeNameChat(int id, int index, String title, {final bool refresh = true}) async { loadingchangeTitle = true; update(); final service = RequestService(RequestHelper.aiChangeChats(id), body: {"title": title}); await service.put(); if (service.isSuccess) { chats[index].title = title; appState = AppState.idle; loadingchangeTitle = false; update(); this.refresh = refresh; return; } appState = AppState.failed; await ActionSheetUtils.showAlert(AlertData( message: 'خطا در برقراری ارتباط', aLertType: ALertType.error)); loadingchangeTitle = false; update(); } Future deleteChat(int id, int index, {final bool refresh = true}) async { final service = RequestService(RequestHelper.deleteChat(id)); await service.delete(); if (service.isSuccess) { chats.removeAt(index); appState = AppState.idle; loadingchangeTitle = false; update(); this.refresh = refresh; return; } appState = AppState.failed; await ActionSheetUtils.showAlert(AlertData( message: 'خطا در برقراری ارتباط', aLertType: ALertType.error)); loadingchangeTitle = false; update(); } Future deleteAllChat({final bool refresh = true}) async { loadingdeleteAll = true; update(); final service = RequestService( RequestHelper.deleteAllChats(), ); await service.delete(); if (service.isSuccess) { chats.clear(); appState = AppState.idle; loadingdeleteAll = false; update(); this.refresh = refresh; return; } appState = AppState.failed; await ActionSheetUtils.showAlert(AlertData( message: 'خطا در برقراری ارتباط', aLertType: ALertType.error)); loadingdeleteAll = false; update(); } Future archivedChat(int id, int index, {final bool refresh = true}) async { update(); final service = RequestService( RequestHelper.archivedChat(id), ); await service.put(); if (service.isSuccess) { chats.removeAt(index); appState = AppState.idle; update(); this.refresh = refresh; return true; } appState = AppState.failed; await ActionSheetUtils.showAlert(AlertData( message: 'خطا در برقراری ارتباط', aLertType: ALertType.error)); update(); return false; } }