use crate::app::App; use crate::tdlib::{AuthState, TdClientTrait}; use crate::utils::{is_non_empty, with_timeout_msg}; use crossterm::event::KeyCode; use std::time::Duration; pub async fn handle(app: &mut App, key_code: KeyCode) { match &app.td_client.auth_state() { AuthState::WaitPhoneNumber => match key_code { KeyCode::Char(c) => { app.phone_input_mut().push(c); app.error_message = None; } KeyCode::Backspace => { app.phone_input_mut().pop(); app.error_message = None; } KeyCode::Enter => { if is_non_empty(app.phone_input()) { app.status_message = Some("Отправка номера...".to_string()); match with_timeout_msg( Duration::from_secs(10), app.td_client .send_phone_number(app.phone_input().to_string()), "Таймаут отправки номера", ) .await { Ok(_) => { app.error_message = None; app.status_message = None; } Err(e) => { app.error_message = Some(e); app.status_message = None; } } } } _ => {} }, AuthState::WaitCode => match key_code { KeyCode::Char(c) if c.is_numeric() => { app.code_input_mut().push(c); app.error_message = None; } KeyCode::Backspace => { app.code_input_mut().pop(); app.error_message = None; } KeyCode::Enter => { if is_non_empty(app.code_input()) { app.status_message = Some("Проверка кода...".to_string()); match with_timeout_msg( Duration::from_secs(10), app.td_client.send_code(app.code_input().to_string()), "Таймаут проверки кода", ) .await { Ok(_) => { app.error_message = None; app.status_message = None; } Err(e) => { app.error_message = Some(e); app.status_message = None; } } } } _ => {} }, AuthState::WaitPassword => match key_code { KeyCode::Char(c) => { app.password_input_mut().push(c); app.error_message = None; } KeyCode::Backspace => { app.password_input_mut().pop(); app.error_message = None; } KeyCode::Enter => { if is_non_empty(app.password_input()) { app.status_message = Some("Проверка пароля...".to_string()); match with_timeout_msg( Duration::from_secs(10), app.td_client .send_password(app.password_input().to_string()), "Таймаут проверки пароля", ) .await { Ok(_) => { app.error_message = None; app.status_message = None; } Err(e) => { app.error_message = Some(e); app.status_message = None; } } } } _ => {} }, _ => {} } }