Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ pub enum Editable<T> {
#[derive(Debug, Clone)]
pub enum Message {
WriteConfig(bool),
SaveRanking,
LoadRanking,
UpdateAvailable,
ResizeWindow(Id, f32),
OpenWindow,
Expand Down
29 changes: 26 additions & 3 deletions src/app/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, i32> {
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<App> {
let mut ranked: Vec<App> = self
.by_name
Expand Down Expand Up @@ -134,6 +153,7 @@ pub struct Tile {
pub query: String,
pub current_mode: String,
pub update_available: bool,
pub ranking: HashMap<String, i32>,
query_lc: String,
results: Vec<App>,
options: AppIndex,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -556,7 +576,7 @@ fn handle_recipient() -> impl futures::Stream<Item = Message> {
})
}

fn check_version() -> impl futures::Stream<Item = Message> {
fn handle_version_and_rankings() -> impl futures::Stream<Item = Message> {
stream::channel(100, async |mut output| {
let current_version = format!("\"{}\"", option_env!("APP_VERSION").unwrap_or(""));

Expand Down Expand Up @@ -599,7 +619,10 @@ fn check_version() -> impl futures::Stream<Item = Message> {
} 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;
}
})
}
11 changes: 11 additions & 0 deletions src/app/tile/elm.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -65,6 +68,13 @@ pub fn new(hotkey: HotKey, config: &Config) -> (Tile, Task<Message>) {
.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,
Expand All @@ -80,6 +90,7 @@ pub fn new(hotkey: HotKey, config: &Config) -> (Tile, Task<Message>) {
frontmost: None,
focused: false,
config: config.clone(),
ranking,
theme: config.theme.to_owned().clone().into(),
clipboard_content: vec![],
tray_icon: None,
Expand Down
18 changes: 17 additions & 1 deletion src/app/tile/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,22 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
},
)
}
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),
Expand Down Expand Up @@ -263,7 +279,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {

tile.theme = new_config.theme.to_owned().into();
tile.config = new_config;
Task::none()
Task::done(Message::LoadRanking)
}

Message::KeyPressed(hk_id) => {
Expand Down
Loading