Application/medcify/lib/pages/main/deliveryPincodes/delivery_pincodes_bloc.dart

73 lines
2.2 KiB
Dart
Raw Normal View History

2022-09-26 06:33:52 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:medcify/models/order_item.dart';
import 'package:medcify/models/pincode_item.dart';
import 'package:medcify/network/api_provider.dart';
import '../../../components/alert.dart';
import '../../../constants.dart';
import '../../../helpers.dart';
import '../../../models/banner_item.dart';
import '../../../models/medicine_item.dart';
import '../../../navigation/navigation.dart';
abstract class DeliveringPincodesState{}
class DeliveringPincodesInitial extends DeliveringPincodesState{}
class DeliveringPincodesLoading extends DeliveringPincodesState{}
class DeliveringPincodesSuccess extends DeliveringPincodesState{}
class DeliveringPincodesFailure extends DeliveringPincodesState{
String error;
DeliveringPincodesFailure(this.error);
}
class DeliveringPincodesCubit extends Cubit<DeliveringPincodesState> {
DeliveringPincodesCubit() : super(DeliveringPincodesInitial());
List<PincodeItem> pincodes = [];
Future<void> fetchPincodes() async{
emit(DeliveringPincodesLoading());
final response = await ApiProvider.instance.fetchPincodes();
if(response.status ?? false){
pincodes = response.pincodes ?? [];
emit(DeliveringPincodesSuccess());
}else{
emit(DeliveringPincodesFailure(response.message ?? "Something went wrong"));
}
}
Future<void> deletePincode(String id) async{
var isNetworkConnection = await Helpers.isNetwork();
if(isNetworkConnection){
Navigation.instance.navigate("/loading");
final response = await ApiProvider.instance.deletePincode(id);
Navigation.instance.goBack();
if(response.status ?? false){
fetchPincodes();
}else{
showAlert(response.message ?? "Something went wrong");
}
}else{
showAlert(Constants.noInternet);
}
}
Future<void> editPincode(PincodeItem pincode) async{
final response = await Navigation.instance.navigate("/editPincode",args: pincode);
if(response != null){
fetchPincodes();
}
}
void showAlert(String msg){
AlertX.instance.showAlert(
title: "Error",
msg: msg,
positiveButtonText: "Done",
positiveButtonPressed: (){
Navigation.instance.goBack();
}
);
}
}