Skip to content

Commit

Permalink
fix(avatar): when avatar empty don't panic
Browse files Browse the repository at this point in the history
  • Loading branch information
saying121 committed Sep 13, 2024
1 parent 727a327 commit e6040b6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
31 changes: 20 additions & 11 deletions crates/lcode/src/app/impl_app/get_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ impl<'app> App<'app> {
.await;

let avatar_path = avatar_path
.as_os_str()
.to_str()
.as_ref()
.map(|v| {
v.as_os_str()
.to_str()
.unwrap_or_default()
})
.unwrap_or_default();
if res_cn.checkin_ok() {
Notification::new()
Expand Down Expand Up @@ -102,7 +106,7 @@ impl<'app> App<'app> {

pub fn get_status_done(
&mut self,
info: (UserStatus, TotalPoints, PassData, PathBuf),
info: (UserStatus, TotalPoints, PassData, Option<PathBuf>),
) -> miette::Result<()> {
(
self.info.user_status,
Expand All @@ -111,7 +115,7 @@ impl<'app> App<'app> {
self.info.avatar_path,
) = info;

if self.img_state.is_none() {
if self.img_state.is_none() && self.info.avatar_path.is_some() {
#[cfg(not(target_os = "windows"))]
let mut picker =
Picker::from_termios().or(Err(miette::miette!("Image Picker error")))?;
Expand All @@ -120,13 +124,18 @@ impl<'app> App<'app> {

picker.guess_protocol();
picker.background_color = Some(Rgb::<u8>([255, 0, 255]));
let dyn_img = image::ImageReader::open(&self.info.avatar_path)
.into_diagnostic()?
.with_guessed_format()
.into_diagnostic()?
.decode()
.into_diagnostic()?
.resize_to_fill(150, 150, ratatui_image::FilterType::Triangle);
let dyn_img = image::ImageReader::open(
self.info
.avatar_path
.as_ref()
.expect("No avatar file"),
)
.into_diagnostic()?
.with_guessed_format()
.into_diagnostic()?
.decode()
.into_diagnostic()?
.resize_to_fill(150, 150, ratatui_image::FilterType::Triangle);

// Send a [ResizeProtocol] to resize and encode it in a separate thread.
let (tx_worker, rec_worker) =
Expand Down
2 changes: 1 addition & 1 deletion crates/lcode/src/app/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Info<'tab3> {

pub points: TotalPoints,
pub pass_data: PassData,
pub avatar_path: PathBuf,
pub avatar_path: Option<PathBuf>,
}

// keymaps
Expand Down
9 changes: 3 additions & 6 deletions crates/leetcode-api/src/leetcode/impl_lc/user_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ use crate::{
// some info
impl LeetCode {
/// download user avatar image
pub async fn dow_user_avator(&self, status: &UserStatus) -> PathBuf {
let avatar_url = status
.avatar
.as_deref()
.unwrap_or_default();
pub async fn dow_user_avator(&self, status: &UserStatus) -> Option<PathBuf> {
let avatar_url = status.avatar.as_deref()?;
let mut avatar_path = G_CACHE_DIR.clone();
if let Ok(url) = Url::parse(avatar_url) {
if let Some(url_path) = url.path_segments() {
Expand Down Expand Up @@ -58,7 +55,7 @@ impl LeetCode {
}
}
}
avatar_path
Some(avatar_path)
}
pub async fn pass_qs_status(&self, user_slug: &str) -> Result<PassData> {
let json = GraphqlQuery::pass_status(user_slug);
Expand Down

0 comments on commit e6040b6

Please sign in to comment.