kitchen/web/src/app_state.rs

367 lines
14 KiB
Rust
Raw Normal View History

// Copyright 2022 Jeremy Wall
2022-03-06 18:04:49 -05:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2022-03-06 18:04:49 -05:00
//
// http://www.apache.org/licenses/LICENSE-2.0
2022-03-06 18:04:49 -05:00
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::{
collections::{BTreeMap, BTreeSet},
fmt::Debug,
};
use client_api::UserData;
2022-12-29 11:58:37 -06:00
use recipes::{parse, IngredientKey, Recipe, RecipeEntry};
2022-12-28 12:59:11 -06:00
use sycamore::futures::spawn_local_scoped;
use sycamore::prelude::*;
use sycamore_state::{Handler, MessageMapper};
2022-12-28 12:59:11 -06:00
use tracing::{debug, error, info, instrument, warn};
2023-01-02 14:18:40 -06:00
use wasm_bindgen::throw_str;
use crate::api::{HttpStore, LocalStore};
2022-12-28 11:54:24 -06:00
#[derive(Debug, Clone, PartialEq)]
pub struct AppState {
pub recipe_counts: BTreeMap<String, usize>,
pub extras: Vec<(String, String)>,
pub staples: Option<Recipe>,
pub recipes: BTreeMap<String, Recipe>,
2022-12-28 14:55:15 -06:00
pub category_map: String,
pub filtered_ingredients: BTreeSet<IngredientKey>,
pub modified_amts: BTreeMap<IngredientKey, String>,
pub auth: Option<UserData>,
}
impl AppState {
pub fn new() -> Self {
Self {
recipe_counts: BTreeMap::new(),
extras: Vec::new(),
staples: None,
recipes: BTreeMap::new(),
2022-12-28 14:55:15 -06:00
category_map: String::new(),
filtered_ingredients: BTreeSet::new(),
modified_amts: BTreeMap::new(),
auth: None,
}
}
}
pub enum Message {
2022-12-28 19:33:19 -06:00
ResetRecipeCounts,
UpdateRecipeCount(String, usize),
AddExtra(String, String),
RemoveExtra(usize),
UpdateExtra(usize, String, String),
2022-12-28 14:27:45 -06:00
SaveRecipe(RecipeEntry),
SetRecipe(String, Recipe),
2022-12-28 14:55:15 -06:00
SetCategoryMap(String),
2022-12-28 19:33:19 -06:00
ResetInventory,
AddFilteredIngredient(IngredientKey),
UpdateAmt(IngredientKey, String),
SetUserData(UserData),
SaveState(Option<Box<dyn FnOnce()>>),
LoadState(Option<Box<dyn FnOnce()>>),
}
impl Debug for Message {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ResetRecipeCounts => write!(f, "ResetRecipeCounts"),
Self::UpdateRecipeCount(arg0, arg1) => f
.debug_tuple("UpdateRecipeCount")
.field(arg0)
.field(arg1)
.finish(),
Self::AddExtra(arg0, arg1) => {
f.debug_tuple("AddExtra").field(arg0).field(arg1).finish()
}
Self::RemoveExtra(arg0) => f.debug_tuple("RemoveExtra").field(arg0).finish(),
Self::UpdateExtra(arg0, arg1, arg2) => f
.debug_tuple("UpdateExtra")
.field(arg0)
.field(arg1)
.field(arg2)
.finish(),
Self::SaveRecipe(arg0) => f.debug_tuple("SaveRecipe").field(arg0).finish(),
Self::SetRecipe(arg0, arg1) => {
f.debug_tuple("SetRecipe").field(arg0).field(arg1).finish()
}
Self::SetCategoryMap(arg0) => f.debug_tuple("SetCategoryMap").field(arg0).finish(),
Self::ResetInventory => write!(f, "ResetInventory"),
Self::AddFilteredIngredient(arg0) => {
f.debug_tuple("AddFilteredIngredient").field(arg0).finish()
}
Self::UpdateAmt(arg0, arg1) => {
f.debug_tuple("UpdateAmt").field(arg0).field(arg1).finish()
}
Self::SetUserData(arg0) => f.debug_tuple("SetUserData").field(arg0).finish(),
Self::SaveState(_) => write!(f, "SaveState"),
Self::LoadState(_) => write!(f, "LoadState"),
}
}
}
pub struct StateMachine {
store: HttpStore,
local_store: LocalStore,
}
2022-12-28 12:59:11 -06:00
#[instrument]
fn filter_recipes(
recipe_entries: &Option<Vec<RecipeEntry>>,
) -> Result<(Option<Recipe>, Option<BTreeMap<String, Recipe>>), String> {
match recipe_entries {
Some(parsed) => {
let mut staples = None;
let mut parsed_map = BTreeMap::new();
for r in parsed {
let recipe = match parse::as_recipe(&r.recipe_text()) {
Ok(r) => r,
Err(e) => {
error!("Error parsing recipe {}", e);
continue;
}
};
if recipe.title == "Staples" {
staples = Some(recipe);
} else {
parsed_map.insert(r.recipe_id().to_owned(), recipe);
}
}
Ok((staples, Some(parsed_map)))
}
None => Ok((None, None)),
}
}
2022-12-28 19:33:19 -06:00
2022-12-28 12:59:11 -06:00
impl StateMachine {
pub fn new(store: HttpStore, local_store: LocalStore) -> Self {
Self { store, local_store }
}
async fn load_state(
store: &HttpStore,
local_store: &LocalStore,
original: &Signal<AppState>,
) -> Result<(), crate::api::Error> {
2022-12-28 12:59:11 -06:00
let mut state = original.get().as_ref().clone();
info!("Synchronizing Recipes");
let recipe_entries = &store.get_recipes().await?;
let (staples, recipes) = filter_recipes(&recipe_entries)?;
if let Some(recipes) = recipes {
state.staples = staples;
state.recipes = recipes;
2022-12-28 12:59:11 -06:00
};
let plan = store.get_plan().await?;
if let Some(plan) = plan {
2022-12-28 12:59:11 -06:00
// set the counts.
let mut plan_map = BTreeMap::new();
for (id, count) in plan {
plan_map.insert(id, count as usize);
}
state.recipe_counts = plan_map;
} else {
// Initialize things to zero
if let Some(rs) = recipe_entries {
for r in rs {
state.recipe_counts.insert(r.recipe_id().to_owned(), 0);
}
}
}
info!("Checking for user_data in local storage");
let user_data = local_store.get_user_data();
state.auth = user_data;
2022-12-28 12:59:11 -06:00
info!("Synchronizing categories");
match store.get_categories().await {
Ok(Some(categories_content)) => {
debug!(categories=?categories_content);
2022-12-28 14:55:15 -06:00
state.category_map = categories_content;
2022-12-28 12:59:11 -06:00
}
Ok(None) => {
warn!("There is no category file");
}
Err(e) => {
error!("{:?}", e);
}
}
info!("Synchronizing inventory data");
match store.get_inventory_data().await {
Ok((filtered_ingredients, modified_amts, extra_items)) => {
state.modified_amts = modified_amts;
state.filtered_ingredients = filtered_ingredients;
state.extras = extra_items;
2022-12-28 12:59:11 -06:00
}
Err(e) => {
error!("{:?}", e);
}
}
original.set(state);
Ok(())
2022-12-28 12:59:11 -06:00
}
}
impl MessageMapper<Message, AppState> for StateMachine {
2022-12-28 11:54:24 -06:00
#[instrument(skip_all, fields(?msg))]
2022-12-28 12:59:11 -06:00
fn map<'ctx>(&self, cx: Scope<'ctx>, msg: Message, original: &'ctx Signal<AppState>) {
let mut original_copy = original.get().as_ref().clone();
debug!("handling state message");
match msg {
2022-12-28 19:33:19 -06:00
Message::ResetRecipeCounts => {
let mut map = BTreeMap::new();
for (id, _) in original_copy.recipes.iter() {
map.insert(id.clone(), 0);
}
let plan: Vec<(String, i32)> =
map.iter().map(|(s, i)| (s.clone(), *i as i32)).collect();
self.local_store.save_plan(&plan);
original_copy.recipe_counts = map;
}
Message::UpdateRecipeCount(id, count) => {
original_copy.recipe_counts.insert(id, count);
let plan: Vec<(String, i32)> = original_copy
.recipe_counts
.iter()
.map(|(s, i)| (s.clone(), *i as i32))
.collect();
self.local_store.save_plan(&plan);
}
Message::AddExtra(amt, name) => {
original_copy.extras.push((amt, name));
self.local_store.set_inventory_data((
&original_copy.filtered_ingredients,
&original_copy.modified_amts,
&original_copy.extras,
))
}
Message::RemoveExtra(idx) => {
original_copy.extras.remove(idx);
self.local_store.set_inventory_data((
&original_copy.filtered_ingredients,
&original_copy.modified_amts,
&original_copy.extras,
))
}
Message::UpdateExtra(idx, amt, name) => {
match original_copy.extras.get_mut(idx) {
Some(extra) => {
extra.0 = amt;
extra.1 = name;
}
None => {
throw_str("Attempted to remove extra that didn't exist");
}
}
self.local_store.set_inventory_data((
&original_copy.filtered_ingredients,
&original_copy.modified_amts,
&original_copy.extras,
))
}
Message::SetRecipe(id, recipe) => {
original_copy.recipes.insert(id, recipe);
}
2022-12-28 14:27:45 -06:00
Message::SaveRecipe(entry) => {
let recipe =
parse::as_recipe(entry.recipe_text()).expect("Failed to parse RecipeEntry");
original_copy
.recipes
.insert(entry.recipe_id().to_owned(), recipe);
original_copy
.recipe_counts
.insert(entry.recipe_id().to_owned(), 0);
let store = self.store.clone();
self.local_store.set_recipe_entry(&entry);
2022-12-28 14:27:45 -06:00
spawn_local_scoped(cx, async move {
if let Err(e) = store.save_recipes(vec![entry]).await {
error!(err=?e, "Unable to save Recipe");
}
});
}
2022-12-28 14:55:15 -06:00
Message::SetCategoryMap(category_text) => {
original_copy.category_map = category_text.clone();
self.local_store.set_categories(Some(&category_text));
let store = self.store.clone();
2022-12-28 14:55:15 -06:00
spawn_local_scoped(cx, async move {
if let Err(e) = store.save_categories(category_text).await {
error!(?e, "Failed to save categories");
}
});
}
2022-12-28 19:33:19 -06:00
Message::ResetInventory => {
original_copy.filtered_ingredients = BTreeSet::new();
original_copy.modified_amts = BTreeMap::new();
original_copy.extras = Vec::new();
self.local_store.set_inventory_data((
&original_copy.filtered_ingredients,
&original_copy.modified_amts,
&original_copy.extras,
));
2022-12-28 19:33:19 -06:00
}
Message::AddFilteredIngredient(key) => {
original_copy.filtered_ingredients.insert(key);
self.local_store.set_inventory_data((
&original_copy.filtered_ingredients,
&original_copy.modified_amts,
&original_copy.extras,
));
}
Message::UpdateAmt(key, amt) => {
original_copy.modified_amts.insert(key, amt);
self.local_store.set_inventory_data((
&original_copy.filtered_ingredients,
&original_copy.modified_amts,
&original_copy.extras,
));
}
Message::SetUserData(user_data) => {
original_copy.auth = Some(user_data);
}
Message::SaveState(f) => {
2022-12-28 11:54:24 -06:00
let original_copy = original_copy.clone();
let store = self.store.clone();
2022-12-28 12:59:11 -06:00
spawn_local_scoped(cx, async move {
2022-12-28 11:54:24 -06:00
if let Err(e) = store.save_app_state(original_copy).await {
error!(err=?e, "Error saving app state")
};
f.map(|f| f());
2022-12-28 11:54:24 -06:00
});
}
Message::LoadState(f) => {
let store = self.store.clone();
let local_store = self.local_store.clone();
2022-12-28 12:59:11 -06:00
spawn_local_scoped(cx, async move {
Self::load_state(&store, &local_store, original)
.await
2023-01-02 14:18:40 -06:00
.expect("Failed to load_state.");
local_store.set_inventory_data((
&original.get().filtered_ingredients,
&original.get().modified_amts,
&original.get().extras,
));
f.map(|f| f());
2022-12-28 12:59:11 -06:00
});
return;
}
}
2022-12-28 12:59:11 -06:00
original.set(original_copy);
}
}
2022-12-26 21:29:09 -05:00
pub type StateHandler<'ctx> = &'ctx Handler<'ctx, StateMachine, AppState, Message>;
2022-12-28 11:54:24 -06:00
pub fn get_state_handler<'ctx>(
cx: Scope<'ctx>,
initial: AppState,
store: HttpStore,
) -> StateHandler<'ctx> {
Handler::new(cx, initial, StateMachine::new(store, LocalStore::new()))
}