From 4af0224bb6bf67c16de27c81d45374dd84fc4476 Mon Sep 17 00:00:00 2001 From: Tom Bursch Date: Wed, 24 Apr 2024 13:01:59 +0200 Subject: [PATCH] fix: Drastically improve initial app load --- backend/app/models/recipe.py | 32 ++++++++------------ kitchenowl/lib/cubits/recipe_list_cubit.dart | 21 ++++++++++++- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/backend/app/models/recipe.py b/backend/app/models/recipe.py index b602a561..64518579 100644 --- a/backend/app/models/recipe.py +++ b/backend/app/models/recipe.py @@ -31,15 +31,15 @@ class Recipe(db.Model, DbModelMixin, TimestampMixin, DbModelAuthorizeMixin): "RecipeHistory", back_populates="recipe", cascade="all, delete-orphan" ) items = db.relationship( - "RecipeItems", back_populates="recipe", cascade="all, delete-orphan" + "RecipeItems", back_populates="recipe", cascade="all, delete-orphan", lazy='selectin', order_by="RecipeItems._name", ) tags = db.relationship( - "RecipeTags", back_populates="recipe", cascade="all, delete-orphan" + "RecipeTags", back_populates="recipe", cascade="all, delete-orphan", lazy='selectin', order_by="RecipeTags._name", ) plans = db.relationship( - "Planner", back_populates="recipe", cascade="all, delete-orphan" + "Planner", back_populates="recipe", cascade="all, delete-orphan", lazy='selectin' ) - photo_file = db.relationship("File", back_populates="recipe", uselist=False) + photo_file = db.relationship("File", back_populates="recipe", uselist=False, lazy='selectin') def obj_to_dict(self) -> dict: res = super().obj_to_dict() @@ -51,20 +51,8 @@ def obj_to_dict(self) -> dict: def obj_to_full_dict(self) -> dict: res = self.obj_to_dict() - items = ( - RecipeItems.query.filter(RecipeItems.recipe_id == self.id) - .join(RecipeItems.item) - .order_by(Item.name) - .all() - ) - res["items"] = [e.obj_to_item_dict() for e in items] - tags = ( - RecipeTags.query.filter(RecipeTags.recipe_id == self.id) - .join(RecipeTags.tag) - .order_by(Tag.name) - .all() - ) - res["tags"] = [e.obj_to_item_dict() for e in tags] + res["items"] = [e.obj_to_item_dict() for e in self.items] + res["tags"] = [e.obj_to_item_dict() for e in self.tags] return res def obj_to_export_dict(self) -> dict: @@ -199,9 +187,11 @@ class RecipeItems(db.Model, DbModelMixin, TimestampMixin): description = db.Column("description", db.String()) optional = db.Column("optional", db.Boolean) - item = db.relationship("Item", back_populates="recipes") + item = db.relationship("Item", back_populates="recipes", lazy="joined") recipe = db.relationship("Recipe", back_populates="items") + _name = db.column_property(db.select(Item.name).where(Item.id == item_id).scalar_subquery()) + def obj_to_item_dict(self) -> dict: res = self.item.obj_to_dict() res["description"] = getattr(self, "description") @@ -235,7 +225,9 @@ class RecipeTags(db.Model, DbModelMixin, TimestampMixin): tag_id = db.Column(db.Integer, db.ForeignKey("tag.id"), primary_key=True) tag = db.relationship("Tag", back_populates="recipes") - recipe = db.relationship("Recipe", back_populates="tags") + recipe = db.relationship("Recipe", back_populates="tags", lazy="joined") + + _name = db.column_property(db.select(Tag.name).where(Tag.id == tag_id).scalar_subquery()) def obj_to_item_dict(self) -> dict: res = self.tag.obj_to_dict() diff --git a/kitchenowl/lib/cubits/recipe_list_cubit.dart b/kitchenowl/lib/cubits/recipe_list_cubit.dart index af13020b..198e4e16 100644 --- a/kitchenowl/lib/cubits/recipe_list_cubit.dart +++ b/kitchenowl/lib/cubits/recipe_list_cubit.dart @@ -20,7 +20,7 @@ class RecipeListCubit extends Cubit { toggleView(false); } }); - refresh(); + _initialLoad(); } String get query => (state is SearchRecipeListState) @@ -79,6 +79,25 @@ class RecipeListCubit extends Cubit { return _refreshThread!; } + Future _initialLoad() async { + final tags = TransactionHandler.getInstance().runTransaction( + TransactionTagGetAll(household: household), + forceOffline: true, + ); + recipeList = await TransactionHandler.getInstance().runTransaction( + TransactionRecipeGetRecipes(household: household), + forceOffline: true, + ); + + if (state is LoadingRecipeListState) { + emit(ListRecipeListState( + recipes: recipeList, + tags: await tags, + listView: state.listView, + )); + } + } + Future _refresh([String? query, bool runOffline = false]) async { late ListRecipeListState _state; if (state is ListRecipeListState &&