// ignore_for_file: constant_identifier_names, non_constant_identifier_names import 'dart:async'; import 'package:dio/dio.dart'; import 'package:fikoyt_ui/migration/dio/dio.dart'; import 'package:flutter/foundation.dart'; enum FikoYTRequestType { IDLE, ERROR, REQUESTING, REQUESTED, PROGRESS_REQUEST, RESPONSE_HEADER, RESPONSING, RESPONDED, PROGRESS_RESPONSE, CUSTOM } class FikoYTError { FikoYTError({this.category, this.code, this.title, this.message, this.systemMessage}); String? category; double? code; String? title; String? message; String? systemMessage; } class FikoYTErrors { FikoYTErrors(); List list = []; } class FikoYTRequestProgress { FikoYTRequestProgress({this.total, this.status, this.percent}); int? total; int? status; double? percent; } class FikoYTResponseProgress { FikoYTResponseProgress({this.total, this.status, this.percent}); int? total; int? status; double? percent; } abstract class FikoYTRequestStatus { bool? hasError; List? listError; } /// BİLGİ: Şimdilik Bir İşe Yaramıyor Ama Lazım Olur Diye Silmiyorum */ abstract class RequestSended { RequestSended({this.ismi, this.parametreler, this.form, this.kwargs, this.json}); String? ismi; dynamic parametreler; dynamic form; dynamic kwargs; dynamic json; } /// NOT: Sonuc İstekleri Standarda Bağlanıyor */ class FikoYTRequestResponse { FikoYTRequestResponse({this.result, this.type, this.status, this.requestProgress, this.responseProgresss, this.listError, this.error, this.request}); FikoYTRequestType? type; FikoYTRequestProgress? requestProgress; FikoYTResponseProgress? responseProgresss; FikoYTRequestStatus? status; RequestSended? request; T? result; bool? error = false; FikoYTErrors? listError; } Stream> istek_yap({String method = "POST", String? command, dynamic parameters = const {}}) { late StreamController> controller; late FikoYTRequestResponse requestResult = FikoYTRequestResponse(); // print(aktifKullanici.bilgiler?.value); void onListen() async { try { Response? response = await dio?.post("/fapi/fikoyt.com.$command", options: Options(/*headers: Map.from({"fikoak": "${FikoYTService.aktifKullanici.bilgiler?.value.id}"}),*/ sendTimeout: Duration(days: 3), receiveTimeout: Duration(days: 3)), data: parameters, onSendProgress: (int sent, int total) { if (sent < 1) { requestResult.type = FikoYTRequestType.REQUESTING; controller.add(requestResult); } requestResult.requestProgress = FikoYTRequestProgress(total: total, status: sent, percent: (sent / total) * 100); controller.add(requestResult); if (sent + 1 >= total) { requestResult.type = FikoYTRequestType.REQUESTED; controller.add(requestResult); print(requestResult); } }, onReceiveProgress: (int count, int total) { if (count < 1) { requestResult.type = FikoYTRequestType.RESPONSING; controller.add(requestResult); } requestResult.responseProgresss = FikoYTResponseProgress(total: total, status: count, percent: (count / total) * 100); controller.add(requestResult); if (count + 1 >= total) {} }); requestResult.type = FikoYTRequestType.RESPONDED; requestResult.result = response?.data; controller.add(requestResult); controller.close(); } catch (e) { if (kDebugMode) { print(e); } if (e is DioException) { if (e.response?.data is List) { FikoYTErrors hatalar = FikoYTErrors(); e.response?.data.forEach((element) { hatalar.list.add(FikoYTError(title: element["baslik"], message: element["mesaj"])); }); controller.addError(hatalar); } else { FikoYTErrors hatalar = FikoYTErrors(); hatalar.list.add(FikoYTError(title: e.response?.data["baslik"], message: e.response?.data["mesaj"])); controller.addError(hatalar); } } else { FikoYTErrors hatalar = FikoYTErrors(); hatalar.list.add(FikoYTError(title: "Bilinmeyen Hata", message: e.toString())); controller.addError(hatalar); } controller.close(); } } controller = StreamController>(onListen: onListen); requestResult.type = FikoYTRequestType.IDLE; controller.add(requestResult); return controller.stream; }