init
This commit is contained in:
201
src/app/mod.rs
Normal file
201
src/app/mod.rs
Normal file
@@ -0,0 +1,201 @@
|
||||
use crate::telegram::{Chat, Message};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct App {
|
||||
pub tabs: Vec<String>,
|
||||
pub selected_tab: usize,
|
||||
pub chats: Vec<Chat>,
|
||||
pub selected_chat: Option<usize>,
|
||||
pub messages: Vec<Message>,
|
||||
pub input: String,
|
||||
pub search_query: String,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
tabs: vec![
|
||||
"All".to_string(),
|
||||
"Personal".to_string(),
|
||||
"Work".to_string(),
|
||||
"Bots".to_string(),
|
||||
],
|
||||
selected_tab: 0,
|
||||
chats: Self::mock_chats(),
|
||||
selected_chat: Some(0),
|
||||
messages: Self::mock_messages(),
|
||||
input: String::new(),
|
||||
search_query: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select_tab(&mut self, index: usize) {
|
||||
if index < self.tabs.len() {
|
||||
self.selected_tab = index;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_chat(&mut self) {
|
||||
if !self.chats.is_empty() {
|
||||
self.selected_chat = Some(
|
||||
self.selected_chat
|
||||
.map(|i| (i + 1) % self.chats.len())
|
||||
.unwrap_or(0),
|
||||
);
|
||||
self.load_messages();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn previous_chat(&mut self) {
|
||||
if !self.chats.is_empty() {
|
||||
self.selected_chat = Some(
|
||||
self.selected_chat
|
||||
.map(|i| if i == 0 { self.chats.len() - 1 } else { i - 1 })
|
||||
.unwrap_or(0),
|
||||
);
|
||||
self.load_messages();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_chat(&mut self) {
|
||||
self.load_messages();
|
||||
}
|
||||
|
||||
fn load_messages(&mut self) {
|
||||
self.messages = Self::mock_messages();
|
||||
}
|
||||
|
||||
fn mock_chats() -> Vec<Chat> {
|
||||
vec![
|
||||
Chat {
|
||||
name: "Saved Messages".to_string(),
|
||||
last_message: "My notes...".to_string(),
|
||||
unread_count: 0,
|
||||
is_pinned: true,
|
||||
is_online: false,
|
||||
},
|
||||
Chat {
|
||||
name: "Mom".to_string(),
|
||||
last_message: "Отлично, захвати хлеба.".to_string(),
|
||||
unread_count: 2,
|
||||
is_pinned: false,
|
||||
is_online: true,
|
||||
},
|
||||
Chat {
|
||||
name: "Boss".to_string(),
|
||||
last_message: "Meeting at 3pm".to_string(),
|
||||
unread_count: 0,
|
||||
is_pinned: false,
|
||||
is_online: false,
|
||||
},
|
||||
Chat {
|
||||
name: "Rust Community".to_string(),
|
||||
last_message: "Check out this crate...".to_string(),
|
||||
unread_count: 0,
|
||||
is_pinned: false,
|
||||
is_online: false,
|
||||
},
|
||||
Chat {
|
||||
name: "Durov".to_string(),
|
||||
last_message: "Privacy matters".to_string(),
|
||||
unread_count: 0,
|
||||
is_pinned: false,
|
||||
is_online: false,
|
||||
},
|
||||
Chat {
|
||||
name: "News Channel".to_string(),
|
||||
last_message: "Breaking news...".to_string(),
|
||||
unread_count: 0,
|
||||
is_pinned: false,
|
||||
is_online: false,
|
||||
},
|
||||
Chat {
|
||||
name: "Spam Bot".to_string(),
|
||||
last_message: "Click here!!!".to_string(),
|
||||
unread_count: 0,
|
||||
is_pinned: false,
|
||||
is_online: false,
|
||||
},
|
||||
Chat {
|
||||
name: "Wife".to_string(),
|
||||
last_message: "Don't forget the milk".to_string(),
|
||||
unread_count: 0,
|
||||
is_pinned: false,
|
||||
is_online: false,
|
||||
},
|
||||
Chat {
|
||||
name: "Team Lead".to_string(),
|
||||
last_message: "Code review please".to_string(),
|
||||
unread_count: 0,
|
||||
is_pinned: false,
|
||||
is_online: false,
|
||||
},
|
||||
Chat {
|
||||
name: "DevOps Chat".to_string(),
|
||||
last_message: "Server is down!".to_string(),
|
||||
unread_count: 9,
|
||||
is_pinned: false,
|
||||
is_online: false,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
fn mock_messages() -> Vec<Message> {
|
||||
vec![
|
||||
Message {
|
||||
sender: "Mom".to_string(),
|
||||
text: "Привет! Ты покормил кота?".to_string(),
|
||||
time: "14:20".to_string(),
|
||||
is_outgoing: false,
|
||||
read_status: 0,
|
||||
},
|
||||
Message {
|
||||
sender: "You".to_string(),
|
||||
text: "Да, конечно. Купил ему корм.".to_string(),
|
||||
time: "14:22".to_string(),
|
||||
is_outgoing: true,
|
||||
read_status: 2,
|
||||
},
|
||||
Message {
|
||||
sender: "You".to_string(),
|
||||
text: "Скоро буду дома.".to_string(),
|
||||
time: "14:22".to_string(),
|
||||
is_outgoing: true,
|
||||
read_status: 2,
|
||||
},
|
||||
Message {
|
||||
sender: "Mom".to_string(),
|
||||
text: "Отлично, захвати хлеба.".to_string(),
|
||||
time: "14:23".to_string(),
|
||||
is_outgoing: false,
|
||||
read_status: 0,
|
||||
},
|
||||
Message {
|
||||
sender: "You".to_string(),
|
||||
text: "Ок.".to_string(),
|
||||
time: "14:25".to_string(),
|
||||
is_outgoing: true,
|
||||
read_status: 1,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
pub fn get_current_chat_name(&self) -> String {
|
||||
self.selected_chat
|
||||
.and_then(|i| self.chats.get(i))
|
||||
.map(|chat| {
|
||||
if chat.is_online {
|
||||
format!("👤 {} (online)", chat.name)
|
||||
} else {
|
||||
format!("👤 {}", chat.name)
|
||||
}
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for App {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user