finished implementing setting category

This commit is contained in:
ide
2026-08-17 22:47:02 +02:00
parent b3dd373403
commit 495b0de7a7
8 changed files with 1712 additions and 1176 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};
use serde_json;
@@ -34,7 +36,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");
+34 -1
View File
@@ -17,7 +17,7 @@ use std::{
use inkopslista_lib::{ButtonItem, Item, ItemData};
use reqwest::Url;
use slint::{Model, ModelRc, SharedString, ToSharedString, VecModel};
use slint::{Model, ModelRc, ToSharedString, VecModel};
use crate::{config::Config, runtime::spawn};
@@ -60,6 +60,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<SItem>) {
list.clear();
@@ -322,6 +342,19 @@ pub fn run() -> Result<(), anyhow::Error> {
}
});
let conf = conf_shared.clone();
state.on_set_cat(move |itemname, cat| {
let conf = conf.clone();
spawn(async move {
let res = put_category(&itemname, &cat, &conf).await;
if let Err(err) = res {
eprintln!("{err:?}");
}
});
return;
});
state.on_set_server_address(move |address| {
conf_shared.lock().unwrap().address = address.to_string();
#[cfg(target_os = "linux")]
+6 -2
View File
@@ -13,8 +13,6 @@ import {
import { SettingsView } from "settings.slint";
import { State, SItem, SButtonItem } from "state.slint";
export { State, SItem }
component GoodListItem inherits Rectangle {
in-out property <SItem> item;
background: #ffffff10;
@@ -281,16 +279,22 @@ component ShopView inherits VerticalLayout {
component CatItem inherits HorizontalBox {
in-out property <SButtonItem> item;
Text {
text: item.name;
vertical-alignment: center;
font-size: 16px;
}
ComboBox {
model: ["kyl", "skafferi", "frys", "övrigt"];
current-value: item.category;
width: 60%;
selected(category) => { State.set-cat(item.name, category) }
}
}
component CatView inherits VerticalLayout {
in-out property <[SButtonItem]> cats;
+2 -3
View File
@@ -4,9 +4,7 @@ export struct SItem {
amount: int,
}
export struct SButtonItem{
name: string,
category: string}
export struct SButtonItem { name: string, category: string }
export global State {
in-out property <[SItem]> list: [
@@ -20,6 +18,7 @@ export global State {
callback delete-checked();
callback inc-amount(string);
callback dec-amount(string);
callback set-cat(string, string);
in-out property <string> server-address;
in property <[SButtonItem]> favorites: [
{ name: "mjölk", category: "kyl" },