95 lines
2.5 KiB
Dart
95 lines
2.5 KiB
Dart
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:medcify/models/order_item.dart';
|
|
import 'package:medcify/network/api_provider.dart';
|
|
|
|
import '../../../components/alert.dart';
|
|
import '../../../models/banner_item.dart';
|
|
import '../../../navigation/navigation.dart';
|
|
|
|
abstract class OrdersState{}
|
|
|
|
class OrdersInitial extends OrdersState{}
|
|
class OrdersLoading extends OrdersState{}
|
|
class OrdersSuccess extends OrdersState{}
|
|
class OrdersFailure extends OrdersState{
|
|
String error;
|
|
OrdersFailure(this.error);
|
|
}
|
|
|
|
class OrdersCubit extends Cubit<OrdersState> {
|
|
OrdersCubit() : super(OrdersInitial());
|
|
List<OrderItem> orders = [];
|
|
List<String> urls = [
|
|
"pendingOrderList",
|
|
"CompletedOrderList",
|
|
"cencelledOrderList",
|
|
];
|
|
int currentIndex = 0;
|
|
|
|
void onTap(int index){
|
|
currentIndex = index;
|
|
fetchOrders();
|
|
}
|
|
|
|
Future<void> fetchOrders() async{
|
|
emit(OrdersLoading());
|
|
final response = await ApiProvider.instance.fetchOrders(urls[currentIndex]);
|
|
if(response.status ?? false){
|
|
orders = response.orders ?? [];
|
|
emit(OrdersSuccess());
|
|
}else{
|
|
emit(OrdersFailure(response.message ?? "Something went wrong"));
|
|
}
|
|
}
|
|
|
|
Future<void> acceptOrder(OrderItem item) async{
|
|
Navigation.instance.navigate("/loading");
|
|
final response = await ApiProvider.instance.acceptOrder("${item.id ?? 0}");
|
|
Navigation.instance.goBack();
|
|
if(response.status ?? false){
|
|
fetchOrders();
|
|
showToast("Accepted order #${item.id}");
|
|
}else{
|
|
showAlert(response.message ?? "Something went wrong");
|
|
}
|
|
}
|
|
|
|
Future<void> cancelOrder(OrderItem item) async{
|
|
Navigation.instance.navigate("/loading");
|
|
final response = await ApiProvider.instance.cancelOrder("${item.id ?? 0}");
|
|
Navigation.instance.goBack();
|
|
if(response.status ?? false){
|
|
fetchOrders();
|
|
showToast("Cancelled order #${item.id}");
|
|
}else{
|
|
showAlert(response.message ?? "Something went wrong");
|
|
}
|
|
}
|
|
|
|
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
|
|
);
|
|
}
|
|
|
|
void showAlert(String err){
|
|
AlertX.instance.showAlert(
|
|
title: "Error",
|
|
msg: err,
|
|
positiveButtonText: "Done",
|
|
positiveButtonPressed: (){
|
|
Navigation.instance.goBack();
|
|
}
|
|
);
|
|
}
|
|
|
|
} |