Application/medcify/lib/pages/main/changeProfile/change_profile_bloc.dart

137 lines
5.0 KiB
Dart
Raw Normal View History

2022-09-26 06:33:52 +00:00
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:image_picker/image_picker.dart';
import 'package:intl/intl.dart';
import 'package:medcify/models/store_item.dart';
import '../../../components/alert.dart';
import '../../../constants.dart';
import '../../../helpers.dart';
import '../../../navigation/navigation.dart';
import '../../../network/api_provider.dart';
abstract class ChangeProfileState{}
class ChangeProfileInitial extends ChangeProfileState{}
class ChangeProfileLoading extends ChangeProfileState{}
class ChangeProfileSuccess extends ChangeProfileState{}
class ChangeProfileFailure extends ChangeProfileState{
String error;
ChangeProfileFailure(this.error);
}
class ChangeProfileChangeType extends ChangeProfileState{}
class ChangeProfileCubit extends Cubit<ChangeProfileState> {
ChangeProfileCubit() : super(ChangeProfileInitial());
TextEditingController storeNameController = TextEditingController();
TextEditingController ownerNameController = TextEditingController();
TextEditingController whatsappNumberController = TextEditingController();
TextEditingController address1Controller = TextEditingController();
TextEditingController address2Controller = TextEditingController();
TextEditingController pinCodeController = TextEditingController();
TextEditingController locationController = TextEditingController();
TextEditingController featuresController = TextEditingController();
TextEditingController disclaimerController = TextEditingController();
List<String> types = [
"Own Pharmacy",
"Hospital"
];
String? selectedType;
StoreItem? store;
Future<void> fetchMyStore() async{
emit(ChangeProfileLoading());
final response = await ApiProvider.instance.fetchMyStore();
if(response.status ?? false){
store = response.store;
storeNameController.text = store?.storeName ?? "";
ownerNameController.text = store?.ownerName ?? "";
whatsappNumberController.text = store?.whatsAppNumber ?? "";
address1Controller.text = store?.address1 ?? "";
address2Controller.text = store?.address2 ?? "";
pinCodeController.text = "${store?.pincode ?? 0}";
locationController.text = store?.location ?? "";
featuresController.text = store?.features ?? "";
disclaimerController.text = store?.disclaimer ?? "";
selectedType = (store?.storeStatus ?? true) ? "Own Pharmacy" : "Hospital";
emit(ChangeProfileSuccess());
}else{
emit(ChangeProfileFailure(response.message ?? "Something went wrong"));
}
}
void changeType(String? val){
selectedType = val;
emit(ChangeProfileChangeType());
}
Future<void> updateStore() async{
if(storeNameController.text.isEmpty){
showToast("Enter store name");
}else if(ownerNameController.text.isEmpty){
showToast("Enter owner name");
}else if(whatsappNumberController.text.isEmpty){
showToast("Enter whatsapp number");
}else if(whatsappNumberController.text.length != 10){
showToast("Invalid whatsapp number");
}else if(address1Controller.text.isEmpty){
showToast("Enter address1");
}else if(address2Controller.text.isEmpty){
showToast("Enter address2");
}else if(pinCodeController.text.isEmpty){
showToast("Enter pincode");
}else if(locationController.text.isEmpty){
showToast("Enter location");
}else if(featuresController.text.isEmpty){
showToast("Enter features");
}else if(disclaimerController.text.isEmpty){
showToast("Enter disclaimer");
}else if(selectedType == null){
showToast("Select type");
}else{
var isNetworkConnection = await Helpers.isNetwork();
if(isNetworkConnection){
Navigation.instance.navigate("/loading");
final response = await ApiProvider.instance.updateStore(storeNameController.text, ownerNameController.text,
whatsappNumberController.text, address1Controller.text, address2Controller.text, pinCodeController.text, locationController.text, featuresController.text, disclaimerController.text, (selectedType == "Own Pharmacy") ? "1" : "2");
Navigation.instance.goBack();
if(response.status ?? false){
showToast("Profile updated successfully");
fetchMyStore();
}else{
showAlert(response.message ?? "Something went wrong");
}
}else{
showAlert(Constants.noInternet);
}
}
}
void showAlert(String err){
AlertX.instance.showAlert(
title: "Error",
msg: err,
positiveButtonText: "Done",
positiveButtonPressed: (){
Navigation.instance.goBack();
}
);
}
void showToast(String msg) {
Fluttertoast.showToast(
msg: msg,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.grey.shade200,
textColor: Colors.black,
fontSize: 14.0
);
}
}