kitchen/web/src/app_state.rs

544 lines
22 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 chrono::NaiveDate;
use client_api::UserData;
2023-01-06 11:04:03 -05:00
use recipes::{parse, Ingredient, IngredientKey, Recipe, RecipeEntry};
use serde::{Deserialize, Serialize};
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;
2023-03-25 08:55:32 -04:00
use crate::{
api::{HttpStore, LocalStore},
linear::LinearSignal,
2023-03-25 08:55:32 -04:00
};
2022-12-28 11:54:24 -06:00
fn bool_true() -> bool {
true
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AppState {
pub recipe_counts: BTreeMap<String, u32>,
pub recipe_categories: BTreeMap<String, String>,
pub extras: Vec<(String, String)>,
// FIXME(jwall): This should really be storable I think?
2024-09-23 20:09:46 -04:00
#[serde(skip_deserializing, skip_serializing)]
2023-01-06 11:04:03 -05:00
pub staples: Option<BTreeSet<Ingredient>>,
// FIXME(jwall): This should really be storable I think?
2024-09-23 20:09:46 -04:00
#[serde(skip_deserializing, skip_serializing)]
pub recipes: BTreeMap<String, Recipe>,
pub category_map: BTreeMap<String, String>,
pub filtered_ingredients: BTreeSet<IngredientKey>,
pub modified_amts: BTreeMap<IngredientKey, String>,
pub auth: Option<UserData>,
pub plan_dates: BTreeSet<NaiveDate>,
pub selected_plan_date: Option<NaiveDate>,
#[serde(default = "bool_true")]
pub use_staples: bool,
}
impl AppState {
pub fn new() -> Self {
Self {
recipe_counts: BTreeMap::new(),
recipe_categories: BTreeMap::new(),
extras: Vec::new(),
staples: None,
recipes: BTreeMap::new(),
category_map: BTreeMap::new(),
filtered_ingredients: BTreeSet::new(),
modified_amts: BTreeMap::new(),
auth: None,
plan_dates: BTreeSet::new(),
selected_plan_date: None,
use_staples: true,
}
}
}
pub enum Message {
2022-12-28 19:33:19 -06:00
ResetRecipeCounts,
UpdateRecipeCount(String, u32),
AddExtra(String, String),
RemoveExtra(usize),
UpdateExtra(usize, String, String),
2023-01-07 16:34:15 -05:00
SaveRecipe(RecipeEntry, Option<Box<dyn FnOnce()>>),
RemoveRecipe(String, Option<Box<dyn FnOnce()>>),
UpdateCategory(String, String, Option<Box<dyn FnOnce()>>),
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()>>),
2023-01-07 16:34:15 -05:00
UpdateStaples(String, Option<Box<dyn FnOnce()>>),
2023-01-18 19:29:44 -05:00
DeletePlan(NaiveDate, Option<Box<dyn FnOnce()>>),
2023-01-17 21:06:27 -05:00
SelectPlanDate(NaiveDate, Option<Box<dyn FnOnce()>>),
UpdateUseStaples(bool), // TODO(jwall): Should this just be various settings?
}
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(),
2023-01-07 16:34:15 -05:00
Self::SaveRecipe(arg0, _) => f.debug_tuple("SaveRecipe").field(arg0).finish(),
Self::RemoveRecipe(arg0, _) => f.debug_tuple("SetCategoryMap").field(arg0).finish(),
Self::UpdateCategory(i, c, _) => {
f.debug_tuple("UpdateCategory").field(i).field(c).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"),
2023-01-07 16:34:15 -05:00
Self::UpdateStaples(arg, _) => f.debug_tuple("UpdateStaples").field(arg).finish(),
Self::UpdateUseStaples(arg) => f.debug_tuple("UpdateUseStaples").field(arg).finish(),
2023-01-17 21:06:27 -05:00
Self::SelectPlanDate(arg, _) => f.debug_tuple("SelectPlanDate").field(arg).finish(),
2023-01-18 19:29:44 -05:00
Self::DeletePlan(arg, _) => f.debug_tuple("DeletePlan").field(arg).finish(),
}
}
}
pub struct StateMachine {
store: HttpStore,
local_store: LocalStore,
}
2022-12-28 12:59:11 -06:00
#[instrument]
pub fn parse_recipes(
2022-12-28 12:59:11 -06:00
recipe_entries: &Option<Vec<RecipeEntry>>,
2023-01-07 15:43:19 -05:00
) -> Result<Option<BTreeMap<String, Recipe>>, String> {
2022-12-28 12:59:11 -06:00
match recipe_entries {
Some(parsed) => {
let mut parsed_map = BTreeMap::new();
for r in parsed {
let mut recipe = match parse::as_recipe(&r.recipe_text()) {
2022-12-28 12:59:11 -06:00
Ok(r) => r,
Err(e) => {
error!("Error parsing recipe {}", e);
continue;
}
};
recipe.serving_count = r.serving_count;
2023-01-07 15:43:19 -05:00
parsed_map.insert(r.recipe_id().to_owned(), recipe);
2022-12-28 12:59:11 -06:00
}
2023-01-07 15:43:19 -05:00
Ok(Some(parsed_map))
2022-12-28 12:59:11 -06:00
}
2023-01-07 15:43:19 -05:00
None => Ok(None),
2022-12-28 12:59:11 -06:00
}
}
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 }
}
#[instrument(skip_all)]
async fn load_state(
store: &HttpStore,
local_store: &LocalStore,
original: &Signal<AppState>,
) -> Result<(), crate::api::Error> {
2024-01-06 10:25:55 -05:00
// NOTE(jwall): We use a linear Signal in here to ensure that we only
// call set on the signal once. When the LinearSignal get's dropped it
// will call set on the contained Signal.
let mut original: LinearSignal<AppState> = original.into();
if let Some(state) = local_store.fetch_app_state().await {
original = original.update(state);
}
2022-12-28 12:59:11 -06:00
let mut state = original.get().as_ref().clone();
info!("Synchronizing Recipes");
2023-01-05 11:45:58 -05:00
let recipe_entries = &store.fetch_recipes().await?;
2023-01-07 15:43:19 -05:00
let recipes = parse_recipes(&recipe_entries)?;
debug!(?recipes, "Parsed Recipes");
if let Some(recipes) = recipes {
state.recipes = recipes;
2022-12-28 12:59:11 -06:00
};
2023-01-06 11:04:03 -05:00
info!("Synchronizing staples");
2023-01-06 11:04:03 -05:00
state.staples = if let Some(content) = store.fetch_staples().await? {
// now we need to parse staples as ingredients
let mut staples = parse::as_ingredient_list(&content)?;
Some(staples.drain(0..).collect())
} else {
Some(BTreeSet::new())
2023-01-06 11:04:03 -05:00
};
info!("Synchronizing recipe");
2023-01-04 18:11:35 -05:00
if let Some(recipe_entries) = recipe_entries {
local_store.set_all_recipes(recipe_entries).await;
state.recipe_categories = recipe_entries
.iter()
.map(|entry| {
debug!(recipe_entry=?entry, "Getting recipe category");
(
entry.recipe_id().to_owned(),
entry
.category()
.cloned()
2023-02-04 12:06:16 -05:00
.unwrap_or_else(|| "Entree".to_owned()),
)
})
.collect::<BTreeMap<String, String>>();
2023-01-04 18:11:35 -05:00
}
2022-12-28 12:59:11 -06:00
info!("Fetching meal plan list");
if let Some(mut plan_dates) = store.fetch_plan_dates().await? {
debug!(?plan_dates, "meal plan list");
state.plan_dates = BTreeSet::from_iter(plan_dates.drain(0..));
}
info!("Synchronizing meal plan");
let plan = if let Some(ref cached_plan_date) = state.selected_plan_date {
store
.fetch_plan_for_date(cached_plan_date)
.await?
.or_else(|| Some(Vec::new()))
} else {
None
};
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 u32);
2022-12-28 12:59:11 -06:00
}
state.recipe_counts = plan_map;
2023-01-18 20:12:29 -05:00
for (id, _) in state.recipes.iter() {
if !state.recipe_counts.contains_key(id) {
state.recipe_counts.insert(id.clone(), 0);
}
}
2022-12-28 12:59:11 -06:00
} 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);
2022-12-28 12:59:11 -06:00
}
}
}
2023-01-04 18:42:02 -05:00
info!("Checking for user account data");
2023-01-05 11:45:58 -05:00
if let Some(user_data) = store.fetch_user_data().await {
2023-01-04 18:42:02 -05:00
debug!("Successfully got account data from server");
local_store.set_user_data(Some(&user_data)).await;
2023-01-04 18:42:02 -05:00
state.auth = Some(user_data);
} else {
debug!("Using account data from local store");
let user_data = local_store.get_user_data().await;
2023-01-04 18:42:02 -05:00
state.auth = user_data;
}
2022-12-28 12:59:11 -06:00
info!("Synchronizing categories");
2023-01-05 11:45:58 -05:00
match store.fetch_categories().await {
Ok(Some(mut categories_content)) => {
2022-12-28 12:59:11 -06:00
debug!(categories=?categories_content);
let category_map = BTreeMap::from_iter(categories_content.drain(0..));
state.category_map = category_map;
2022-12-28 12:59:11 -06:00
}
Ok(None) => {
warn!("There is no category file");
}
Err(e) => {
error!("{:?}", e);
}
}
let inventory_data = if let Some(cached_plan_date) = &state.selected_plan_date {
store.fetch_inventory_for_date(cached_plan_date).await
} else {
store.fetch_inventory_data().await
};
2022-12-28 12:59:11 -06:00
info!("Synchronizing inventory data");
match inventory_data {
2022-12-28 12:59:11 -06:00
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);
}
}
// Finally we store all of this app state back to our localstore
local_store.store_app_state(&state).await;
2023-07-22 16:14:23 -05:00
original.update(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);
}
original_copy.recipe_counts = map;
}
Message::UpdateRecipeCount(id, count) => {
original_copy.recipe_counts.insert(id, count);
}
Message::AddExtra(amt, name) => {
original_copy.extras.push((amt, name));
}
Message::RemoveExtra(idx) => {
original_copy.extras.remove(idx);
}
2023-07-22 16:14:23 -05:00
Message::UpdateExtra(idx, amt, name) => match original_copy.extras.get_mut(idx) {
Some(extra) => {
extra.0 = amt;
extra.1 = name;
}
2023-07-22 16:14:23 -05:00
None => {
throw_str("Attempted to remove extra that didn't exist");
}
},
2023-01-07 16:34:15 -05:00
Message::SaveRecipe(entry, callback) => {
2022-12-28 14:27:45 -06:00
let recipe =
parse::as_recipe(entry.recipe_text()).expect("Failed to parse RecipeEntry");
original_copy
.recipes
.insert(entry.recipe_id().to_owned(), recipe);
2023-02-04 12:06:16 -05:00
if !original_copy.recipe_counts.contains_key(entry.recipe_id()) {
original_copy
.recipe_counts
.insert(entry.recipe_id().to_owned(), 0);
}
if let Some(cat) = entry.category().cloned() {
original_copy
.recipe_categories
.entry(entry.recipe_id().to_owned())
.and_modify(|c| *c = cat.clone())
.or_insert(cat);
}
let store = self.store.clone();
let local_store = self.local_store.clone();
2022-12-28 14:27:45 -06:00
spawn_local_scoped(cx, async move {
local_store.set_recipe_entry(&entry).await;
2023-01-05 13:06:19 -05:00
if let Err(e) = store.store_recipes(vec![entry]).await {
2023-02-04 12:06:16 -05:00
// FIXME(jwall): We should have a global way to trigger error messages
2022-12-28 14:27:45 -06:00
error!(err=?e, "Unable to save Recipe");
2023-03-25 08:55:32 -04:00
// FIXME(jwall): This should be an error message
} else {
2022-12-28 14:27:45 -06:00
}
2023-01-07 16:34:15 -05:00
callback.map(|f| f());
2022-12-28 14:27:45 -06:00
});
}
2023-01-07 16:34:15 -05:00
Message::RemoveRecipe(recipe, callback) => {
2023-01-07 15:43:19 -05:00
original_copy.recipe_counts.remove(&recipe);
original_copy.recipes.remove(&recipe);
let store = self.store.clone();
let local_store = self.local_store.clone();
spawn_local_scoped(cx, async move {
local_store.delete_recipe_entry(&recipe).await;
2023-01-07 15:43:19 -05:00
if let Err(err) = store.delete_recipe(&recipe).await {
error!(?err, "Failed to delete recipe");
}
2023-01-07 16:34:15 -05:00
callback.map(|f| f());
});
}
2023-01-07 16:34:15 -05:00
Message::UpdateCategory(ingredient, category, callback) => {
original_copy
.category_map
.insert(ingredient.clone(), category.clone());
let store = self.store.clone();
2022-12-28 14:55:15 -06:00
spawn_local_scoped(cx, async move {
if let Err(e) = store.store_categories(&vec![(ingredient, category)]).await {
2022-12-28 14:55:15 -06:00
error!(?e, "Failed to save categories");
}
2023-01-07 16:34:15 -05:00
callback.map(|f| f());
2022-12-28 14:55:15 -06:00
});
}
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();
2022-12-28 19:33:19 -06:00
}
Message::AddFilteredIngredient(key) => {
original_copy.filtered_ingredients.insert(key);
}
Message::UpdateAmt(key, amt) => {
original_copy.modified_amts.insert(key, amt);
}
Message::SetUserData(user_data) => {
let local_store = self.local_store.clone();
original_copy.auth = Some(user_data.clone());
spawn_local_scoped(cx, async move {
local_store.set_user_data(Some(&user_data)).await;
});
}
Message::SaveState(f) => {
2023-01-18 20:12:29 -05:00
let mut original_copy = original_copy.clone();
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 {
2023-01-18 20:12:29 -05:00
if original_copy.selected_plan_date.is_none() {
original_copy.selected_plan_date = Some(chrono::Local::now().date_naive());
}
original_copy.plan_dates.insert(
original_copy
.selected_plan_date
.as_ref()
.map(|d| d.clone())
.unwrap(),
);
2023-01-18 20:12:29 -05:00
if let Err(e) = store.store_app_state(&original_copy).await {
2023-03-25 08:55:32 -04:00
error!(err=?e, "Error saving app state");
2022-12-28 11:54:24 -06:00
};
local_store.store_app_state(&original_copy).await;
2023-01-18 20:12:29 -05:00
original.set(original_copy);
f.map(|f| f());
2022-12-28 11:54:24 -06:00
});
2023-01-18 20:12:29 -05:00
// NOTE(jwall): We set the original signal in the async above
// so we return immediately here.
return;
2022-12-28 11:54:24 -06:00
}
Message::LoadState(f) => {
let store = self.store.clone();
let local_store = self.local_store.clone();
debug!("Loading user state.");
2022-12-28 12:59:11 -06:00
spawn_local_scoped(cx, async move {
if let Err(err) = Self::load_state(&store, &local_store, original).await {
2023-03-25 08:55:32 -04:00
error!(?err, "Failed to load user state");
}
f.map(|f| f());
2022-12-28 12:59:11 -06:00
});
return;
}
2023-01-07 16:34:15 -05:00
Message::UpdateStaples(content, callback) => {
2023-01-06 11:04:03 -05:00
let store = self.store.clone();
spawn_local_scoped(cx, async move {
2023-03-25 08:55:32 -04:00
if let Err(err) = store.store_staples(content).await {
error!(?err, "Failed to store staples");
} else {
callback.map(|f| f());
}
2023-01-06 11:04:03 -05:00
});
return;
}
Message::UpdateUseStaples(value) => {
original_copy.use_staples = value;
}
2023-01-17 21:06:27 -05:00
Message::SelectPlanDate(date, callback) => {
let store = self.store.clone();
2023-07-22 16:14:23 -05:00
let local_store = self.local_store.clone();
spawn_local_scoped(cx, async move {
if let Some(mut plan) = store
.fetch_plan_for_date(&date)
.await
.expect("Failed to fetch plan for date")
{
// Note(jwall): This is a little unusual but because this
// is async code we can't rely on the set below.
original_copy.recipe_counts =
BTreeMap::from_iter(plan.drain(0..).map(|(k, v)| (k, v as u32)));
}
let (filtered, modified, extras) = store
.fetch_inventory_for_date(&date)
.await
.expect("Failed to fetch inventory_data for date");
original_copy.plan_dates.insert(date.clone());
original_copy.modified_amts = modified;
original_copy.filtered_ingredients = filtered;
original_copy.extras = extras;
original_copy.selected_plan_date = Some(date.clone());
store
.store_plan_for_date(vec![], &date)
.await
.expect("Failed to init meal plan for date");
local_store.store_app_state(&original_copy).await;
original.set(original_copy);
2023-01-17 21:06:27 -05:00
callback.map(|f| f());
});
// NOTE(jwall): Because we do our signal set above in the async block
// we have to return here to avoid lifetime issues and double setting
// the original signal.
return;
}
2023-01-18 19:29:44 -05:00
Message::DeletePlan(date, callback) => {
let store = self.store.clone();
2023-07-22 16:14:23 -05:00
let local_store = self.local_store.clone();
2023-01-18 19:29:44 -05:00
spawn_local_scoped(cx, async move {
2023-03-25 08:55:32 -04:00
if let Err(err) = store.delete_plan_for_date(&date).await {
error!(?err, "Error deleting plan");
} else {
original_copy.plan_dates.remove(&date);
// Reset all meal planning state;
let _ = original_copy.recipe_counts.iter_mut().map(|(_, v)| *v = 0);
original_copy.filtered_ingredients = BTreeSet::new();
original_copy.modified_amts = BTreeMap::new();
original_copy.extras = Vec::new();
local_store.store_app_state(&original_copy).await;
2023-03-25 08:55:32 -04:00
original.set(original_copy);
2023-01-18 19:29:44 -05:00
2023-03-25 08:55:32 -04:00
callback.map(|f| f());
}
2023-01-18 19:29:44 -05:00
});
// NOTE(jwall): Because we do our signal set above in the async block
// we have to return here to avoid lifetime issues and double setting
// the original signal.
2023-01-18 19:29:44 -05:00
return;
}
}
spawn_local_scoped(cx, {
let local_store = self.local_store.clone();
async move {
2024-09-23 20:09:46 -04:00
local_store.store_app_state(&original_copy).await;
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()))
}