import 'dart:ui'; import 'package:dio/dio.dart'; import 'package:fikoyt_ui/core/fikoyt_request_manager/fikoyt_request_manager.dart'; import 'package:fikoyt_ui/core/fikoyt_request_manager/fikoyt_request_queue.dart'; import 'package:fikoyt_ui/services/fikoyt_core.service.dart'; import 'package:fikoyt_ui/migration/dio/dio.dart'; import 'package:get/get_rx/get_rx.dart'; typedef FikoYTCallback = void Function(FikoYTRequestEngine, FikoYTRequestResponse); typedef FikoYTHataCallback = void Function(FikoYTRequestEngine, FikoYTErrors); typedef FikoYTVoidCallback = void Function(FikoYTRequestEngine); class FikoYTRequestEngine { final String method; final String? command; dynamic parameters; final bool onEkOlsun; final int retryCount; final Duration retryDelay; final CancelToken cancelToken = CancelToken(); final RxBool isBusy = RxBool(false); final bool autoIsBusy; final bool autoQueue; final String? queueTag; final int queuePriority; final List _onStart = []; final List> _onProgress = []; final List> _onResult = []; final List _onDone = []; final List _onError = []; final List beforeSendList = []; FikoYTRequestEngine({ this.method = "POST", this.command, this.parameters = const {}, this.onEkOlsun = true, this.retryCount = 0, this.retryDelay = const Duration(milliseconds: 300), this.autoIsBusy = true, this.autoQueue = false, this.queueTag, this.queuePriority = 0, }); FikoYTRequestEngine onStart(FikoYTVoidCallback callback) { _onStart.add(callback); return this; } FikoYTRequestEngine onProgress(FikoYTCallback callback) { _onProgress.add(callback); return this; } FikoYTRequestEngine onResult(FikoYTCallback callback) { _onResult.add(callback); return this; } FikoYTRequestEngine onDone(FikoYTVoidCallback callback) { _onDone.add(callback); return this; } FikoYTRequestEngine onError(FikoYTHataCallback callback) { _onError.add(callback); return this; } FikoYTRequestEngine beforeSend(FikoYTVoidCallback callback) { beforeSendList.add(callback); return this; } void clearEvents() { _onStart.clear(); _onProgress.clear(); _onResult.clear(); _onDone.clear(); _onError.clear(); } void cancel() { if (!cancelToken.isCancelled) { cancelToken.cancel("İstek kullanıcı tarafından iptal edildi."); } } Future start() async { if (autoQueue) { startWithQueue(tag: queueTag, priority: queuePriority); // otomatik sıraya girer } else { await _startInternal(); // doğrudan çalışır } } void startWithQueue({String? tag, int? priority}) { FikoYTRequestQueue().enqueue( () => _startInternal(), tag: tag ?? queueTag, priority: priority ?? queuePriority, // yüksek olanlara öncelik önce ); } Future _startInternal() async { _onStart.forEach((fn) => fn(this)); if (autoIsBusy) isBusy.value = true; int tryCount = 0; final responseModel = FikoYTRequestResponse()..type = FikoYTRequestType.REQUESTING; while (tryCount <= retryCount) { try { responseModel.startTime = DateTime.now(); final url = "${onEkOlsun ? FikoYTCoreService.serviceSettings.fikoytServerIstekOnEk : ''}$command"; beforeSendList.forEach((fn) => fn(this)); final response = await dio?.post( url, data: parameters, cancelToken: cancelToken, options: Options( headers: {"fsid": "${FikoYTCoreService.socket.id}"}, sendTimeout: const Duration(seconds: 30), receiveTimeout: const Duration(seconds: 30), ), onSendProgress: (sent, total) { responseModel.type = FikoYTRequestType.REQUESTING; responseModel.requestProgress = FikoYTRequestProgress( total: total, status: sent, percent: total == 0 ? 0 : (sent / total) * 100, ); _onProgress.forEach((fn) => fn(this, responseModel)); }, onReceiveProgress: (received, total) { responseModel.type = FikoYTRequestType.RESPONSING; responseModel.responseProgresss = FikoYTResponseProgress( total: total, status: received, percent: total == 0 ? 0 : (received / total) * 100, ); _onProgress.forEach((fn) => fn(this, responseModel)); }, ); responseModel.type = FikoYTRequestType.RESPONDED; responseModel.result = response?.data; responseModel.endTime = DateTime.now(); /** */ print(response?.data); final data = response?.data; if (data is Map) { final durum = data["durum"]; if (durum is Map) { final hata = durum["hata"]; if (hata == true) { final hatalarRaw = durum["hatalar"]; if (hatalarRaw is List) { final hatalar = FikoYTErrors(); for (final element in hatalarRaw) { if (element is Map) { hatalar.list.add( FikoYTError( title: element["baslik"]?.toString() ?? "Bilinmeyen Başlık", message: element["mesaj"]?.toString() ?? "Bilinmeyen Mesaj", ), ); } } _onError.forEach((fn) => fn(this, hatalar)); throw hatalar; } } } } /** */ _onResult.forEach((fn) => fn(this, responseModel)); break; } catch (e) { tryCount++; final isFinalTry = tryCount > retryCount; final isUserCancelled = e is DioException && CancelToken.isCancel(e); if (isFinalTry || isUserCancelled) { final errors = _parseError(e); _onError.forEach((fn) => fn(this, errors)); break; } else { await Future.delayed(retryDelay); } } } _onDone.forEach((fn) => fn(this)); if (autoIsBusy) isBusy.value = false; } Future start111() async { _onStart.forEach((fn) => fn( this, )); if (autoIsBusy) isBusy.value = true; int tryCount = 0; final responseModel = FikoYTRequestResponse()..type = FikoYTRequestType.REQUESTING; while (tryCount <= retryCount) { try { responseModel.startTime = DateTime.now(); final url = "${onEkOlsun ? FikoYTCoreService.serviceSettings.fikoytServerIstekOnEk : ''}$command"; final response = await dio?.post( url, data: parameters, cancelToken: cancelToken, options: Options( headers: {"fsid": "\${FikoYTCoreService.socket.id}"}, sendTimeout: const Duration(seconds: 30), receiveTimeout: const Duration(seconds: 30), ), onSendProgress: (sent, total) { responseModel.type = FikoYTRequestType.REQUESTING; responseModel.requestProgress = FikoYTRequestProgress( total: total, status: sent, percent: total == 0 ? 0 : (sent / total) * 100, ); _onProgress.forEach((fn) => fn(this, responseModel)); }, onReceiveProgress: (received, total) { responseModel.type = FikoYTRequestType.RESPONSING; responseModel.responseProgresss = FikoYTResponseProgress( total: total, status: received, percent: total == 0 ? 0 : (received / total) * 100, ); _onProgress.forEach((fn) => fn(this, responseModel)); }, ); responseModel.type = FikoYTRequestType.RESPONDED; responseModel.result = response?.data; responseModel.endTime = DateTime.now(); _onResult.forEach((fn) => fn(this, responseModel)); break; } catch (e) { tryCount++; if (tryCount > retryCount || e is! DioException || CancelToken.isCancel(e)) { final errors = _parseError(e); _onError.forEach((fn) => fn(this, errors)); break; } else { await Future.delayed(retryDelay); } } } _onDone.forEach((fn) => fn(this)); if (autoIsBusy) isBusy.value = false; } FikoYTErrors _parseError(Object e) { final hatalar = FikoYTErrors(); if (e is DioException) { final data = e.response?.data; if (data is List) { for (final item in data) { if (item is Map) { hatalar.list.add(FikoYTError( title: item["baslik"]?.toString() ?? "Hata", message: item["mesaj"]?.toString() ?? "Bir hata oluştu", )); } } } else if (data is Map) { hatalar.list.add(FikoYTError( title: data["baslik"]?.toString() ?? "Hata", message: data["mesaj"]?.toString() ?? "Bir hata oluştu", )); } else { hatalar.list.add(FikoYTError(title: "Hata", message: e.toString())); } } else { hatalar.list.add(FikoYTError(title: "Bilinmeyen Hata", message: e.toString())); } return hatalar; } }