import 'dart:async'; import 'package:diplomaticquarterapp/config/config.dart'; import 'package:diplomaticquarterapp/config/shared_pref_kay.dart'; import 'package:diplomaticquarterapp/core/model/pharmacies/Addresses.dart'; import 'package:diplomaticquarterapp/core/model/pharmacies/Country.dart'; import 'package:diplomaticquarterapp/core/service/AlHabibMedicalService/customer_addresses_service.dart'; import 'package:diplomaticquarterapp/core/service/base_service.dart'; import 'package:diplomaticquarterapp/services/authentication/auth_provider.dart'; class PharmacyAddressService extends BaseService { List addresses = List(); CountryData country; int selectedAddressIndex = 0; Future> getAddresses() async { var customerId = await sharedPref.getString(PHARMACY_CUSTOMER_ID); Map queryParams = {'fields': 'addresses'}; hasError = false; Addresses selectedAddress; try { var completer = Completer(); await baseAppClient.getPharmacy("$GET_CUSTOMERS_ADDRESSES$customerId", onSuccess: (dynamic response, int statusCode) async { addresses.clear(); var savedAddress = await sharedPref.getObject(PHARMACY_SELECTED_ADDRESS); if (savedAddress != null) { selectedAddress = Addresses.fromJson(savedAddress); } int index = 0; response['customers'][0]['addresses'].forEach((item) { AddressInfo address = AddressInfo.fromJson(item); if (selectedAddress != null && selectedAddress.id == item["id"]) { selectedAddressIndex = index; } addresses.add(address); index++; }); completer.complete(); }, onFailure: (String error, int statusCode) { hasError = true; super.error = error; }, queryParams: queryParams); await completer.future; } catch (error) { throw error; } return addresses; } Future getCountries(String countryName) async { hasError = false; try { await baseAppClient.getPharmacy("$PHARMACY_GET_COUNTRY", onSuccess: (dynamic response, int statusCode) { // countries.clear(); response['countries'].forEach((item) { if (CountryData.fromJson(item).name == countryName || CountryData.fromJson(item).namen == countryName) { country = CountryData.fromJson(item); } // countries.add(CountryData.fromJson(item)); }); }, onFailure: (String error, int statusCode) { hasError = true; super.error = error; }); } catch (error) { throw error; } } Future addCustomerAddress(AddressInfo address) async { makeCustomerAddress(address, ADD_CUSTOMER_ADDRESS); } Future editCustomerAddress(AddressInfo address) async { makeCustomerAddress(address, EDIT_CUSTOMER_ADDRESS); } Future deleteCustomerAddress(AddressInfo address) async { makeCustomerAddress(address, DELETE_CUSTOMER_ADDRESS); } Future makeCustomerAddress(AddressInfo address, String url) async { var customerId = await sharedPref.getString(PHARMACY_CUSTOMER_ID); hasError = false; super.error = ""; Map customerObject = Map(); customerObject["addresses"] = [address]; customerObject["id"] = customerId; customerObject["email"] = address.email; customerObject["role_ids"] = [3]; Map body = Map(); body["customer"] = customerObject; await baseAppClient.postPharmacy("$url", onSuccess: (response, statusCode) async { addresses.clear(); response['customers'][0]['addresses'].forEach((item) { addresses.add(AddressInfo.fromJson(item)); }); }, onFailure: (String error, int statusCode) { hasError = true; super.error = error; }, body: body); } }