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

fix: Drastically improve initial app load with many recipes #435

Merged
merged 1 commit into from
Apr 24, 2024
Merged
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
32 changes: 12 additions & 20 deletions backend/app/models/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand Down
21 changes: 20 additions & 1 deletion kitchenowl/lib/cubits/recipe_list_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RecipeListCubit extends Cubit<RecipeListState> {
toggleView(false);
}
});
refresh();
_initialLoad();
}

String get query => (state is SearchRecipeListState)
Expand Down Expand Up @@ -79,6 +79,25 @@ class RecipeListCubit extends Cubit<RecipeListState> {
return _refreshThread!;
}

Future<void> _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<void> _refresh([String? query, bool runOffline = false]) async {
late ListRecipeListState _state;
if (state is ListRecipeListState &&
Expand Down
Loading