// 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 { Beklemede, Hata, Isteniyor, Istendi, IlerlemeIstek, BaslikCevap, Cevaplaniyor, Cevaplandi, IlerlemeCevap, Ozel } 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 IstekCevap { IstekCevap({this.sonuc, this.turu, this.durum, this.istekIlerleme, this.cevapIlerleme, this.hatalar, this.hata, this.istek}); FikoYTRequestType? turu; FikoYTRequestProgress? istekIlerleme; FikoYTResponseProgress? cevapIlerleme; FikoYTRequestStatus? durum; RequestSended? istek; T? sonuc; bool? hata = false; FikoYTErrors? hatalar; } Stream> istek_yap({String method = "POST", String? komut, dynamic parametreler = const {}}) { late StreamController> controller; late IstekCevap istekSonuc = IstekCevap(); // print(aktifKullanici.bilgiler?.value); void onListen() async { try { Response? cevap = await dio?.post("fapi/fikoyt.com.$komut", options: Options(/*headers: Map.from({"fikoak": "${FikoYTService.aktifKullanici.bilgiler?.value.id}"}),*/ sendTimeout: Duration(seconds: 3), receiveTimeout: Duration(seconds: 3)), data: parametreler, onSendProgress: (int sent, int total) { if (sent < 1) { istekSonuc.turu = FikoYTRequestType.Isteniyor; controller.add(istekSonuc); } istekSonuc.istekIlerleme = FikoYTRequestProgress(total: total, status: sent, percent: (sent / total) * 100); controller.add(istekSonuc); if (sent + 1 >= total) { istekSonuc.turu = FikoYTRequestType.Istendi; controller.add(istekSonuc); } }, onReceiveProgress: (int count, int total) { if (count < 1) { istekSonuc.turu = FikoYTRequestType.Cevaplaniyor; controller.add(istekSonuc); } istekSonuc.cevapIlerleme = FikoYTResponseProgress(total: total, status: count, percent: (count / total) * 100); controller.add(istekSonuc); if (count + 1 >= total) {} }); istekSonuc.turu = FikoYTRequestType.Cevaplandi; istekSonuc.sonuc = cevap?.data; controller.add(istekSonuc); 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); istekSonuc.turu = FikoYTRequestType.Beklemede; controller.add(istekSonuc); return controller.stream; }