You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
car_provider_app/lib/view_models/branch_view_model.dart

209 lines
7.3 KiB
Dart

import 'dart:io';
import 'package:car_provider_app/repositories/branch_repo.dart';
import 'package:file_picker/file_picker.dart';
import 'package:mc_common_app/models/m_response.dart';
import 'package:mc_common_app/models/model/branch2.dart';
import 'package:mc_common_app/models/profile/branch.dart';
import 'package:mc_common_app/models/profile/categroy.dart';
import 'package:mc_common_app/models/profile/document.dart';
import 'package:mc_common_app/models/profile/services.dart';
import 'package:mc_common_app/models/user/cities.dart';
import 'package:mc_common_app/models/user/country.dart';
import 'package:mc_common_app/repositories/common_repo.dart';
import 'package:mc_common_app/services/services.dart';
import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/utils/utils.dart';
import 'package:mc_common_app/view_models/base_view_model.dart';
import 'package:mc_common_app/widgets/dropdown/dropdow_field.dart';
import 'package:permission_handler/permission_handler.dart';
class BranchVM extends BaseVM {
final BranchRepo branchRepo;
final CommonServices commonServices;
final CommonRepo commonRepo;
BranchVM({required this.branchRepo, required this.commonServices, required this.commonRepo});
Document? document;
Branch2? branchs;
//Create Branch
String countryCode = "", address = "", branchName = "", branchDescription = "";
double latitude = 0, longitude = 0;
int role = -1, countryId = -1, cityId = -1;
List<DropValue> countryDropList = [];
List<DropValue> citiesDropList = [];
DropValue? countryValue;
DropValue? cityValue;
Country? country;
Cities? cities;
//Create Service
String? branchNameForService;
int categoryId = -1, branchId = -1;
DropValue? branchValue;
List<DropValue> countryDropListForService = [];
List<DropValue> categoryDropList = [];
Branch? branch;
Category? category;
Services? services;
getServiceProviderDocument(int providerId) async {
setState(ViewState.busy);
document = await branchRepo.getServiceProviderDocument(providerId);
setState(ViewState.idle);
}
selectFile(int index) async {
File? file = await commonServices.pickFile(fileType: FileType.custom, allowedExtensions: ['png', 'pdf', 'jpeg']);
if (file != null) {
int sizeInBytes = file.lengthSync();
// double sizeInMb = sizeInBytes / (1024 * 1024);
if (sizeInBytes > 1000) {
Utils.showToast("File is larger then 1KB");
} else {
document!.data![index].document = Utils.convertFileToBase64(file);
document!.data![index].fileExt = Utils.checkFileExt(file.path);
document!.data![index].documentUrl = file.path;
setState(ViewState.idle);
}
} else {
// User canceled the picker
}
}
Future<MResponse> updateDocument(List<DocumentData>? data) async {
return await branchRepo.serviceProviderDocumentsUpdate(data);
}
//Create new branch
getBranchAndServices() async {
setState(ViewState.busy);
branchs = await branchRepo.getBranchAndServices();
setState(ViewState.idle);
}
getAllCountriesList(ServiceProviderBranch? branchData, String countryCode) async {
setState(ViewState.busy);
resetValues();
country = await commonRepo.getAllCountries();
country!.data?.forEach((element) {
if (branchData != null) if (branchData.id != null) {
if (element.id == branchData.countryID) {
countryValue = DropValue(element.id ?? 0, countryCode == "SA" ? (element.countryNameN ?? "") : (element.countryName ?? ""), element.countryCode ?? "");
}
}
countryDropList.add(DropValue(element.id ?? 0, countryCode == "SA" ? (element.countryNameN ?? "") : (element.countryName ?? ""), element.countryCode ?? ""));
});
if (branchData != null) if (branchData.id != null) getAllCities(branchData, countryCode);
setState(ViewState.idle);
}
getAllCities(ServiceProviderBranch? branchData, String countryCode) async {
setState(ViewState.busy);
citiesDropList.clear();
cities = null;
cityId = -1;
cities = await commonRepo.getAllCites(countryId.toString());
cities!.data?.forEach((element) {
if (branchData != null && branchData.id != null) {
if (element.id == branchData.cityId) {
address = branchData.address!;
branchName = branchData.branchName!;
branchDescription = branchData.branchDescription!;
latitude = double.parse(branchData.latitude ?? "");
longitude = double.parse(branchData.longitude ?? "");
countryId = branchData.countryID!;
cityId = branchData.cityId!;
cityValue = DropValue(element.id ?? 0, countryCode == "SA" ? (element.cityNameN ?? "") : (element.cityName ?? ""), element.id.toString() ?? "");
}
}
citiesDropList.add(DropValue(element.id ?? 0, countryCode == "SA" ? (element.cityNameN ?? "") : (element.cityName ?? ""), element.id.toString() ?? ""));
});
setState(ViewState.idle);
}
resetValues() {
countryCode = "";
address = "";
branchName = "";
branchDescription = "";
latitude = 0;
longitude = 0;
role = -1;
countryId = -1;
cityId = -1;
countryDropList.clear();
countryId = -1;
cityId = -1;
cities = null;
branchNameForService = null;
categoryId = -1;
branchId = -1;
branchValue = null;
countryDropListForService = [];
categoryDropList = [];
branch = null;
category = null;
services = null;
}
Future<MResponse> createBranch(String branchName, String branchDescription, String cityId, String address, String latitude, String longitude) async {
return await branchRepo.createBranch(branchName, branchDescription, cityId.toString(), address, latitude.toString(), longitude.toString());
}
Future<MResponse> updateBranch(int id, String branchName, String branchDescription, String cityId, String address, String latitude, String longitude, {bool isNeedToDelete = true}) async {
return await branchRepo.updateBranch(id ?? 0, branchName, branchDescription, cityId.toString(), address, latitude.toString(), longitude.toString());
}
//Create Service
fetchBranches() async {
resetValues();
setState(ViewState.busy);
branch = await branchRepo.fetchAllBranches();
branch!.data?.forEach((element) {
countryDropListForService.add(DropValue(element.id ?? 0, ((element.branchName!.isEmpty ? "N/A" : element.branchName) ?? "N/A"), ""));
});
setState(ViewState.idle);
}
fetchBranchCategory(String countryCode) async {
setState(ViewState.busy);
category = await branchRepo.fetchBranchCategory();
category!.data?.forEach((element) {
categoryDropList.add(DropValue(
element.id ?? 0,
((element.categoryName!.isEmpty
? "N/A"
: countryCode == "SA"
? element.categoryNameN
: element.categoryName) ??
"N/A"),
""));
});
setState(ViewState.idle);
}
fetchServicesByCategoryId() async {
setState(ViewState.busy);
services = await branchRepo.fetchServicesByCategoryId(categoryId.toString());
setState(ViewState.idle);
}
Future<MResponse> createService(List<Map<String, dynamic>> map) async {
return await branchRepo.createService(map);
}
Future<MResponse> updateServices(List<Map<String, dynamic>> map) async {
return await branchRepo.updateService(map);
}
}