diff --git a/lib/cubits/shoppinglist_cubit.dart b/lib/cubits/shoppinglist_cubit.dart index 34c88e60..b1ec53bd 100644 --- a/lib/cubits/shoppinglist_cubit.dart +++ b/lib/cubits/shoppinglist_cubit.dart @@ -33,6 +33,7 @@ class ShoppinglistCubit extends Cubit { ); } }); + _initialLoad(); refresh(); } @@ -194,6 +195,59 @@ class ShoppinglistCubit extends Cubit { return _refreshThread!; } + Future _initialLoad() async { + final shoppingLists = await TransactionHandler.getInstance().runTransaction( + TransactionShoppingListGet(household: household), + forceOffline: true, + ); + + final shoppinglist = + state.selectedShoppinglist ?? shoppingLists.firstOrNull; + + if (shoppinglist == null) return; + + Future> items = + TransactionHandler.getInstance().runTransaction( + TransactionShoppingListGetItems( + shoppinglist: shoppinglist, + sorting: state.sorting, + ), + forceOffline: true, + ); + + Future> categories = + TransactionHandler.getInstance().runTransaction( + TransactionCategoriesGet(household: household), + forceOffline: true, + ); + + final recent = TransactionHandler.getInstance().runTransaction( + TransactionShoppingListGetRecentItems( + shoppinglist: shoppinglist, + itemsCount: recentItemCountProvider(), + ), + forceOffline: true, + ); + List loadedShoppinglistItems = await items; + final resState = ShoppinglistCubitState( + shoppinglists: shoppingLists, + selectedShoppinglist: shoppinglist, + listItems: loadedShoppinglistItems, + recentItems: await recent, + categories: await categories, + sorting: state.sorting, + selectedListItems: state.selectedListItems + .map((e) => (loadedShoppinglistItems) + .firstWhereOrNull((item) => item.id == e.id)) + .whereNotNull() + .toList(), + ); + + if (state is LoadingShoppinglistCubitState) { + emit(resState); + } + } + // ignore: long-method Future _refresh([String? query]) async { // Get required information diff --git a/lib/services/transaction_handler.dart b/lib/services/transaction_handler.dart index 6a3f269d..bccffc3e 100644 --- a/lib/services/transaction_handler.dart +++ b/lib/services/transaction_handler.dart @@ -32,11 +32,15 @@ class TransactionHandler { } } - Future runTransaction(Transaction t) async { + Future runTransaction( + Transaction t, { + bool forceOffline = false, + }) async { + forceOffline = forceOffline || App.isForcedOffline; if (!ApiService.getInstance().isConnected()) { await ApiService.getInstance().refresh(); } - if (!App.isForcedOffline && ApiService.getInstance().isConnected()) { + if (!forceOffline && ApiService.getInstance().isConnected()) { T? res = await t.runOnline(); if (res != null && (res is! bool || res)) return res; }