2022-12-27 05:19:59 +00:00
|
|
|
import 'package:cubit_form/cubit_form.dart';
|
|
|
|
import 'package:equatable/equatable.dart';
|
|
|
|
import 'package:selfprivacy/logic/server_api.dart';
|
|
|
|
import 'package:selfprivacy/models/order.dart';
|
|
|
|
|
|
|
|
export 'package:provider/provider.dart';
|
|
|
|
|
|
|
|
part 'ordersstate.dart';
|
|
|
|
|
2022-12-27 05:29:50 +00:00
|
|
|
class OrdersCubit extends Cubit<OrdersState> {
|
|
|
|
OrdersCubit()
|
2022-12-27 05:19:59 +00:00
|
|
|
: super(
|
|
|
|
const OrdersState(
|
|
|
|
<Order>[],
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
final ServerApi api = ServerApi();
|
|
|
|
|
|
|
|
Future<void> load() async {
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> refresh() async {
|
|
|
|
emit(state.copyWith(isLoading: true));
|
|
|
|
final resp = await api.getOrders();
|
|
|
|
final List<Order> items = resp.data;
|
|
|
|
if (items.isNotEmpty) {
|
|
|
|
emit(
|
|
|
|
OrdersState(
|
|
|
|
items,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
emit(state.copyWith(isLoading: false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> create(final Order order) async {
|
|
|
|
await api.createOrder(order);
|
|
|
|
|
|
|
|
final List<Order> items = List<Order>.from(state.orders);
|
|
|
|
items.add(order);
|
|
|
|
emit(state.copyWith(orders: items));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void clear() async {
|
|
|
|
emit(
|
|
|
|
const OrdersState(
|
|
|
|
<Order>[],
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|