feat: Add cubits

This commit is contained in:
NaiJi 2022-12-27 09:19:59 +04:00
parent acd1c379b1
commit 83f5241ebc
6 changed files with 240 additions and 0 deletions

View file

@ -0,0 +1,57 @@
import 'package:cubit_form/cubit_form.dart';
import 'package:equatable/equatable.dart';
import 'package:selfprivacy/models/courier.dart';
import 'package:selfprivacy/logic/server_api.dart';
export 'package:provider/provider.dart';
part 'couriersstate.dart';
class CouriersCubit extends Cubit<CouriersState> {
CouriersCubit()
: super(
const CouriersState(
<Courier>[],
false,
),
);
final ServerApi api = ServerApi();
Future<void> load() async {
refresh();
}
Future<void> refresh() async {
emit(state.copyWith(isLoading: true));
final resp = await api.getCouriers();
final List<Courier> items = resp.data;
if (items.isNotEmpty) {
emit(
CouriersState(
items,
false,
),
);
} else {
emit(state.copyWith(isLoading: false));
}
}
Future<void> create(final Courier item) async {
await api.addCourier(item);
final List<Courier> items = List<Courier>.from(state.couriers);
items.add(item);
emit(state.copyWith(couriers: items));
}
@override
void clear() async {
emit(
const CouriersState(
<Courier>[],
false,
),
);
}
}

View file

@ -0,0 +1,20 @@
part of 'courierscubit.dart';
class CouriersState extends Equatable {
const CouriersState(this.couriers, this.isLoading);
final List<Courier> couriers;
final bool isLoading;
@override
List<Object> get props => [couriers, isLoading];
CouriersState copyWith({
final List<Courier>? couriers,
final bool? isLoading,
}) =>
CouriersState(
couriers ?? this.couriers,
isLoading ?? this.isLoading,
);
}

65
lib/logic/itemscubit.dart Normal file
View file

@ -0,0 +1,65 @@
import 'package:cubit_form/cubit_form.dart';
import 'package:equatable/equatable.dart';
import 'package:selfprivacy/models/item.dart';
import 'package:selfprivacy/logic/server_api.dart';
export 'package:provider/provider.dart';
part 'itemsstate.dart';
class ItemsCubit extends Cubit<ItemsState> {
ItemsCubit()
: super(
const ItemsState(
<Item>[],
false,
),
);
final ServerApi api = ServerApi();
Future<void> load() async {
refresh();
}
Future<void> refresh() async {
emit(state.copyWith(isLoading: true));
final resp = await api.getItems();
final List<Item> items = resp.data;
if (items.isNotEmpty) {
emit(
ItemsState(
items,
false,
),
);
} else {
emit(state.copyWith(isLoading: false));
}
}
Future<void> create(final Item item) async {
await api.createItem(item);
final List<Item> items = List<Item>.from(state.items);
items.add(item);
emit(state.copyWith(items: items));
}
Future<void> delete(final Item item) async {
final List<Item> items = List<Item>.from(state.items);
await api.deleteItem(item);
items.removeWhere((final Item u) => u.id == item.id);
emit(state.copyWith(items: items));
}
@override
void clear() async {
emit(
const ItemsState(
<Item>[],
false,
),
);
}
}

20
lib/logic/itemsstate.dart Normal file
View file

@ -0,0 +1,20 @@
part of 'itemscubit.dart';
class ItemsState extends Equatable {
const ItemsState(this.items, this.isLoading);
final List<Item> items;
final bool isLoading;
@override
List<Object> get props => [items, isLoading];
ItemsState copyWith({
final List<Item>? items,
final bool? isLoading,
}) =>
ItemsState(
items ?? this.items,
isLoading ?? this.isLoading,
);
}

View file

@ -0,0 +1,58 @@
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';
class ItemsCubit extends Cubit<OrdersState> {
ItemsCubit()
: 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,
),
);
}
}

View file

@ -0,0 +1,20 @@
part of 'orderscubit.dart';
class OrdersState extends Equatable {
const OrdersState(this.orders, this.isLoading);
final List<Order> orders;
final bool isLoading;
@override
List<Object> get props => [orders, isLoading];
OrdersState copyWith({
final List<Order>? orders,
final bool? isLoading,
}) =>
OrdersState(
orders ?? this.orders,
isLoading ?? this.isLoading,
);
}