Skip to content
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: 11 additions & 21 deletions crates/openmacro-app/src/components/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,9 @@ use crate::back::use_back_handler;
use crate::food_search::{self, Database, FoundFood};
use crate::foods::plate_item;
use crate::keypad::{self, Key};
use crate::prefs;
use crate::prefs::{self, FoodEntryTab as Tab};
use crate::state::{AppState, CoachSnapshot, LogEntry, PlateItem, day_totals};

#[derive(Debug, Clone, Copy, PartialEq)]
enum Tab {
Search,
Scan,
Ai,
QuickAdd,
Library,
}

#[component]
pub fn Logger() -> Element {
let mut state = use_context::<AppState>();
Expand All @@ -43,7 +34,13 @@ pub fn Logger() -> Element {
let foods_db = use_context::<Resource<Option<Database>>>();
// A food being copied into a new custom food.
let mut copying_food = use_signal(|| None::<Food>);
let mut tab = use_signal(|| Tab::Search);
let mut tab = use_signal(|| {
if state.recipe_edit.peek().is_some() {
Tab::Search
} else {
prefs::PREFS.peek().default_food_tab
}
});
let mut detail = use_signal(|| None::<Food>);
// Set when a scanned barcode isn't found, to prefill a new custom food.
let mut new_barcode = use_signal(|| None::<String>);
Expand Down Expand Up @@ -152,22 +149,15 @@ pub fn Logger() -> Element {
}
if !saving_recipe() {
nav { class: "sheet-tabs",
for (value , label) in [
(Tab::Search, "Search"),
(Tab::Scan, "Scan"),
(Tab::Ai, "AI"),
(Tab::QuickAdd, "Quick Add"),
(Tab::Library, "Library"),
]
{
for value in Tab::ALL {
button {
key: "{label}",
key: "{value.label()}",
class: if tab() == value && detail().is_none() { "active" },
onclick: move |_| {
detail.set(None);
tab.set(value);
},
"{label}"
"{value.label()}"
}
}
}
Expand Down
16 changes: 15 additions & 1 deletion crates/openmacro-app/src/components/more.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::health::HealthConnect;
use super::hour_label;
use crate::export;
use crate::food_search::{self, Database, FoodRegion};
use crate::prefs::{EnergyUnit, PREFS, Theme, WeightUnit};
use crate::prefs::{EnergyUnit, FoodEntryTab, PREFS, Theme, WeightUnit};
use crate::state::{AppState, CoachSnapshot, Data, today};

#[component]
Expand Down Expand Up @@ -419,6 +419,20 @@ fn DisplaySettings() -> Element {
}
}
}
div { class: "choice",
span { class: "choice-label", "Default food entry tab" }
div { class: "segmented even",
for tab in FoodEntryTab::ALL {
button {
key: "{tab.label()}",
class: if prefs.default_food_tab == tab { "active" },
aria_pressed: prefs.default_food_tab == tab,
onclick: move |_| PREFS.write().default_food_tab = tab,
"{tab.label()}"
}
}
}
}
div { class: "field-grid",
label { class: "field",
span { "Food log starts at" }
Expand Down
20 changes: 3 additions & 17 deletions crates/openmacro-app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,20 @@ mod health;
mod keypad;
mod persist;
mod prefs;
mod routes;
mod seed;
mod state;
mod updater;

use dioxus::prelude::*;

use components::{Dashboard, FoodLog, Insight, More, Plan, Shell, check_for_update};
use components::check_for_update;
pub use routes::Route;
use state::{AppState, today};

const MAIN_CSS: Asset = asset!("/assets/main.css");
const ICON: Asset = asset!("/assets/icon-192.png");

#[derive(Debug, Clone, Routable, PartialEq)]
#[rustfmt::skip]
pub enum Route {
#[layout(Shell)]
#[route("/")]
Dashboard {},
#[route("/log")]
FoodLog {},
#[route("/plan")]
Plan {},
#[route("/more")]
More {},
#[route("/insight/:metric")]
Insight { metric: String },
}

fn main() {
dioxus::launch(App);
}
Expand Down
61 changes: 61 additions & 0 deletions crates/openmacro-app/src/prefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,40 @@ impl Theme {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(
not(target_arch = "wasm32"),
derive(serde::Serialize, serde::Deserialize)
)]
pub enum FoodEntryTab {
#[default]
Search,
Scan,
Ai,
QuickAdd,
Library,
}

impl FoodEntryTab {
pub const ALL: [Self; 5] = [
Self::Search,
Self::Scan,
Self::Ai,
Self::QuickAdd,
Self::Library,
];

pub fn label(self) -> &'static str {
match self {
Self::Search => "Search",
Self::Scan => "Scan",
Self::Ai => "AI",
Self::QuickAdd => "Quick Add",
Self::Library => "Library",
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
not(target_arch = "wasm32"),
Expand All @@ -47,6 +81,7 @@ impl Theme {
pub struct Prefs {
pub theme: Theme,
pub clock_24h: bool,
pub default_food_tab: FoodEntryTab,
pub energy: EnergyUnit,
pub weight: WeightUnit,
/// The food log's first and last hours (earlier or later entries
Expand All @@ -62,6 +97,7 @@ impl Default for Prefs {
Self {
theme: Theme::default(),
clock_24h: false,
default_food_tab: FoodEntryTab::default(),
energy: EnergyUnit::default(),
weight: WeightUnit::default(),
log_first_hour: 6,
Expand Down Expand Up @@ -166,3 +202,28 @@ pub fn apply_theme(theme: Theme) {
theme.code()
));
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
use super::*;

#[test]
fn older_preferences_keep_their_settings_and_open_search() {
let prefs: Prefs = serde_json::from_str(r#"{"theme":"Light","clock_24h":true}"#).unwrap();
assert_eq!(prefs.theme, Theme::Light);
assert!(prefs.clock_24h);
assert_eq!(prefs.default_food_tab, FoodEntryTab::Search);
}

#[test]
fn every_food_tab_can_be_saved_and_restored() {
for tab in FoodEntryTab::ALL {
let prefs = Prefs {
default_food_tab: tab,
..Prefs::default()
};
let saved = serde_json::to_string(&prefs).unwrap();
assert_eq!(serde_json::from_str::<Prefs>(&saved).unwrap(), prefs);
}
}
}
22 changes: 22 additions & 0 deletions crates/openmacro-app/src/routes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Routable generates an unreachable error branch when parsing a String route segment.
#![allow(unreachable_code)]

use dioxus::prelude::*;

use crate::components::{Dashboard, FoodLog, Insight, More, Plan, Shell};

#[derive(Debug, Clone, Routable, PartialEq)]
#[rustfmt::skip]
pub enum Route {
#[layout(Shell)]
#[route("/")]
Dashboard {},
#[route("/log")]
FoodLog {},
#[route("/plan")]
Plan {},
#[route("/more")]
More {},
#[route("/insight/:metric")]
Insight { metric: String },
}
Loading