From 3f01d9263a83b5dc4484b37ba71a751b5333c9bb Mon Sep 17 00:00:00 2001 From: Ward from fusion-voyager-3 Date: Tue, 18 Jun 2024 21:34:53 +0300 Subject: [PATCH] download db only once --- src/build_ui/mod.rs | 11 +++- src/content/mod.rs | 141 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 117 insertions(+), 35 deletions(-) diff --git a/src/build_ui/mod.rs b/src/build_ui/mod.rs index e5b25a6..da281c1 100644 --- a/src/build_ui/mod.rs +++ b/src/build_ui/mod.rs @@ -1,6 +1,7 @@ use crate::{content, kernel_pkg, sched_ext, KernelBranch, PRETTY_NAME}; use adw::prelude::*; use adw::*; +use glib::property::PropertyGet; use glib::{clone, MainContext}; use gtk::prelude::*; use gtk::*; @@ -8,11 +9,14 @@ use std::cell::RefCell; use std::process::Command; use std::rc::Rc; use std::{thread, time}; -use glib::property::PropertyGet; pub fn build_ui(app: &adw::Application) { let internet_connected = Rc::new(RefCell::new(false)); - let selected_kernel_branch: Rc> = Rc::new(RefCell::new(KernelBranch{name: "?".to_owned(), db_url:"?".to_owned() , db:"?".to_owned()})); + let selected_kernel_branch: Rc> = Rc::new(RefCell::new(KernelBranch { + name: "?".to_owned(), + db_url: "?".to_owned(), + db: "?".to_owned(), + })); let (internet_loop_sender, internet_loop_receiver) = async_channel::unbounded(); let internet_loop_sender = internet_loop_sender.clone(); @@ -66,7 +70,8 @@ pub fn build_ui(app: &adw::Application) { content_stack.add_named( &content::content(&content_stack, &selected_kernel_branch2), - Some("content_page")); + Some("content_page"), + ); content_stack.add_named( &sched_ext::sched_ext_page(&content_stack), Some("sched_ext_page"), diff --git a/src/content/mod.rs b/src/content/mod.rs index 7dc5192..3124135 100644 --- a/src/content/mod.rs +++ b/src/content/mod.rs @@ -1,23 +1,36 @@ -use std::cell::RefCell; use crate::{KernelBranch, RunningKernelInfo}; use adw::prelude::*; use adw::ExpanderRow; +use async_channel::Receiver; use duct::cmd; +use glib::property::PropertyGet; use glib::*; use gtk::prelude::*; use gtk::*; +use homedir::get_my_home; +use std::cell::RefCell; use std::fs; use std::path::Path; use std::process::{Command, Stdio}; use std::rc::Rc; use version_compare::Version; -use homedir::get_my_home; use Vec; -use glib::property::PropertyGet; -pub fn content(content_stack: >k::Stack, selected_kernel_branch: &Rc>) -> gtk::Box { +pub fn content( + content_stack: >k::Stack, + selected_kernel_branch: &Rc>, +) -> gtk::Box { let running_kernel_info = get_running_kernel_info(); + let (get_kernel_branches_sender, get_kernel_branches_receiver) = async_channel::unbounded(); + let get_kernel_branches_sender = get_kernel_branches_sender.clone(); + + std::thread::spawn(move || { + get_kernel_branches_sender + .send_blocking(get_kernel_branches()) + .expect("channel needs to be open."); + }); + let content_box = gtk::Box::builder() .hexpand(true) .vexpand(true) @@ -48,7 +61,11 @@ pub fn content(content_stack: >k::Stack, selected_kernel_branch: &Rc async move { + while let Ok(data) = get_kernel_branches_receiver.recv().await { + let selected_kernel_branch_clone0 = selected_kernel_branch.clone(); + let selected_kernel_branch_clone1 = selected_kernel_branch.clone(); - let selected_kernel_branch_clone0 = selected_kernel_branch.clone(); - let selected_kernel_branch_clone1 = selected_kernel_branch.clone(); - - match get_my_home().unwrap().unwrap().join(".config/fedora-kernel-manager/branch").exists() { - true => { - let narrow_branch: Vec = get_kernel_branches().clone().into_iter() - .filter(|a| a.name == fs::read_to_string(get_my_home().unwrap().unwrap().join(".config/fedora-kernel-manager/branch")).unwrap()) - .collect(); - *selected_kernel_branch_clone0.borrow_mut()=narrow_branch.get(0).unwrap().clone() - } - _ => { - let normal_branch = get_kernel_branches().clone().get(0).unwrap().clone(); - *selected_kernel_branch_clone0.borrow_mut()=normal_branch - } - }; + match get_my_home() + .unwrap() + .unwrap() + .join(".config/fedora-kernel-manager/branch") + .exists() + { + true => { + let narrow_branch: Vec = data + .clone() + .into_iter() + .filter(|a| { + a.name + == fs::read_to_string( + get_my_home() + .unwrap() + .unwrap() + .join(".config/fedora-kernel-manager/branch"), + ) + .unwrap() + }) + .collect(); + *selected_kernel_branch_clone0.borrow_mut() = + narrow_branch.get(0).unwrap().clone() + } + _ => { + let normal_branch = data.clone().get(0).unwrap().clone(); + *selected_kernel_branch_clone0.borrow_mut() = normal_branch + } + }; + } + }), + ); content_box } -fn kernel_branch_expandable(expander_row: &adw::ExpanderRow, selected_kernel_branch: &Rc>) -> gtk::ListBox { +fn kernel_branch_expandable( + expander_row: &adw::ExpanderRow, + selected_kernel_branch: &Rc>, + get_kernel_branches_receiver: Receiver>, +) -> gtk::ListBox { let searchbar = gtk::SearchEntry::builder().search_delay(500).build(); searchbar.add_css_class("round-border-only-top"); @@ -155,7 +200,11 @@ fn kernel_branch_expandable(expander_row: &adw::ExpanderRow, selected_kernel_bra .label("No branch selected") .build(); - for branch in get_kernel_branches() { + let get_kernel_branches_loop_context = MainContext::default(); + // The main loop executes the asynchronous block + get_kernel_branches_loop_context.spawn_local(clone!(@weak expander_row, @weak branch_container, @strong selected_kernel_branch => async move { + while let Ok(data) = get_kernel_branches_receiver.recv().await { +for branch in data { let branch_clone0 = branch.clone(); let branch_clone1 = branch.clone(); let branch_checkbutton = gtk::CheckButton::builder() @@ -180,11 +229,28 @@ fn kernel_branch_expandable(expander_row: &adw::ExpanderRow, selected_kernel_bra }), ); - match get_my_home().unwrap().unwrap().join(".config/fedora-kernel-manager/branch").exists() { - true if fs::read_to_string(get_my_home().unwrap().unwrap().join(".config/fedora-kernel-manager/branch")).unwrap() == branch_clone1.name => branch_checkbutton.set_active(true), - _ => {} + match get_my_home() + .unwrap() + .unwrap() + .join(".config/fedora-kernel-manager/branch") + .exists() + { + true if fs::read_to_string( + get_my_home() + .unwrap() + .unwrap() + .join(".config/fedora-kernel-manager/branch"), + ) + .unwrap() + == branch_clone1.name => + { + branch_checkbutton.set_active(true) + } + _ => {} } } + } + })); let branch_container_viewport = gtk::ScrolledWindow::builder() .child(&branch_container) @@ -280,19 +346,27 @@ fn get_kernel_branches() -> Vec { let data = fs::read_to_string( "/home/ward/RustroverProjects/fedora-kernel-manager/data/kernel_branches.json", ) - .expect("Unable to read file"); + .expect("Unable to read file"); let res: serde_json::Value = serde_json::from_str(&data).expect("Unable to parse"); if let serde_json::Value::Array(branches) = &res["branches"] { for branch in branches { - - println!("Downloading & Parsing package DB for {}.", branch.name); + println!( + "Downloading & Parsing package DB for {}.", + branch["name"].as_str().to_owned().unwrap().to_string() + ); let branch = KernelBranch { name: branch["name"].as_str().to_owned().unwrap().to_string(), db_url: branch["db_url"].as_str().to_owned().unwrap().to_string(), - db: reqwest::blocking::get(branch["db_url"].as_str().to_owned().unwrap().to_string(),).unwrap().text().unwrap() + db: reqwest::blocking::get( + branch["db_url"].as_str().to_owned().unwrap().to_string(), + ) + .unwrap() + .text() + .unwrap(), }; kernel_branches_array.push(branch) - }}; + } + }; kernel_branches_array } @@ -418,7 +492,10 @@ fn create_kernel_badges(badge_box: >k::Box, running_kernel_info: &RunningKerne } fn save_branch_config(branch: &str) { - let config_path = get_my_home().unwrap().unwrap().join(".config/fedora-kernel-manager"); + let config_path = get_my_home() + .unwrap() + .unwrap() + .join(".config/fedora-kernel-manager"); match &config_path.exists() { true => fs::write(config_path.join("branch"), branch).unwrap(), _ => { @@ -426,4 +503,4 @@ fn save_branch_config(branch: &str) { fs::write(config_path.join("branch"), branch).unwrap(); } } -} \ No newline at end of file +}