Add automatic generation of favourite buttons

favourite buttons are based on how often a good i added to list
finished implementing setting category
made number of days until button an environment variable

rebase on main and fix

ran cargo fmt
This commit is contained in:
ide
2026-08-19 23:48:19 +02:00
parent c7782c82c4
commit 22cf928852
10 changed files with 314 additions and 36 deletions
+3 -1
View File
@@ -18,6 +18,8 @@ pub use fs::*;
#[cfg(target_os = "linux")]
mod fs {
use crate::runtime::spawn;
use super::Config;
use anyhow::{Context, Ok};
@@ -33,7 +35,7 @@ mod fs {
.place_config_file(CONFIG_FILE_NAME)
.context("unable to create config directory")?;
tokio::spawn(async {
spawn(async {
let res = tokio::fs::write(path, conf)
.await
.context("unable to write config file");
+58 -1
View File
@@ -16,7 +16,7 @@ use std::{
sync::{Arc, Mutex},
};
use inkopslista_lib::{Item, ItemData};
use inkopslista_lib::{ButtonItem, Item, ItemData};
use reqwest::Url;
use slint::{ComponentHandle, Model, ModelRc, ToSharedString, VecModel};
@@ -35,6 +35,13 @@ async fn get_list(conf: &Mutex<Config>) -> Result<Vec<Item>, anyhow::Error> {
Ok(list)
}
async fn get_favorites(conf: &Mutex<Config>) -> Result<Vec<ButtonItem>, anyhow::Error> {
let base = Url::parse(&conf.lock().unwrap().address)?;
let url = base.join("/api/favorites")?;
let favs = reqwest::get(url).await?.json::<Vec<ButtonItem>>().await?;
Ok(favs)
}
/// Add or update an item in the list
async fn put_list(item: &str, data: &ItemData, conf: &Mutex<Config>) -> Result<(), anyhow::Error> {
let client = reqwest::Client::new();
@@ -57,6 +64,26 @@ async fn delete_item(item: &str, conf: &Mutex<Config>) -> Result<(), anyhow::Err
Ok(())
}
async fn put_category(
itemname: &str,
category: &str,
conf: &Mutex<Config>,
) -> Result<(), anyhow::Error> {
let client = reqwest::Client::new();
let url: Url = format!(
"{address}/api/favourites/{itemname}/category",
address = conf.lock().unwrap().address
)
.parse()?;
let _response = client
.put(url)
.body(category.to_string())
.send()
.await?
.error_for_status()?;
Ok(())
}
/// Convert shopping list to slint types
fn update_slint_list(new_list: Vec<Item>, list: &VecModel<ui::SItem>) {
list.clear();
@@ -70,6 +97,17 @@ fn update_slint_list(new_list: Vec<Item>, list: &VecModel<ui::SItem>) {
.for_each(|item| list.push(item));
}
fn update_slint_favs(new_favs: Vec<ButtonItem>, favs: &VecModel<ui::SButtonItem>) {
favs.clear();
new_favs
.into_iter()
.map(|item| ui::SButtonItem {
name: item.name.into(),
category: item.category.unwrap_or_default().to_shared_string(),
})
.for_each(|item| favs.push(item));
}
fn refresh_list(conf: &Arc<Mutex<Config>>, app_weak: &slint::Weak<ui::App>, io: &Io) {
let conf = conf.clone();
let app_weak = app_weak.clone();
@@ -81,6 +119,13 @@ fn refresh_list(conf: &Arc<Mutex<Config>>, app_weak: &slint::Weak<ui::App>, io:
list.as_any().downcast_ref().expect("list is a VecModel");
update_slint_list(new_list, list);
});
let new_favs = get_favorites(&conf).await?;
let _ = app_weak.upgrade_in_event_loop(|app| {
let favs: ModelRc<ui::SButtonItem> = app.global::<ui::State>().get_favorites();
let favs: &VecModel<ui::SButtonItem> =
favs.as_any().downcast_ref().expect("favs is a vec model");
update_slint_favs(new_favs, favs);
});
Ok(())
});
}
@@ -90,7 +135,9 @@ pub fn run() -> Result<(), anyhow::Error> {
let io_ = Io::new(&app);
let state = app.global::<ui::State>();
let list_shared = Rc::new(VecModel::default());
let fav_shared = Rc::new(VecModel::default());
state.set_list(ModelRc::new(list_shared.clone()));
state.set_favorites(ModelRc::new(fav_shared.clone()));
#[cfg(target_os = "linux")]
let conf = runtime::RT
@@ -275,6 +322,16 @@ pub fn run() -> Result<(), anyhow::Error> {
}
});
let conf = conf_shared.clone();
let io = io_.clone();
state.on_set_cat(move |itemname, cat| {
let conf = conf.clone();
io.spawn(async move || put_category(&itemname, &cat, &conf).await);
return;
});
state.on_set_server_address(move |address| {
conf_shared.lock().unwrap().address = address.to_string();
#[cfg(target_os = "linux")]