import 'dart:async'; import 'dart:math'; import 'package:bson/bson.dart'; import 'package:dio/dio.dart'; import 'package:fikoyt_ui/core/fiko_request.dart'; import 'package:fikoyt_ui/fikoyt_ui.dart' hide FormData, MultipartFile; import 'package:image_picker/image_picker.dart'; class Screen1Controller extends GetxController { // Singleton instance // static Screen1Controller? _instance; // // Private constructor // Screen1Controller._internal(); // // Factory constructor to return the same instance // factory Screen1Controller() { // _instance ??= Screen1Controller._internal(); // return _instance!; // } final Rx tCachhe = FikoYTEvent('FikoYT.uyar.resim.cache.value').$value; final Rx cardId = FikoYTEvent('FikoYT.uyar.cardId.value').$value; final RxList cardReaderList = RxList.empty(); final Rx cardReaderInfo = Rx(null); final Rx deviceSettings = Rx(null); final Rx deviceInfo = Rx(null); final Rx nfcKullanici = FikoYTEvent('FikoYT.uyar.nfc.kullanici').$value; StreamSubscription? listenerCardId; @override void onInit() async { await initCardReader(); super.onInit(); } @override void onClose() { FikoYTCoreService.socket.off("FikoYT.uyar.devices.card.update"); FikoYTCoreService.socket.off("FikoYT.uyar.devices.device.updated"); listenerCardId?.cancel(); super.onClose(); } Future cacheDegistir() async { FikoYTEvent('FikoYT.uyar.resim.cache.value').$value.value = Random().nextInt(10000).toString(); tCachhe.refresh(); } Future initCardReader() async { listenerCardId = cardId.listen( (val) { if (val != "" && val != null) { FikoYTCoreService.ioEmit("FikoYT.uyar.nfc.kullanici", {"nfc": val}, ack: (data) async { nfcKullanici.value = data; if (data != null) { FikoYTCoreService.navigatorKey.currentContext?.vRouter.to("/screen-1/person-dash"); } else { FikoYTCoreService.navigatorKey.currentContext?.vRouter.to("/screen-1/unknown-dash"); } }); } else { FikoYTCoreService.navigatorKey.currentContext?.vRouter.to("/screen-1/intro"); nfcKullanici.value = null; } nfcKullanici.refresh(); }, ); FikoYTCoreService.ioEmit("FikoYT.uyar.devices.list", "FikoYT Kart Okuyucu", ack: (data) async { cardReaderList.value = data ?? []; }); /** */ FikoYTCoreService.ioEmit("FikoYT.uyar.devices.info", await FikoYTCoreService.getDeviceID(), ack: (data) async { deviceSettings.value = data?["info"]?["settings"]; deviceInfo.value = data; if (deviceSettings.value != null) { FikoYTCoreService.ioEmit("FikoYT.uyar.devices.info", deviceSettings.value?["cardReaderUuid"], ack: (data) async { cardId.value = data?["data"]?["cardId"]; cardReaderInfo.value = data; }); } }); /** */ FikoYTCoreService.socket.on( "FikoYT.uyar.devices.card.update", (data) async { cardId.value = data["cardUuid"]; // print(data); }, ); /** */ FikoYTCoreService.socket.on( "FikoYT.uyar.devices.device.updated", (data) async { if (data?["_id"] == cardReaderInfo.value?["_id"]) { cardId.value = data?["data"]?["cardId"]; cardReaderInfo.value = data; } }, ); } Future profilResmiDegistir() async { final ImagePicker picker = ImagePicker(); // Pick an image. var image = await picker.pickImage(source: ImageSource.gallery); // print(toJson()); var formData = FormData(); if (image != null) { var resimId = ObjectId().oid; formData.files.add(MapEntry(resimId, MultipartFile.fromFileSync(image.path, filename: resimId))); } formData.fields.add(MapEntry("req_body", "{}")); istek_yap(command: "FikoYT.sistem.kullanici.${FikoYTEvent('FikoYT.uyar.nfc.kullanici').value?["_id"]}.profilResmi.degistir", parameters: formData).listen((sonuc) async { // print("${sonuc.type} ${sonuc.requestProgress?.percent} ${sonuc.requestProgress?.total}"); if (sonuc.type == FikoYTRequestType.RESPONDED) { // await Future.delayed(Duration(seconds: 1)); await cacheDegistir(); } }).onError((hata) async {}); } }