-
-
Notifications
You must be signed in to change notification settings - Fork 47
Implement favorite paired devices (#72) #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
706e3e1
adbbef7
b225643
34cb92c
ec432f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ use bluer::{ | |
|
|
||
| use bluer::Device as BTDevice; | ||
|
|
||
| use clap::crate_name; | ||
| use tokio::sync::oneshot; | ||
|
|
||
| use crate::app::AppResult; | ||
|
|
@@ -35,6 +36,7 @@ pub struct Device { | |
| pub is_trusted: bool, | ||
| pub is_connected: bool, | ||
| pub battery_percentage: Option<u8>, | ||
| pub is_favorite: bool, | ||
| } | ||
|
|
||
| impl Device { | ||
|
|
@@ -58,6 +60,53 @@ impl Device { | |
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| pub async fn get_if_favorite(addr: Address) -> bool { | ||
| let state_dir = dirs::state_dir().expect("Could not find state directory."); | ||
| let favorite_addrs_file = state_dir.join(crate_name!()).join("favorite_addrs.txt"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My code taste is to make names as specific as possible. The file contains MAC adresses of favorite devices, so that's why I named it that.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agree with @Jorenar |
||
| if !favorite_addrs_file.exists() { | ||
| return false; | ||
| } | ||
| let contents = tokio::fs::read_to_string(favorite_addrs_file).await; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this read cached or do we read the file every time we ask if the addr is a favorite? Then you could have the member method
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not cached, and it called everytime the I think your idea is good. I will try to implement! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think getting some input from @pythops as well would be a good idea before running with it :)
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is not good idea to have an io for every tick |
||
| if let Ok(contents) = contents { | ||
| for line in contents.lines() { | ||
| if line.trim() == addr.to_string() { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| false | ||
| } | ||
|
|
||
| pub async fn toggle_favorite(&self) -> AppResult<()> { | ||
| let state_dir = dirs::state_dir().expect("Could not find state directory."); | ||
| let favorite_addrs_dir = state_dir.join(crate_name!()); | ||
| if !favorite_addrs_dir.exists() { | ||
| tokio::fs::create_dir_all(&favorite_addrs_dir).await?; | ||
| } | ||
| let favorite_addrs_file = favorite_addrs_dir.join("favorite_addrs.txt"); | ||
|
|
||
| let mut favorite_addrs: Vec<String> = Vec::new(); | ||
| if favorite_addrs_file.exists() { | ||
| let contents = tokio::fs::read_to_string(&favorite_addrs_file).await?; | ||
| for line in contents.lines() { | ||
| favorite_addrs.push(line.trim().to_string()); | ||
| } | ||
| } | ||
|
|
||
| if self.is_favorite { | ||
| // remove from favorites | ||
| favorite_addrs.retain(|addr| addr != &self.addr.to_string()); | ||
| } else { | ||
| // add to favorites | ||
| favorite_addrs.push(self.addr.to_string()); | ||
| } | ||
|
|
||
| let new_contents = favorite_addrs.join("\n"); | ||
| tokio::fs::write(&favorite_addrs_file, new_contents).await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl Controller { | ||
|
|
@@ -111,6 +160,7 @@ impl Controller { | |
| let is_trusted = device.is_trusted().await?; | ||
| let is_connected = device.is_connected().await?; | ||
| let battery_percentage = device.battery_percentage().await?; | ||
| let is_favorite = Device::get_if_favorite(addr).await; | ||
|
|
||
| let dev = Device { | ||
| device, | ||
|
|
@@ -121,6 +171,7 @@ impl Controller { | |
| is_trusted, | ||
| is_connected, | ||
| battery_percentage, | ||
| is_favorite, | ||
| }; | ||
|
|
||
| if dev.is_paired { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if user's favorites aren't important enough to warrant placement in
$XDG_DATA_HOME?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might be right. According to Gentoo's Wiki,
$XDG_STATE_HOMEis more for data that isn't important/portable enough.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the program should not exit because we can not find favorite directory or we can not read from it.