mirror of
https://github.com/hulthe/inkopslista.git
synced 2026-08-03 23:11:28 +02:00
implemented amounts in gui
This commit is contained in:
+87
-2
@@ -61,6 +61,7 @@ fn update_slint_list(new_list: Vec<Item>, list: &VecModel<SItem>) {
|
||||
.map(|item| SItem {
|
||||
name: item.name.to_shared_string(),
|
||||
checked: item.data.checked,
|
||||
amount: item.data.amount as i32,
|
||||
})
|
||||
.for_each(|item| list.push(item));
|
||||
}
|
||||
@@ -122,13 +123,32 @@ pub fn run() -> Result<(), anyhow::Error> {
|
||||
let conf = conf_shared.clone();
|
||||
let list = list_shared.clone();
|
||||
state.on_add_to_list(move |item| {
|
||||
if list.iter().any(|item2| item2.name == item) {
|
||||
if let Some(index) = list.iter().position(|item2| item2.name == item) {
|
||||
let mut item2 = list.row_data(index).unwrap();
|
||||
item2.amount = item2.amount + 1;
|
||||
let item3 = item2.clone();
|
||||
list.set_row_data(index, item2);
|
||||
let conf = conf.clone();
|
||||
spawn(async move {
|
||||
let res = put_list(
|
||||
&item,
|
||||
&ItemData {
|
||||
checked: item3.checked,
|
||||
amount: item3.amount as i64,
|
||||
},
|
||||
&conf,
|
||||
)
|
||||
.await;
|
||||
if let Err(err) = res {
|
||||
eprintln!("{err:?}");
|
||||
}
|
||||
});
|
||||
return;
|
||||
// TODO: implement increment when number is added
|
||||
}
|
||||
list.push(SItem {
|
||||
checked: false,
|
||||
name: item.clone(),
|
||||
amount: 1,
|
||||
});
|
||||
let conf = conf.clone();
|
||||
spawn(async move {
|
||||
@@ -204,6 +224,71 @@ pub fn run() -> Result<(), anyhow::Error> {
|
||||
});
|
||||
});
|
||||
|
||||
let conf = conf_shared.clone();
|
||||
let list = list_shared.clone();
|
||||
state.on_inc_amount(move |item| {
|
||||
if let Some(index) = list.iter().position(|item2| item2.name == item) {
|
||||
let mut item2 = list.row_data(index).unwrap();
|
||||
item2.amount = item2.amount + 1;
|
||||
let item3 = item2.clone();
|
||||
list.set_row_data(index, item2);
|
||||
let conf = conf.clone();
|
||||
spawn(async move {
|
||||
let res = put_list(
|
||||
&item,
|
||||
&ItemData {
|
||||
checked: item3.checked,
|
||||
amount: item3.amount as i64,
|
||||
},
|
||||
&conf,
|
||||
)
|
||||
.await;
|
||||
if let Err(err) = res {
|
||||
eprintln!("{err:?}");
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
let conf = conf_shared.clone();
|
||||
let list = list_shared.clone();
|
||||
state.on_dec_amount(move |item| {
|
||||
if let Some(index) = list.iter().position(|item2| item2.name == item) {
|
||||
let mut item2 = list.row_data(index).unwrap();
|
||||
item2.amount = item2.amount - 1;
|
||||
let item3 = item2.clone();
|
||||
list.set_row_data(index, item2);
|
||||
let conf = conf.clone();
|
||||
if item3.amount < 1 {
|
||||
list.remove(index);
|
||||
spawn(async move {
|
||||
let res = delete_item(&item3.name, &conf).await;
|
||||
if let Err(err) = res {
|
||||
eprintln!("{err:?}");
|
||||
}
|
||||
});
|
||||
} else {
|
||||
spawn(async move {
|
||||
// TODO: add number to increment
|
||||
let res = put_list(
|
||||
&item,
|
||||
&ItemData {
|
||||
checked: item3.checked,
|
||||
amount: item3.amount as i64,
|
||||
},
|
||||
&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")]
|
||||
|
||||
Reference in New Issue
Block a user