Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loan rework #334

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/admin/ui/pages/add_loaner_page/add_loaner_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AddLoanerPage extends HookConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final loanerListNotifier = ref.watch(loanerListProvider.notifier);
final loaners = ref.watch(allLoanerList);
final loaners = ref.watch(allLoanerListProvider);
final associations = ref.watch(allGroupListProvider);
final loanersId = loaners.map((x) => x.groupManagerId).toList();
void displayToastWithContext(TypeMsg type, String msg) {
Expand All @@ -39,7 +39,8 @@ class AddLoanerPage extends HookConsumerWidget {
child: Column(
children: [
const AlignLeftText(
AdminTextConstants.addLoaningAssociation),
AdminTextConstants.addLoaningAssociation,
),
const SizedBox(height: 30),
AsyncChild(
value: associations,
Expand Down
2 changes: 1 addition & 1 deletion lib/admin/ui/pages/edit_page/search_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class SearchUser extends HookConsumerWidget {
.toList(),
);
final value = await groupNotifier.deleteMember(
newGroup, x);
newGroup, x,);
if (value) {
simpleGroupGroupsNotifier.setTData(
newGroup.id,
Expand Down
2 changes: 1 addition & 1 deletion lib/advert/ui/components/advert_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class AdvertCard extends HookConsumerWidget {
),
Container(
padding: const EdgeInsets.only(
top: 20, left: 10, right: 10),
top: 20, left: 10, right: 10,),
width: width,
height: height - imageHeight,
child: Column(
Expand Down
4 changes: 2 additions & 2 deletions lib/loan/notification_service.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:myecl/loan/providers/loaner_loan_list_provider.dart';
import 'package:myecl/loan/providers/loan_list_provider.dart';
import 'package:myecl/loan/providers/user_loaner_list_provider.dart';
import 'package:myecl/loan/router.dart';
import 'package:tuple/tuple.dart';
Expand All @@ -15,6 +15,6 @@ final Map<String, Tuple2<String, List<StateNotifierProvider>>> loanProviders = {
),
"loans": Tuple2(
LoanRouter.root,
[loanerLoanListProvider],
[loanListProvider],
),
};
29 changes: 0 additions & 29 deletions lib/loan/providers/admin_history_loan_list_provider.dart

This file was deleted.

28 changes: 0 additions & 28 deletions lib/loan/providers/admin_loan_list_provider.dart

This file was deleted.

6 changes: 3 additions & 3 deletions lib/loan/providers/all_loaner_list_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:myecl/loan/class/loaner.dart';
import 'package:myecl/loan/providers/loaner_list_provider.dart';

final allLoanerList = Provider<List<Loaner>>((ref) {
final deliveryProvider = ref.watch(loanerListProvider);
return deliveryProvider.maybeWhen(data: (loans) => loans, orElse: () => []);
final allLoanerListProvider = Provider<List<Loaner>>((ref) {
final loaners = ref.watch(loanerListProvider);
return loaners.maybeWhen(data: (loans) => loans, orElse: () => []);
});
113 changes: 0 additions & 113 deletions lib/loan/providers/history_loaner_loan_list_provider.dart

This file was deleted.

40 changes: 9 additions & 31 deletions lib/loan/providers/item_list_provider.dart
Original file line number Diff line number Diff line change
@@ -1,64 +1,42 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:myecl/loan/class/item.dart';
import 'package:myecl/loan/providers/loaner_id_provider.dart';
import 'package:myecl/loan/repositories/item_repository.dart';
import 'package:myecl/tools/providers/list_notifier.dart';
import 'package:myecl/tools/token_expire_wrapper.dart';

class ItemListNotifier extends ListNotifier<Item> {
final ItemRepository itemrepository;
ItemListNotifier({required this.itemrepository})
final ItemRepository itemRepository;
ItemListNotifier({required this.itemRepository})
: super(const AsyncValue.loading());

Future<AsyncValue<List<Item>>> loadItemList(String id) async {
return await loadList(() async => itemrepository.getItemList(id));
Future<AsyncValue<List<Item>>> loadItemList(String loanerId) async {
return await loadList(() async => itemRepository.getItemList(loanerId));
}

Future<bool> addItem(Item item, String loanerId) async {
return await add((i) async => itemrepository.createItem(loanerId, i), item);
return await add((i) async => itemRepository.createItem(loanerId, i), item);
}

Future<bool> updateItem(Item item, String loanerId) async {
return await update(
(i) async => itemrepository.updateItem(loanerId, i),
(i) async => itemRepository.updateItem(loanerId, i),
(items, item) => items..[items.indexWhere((i) => i.id == item.id)] = item,
item,
);
}

Future<bool> deleteItem(Item item, String loanerId) async {
return await delete(
(id) async => itemrepository.deleteItem(loanerId, id),
(id) async => itemRepository.deleteItem(loanerId, id),
(items, item) => items..removeWhere((i) => i.id == item.id),
item.id,
item,
);
}

Future<AsyncValue<List<Item>>> copy() async {
return state.whenData((d) => d.sublist(0));
}

Future<AsyncValue<List<Item>>> filterItems(String query) async {
return state.whenData(
(items) => items
.where(
(item) => item.name.toLowerCase().contains(query.toLowerCase()))
.toList(),
);
}
}

//The provider give access to the notifier but should not be use directly as the state may be modified for any loaner at any time. Access items from loanersItemsMapProvider instead.
final itemListProvider =
StateNotifierProvider<ItemListNotifier, AsyncValue<List<Item>>>((ref) {
final itemRepository = ref.watch(itemRepositoryProvider);
ItemListNotifier itemListNotifier =
ItemListNotifier(itemrepository: itemRepository);
tokenExpireWrapperAuth(ref, () async {
final loanerId = ref.watch(loanerIdProvider);
if (loanerId != "") {
await itemListNotifier.loadItemList(loanerId);
}
});
return itemListNotifier;
return ItemListNotifier(itemRepository: itemRepository);
});
48 changes: 48 additions & 0 deletions lib/loan/providers/item_quantities_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:myecl/loan/class/loan.dart';
import 'package:myecl/loan/providers/loan_provider.dart';

class ItemQuantitiesMapNotifier extends StateNotifier<Map<String, int>> {
ItemQuantitiesMapNotifier() : super(<String, int>{});

Future<Map<String, int>> toggle(String id, int quantity) async {
state[id] = state[id] == 0 ? quantity : 0;
state = Map.from(state);
return state;
}

Future<Map<String, int>> set(String id, int quantity) async {
state[id] = quantity;
state = Map.from(state);
return state;
}

void initWithLoan(Loan loan) {
state = <String, int>{};
for (var itemQty in loan.itemsQuantity) {
state[itemQty.itemSimple.id] = itemQty.quantity;
}
state = Map.from(state);
}

void clear() {
state.forEach((id, _) => state[id] = 0);
state = Map.from(state);
}
}

final selectedItemQuantitiesMapProvider =
StateNotifierProvider<ItemQuantitiesMapNotifier, Map<String, int>>((ref) {
return ItemQuantitiesMapNotifier();
});

final loanItemQuantitiesMapProvider =
StateNotifierProvider<ItemQuantitiesMapNotifier, Map<String, int>>((ref) {
final loan = ref.watch(loanProvider);

ItemQuantitiesMapNotifier itemQuantitiesMapNotifier =
ItemQuantitiesMapNotifier();
itemQuantitiesMapNotifier.initWithLoan(loan);

return itemQuantitiesMapNotifier;
});
Loading