import 'dart:io'; import 'package:flutter/material.dart'; import 'package:initial_folder/models/certificate_model.dart'; import 'package:initial_folder/models/check_certificate.model.dart'; import 'package:initial_folder/models/my_certificate.dart'; import 'package:initial_folder/services/all_certificate_service.dart'; enum ResultState { Loading, NoData, HasData, Error } class CertificateProvider with ChangeNotifier { final AllCertificateServices certificateServices; CertificateProvider({required this.certificateServices}) { getAllCertif(); } bool _isChecked = true; bool get isChecked => _isChecked; ResultState? _state; List? _allCertificate; String? _userId; String? _idPayment; String? _certificateNo; String? _username; String? _title; String? _idCourse; int? _progress; dynamic _finishDate; int _allCertificateCount = 0; bool _isSearch = false; List? get allCertificate => _allCertificate; int? get allCertificateCount => _allCertificateCount; dynamic get finishDate => _finishDate; String? get idPayment => _idPayment; String? get certificateNo => _certificateNo; String? get name => _username; String? get title => _title; String? get userId => _userId; String? get idCourse => _idCourse; int? get progress => _progress; bool get isSearch => _isSearch; ResultState? get state => _state; Future uploadCertificate(String idPayment, File pdfFile) async { try { _state = ResultState.Loading; notifyListeners(); await certificateServices.uploadCertificate(idPayment, pdfFile); _state = ResultState.HasData; notifyListeners(); } catch (e) { _state = ResultState.Error; notifyListeners(); print( "Error download sertifikat $e", ); } } Future getCertif(String idCourse) async { try { _state = ResultState.Loading; notifyListeners(); List certif = await certificateServices.getSertif(idCourse); if (certif.isEmpty) { print("Jangan loading terus"); _state = ResultState.NoData; notifyListeners(); } else { _state = ResultState.HasData; notifyListeners(); _idPayment = certif[0].idPayment; _certificateNo = certif[0].certificateNo; _username = certif[0].name; _title = certif[0].title; _finishDate = certif[0].finishDate; notifyListeners(); } } catch (e) { print("Ini gagal pas pindah ke certif cuy ${e}"); } } Future getAllCertif() async { try { _state = ResultState.Loading; notifyListeners(); List allCertificate = await AllCertificateServices().getAllCertificate(); allCertificate.sort((a, b) => b.progress!.compareTo(a.progress!)); _allCertificateCount = 0; if (allCertificate.isEmpty) { _state = ResultState.NoData; notifyListeners(); } else { _state = ResultState.HasData; _isSearch = false; notifyListeners(); _allCertificate = allCertificate; for (var data in allCertificate) { if (data.progress == 100) _allCertificateCount++; } notifyListeners(); } } catch (e) { print("Jangan loading terus ${e}"); } } Future checkSertif(String text) async { try { _state = ResultState.Loading; notifyListeners(); CheckCertificateData certif = await certificateServices.checkSertif(text); if (certif.certificateNo!.isEmpty) { _state = ResultState.NoData; notifyListeners(); } else { _state = ResultState.HasData; _idCourse = certif.courseId; _idPayment = certif.idPayment; _certificateNo = certif.certificateNo; _username = certif.name; _title = certif.title; _finishDate = certif.finishDate; _progress = certif.progress; notifyListeners(); } } catch (e) { print("Ini error input no sertif${e}"); } } Future clearCheckSertif() async { _idPayment = null; } Future searchCertif(String keyword) async { try { _state = ResultState.Loading; notifyListeners(); List allCertificate = await AllCertificateServices().searchCertif(keyword); _allCertificateCount = 0; if (allCertificate.isEmpty) { _state = ResultState.NoData; notifyListeners(); } else { _state = ResultState.HasData; _isSearch = true; notifyListeners(); _allCertificate = allCertificate; for (var data in allCertificate) { if (data.progress == 100) { _allCertificateCount++; } } notifyListeners(); } } catch (e) { print(e); } } bool toggleCheckbox([bool value = false]) { _isChecked = !_isChecked; notifyListeners(); return _isChecked; } }