Skip to content
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

fix(avatar): when avatar empty don't panic #23

Merged
merged 1 commit into from
Sep 13, 2024
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
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
2 changes: 1 addition & 1 deletion crates/lcode/src/mytui/myevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum UserEvent {
SubmitDone(Box<RunResult>),
TestDone(Box<RunResult>),

UserInfo(Box<(UserStatus, TotalPoints, PassData, PathBuf)>),
UserInfo(Box<(UserStatus, TotalPoints, PassData, Option<PathBuf>)>),

Quit,

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