mirror of
https://github.com/zaphar/kitchen.git
synced 2025-07-22 19:40:14 -04:00
Handle staples as a special class
This commit is contained in:
parent
ac4f2118de
commit
652f1c976e
@ -24,6 +24,7 @@ use recipes::{parse, Ingredient, IngredientAccumulator, IngredientKey, Recipe};
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AppService {
|
pub struct AppService {
|
||||||
recipes: Signal<Vec<(usize, Signal<Recipe>)>>,
|
recipes: Signal<Vec<(usize, Signal<Recipe>)>>,
|
||||||
|
staples: Signal<Option<Recipe>>,
|
||||||
menu_list: Signal<BTreeMap<usize, usize>>,
|
menu_list: Signal<BTreeMap<usize, usize>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,6 +32,7 @@ impl AppService {
|
|||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
recipes: Signal::new(Vec::new()),
|
recipes: Signal::new(Vec::new()),
|
||||||
|
staples: Signal::new(None),
|
||||||
menu_list: Signal::new(BTreeMap::new()),
|
menu_list: Signal::new(BTreeMap::new()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,8 +67,10 @@ impl AppService {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_recipes_from_storage() -> Result<Option<Vec<(usize, Recipe)>>, String> {
|
pub fn fetch_recipes_from_storage(
|
||||||
|
) -> Result<(Option<Recipe>, Option<Vec<(usize, Recipe)>>), String> {
|
||||||
let storage = Self::get_storage()?.unwrap();
|
let storage = Self::get_storage()?.unwrap();
|
||||||
|
let mut staples = None;
|
||||||
match storage
|
match storage
|
||||||
.get_item("recipes")
|
.get_item("recipes")
|
||||||
.map_err(|e| format!("{:?}", e))?
|
.map_err(|e| format!("{:?}", e))?
|
||||||
@ -84,17 +88,21 @@ impl AppService {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
console_debug!("We parsed a recipe {}", recipe.title);
|
console_debug!("We parsed a recipe {}", recipe.title);
|
||||||
parsed_list.push(recipe);
|
if recipe.title == "Staples" {
|
||||||
|
staples = Some(recipe);
|
||||||
|
} else {
|
||||||
|
parsed_list.push(recipe);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(Some(parsed_list.drain(0..).enumerate().collect()))
|
Ok((staples, Some(parsed_list.drain(0..).enumerate().collect())))
|
||||||
}
|
}
|
||||||
None => Ok(None),
|
None => Ok((None, None)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn fetch_recipes() -> Result<Option<Vec<(usize, Recipe)>>, String> {
|
pub async fn fetch_recipes() -> Result<(Option<Recipe>, Option<Vec<(usize, Recipe)>>), String> {
|
||||||
if let Some(recipes) = Self::fetch_recipes_from_storage()? {
|
if let (staples, Some(recipes)) = Self::fetch_recipes_from_storage()? {
|
||||||
return Ok(Some(recipes));
|
return Ok((staples, Some(recipes)));
|
||||||
} else {
|
} else {
|
||||||
console_debug!("No recipes in cache synchronizing from api");
|
console_debug!("No recipes in cache synchronizing from api");
|
||||||
// Try to synchronize first
|
// Try to synchronize first
|
||||||
@ -105,8 +113,9 @@ impl AppService {
|
|||||||
|
|
||||||
pub async fn refresh_recipes(&mut self) -> Result<(), String> {
|
pub async fn refresh_recipes(&mut self) -> Result<(), String> {
|
||||||
Self::synchronize_recipes().await?;
|
Self::synchronize_recipes().await?;
|
||||||
if let Some(r) = Self::fetch_recipes().await? {
|
if let (staples, Some(r)) = Self::fetch_recipes().await? {
|
||||||
self.set_recipes(r);
|
self.set_recipes(r);
|
||||||
|
self.staples.set(staples);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -123,6 +132,9 @@ impl AppService {
|
|||||||
acc.accumulate_from(self.get_recipe_by_index(*idx).unwrap().get().as_ref());
|
acc.accumulate_from(self.get_recipe_by_index(*idx).unwrap().get().as_ref());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if let Some(staples) = self.staples.get().as_ref() {
|
||||||
|
acc.accumulate_from(staples);
|
||||||
|
}
|
||||||
acc.ingredients()
|
acc.ingredients()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,10 +57,10 @@ pub fn ui() -> View<G> {
|
|||||||
let mut app_service = app_service.clone();
|
let mut app_service = app_service.clone();
|
||||||
async move {
|
async move {
|
||||||
match AppService::fetch_recipes().await {
|
match AppService::fetch_recipes().await {
|
||||||
Ok(Some(recipes)) => {
|
Ok((_, Some(recipes))) => {
|
||||||
app_service.set_recipes(recipes);
|
app_service.set_recipes(recipes);
|
||||||
}
|
}
|
||||||
Ok(None) => {
|
Ok((_, None)) => {
|
||||||
console_error!("No recipes to find");
|
console_error!("No recipes to find");
|
||||||
}
|
}
|
||||||
Err(msg) => console_error!("Failed to get recipes {}", msg),
|
Err(msg) => console_error!("Failed to get recipes {}", msg),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user