diff --git a/src/app.rs b/src/app.rs index 4ba3146..c074dc0 100644 --- a/src/app.rs +++ b/src/app.rs @@ -80,6 +80,8 @@ pub enum Editable { #[derive(Debug, Clone)] pub enum Message { WriteConfig(bool), + SaveRanking, + LoadRanking, UpdateAvailable, ResizeWindow(Id, f32), OpenWindow, diff --git a/src/app/tile.rs b/src/app/tile.rs index 039bd13..337d8ab 100644 --- a/src/app/tile.rs +++ b/src/app/tile.rs @@ -72,6 +72,25 @@ impl AppIndex { app.ranking += 1; } + fn set_ranking(&mut self, name: &str, rank: i32) { + let app = match self.by_name.get_mut(name) { + Some(a) => a, + None => return, + }; + + app.ranking = rank; + } + + fn get_rankings(&self) -> HashMap { + HashMap::from_iter(self.by_name.iter().filter_map(|(name, app)| { + if app.ranking > 0 { + Some((name.to_owned(), app.ranking.to_owned())) + } else { + None + } + })) + } + fn top_ranked(&self, limit: usize) -> Vec { let mut ranked: Vec = self .by_name @@ -134,6 +153,7 @@ pub struct Tile { pub query: String, pub current_mode: String, pub update_available: bool, + pub ranking: HashMap, query_lc: String, results: Vec, options: AppIndex, @@ -199,7 +219,7 @@ impl Tile { Subscription::run(handle_hot_reloading), keyboard, Subscription::run(handle_recipient), - Subscription::run(check_version), + Subscription::run(handle_version_and_rankings), Subscription::run(handle_clipboard_history), Subscription::run(handle_file_search), window::close_events().map(Message::HideWindow), @@ -556,7 +576,7 @@ fn handle_recipient() -> impl futures::Stream { }) } -fn check_version() -> impl futures::Stream { +fn handle_version_and_rankings() -> impl futures::Stream { stream::channel(100, async |mut output| { let current_version = format!("\"{}\"", option_env!("APP_VERSION").unwrap_or("")); @@ -599,7 +619,10 @@ fn check_version() -> impl futures::Stream { } else { warn!("Error getting resp"); } - tokio::time::sleep(Duration::from_secs(60)).await; + tokio::time::sleep(Duration::from_secs(30)).await; + output.send(Message::SaveRanking).await.ok(); + info!("Sent save ranking"); + tokio::time::sleep(Duration::from_secs(30)).await; } }) } diff --git a/src/app/tile/elm.rs b/src/app/tile/elm.rs index 0d9a4a8..a64bea8 100644 --- a/src/app/tile/elm.rs +++ b/src/app/tile/elm.rs @@ -1,6 +1,9 @@ //! This module handles the logic for the new and view functions according to the elm //! architecture. If the subscription function becomes too large, it should be moved to this file +use std::collections::HashMap; +use std::fs; + use global_hotkey::hotkey::HotKey; use iced::border::Radius; use iced::widget::scrollable::{Anchor, Direction, Scrollbar}; @@ -65,6 +68,13 @@ pub fn new(hotkey: HotKey, config: &Config) -> (Tile, Task) { .unwrap_or("SUPER+SHIFT+C".parse().unwrap()), }; + let home = std::env::var("HOME").unwrap_or("/".to_string()); + + let ranking = toml::from_str( + &fs::read_to_string(home + "/.config/rustcast/ranking.toml").unwrap_or("".to_string()), + ) + .unwrap_or(HashMap::new()); + ( Tile { update_available: false, @@ -80,6 +90,7 @@ pub fn new(hotkey: HotKey, config: &Config) -> (Tile, Task) { frontmost: None, focused: false, config: config.clone(), + ranking, theme: config.theme.to_owned().clone().into(), clipboard_content: vec![], tray_icon: None, diff --git a/src/app/tile/update.rs b/src/app/tile/update.rs index 4aa7ae3..96d1014 100644 --- a/src/app/tile/update.rs +++ b/src/app/tile/update.rs @@ -227,6 +227,22 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task { }, ) } + Message::LoadRanking => { + for (name, rank) in &tile.ranking { + tile.options.set_ranking(name, rank.to_owned()); + } + + Task::none() + } + + Message::SaveRanking => { + tile.ranking = tile.options.get_rankings(); + let string_rep = toml::to_string(&tile.ranking).unwrap_or("".to_string()); + let ranking_file_path = + std::env::var("HOME").unwrap_or("/".to_string()) + "/.config/rustcast/ranking.toml"; + fs::write(ranking_file_path, string_rep).ok(); + Task::none() + } Message::OpenFocused => Task::done(Message::OpenResult(tile.focus_id)), Message::OpenResult(id) => open_result(tile, id as usize), @@ -263,7 +279,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task { tile.theme = new_config.theme.to_owned().into(); tile.config = new_config; - Task::none() + Task::done(Message::LoadRanking) } Message::KeyPressed(hk_id) => {