use software vector instead of json response to create a new row

This commit is contained in:
2026-07-04 22:36:52 +02:00
parent a25e6cc9f1
commit 29e78c645e
3 changed files with 28 additions and 50 deletions
-12
View File
@@ -1,12 +0,0 @@
[target.x86_64-pc-windows-msvc]
# Increase default stack size to avoid running out of stack
# space in debug builds. The size matches Linux's default.
rustflags = [
"-C", "link-arg=/STACK:8000000"
]
[target.aarch64-pc-windows-msvc]
# Increase default stack size to avoid running out of stack
# space in debug builds. The size matches Linux's default.
rustflags = [
"-C", "link-arg=/STACK:8000000"
]
+17 -35
View File
@@ -1,8 +1,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
mod rutracker; mod rutracker;
use slint::{ModelRc, StandardListViewItem, VecModel}; use slint::{Model, ModelRc, StandardListViewItem, VecModel};
use std::cell::RefCell;
use std::error::Error; use std::error::Error;
use std::rc::Rc; use std::rc::Rc;
@@ -10,54 +9,37 @@ slint::include_modules!();
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let ui = AppWindow::new()?; let ui = AppWindow::new()?;
let response = Rc::new(RefCell::new(None)); let items = Rc::new(VecModel::<StandardListViewItem>::from(Vec::new()));
let items = Rc::new(RefCell::new(Vec::<StandardListViewItem>::new()));
let value = items.clone(); let items_clone = items.clone();
ui.on_request_text_input(move |text| { ui.on_request_text_input(move |text| {
println!("User input: {}", text); println!("User input: {}", text);
*response.borrow_mut() = Some(rutracker::search(&text)); let response = rutracker::search(&text);
//let response = rutracker::search(&text);
let response = response.borrow();
let new_items: Vec<StandardListViewItem> = response let new_items: Vec<StandardListViewItem> = match response {
.iter() Ok(softwares) => softwares
.map(|r| { .into_iter()
let text = match r { .map(|s| StandardListViewItem::from(s.title.as_str()))
Ok(value) => value.to_string(), .collect(),
Err(err) => format!("Error: {}", err),
Err(e) => vec![StandardListViewItem::from(format!("Error: {}", e).as_str())],
}; };
StandardListViewItem::from(text.as_str())
})
.collect();
*value.borrow_mut() = new_items; items_clone.set_vec(new_items);
}); });
let table_vec: Vec<ModelRc<StandardListViewItem>> = vec![]; let table_model = Rc::new(VecModel::from(Vec::<ModelRc<StandardListViewItem>>::new()));
let table_model = Rc::new(VecModel::from(table_vec)); let items_clone = items.clone();
ui.set_table_data(table_model.to_owned().into()); ui.set_table_data(table_model.clone().into());
ui.on_add_row({ ui.on_add_row({
//let response = response.clone();
//move || {
// if let Some(_resp) = items.borrow().as_ref() {
// table_model.push(VecModel::from_slice(&[StandardListViewItem::from(
// //slint::SharedString::from(_resp),
// StandardListViewItem::from(_resp),
// )]));
// }
let items = items.clone();
move || { move || {
let items = items.borrow(); for item in items_clone.iter() {
for item in items.iter() { table_model.push(ModelRc::new(VecModel::from_slice(&[item.clone()])));
table_model.push(VecModel::from_slice(&[item.clone()]));
} }
} }
}); });
ui.run().unwrap();
ui.run()?; ui.run()?;
Ok(()) Ok(())
+10 -2
View File
@@ -12,17 +12,25 @@ pub struct Software {
} }
#[tokio::main] #[tokio::main]
pub async fn search(value: &str) -> Result<Value, reqwest::Error> { //pub async fn search(value: &str) -> Result<Value, reqwest::Error> {
pub async fn search(value: &str) -> Result<Vec<Software>, reqwest::Error> {
let api_url = "https://api.michijackson.xyz/search?q=".to_owned(); let api_url = "https://api.michijackson.xyz/search?q=".to_owned();
let results = reqwest::get(api_url + value).await?; let results = reqwest::get(api_url + value).await?;
let text = results.text().await?; let text = results.text().await?;
let v: Value = serde_json::from_str(&text).expect("Failed to parse JSON"); let v: Value = serde_json::from_str(&text).expect("Failed to parse JSON");
let data = &v["data"]; let data = &v["data"];
let mut software_list: Vec<Software> = Vec::new();
for item in data.as_array().unwrap() { for item in data.as_array().unwrap() {
let software: Software = let software: Software =
serde_json::from_value(item.clone()).expect("Failed to deserialize"); serde_json::from_value(item.clone()).expect("Failed to deserialize");
println!("{:?}", software); println!("{:?}", software);
software_list.push(software);
} }
//println!("{:?}", data); //println!("{:?}", data);
Ok(v) //Ok(v)
Ok(software_list)
} }