69 lines
2.3 KiB
Rust
69 lines
2.3 KiB
Rust
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
|
|
|
mod rutracker;
|
|
use slint::{Model, ModelRc, StandardListViewItem, VecModel};
|
|
use std::error::Error;
|
|
use std::rc::Rc;
|
|
|
|
slint::include_modules!();
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
let ui = AppWindow::new()?;
|
|
let ui_weak = ui.as_weak();
|
|
let items = Rc::new(VecModel::<[StandardListViewItem; 4]>::from(Vec::new()));
|
|
|
|
let items_clone = items.clone();
|
|
ui.on_request_text_input(move |text| {
|
|
if let Some(ui) = ui_weak.upgrade() {
|
|
println!("User input: {}", text);
|
|
let now = std::time::Instant::now();
|
|
|
|
let response = rutracker::search(&text);
|
|
|
|
let elapsed_time = now.elapsed();
|
|
println!("Search took {} seconds", elapsed_time.as_secs());
|
|
|
|
let new_items: Vec<[StandardListViewItem; 4]> = match response {
|
|
Ok(softwares) => softwares
|
|
.into_iter()
|
|
.map(|s| {
|
|
[
|
|
StandardListViewItem::from(s.title.as_str()),
|
|
StandardListViewItem::from(s.author.as_str()),
|
|
StandardListViewItem::from(s.seeders.as_str()),
|
|
StandardListViewItem::from(s.leechers.as_str()),
|
|
]
|
|
})
|
|
.collect(),
|
|
|
|
Err(e) => vec![[
|
|
StandardListViewItem::from(format!("Error: {}", e).as_str()),
|
|
StandardListViewItem::from(format!("Error: {}", e).as_str()),
|
|
StandardListViewItem::from(format!("Error: {}", e).as_str()),
|
|
StandardListViewItem::from(format!("Error: {}", e).as_str()),
|
|
]],
|
|
};
|
|
|
|
items_clone.set_vec(new_items);
|
|
let table_model = Rc::new(VecModel::from(Vec::<ModelRc<StandardListViewItem>>::new()));
|
|
|
|
ui.set_table_data(table_model.clone().into());
|
|
add_rows(&items_clone, &table_model);
|
|
}
|
|
});
|
|
|
|
ui.run()?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn add_rows(
|
|
items: &Rc<VecModel<[StandardListViewItem; 4]>>,
|
|
table_model: &Rc<VecModel<ModelRc<StandardListViewItem>>>,
|
|
) {
|
|
for item in items.iter() {
|
|
let row = Rc::new(VecModel::from(item.to_vec()));
|
|
table_model.push(ModelRc::from(row));
|
|
}
|
|
}
|